Commit b8dcbf35 by michaelpastushkov

tcp/udp java

parent 0c771f35
javac net/p4pn/ByteVia.java && java net/p4pn/ByteVia
#!/bin/sh
make clean && make && ./bytevia --local-port=1948 --remote-host=p4pn.net --remote-port=1984 --proto=udp --secret=52341 --log=1
\ No newline at end of file
make clean && make && ./bytevia --local-port=1948 --remote-host=p4pn.net --remote-port=1984 --proto=udp --secret=52341 --log=1 --encrypt=0
import java.io.IOException;
import java.net.*;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.util.Iterator;
public class Proxy {
private static final int LOCAL_PORT = 1948;
private static final int REMOTE_PORT = 1984;
private static final int BUFFER_SIZE = 1024;
private static final String REMOTE_HOST = "p4pn.net"; // Set the actual remote IP
public static void main(String[] args) throws IOException {
if (args.length != 1) {
System.out.println("Usage: java CombinedUDPAndTCPProxy <udp|tcp>");
System.exit(1);
}
String protocol = args[0].toLowerCase();
if (!protocol.equals("udp") && !protocol.equals("tcp")) {
System.out.println("Invalid protocol specified. Use 'udp' or 'tcp'.");
System.exit(1);
}
runProxy(protocol);
}
// Unified method for running both UDP and TCP proxy using NIO and conditionals
private static void runProxy(String protocol) throws IOException {
System.out.println("Running " + protocol.toUpperCase() + " proxy...");
Selector selector = Selector.open();
ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE);
DatagramChannel udpLocalChannel = null;
DatagramChannel udpRemoteChannel = null;
ServerSocketChannel tcpLocalServerChannel = null;
SocketChannel tcpLocalClientChannel = null;
SocketChannel tcpRemoteServerChannel = null;
if (protocol.equals("udp")) {
// Set up the UDP proxy channels
udpLocalChannel = DatagramChannel.open();
udpLocalChannel.socket().bind(new InetSocketAddress(LOCAL_PORT));
udpLocalChannel.configureBlocking(false);
udpLocalChannel.register(selector, SelectionKey.OP_READ);
udpRemoteChannel = DatagramChannel.open();
udpRemoteChannel.connect(new InetSocketAddress(REMOTE_HOST, REMOTE_PORT));
udpRemoteChannel.configureBlocking(false);
udpRemoteChannel.register(selector, SelectionKey.OP_READ);
} else if (protocol.equals("tcp")) {
// Set up the TCP proxy channels
tcpLocalServerChannel = ServerSocketChannel.open();
tcpLocalServerChannel.socket().bind(new InetSocketAddress(LOCAL_PORT));
tcpLocalServerChannel.configureBlocking(false);
tcpLocalServerChannel.register(selector, SelectionKey.OP_ACCEPT);
}
while (true) {
selector.select(); // Wait for events
Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
while (iterator.hasNext()) {
SelectionKey key = iterator.next();
iterator.remove();
if (protocol.equals("udp")) {
handleUDP(key, buffer, udpLocalChannel, udpRemoteChannel);
} else if (protocol.equals("tcp")) {
handleTCP(key, selector, buffer, tcpLocalServerChannel, tcpLocalClientChannel, tcpRemoteServerChannel);
}
}
}
}
// Method to handle UDP proxy functionality
private static void handleUDP(SelectionKey key, ByteBuffer buffer, DatagramChannel localChannel, DatagramChannel remoteChannel) throws IOException {
if (key.isReadable()) {
if (key.channel() == localChannel) {
// Handle data from local client
buffer.clear();
SocketAddress clientAddress = localChannel.receive(buffer); // Correct: receive() for UDP
buffer.flip();
System.out.println("Received from local client: " + new String(buffer.array(), 0, buffer.limit()));
// Forward to remote server
remoteChannel.send(buffer, new InetSocketAddress(REMOTE_HOST, REMOTE_PORT)); // Correct: send() for UDP
System.out.println("Forwarded to remote server");
} else if (key.channel() == remoteChannel) {
// Handle data from remote server
buffer.clear();
SocketAddress remoteAddress = remoteChannel.receive(buffer); // Correct: receive() for UDP
buffer.flip();
System.out.println("Received from remote server: " + new String(buffer.array(), 0, buffer.limit()));
// Forward back to local client
localChannel.send(buffer, new InetSocketAddress("localhost", LOCAL_PORT)); // Correct: send() for UDP
System.out.println("Forwarded back to local client");
}
}
}
// Method to handle TCP proxy functionality
private static void handleTCP(SelectionKey key, Selector selector, ByteBuffer buffer,
ServerSocketChannel tcpLocalServerChannel, SocketChannel tcpLocalClientChannel,
SocketChannel tcpRemoteServerChannel) throws IOException {
if (key.isAcceptable()) {
// Accept a connection from the local client
tcpLocalClientChannel = tcpLocalServerChannel.accept();
if (tcpLocalClientChannel != null) {
tcpLocalClientChannel.configureBlocking(false);
tcpLocalClientChannel.register(selector, SelectionKey.OP_READ);
System.out.println("Accepted new TCP connection from local client");
// Set up the remote server connection
tcpRemoteServerChannel = setupRemoteTCPChannel();
tcpRemoteServerChannel.configureBlocking(false);
tcpRemoteServerChannel.register(selector, SelectionKey.OP_READ);
}
} else if (key.isReadable()) {
// Handle read operations for TCP proxy
SocketChannel channel = (SocketChannel) key.channel();
buffer.clear();
int bytesRead = channel.read(buffer);
if (bytesRead == -1) {
channel.close();
return;
}
buffer.flip();
// Forward data between local and remote
if (channel == tcpLocalClientChannel) {
tcpRemoteServerChannel.write(buffer);
System.out.println("Forwarded to remote server");
} else if (channel == tcpRemoteServerChannel) {
tcpLocalClientChannel.write(buffer);
System.out.println("Forwarded back to local client");
}
}
}
// Helper method to establish a connection to the remote TCP server
private static SocketChannel setupRemoteTCPChannel() throws IOException {
SocketChannel remoteChannel = SocketChannel.open();
remoteChannel.configureBlocking(false);
remoteChannel.connect(new InetSocketAddress(REMOTE_HOST, REMOTE_PORT));
while (!remoteChannel.finishConnect()) {
// Wait for the connection to finish
}
return remoteChannel;
}
}
import java.io.IOException;
import java.net.*;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.util.Iterator;
public class UDPProxy {
private static final int LOCAL_PORT = 1948;
private static final int REMOTE_PORT = 1984;
private static final int BUFFER_SIZE = 1024;
private static final String REMOTE_HOST = "p4pn.net"; // Set the actual remote IP
public static void main(String[] args) throws IOException {
// Initialize channels and buffer
DatagramChannel localChannel = DatagramChannel.open();
DatagramChannel remoteChannel = DatagramChannel.open();
localChannel.socket().bind(new InetSocketAddress(LOCAL_PORT));
localChannel.configureBlocking(false); // Set to non-blocking mode
remoteChannel.connect(new InetSocketAddress(REMOTE_HOST, REMOTE_PORT));
remoteChannel.configureBlocking(false); // Set to non-blocking mode
Selector selector = Selector.open();
ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE);
// Register the channels with the selector for read operations
localChannel.register(selector, SelectionKey.OP_READ);
remoteChannel.register(selector, SelectionKey.OP_READ);
System.out.println("UDP Proxy started on local port " + LOCAL_PORT + ", forwarding to remote " + REMOTE_HOST + ":" + REMOTE_PORT);
while (true) {
selector.select(); // Wait for events
// Iterate over the selected keys (events)
Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
while (iterator.hasNext()) {
SelectionKey key = iterator.next();
iterator.remove(); // Remove the current key to avoid processing it again
if (key.isReadable()) {
if (key.channel() == localChannel) {
// Handle data from local client
buffer.clear();
SocketAddress clientAddress = localChannel.receive(buffer);
buffer.flip();
System.out.println("Received from local client: " + new String(buffer.array(), 0, buffer.limit()));
// Forward to remote server
remoteChannel.write(buffer);
System.out.println("Forwarded to remote server");
} else if (key.channel() == remoteChannel) {
// Handle data from remote server
buffer.clear();
remoteChannel.read(buffer);
buffer.flip();
System.out.println("Received from remote server: " + new String(buffer.array(), 0, buffer.limit()));
// Forward back to local client
localChannel.send(buffer, new InetSocketAddress("localhost", LOCAL_PORT));
System.out.println("Forwarded back to local client");
}
}
}
}
}
}
......@@ -412,15 +412,18 @@ int find_client(struct sockaddr_in client_addr)
{
int i;
for (i = 0; i < MAX_CLIENTS; i++)
{
if (clients[i].pid > 0 && compare_clients(clients[i].addr, client_addr))
{
return i;
}
}
return -1;
}
int wait_reconnect()
{
#ifndef __MINGW32__
struct msghdr message_header;
struct cmsghdr *control_message;
struct iovec io_vector;
......@@ -428,7 +431,7 @@ int wait_reconnect()
char control_buffer[CMSG_SPACE(sizeof(int))];
ssize_t received_length;
int *received_fd_list;
struct timeval timeout;
printf("%d waiting for reconnect...\n", getpid());
// Setting up the I/O vector for message reception
......@@ -442,7 +445,11 @@ int wait_reconnect()
message_header.msg_controllen = CMSG_SPACE(sizeof(int));
message_header.msg_control = control_buffer;
// Receiving the message
// Receiving the message with timeout
timeout.tv_sec = 5;
timeout.tv_usec = 0;
setsockopt(rc.notify_sockets[1], SOL_SOCKET, SO_RCVTIMEO, (const char *)&timeout, sizeof(timeout));
received_length = recvmsg(rc.notify_sockets[1], &message_header, 0);
if (received_length < 0)
{
......@@ -461,14 +468,14 @@ int wait_reconnect()
break;
}
}
printf("%d reconnected with socket %d...\n", getpid(), rc.client_socket);
#endif
return 0;
}
int notify_reconnect(int client_index)
{
#ifndef __MINGW32__
struct msghdr message_header;
struct cmsghdr *control_message;
struct iovec io_vector;
......@@ -504,7 +511,7 @@ int notify_reconnect(int client_index)
sendmsg(rc.notify_sockets[0], &message_header, 0);
printf("%d notification sent...\n", getpid());
#endif
return 0;
}
......@@ -558,7 +565,7 @@ int use()
}
}
/* Processing request from local, sending it to remote*/
/* 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);
......@@ -567,8 +574,9 @@ int use()
{
perror("use: recv(rc.client_socket)");
close(rc.client_socket);
if (wait_reconnect() == 0)
continue;
if (options.proto == SOCK_STREAM)
if (wait_reconnect() == 0)
continue;
close(rc.remote_socket);
return 1;
}
......@@ -625,7 +633,7 @@ int use()
send(rc.client_socket, buffer, count_recv, 0);
if (options.log > 2)
printf("sent %d byets to %s:%d, pid: %d\n",
printf("sent %d bytes to %s:%d, pid: %d\n",
count_recv, inet_ntoa(rc.client_addr.sin_addr), ntohs(rc.client_addr.sin_port), getpid());
}
}
......@@ -655,15 +663,12 @@ void show_clients() {
}
void cleanup_clients() {
printf("cleaning up ...\n");
int i, pid, status;
for (i=0; i<MAX_CLIENTS; i++) {
if (clients[i].pid)
{
pid = waitpid(clients[i].pid, &status, WNOHANG);
printf("checking on %d: %d\n", clients[i].pid, pid);
if (pid)
{
printf("%d exited with code %d\n", clients[i].pid, WEXITSTATUS(status));
......@@ -683,7 +688,6 @@ void forking()
int client_index = find_client(rc.client_addr);
if (client_index == -1)
{
if (options.proto == SOCK_STREAM)
{
if (socketpair(AF_UNIX, SOCK_STREAM, 0, rc.notify_sockets) == -1)
......
#!/bin/sh
gcc -o bytevia bytevia.c && ./bytevia --local-port=1984 --remote-host=p4pn.net --remote-port=1948 --mode=server --secret=52341 --fork --encrypt=1 --log=2
gcc -o bytevia bytevia.c && ./bytevia --local-port=1984 --remote-host=p4pn.net --remote-port=1948 --mode=server --secret=52341 --encrypt=1 --log=2 --fork
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