1 /*
   2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.io.IOException;
  25 import java.net.ServerSocket;
  26 import java.net.Socket;
  27 import java.util.Objects;
  28 import java.util.function.Consumer;
  29 
  30 import javax.net.ServerSocketFactory;
  31 
  32 /*
  33  * A simple socks4 proxy for traveling socket.
  34  */
  35 class SocksProxy implements Runnable, AutoCloseable {
  36 
  37     private ServerSocket server;
  38     private Consumer<Socket> socketConsumer;
  39 
  40     private SocksProxy(ServerSocket server, Consumer<Socket> socketConsumer) {
  41         this.server = server;
  42         this.socketConsumer = socketConsumer;
  43     }
  44 
  45     static SocksProxy startProxy(Consumer<Socket> socketConsumer)
  46             throws IOException {
  47         Objects.requireNonNull(socketConsumer, "socketConsumer cannot be null");
  48 
  49         ServerSocket server
  50                 = ServerSocketFactory.getDefault().createServerSocket(0);
  51 
  52         System.setProperty("socksProxyHost", "127.0.0.1");
  53         System.setProperty("socksProxyPort",
  54                 String.valueOf(server.getLocalPort()));
  55         System.setProperty("socksProxyVersion", "4");
  56 
  57         SocksProxy proxy = new SocksProxy(server, socketConsumer);
  58         Thread proxyThread = new Thread(proxy, "Proxy");
  59         proxyThread.setDaemon(true);
  60         proxyThread.start();
  61 
  62         return proxy;
  63     }
  64 
  65     @Override
  66     public void run() {
  67         while (!server.isClosed()) {
  68             try(Socket socket = server.accept()) {
  69                 System.out.println("Server: accepted connection");
  70                 if (socketConsumer != null) {
  71                     socketConsumer.accept(socket);
  72                 }
  73             } catch (IOException e) {
  74                 if (!server.isClosed()) {
  75                     throw new RuntimeException(
  76                             "Server: accept connection failed", e);
  77                 } else {
  78                     System.out.println("Server is closed.");
  79                 }
  80             }
  81         }
  82     }
  83 
  84     @Override
  85     public void close() throws Exception {
  86         if (!server.isClosed()) {
  87             server.close();
  88         }
  89     }
  90 
  91     int getPort() {
  92         return server.getLocalPort();
  93     }
  94 }