Commit 0e0ac9ea by Michael Pastushkov

java work in progress

parent 6337ac45
/*
* Copyright (C) 2024 Michael Pastushkov <michael@pastushkov.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*
*/
import java.net.*;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
public class ByteVia {
private static final int MAX_UDP_CLIENTS = 10;
private byte[] cipher = new byte[256];
private int cipherTime;
private Options options = new Options();
private DatagramSocket udpSocket;
private ServerSocket tcpSocket;
private Map<InetAddress, Integer> clients = new HashMap<>();
public static void main(String[] args) {
ByteVia server = new ByteVia();
server.initOptions();
server.run();
}
public void initOptions() {
// Initialize default options
options.localPort = 1948;
options.bindAddress = "0.0.0.0";
options.remoteHost = "p4pn.net";
options.remotePort = 1984;
options.bufferSize = 4096;
options.encrypt = false;
options.log = true;
options.secret = 52341;
options.proto = "tcp";
options.mode = "client";
}
public void run() {
try {
if (options.log) {
System.out.println("NetVia Java");
System.out.println(" proto: " + options.proto);
System.out.println(" local: " + options.bindAddress +":"+ options.localPort);
System.out.println(" remote: " + options.remoteHost +":"+ options.remotePort);
System.out.println(" mode: " + options.mode);
System.out.println(" encrypt: " + options.encrypt);
}
if (options.proto == "udp") {
udpSocket = new DatagramSocket(options.localPort);
System.out.println("UDP server started on port " + options.localPort);
udpServerLoop();
} else {
tcpSocket = new ServerSocket(options.localPort);
System.out.println("TCP server started on port " + options.localPort);
tcpServerLoop();
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void udpServerLoop() {
byte[] buffer = new byte[options.bufferSize];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
while (true) {
try {
udpSocket.receive(packet);
InetAddress clientAddress = packet.getAddress();
int clientPort = packet.getPort();
System.out.println(getCurrentTimestamp() + " Received packet from " + clientAddress);
// Add client to list if not already
if (!clients.containsKey(clientAddress)) {
clients.put(clientAddress, clientPort);
}
// Process data (encryption simulation)
if (options.encrypt) {
updateCipher();
for (int i = 0; i < packet.getLength(); i++) {
buffer[i] = cipher[buffer[i] & 0xFF];
}
}
// Echo back the processed data
DatagramPacket response = new DatagramPacket(buffer, packet.getLength(), clientAddress, clientPort);
udpSocket.send(response);
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void tcpServerLoop() {
while (true) {
try {
Socket clientSocket = tcpSocket.accept();
System.out.println(getCurrentTimestamp() + " Accepted connection from " + clientSocket.getInetAddress());
new Thread(() -> handleTcpClient(clientSocket)).start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void handleTcpClient(Socket clientSocket) {
try {
Socket remoteSocket = new Socket();
remoteSocket.connect(new InetSocketAddress(options.remoteHost, options.remotePort));
System.out.println("Connected to remote server at " + options.remoteHost + ":" + options.remotePort);
InputStream in = clientSocket.getInputStream();
OutputStream out = remoteSocket.getOutputStream();
InputStream in2 = remoteSocket.getInputStream();
OutputStream out2 = clientSocket.getOutputStream();
byte[] buffer = new byte[options.bufferSize];
int bytesRead;
while (true) {
if (in.available() > 0) {
bytesRead = in.read(buffer);
if (bytesRead == -1)
break;
System.out.println(" From client " + bytesRead + " bytes");
// Process data (encryption simulation)
if (options.encrypt) {
updateCipher();
for (int i = 0; i < bytesRead; i++) {
buffer[i] = cipher[buffer[i] & 0xFF];
}
}
out.write(buffer, 0, bytesRead);
out.flush();
System.out.println(" Sent");
}
/*******/
if (in2.available() > 0) {
bytesRead = in2.read(buffer);
if (bytesRead == -1)
break;
System.out.println(" From remote " + bytesRead + " bytes");
out2.write(buffer, 0, bytesRead);
out2.flush();
System.out.println(" Sent");
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
private void updateCipher() {
int currentTime = getTime();
if (currentTime == cipherTime) {
return;
}
Random random = new Random(currentTime);
for (int i = 0; i < 256; i++) {
cipher[i] = (byte) i;
}
for (int i = 255; i > 0; i--) {
int j = random.nextInt(i + 1);
byte temp = cipher[i];
cipher[i] = cipher[j];
cipher[j] = temp;
}
cipherTime = currentTime;
System.out.println(getCurrentTimestamp() + " Updated cipher.");
}
private int getTime() {
return (int) (System.currentTimeMillis() / (60 * 10 * 1000));
}
private String getCurrentTimestamp() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(new Date());
}
static class Options {
int localPort;
String bindAddress;
String remoteHost;
int remotePort;
int bufferSize;
boolean encrypt;
boolean log;
int secret;
String proto;
String mode;
}
}
No preview for this file type
#!/bin/sh #!/bin/sh
make clean && make && ./bytevia --local-port=1948 --remote-host=p4pn.net --remote-port=1984 --proto=tcp --encrypt=0 make clean && make && ./bytevia --local-port=1948 --remote-host=p4pn.net --remote-port=1984 --proto=tcp --encrypt=1
...@@ -90,7 +90,7 @@ unsigned int xorshift32(unsigned int *state) ...@@ -90,7 +90,7 @@ unsigned int xorshift32(unsigned int *state)
void shuffle(unsigned char *array, int n, unsigned int seed) void shuffle(unsigned char *array, int n, unsigned int seed)
{ {
unsigned int state = seed; unsigned int state = seed;
srand(seed); // srand(seed);
for (int i = n - 1; i > 0; i--) for (int i = n - 1; i > 0; i--)
{ {
int j = xorshift32(&state) % (i + 1); int j = xorshift32(&state) % (i + 1);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment