Ejemplo n.º 1
0
  public static void main(String[] args) throws IOException {
    if (args.length == 0) {
      System.out.println("Usage:");
      System.out.println("   {sourceHost}@{sourcePort} {destinationHost}@{destinationPort}");

      System.exit(0);
    }

    PyroSelector listenHub = new PyroSelector();
    PyroSelector trafficHub = new PyroSelector();

    GatewayTwoSelectors gateway = new GatewayTwoSelectors(listenHub, trafficHub);

    for (int i = 0; i < args.length; i += 2) {
      String srcArg = args[i + 0];
      String dstArg = args[i + 1];

      String srcHost = Text.before(srcArg, '@');
      String dstHost = Text.before(dstArg, '@');
      int srcPort = Integer.parseInt(Text.after(srcArg, '@'));
      int dstPort = Integer.parseInt(Text.after(dstArg, '@'));
      InetSocketAddress src = new InetSocketAddress(srcHost, srcPort);
      InetSocketAddress dst = new InetSocketAddress(dstHost, dstPort);

      gateway.addBridge(src, dst);
    }

    listenHub.spawnNetworkThread("listen");
    trafficHub.spawnNetworkThread("traffic");
  }
Ejemplo n.º 2
0
  public static void main(String[] args) throws IOException {
    if (args[0].equals("listen")) {
      final LinkedBlockingQueue<Pair<InetSocketAddress, byte[]>> queue;
      queue = new LinkedBlockingQueue<Pair<InetSocketAddress, byte[]>>();

      DatagramSocket socket = new DatagramSocket(8888);
      System.out.println(socket.getLocalAddress());

      new Thread() {
        public void run() {
          while (true) {
            Pair<InetSocketAddress, byte[]> pair;

            try {
              pair = queue.take();
            } catch (InterruptedException exc) {
              break;
            }

            System.out.println();
            System.out.println(pair.first());
            System.out.println(Text.ascii(pair.second()));
          }
        }
      }.start();

      byte[] buffer = new byte[4 * 1024];

      while (true) {
        DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
        socket.receive(packet);
        byte[] data = Arrays.copyOf(buffer, packet.getLength());
        InetSocketAddress addr = (InetSocketAddress) packet.getSocketAddress();

        Pair<InetSocketAddress, byte[]> pair;
        pair = new Pair<InetSocketAddress, byte[]>(addr, data);
        try {
          queue.put(pair);
        } catch (InterruptedException exc) {
          break;
        }
      }
    } else if (args[0].equals("shout")) {
      DatagramSocket socket = new DatagramSocket();
      System.out.println(socket.getLocalSocketAddress());

      byte[] data = Text.ascii("hello world");
      DatagramPacket packet = new DatagramPacket(data, 0, data.length);
      packet.setAddress(InetAddress.getByName("localhost"));
      packet.setPort(8888);
      socket.send(packet);
    }
  }
Ejemplo n.º 3
0
  public static void main(String[] args) throws Exception {
    String confpath = "./ssltunnel.conf";
    Properties props = new Properties();
    props.load(new StringReader(Text.utf8(FileUtil.readFile(new File(confpath)))));

    final String listenHost = props.getProperty("listen.host");
    final String targetHost = props.getProperty("target.host");
    final int listenPort = Integer.parseInt(props.getProperty("listen.port"));
    final int targetPort = Integer.parseInt(props.getProperty("target.port"));

    String alias = props.getProperty("ssl.alias");
    String storePasswd = props.getProperty("ssl.store.pass");
    String aliasPasswd = props.getProperty("ssl.alias.pass");

    String keystorePath = props.getProperty("ssl.store.path");
    KeytoolStore store = new KeytoolStore(new File(keystorePath), Text.ascii(storePasswd));
    KeytoolKey key = new KeytoolKey(store, alias, Text.ascii(aliasPasswd));

    int backlog = 50;
    final InetAddress listenAddr = InetAddress.getByName(listenHost);
    SSLServerSocket listen = createSSLServerSocket(key, listenPort, backlog, listenAddr);

    final int threadCount = Integer.parseInt(props.getProperty("thread.count"));
    final int threadIdle = Integer.parseInt(props.getProperty("thread.idle"));
    final int threadStack = Integer.parseInt(props.getProperty("thread.stack"));
    final int tcpTimeout = Integer.parseInt(props.getProperty("tcp.timeout"));

    final ExecutorService pool = TcpServer.pool(threadCount, threadIdle, threadStack);

    TcpServer.listen(
        listen,
        new Callback<Socket>() {
          @Override
          public void callback(Socket item) {
            final SSLSocket client = (SSLSocket) item;
            String clientString = client.getInetAddress().toString();

            Socket target = null;
            Future<Long> received = null;
            long sent = 0L;
            try {
              client.startHandshake();

              InetAddress targetAddr = InetAddress.getByName(targetHost);
              target = new Socket(targetAddr, targetPort);

              System.out.println(TextDateTime.now() + " " + clientString + " -> " + targetHost);

              client.setSoTimeout(tcpTimeout);
              target.setSoTimeout(tcpTimeout);

              final Socket target0 = target;

              received =
                  pool.submit(
                      new Callable<Long>() {
                        @Override
                        public Long call() {
                          try {
                            long got = copy(client.getInputStream(), target0.getOutputStream());
                            return Long.valueOf(got);
                          } catch (IOException exc) {
                            return Long.valueOf(0L);
                          } finally {
                            Streams.safeClose(client);
                            Streams.safeClose(target0);
                          }
                        }
                      });

              sent = copy(target.getInputStream(), client.getOutputStream());

            } catch (Exception exc) {
              // exc.printStackTrace();
            } finally {
              Streams.safeClose(client);
              Streams.safeClose(target);
            }

            try {
              System.out.println(
                  TextDateTime.now()
                      + " "
                      + clientString
                      + ", R="
                      + received.get().longValue() / 1024
                      + "K, S="
                      + sent / 1024
                      + "K");
            } catch (Exception exc) {
              //
            }
          }
        },
        pool);
  }