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());
 }
 @Override
 public void setPerformancePreferences(int connectionTime, int latency, int bandwidth) {
   proxy.getProxySocket().setPerformancePreferences(connectionTime, latency, bandwidth);
 }
 /**
  * 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);
 }
  @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);
  }
Exemple #5
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());
  }
Exemple #6
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());
  }
 /**
  * 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);
 }
Exemple #8
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());
  }
 @Override
 public boolean isClosed() {
   return proxy.getProxySocket().isClosed();
 }
 @Override
 public synchronized void close() throws IOException {
   proxy.getProxySocket().close();
   proxy.setProxySocket(null);
 }
 @Override
 public boolean getReuseAddress() throws SocketException {
   return proxy.getProxySocket().getReuseAddress();
 }
 @Override
 public int getTrafficClass() throws SocketException {
   return proxy.getProxySocket().getTrafficClass();
 }
 @Override
 public boolean getKeepAlive() throws SocketException {
   return proxy.getProxySocket().getKeepAlive();
 }
 @Override
 public void setTcpNoDelay(boolean on) throws SocketException {
   proxy.getProxySocket().setTcpNoDelay(on);
 }
 @Override
 public boolean getTcpNoDelay() throws SocketException {
   return proxy.getProxySocket().getTcpNoDelay();
 }
 @Override
 public synchronized int getReceiveBufferSize() throws SocketException {
   return proxy.getProxySocket().getReceiveBufferSize();
 }
 @Override
 public synchronized void setReceiveBufferSize(int size) throws SocketException {
   proxy.getProxySocket().setReceiveBufferSize(size);
 }
 @Override
 public void setSoLinger(boolean on, int linger) throws SocketException {
   proxy.getProxySocket().setSoLinger(on, linger);
 }
 @Override
 public void setKeepAlive(boolean on) throws SocketException {
   proxy.getProxySocket().setKeepAlive(on);
 }
 @Override
 public int getSoLinger() throws SocketException {
   return proxy.getProxySocket().getSoLinger();
 }
 @Override
 public void setTrafficClass(int tc) throws SocketException {
   proxy.getProxySocket().setTrafficClass(tc);
 }
 @Override
 public void sendUrgentData(int data) throws IOException {
   proxy.getProxySocket().sendUrgentData(data);
 }
 @Override
 public void setReuseAddress(boolean on) throws SocketException {
   proxy.getProxySocket().setReuseAddress(on);
 }
 @Override
 public boolean getOOBInline() throws SocketException {
   return proxy.getProxySocket().getOOBInline();
 }
 @Override
 public void shutdownOutput() throws IOException {
   proxy.getProxySocket().shutdownOutput();
 }
 @Override
 public void setOOBInline(boolean on) throws SocketException {
   proxy.getProxySocket().setOOBInline(on);
 }
 @Override
 public boolean isOutputShutdown() {
   return proxy.getProxySocket().isOutputShutdown();
 }
 @Override
 public synchronized int getSoTimeout() throws SocketException {
   return proxy.getProxySocket().getSoTimeout();
 }
 public Socket getProxySocket() {
   return proxy.getProxySocket();
 }
 @Override
 public synchronized void setSoTimeout(int timeout) throws SocketException {
   proxy.getProxySocket().setSoTimeout(timeout);
 }