Commit 6337ac45 by Michael Pastushkov

x

parent e0efbfb9
#!/bin/bash
/usr/bin/open -a Terminal "`dirname \"$0\"`/bytevia_mac"
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleExecutable</key>
<string>run</string>
<key>CFBundleIdentifier</key>
<string>net.p4pn.bytevia</string>
<key>CFBundleName</key>
<string>bytevia_intel</string>
<key>CFBundleVersion</key>
<string>1.0</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
</dict>
</plist>
/*
* 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.remoteHost = "127.0.0.1";
options.remotePort = 1984;
options.bufferSize = 4096;
options.encrypt = false;
options.log = true;
options.secret = 52341;
options.proto = "tcp";
}
public void run() {
try {
if ("udp".equalsIgnoreCase(options.proto)) {
udpSocket = new DatagramSocket(options.localPort);
System.out.println("UDP server started on port " + options.localPort);
udpServerLoop();
} else if ("tcp".equalsIgnoreCase(options.proto)) {
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 (InputStream in = clientSocket.getInputStream();
OutputStream out = clientSocket.getOutputStream()) {
byte[] buffer = new byte[options.bufferSize];
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) {
System.out.println(getCurrentTimestamp() + " Received " + 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);
}
} 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 remoteHost;
int remotePort;
int bufferSize;
boolean encrypt;
boolean log;
int secret;
String proto;
}
}
javac bytevia.java && java ByteVia
#!/bin/sh
make clean && make && ./bytevia --local-port=1948 --remote-host=p4pn.net --remote-port=1984 --proto=tcp
make clean && make && ./bytevia --local-port=1948 --remote-host=p4pn.net --remote-port=1984 --proto=tcp --encrypt=0
......@@ -438,7 +438,9 @@ int use()
}
/* Processing request from local, sending it to remote*/
count_recv = (options.proto == SOCK_DGRAM) ? recvfrom(rc.client_socket, buffer, sizeof(buffer), 0, (struct sockaddr *)&client_addr, &addr_len) : recv(rc.client_socket, buffer, sizeof(buffer), 0);
count_recv = (options.proto == SOCK_DGRAM) ?
recvfrom(rc.client_socket, buffer, sizeof(buffer), 0, (struct sockaddr *)&client_addr, &addr_len) :
recv(rc.client_socket, buffer, sizeof(buffer), 0);
if (count_recv < 0)
{
......@@ -463,7 +465,8 @@ int use()
send(rc.remote_socket, buffer, count_recv, 0);
if (options.log > 1)
printf("recv %d\t%s:%d\tpid: %d\n", count_recv, inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port), getpid());
printf("recv %d bytes from %s:%d, pid: %d\n",
count_recv, inet_ntoa(rc.client_addr.sin_addr), ntohs(rc.client_addr.sin_port), getpid());
}
if (FD_ISSET(rc.remote_socket, &io))
......@@ -493,7 +496,8 @@ int use()
send(rc.client_socket, buffer, count_recv, 0);
if (options.log > 1)
printf("sent %d\t%s:%d\tpid: %d\n", count_recv, inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port), getpid());
printf("sent %d byets to %s:%d, pid: %d\n",
count_recv, inet_ntoa(rc.client_addr.sin_addr), ntohs(rc.client_addr.sin_port), getpid());
}
}
......
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