0
I’m writing a TCP proxy to capture packets sent from my computer to a remote server and vice versa. I connected a fake test server on localhost and Proxy works well, intercepting and directing the incoming packets by a client, using telnet, to the test server (on my pc). The problem is that when I start the Proxy and connect to the real remote server, the Proxy does not capture the packets from that connection. How can I fix this?
proxy.Dart:
class Proxy {
  Proxy(String host, int port) {
    ServerSocket.bind(localhost, port).then((server) {
      print('Listening on $localhost:$port');
      server.listen((socket) async {
        print('Client connected to proxy');
        final clientConn = await Socket.connect(host /* localhost*/, port);
        final client2proxy = ClientToProxy(socket, clientConn);
        final proxy2server = ProxyToServer(clientConn, socket);
      });
    });
  }
}
client_to_proxy.Dart:
class ClientToProxy {
  Socket socket;
  Socket remote;
  String address;
  int port;
  ClientToProxy(Socket sock, Socket rem) {
    socket = sock;
    remote = rem;
    address = socket.remoteAddress.address;
    port = socket.remotePort;
    msgPattern = '[client] ($address:$port) ->';
    socket.listen(onDataHandler,
        onError: onErrorHandler, onDone: onDoneHandler);
  }
  void onDataHandler(List<int> data) {
    if (debug) print('$msgPattern Data: ${String.fromCharCodes(data)}');
    pipeSocket(remote, data);
  }
  void pipeSocket(Socket sock, dynamic data) => sock.add(data);
}
proxy_to_server.Dart:
class ProxyToServer {
  Socket socket;
  Socket remote;
  String address;
  int port;
  ProxyToServer(Socket sock, Socket rem) {
    socket = sock;
    remote = rem;
    address = socket.remoteAddress.address;
    port = socket.remotePort;
    msgPattern = '[server] ($address:$port) ->';
    socket.listen(onDataHandler,
        onError: onErrorHandler, onDone: onDoneHandler);
  }
  void onDataHandler(List<int> data) {
    if (debug) print('$msgPattern Data: ${String.fromCharCodes(data)}');
    pipeSocket(remote, data);
  }
  void pipeSocket(Socket sock, dynamic data) => sock.add(data);
}
How should I intercept these packages?
do not know Dart, but it seems to me that in this line "final clientConn = await Socket.connect(host /* localhost*/, port);" you need to pass the ip of the remote server, where you are "/localhost/"
– zentrunix
Actually I already pass the remote server IP in "host", in case the "localhost" is commented.
– Gabriel Pacheco
truth...does not have a firewall on your machine or remote machine blocking connections ?
– zentrunix
I believe not, I think that if I blocked the connection, the socket would make an exception and end the execution of the program. Maybe I’m forgetting something related to the TCP protocol?
– Gabriel Pacheco