Beispiel #1
0
 /**
  * Connect to SOCKS Server and server will proxy remote server.
  *
  * @param host Remote server's host.
  * @param port Remote server's port.
  * @throws SocksException If any error about SOCKS protocol occurs.
  * @throws IOException If I/O error occurs.
  */
 public void connect(String host, int port) throws SocksException, IOException {
   this.remoteServerHost = checkNotNull(host, "Argument [host] may not be null");
   this.remoteServerPort = checkNotNull(port, "Argument [port] may not be null");
   proxy.buildConnection();
   initProxyChain();
   proxy.requestConnect(remoteServerHost, remoteServerPort);
 }
Beispiel #2
0
  @Override
  public void connect(SocketAddress endpoint, int timeout) throws SocksException, IOException {

    if (!(endpoint instanceof InetSocketAddress)) {
      throw new IllegalArgumentException("Unsupported address type");
    }

    remoteServerHost = ((InetSocketAddress) endpoint).getHostName();
    remoteServerPort = ((InetSocketAddress) endpoint).getPort();

    proxy.getProxySocket().setSoTimeout(timeout);
    proxy.buildConnection();
    initProxyChain();
    proxy.requestConnect(endpoint);
  }
Beispiel #3
0
  @Test
  public void proxySelector() throws Exception {
    server.enqueue(new MockResponse().setBody("abc"));

    ProxySelector proxySelector =
        new ProxySelector() {
          @Override
          public List<Proxy> select(URI uri) {
            return Collections.singletonList(socksProxy.proxy());
          }

          @Override
          public void connectFailed(URI uri, SocketAddress socketAddress, IOException e) {
            throw new AssertionError();
          }
        };

    OkHttpClient client = new OkHttpClient.Builder().setProxySelector(proxySelector).build();

    Request request = new Request.Builder().url(server.url("/")).build();
    Response response = client.newCall(request).execute();
    assertEquals("abc", response.body().string());

    assertEquals(1, socksProxy.connectionCount());
  }
Beispiel #4
0
  @Test
  public void checkRemoteDNSResolve() throws Exception {
    // This testcase will fail if the target is resolved locally instead of through the proxy.
    server.enqueue(new MockResponse().setBody("abc"));

    OkHttpClient client = new OkHttpClient.Builder().setProxy(socksProxy.proxy()).build();

    HttpUrl url =
        server.url("/").newBuilder().host(socksProxy.HOSTNAME_THAT_ONLY_THE_PROXY_KNOWS).build();

    Request request = new Request.Builder().url(url).build();
    Response response1 = client.newCall(request).execute();
    assertEquals("abc", response1.body().string());

    assertEquals(1, socksProxy.connectionCount());
  }
Beispiel #5
0
 /**
  * Creates a SocksSocket instance with a {@link SocksProxy} and a
  *
  * @param proxy SOCKS proxy.
  * @param proxySocket a unconnected socket. it will connect SOCKS server later.
  */
 public SocksSocket(SocksProxy proxy, Socket proxySocket) {
   checkNotNull(proxy, "Argument [proxy] may not be null");
   checkNotNull(proxySocket, "Argument [proxySocket] may not be null");
   checkArgument(!proxySocket.isConnected(), "Proxy socket should be unconnected");
   this.proxySocket = proxySocket;
   this.proxy = proxy.copy();
   this.proxy.setProxySocket(proxySocket);
 }
Beispiel #6
0
  @Test
  public void proxy() throws Exception {
    server.enqueue(new MockResponse().setBody("abc"));
    server.enqueue(new MockResponse().setBody("def"));

    OkHttpClient client = new OkHttpClient.Builder().setProxy(socksProxy.proxy()).build();

    Request request1 = new Request.Builder().url(server.url("/")).build();
    Response response1 = client.newCall(request1).execute();
    assertEquals("abc", response1.body().string());

    Request request2 = new Request.Builder().url(server.url("/")).build();
    Response response2 = client.newCall(request2).execute();
    assertEquals("def", response2.body().string());

    // The HTTP calls should share a single connection.
    assertEquals(1, socksProxy.connectionCount());
  }
Beispiel #7
0
 public SocksSocket(SocksProxy proxy, SocketAddress socketAddress)
     throws SocksException, IOException {
   checkNotNull(proxy, "Argument [proxy] may not be null");
   checkNotNull(socketAddress, "Argument [socketAddress] may not be null");
   checkArgument(socketAddress instanceof InetSocketAddress, "Unsupported address type");
   InetSocketAddress address = (InetSocketAddress) socketAddress;
   this.proxy = proxy.copy();
   this.remoteServerHost = address.getHostString();
   this.remoteServerPort = address.getPort();
   this.proxy.buildConnection();
   proxySocket = this.proxy.getProxySocket();
   initProxyChain();
   this.proxy.requestConnect(address.getAddress(), address.getPort());
 }
Beispiel #8
0
 @Override
 public void setPerformancePreferences(int connectionTime, int latency, int bandwidth) {
   proxy.getProxySocket().setPerformancePreferences(connectionTime, latency, bandwidth);
 }
Beispiel #9
0
 @Override
 public boolean isClosed() {
   return proxy.getProxySocket().isClosed();
 }
Beispiel #10
0
 @Override
 public synchronized void close() throws IOException {
   proxy.getProxySocket().close();
   proxy.setProxySocket(null);
 }
Beispiel #11
0
 @Override
 public boolean getReuseAddress() throws SocketException {
   return proxy.getProxySocket().getReuseAddress();
 }
Beispiel #12
0
 @Override
 public int getTrafficClass() throws SocketException {
   return proxy.getProxySocket().getTrafficClass();
 }
Beispiel #13
0
 @Override
 public boolean getKeepAlive() throws SocketException {
   return proxy.getProxySocket().getKeepAlive();
 }
Beispiel #14
0
 @Override
 public void setTcpNoDelay(boolean on) throws SocketException {
   proxy.getProxySocket().setTcpNoDelay(on);
 }
Beispiel #15
0
 @Override
 public boolean getTcpNoDelay() throws SocketException {
   return proxy.getProxySocket().getTcpNoDelay();
 }
Beispiel #16
0
 @Override
 public synchronized int getReceiveBufferSize() throws SocketException {
   return proxy.getProxySocket().getReceiveBufferSize();
 }
Beispiel #17
0
 @Override
 public synchronized void setReceiveBufferSize(int size) throws SocketException {
   proxy.getProxySocket().setReceiveBufferSize(size);
 }
Beispiel #18
0
 @Override
 public void setSoLinger(boolean on, int linger) throws SocketException {
   proxy.getProxySocket().setSoLinger(on, linger);
 }
Beispiel #19
0
 @Override
 public void setKeepAlive(boolean on) throws SocketException {
   proxy.getProxySocket().setKeepAlive(on);
 }
Beispiel #20
0
 @Override
 public int getSoLinger() throws SocketException {
   return proxy.getProxySocket().getSoLinger();
 }
Beispiel #21
0
 @Override
 public void setTrafficClass(int tc) throws SocketException {
   proxy.getProxySocket().setTrafficClass(tc);
 }
Beispiel #22
0
 @Override
 public void sendUrgentData(int data) throws IOException {
   proxy.getProxySocket().sendUrgentData(data);
 }
Beispiel #23
0
 @Override
 public void setReuseAddress(boolean on) throws SocketException {
   proxy.getProxySocket().setReuseAddress(on);
 }
Beispiel #24
0
 @Override
 public boolean getOOBInline() throws SocketException {
   return proxy.getProxySocket().getOOBInline();
 }
Beispiel #25
0
 @Override
 public void shutdownOutput() throws IOException {
   proxy.getProxySocket().shutdownOutput();
 }
Beispiel #26
0
 @Override
 public void setOOBInline(boolean on) throws SocketException {
   proxy.getProxySocket().setOOBInline(on);
 }
Beispiel #27
0
 @Override
 public boolean isOutputShutdown() {
   return proxy.getProxySocket().isOutputShutdown();
 }
Beispiel #28
0
 @Override
 public synchronized int getSoTimeout() throws SocketException {
   return proxy.getProxySocket().getSoTimeout();
 }
Beispiel #29
0
 public Socket getProxySocket() {
   return proxy.getProxySocket();
 }
Beispiel #30
0
 @Override
 public synchronized void setSoTimeout(int timeout) throws SocketException {
   proxy.getProxySocket().setSoTimeout(timeout);
 }