@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); }
@Override public void connect(SocketAddress endpoint, int timeout) throws SocksException, IOException { if (!(endpoint instanceof InetSocketAddress)) { throw new IllegalArgumentException("Unsupported address type"); } if (((InetSocketAddress) endpoint).getHostName().contains("127.0.0.1")) { Socket socket = new Socket(); socket.connect(endpoint); } remoteServerHost = ((InetSocketAddress) endpoint).getHostName(); remoteServerPort = ((InetSocketAddress) endpoint).getPort(); proxy.getProxySocket().setSoTimeout(timeout); proxy.buildConnection(); initProxyChain(); proxy.requestConnect(endpoint); }
/** * 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); }
/** * 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); }
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()); }
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()); }
/** * Initialize proxy chain. * * @throws SocketException If a SOCKS protocol error occurred. * @throws IOException If an I/O error occurred. */ private void initProxyChain() throws SocketException, IOException { List<SocksProxy> proxyChain = new ArrayList<SocksProxy>(); SocksProxy temp = proxy; while (temp.getChainProxy() != null) { temp.getChainProxy().setProxySocket(proxySocket); proxyChain.add(temp.getChainProxy()); temp = temp.getChainProxy(); } logger.debug("Proxy chain has:{} proxy", proxyChain.size()); if (proxyChain.size() > 0) { SocksProxy pre = proxy; for (int i = 0; i < proxyChain.size(); i++) { SocksProxy chain = proxyChain.get(i); pre.requestConnect(chain.getInetAddress(), chain.getPort()); proxy.getChainProxy().buildConnection(); pre = chain; } } }
/** * Constructs a server socket. This server socket will established in SOCKS server. * * @param proxy SOCKS proxy. * @param inetAddress The IP address that server socket will accept. * @param port The port that server socket will accept. * @throws SocksException If any error about SOCKS protocol occurs. * @throws IOException If any I/O error occurs. */ public SocksServerSocket(SocksProxy proxy, InetAddress inetAddress, int port) throws SocksException, IOException { this.proxy = proxy.copy(); this.incomePort = port; this.incomeAddress = inetAddress; this.proxy.buildConnection(); // Send BIND command to SOCKS server. CommandReplyMessage replyMesasge = this.proxy.requestBind(incomeAddress, incomePort); // Get a bind IP and port in proxy server. bindAddress = replyMesasge.getIp(); bindPort = replyMesasge.getPort(); logger.debug("Bind at {}:{}", bindAddress, bindPort); }
/** * Create a socket and connect SOCKS Server. * * @param proxy Socks proxy. * @param remoteServerHost Remote sever host. * @param remoteServerPort Remote server port. * @throws SocksException If any errors about SOCKS protocol occurred. * @throws IOException If any IO errors occurred. */ public SocksSocket(SocksProxy proxy, String remoteServerHost, int remoteServerPort) throws SocksException, IOException { this.proxy = checkNotNull(proxy, "Argument [proxy] may not be null").copy(); this.proxy.setProxySocket(proxySocket); this.remoteServerHost = checkNotNull(remoteServerHost, "Argument [remoteServerHost] may not be null"); this.remoteServerPort = remoteServerPort; this.proxy.buildConnection(); proxySocket = this.proxy.getProxySocket(); initProxyChain(); this.proxy.requestConnect(remoteServerHost, remoteServerPort); }
/** * Initialize proxy chain. * * @throws SocketException If a SOCKS protocol error occurred. * @throws IOException If an I/O error occurred. */ private void initProxyChain() throws SocketException, IOException { List<SocksProxy> proxyChain = new ArrayList<SocksProxy>(); SocksProxy temp = proxy; while (temp.getChainProxy() != null) { temp.getChainProxy().setProxySocket(proxySocket); proxyChain.add(temp.getChainProxy()); temp = temp.getChainProxy(); } logger.debug("Proxy chain has:{} proxy", proxyChain.size()); if (proxyChain.size() > 0) { SocksProxy pre = proxy; for (int i = 0; i < proxyChain.size(); i++) { SocksProxy chain = proxyChain.get(i); pre.requestConnect(chain.getInetAddress(), chain.getPort()); proxy.getChainProxy().buildConnection(); pre = chain; } } }
/** * Create a socket and connect SOCKS Server. * * @param proxy Socks proxy. * @param remoteServerHost Remote sever host. * @param remoteServerPort Remote server port. * @throws SocksException If any errors about SOCKS protocol occurred. * @throws IOException If any IO errors occurred. */ public SocksSocket(SocksProxy proxy, String remoteServerHost, int remoteServerPort) throws SocksException, IOException { this.proxy = checkNotNull(proxy, "Argument [proxy] may not be null").copy(); this.proxy.setProxySocket(proxySocket); this.remoteServerHost = checkNotNull(remoteServerHost, "Argument [remoteServerHost] may not be null"); this.remoteServerPort = remoteServerPort; this.proxy.buildConnection(); proxySocket = this.proxy.getProxySocket(); initProxyChain(); this.proxy.requestConnect(remoteServerHost, remoteServerPort); }
/** * Constructs a server socket. This server socket will established in SOCKS server. * * @param proxy SOCKS proxy. * @param inetAddress The IP address that server socket will accept. * @param port The port that server socket will accept. * @throws SocksException If any error about SOCKS protocol occurs. * @throws IOException If any I/O error occurs. */ public SocksServerSocket(SocksProxy proxy, InetAddress inetAddress, int port) throws SocksException, IOException { this.proxy = proxy.copy(); this.incomePort = port; this.incomeAddress = inetAddress; this.proxy.buildConnection(); // Send BIND command to SOCKS server. CommandReplyMessage replyMesasge = this.proxy.requestBind(incomeAddress, incomePort); // Get a bind IP and port in proxy server. bindAddress = replyMesasge.getIp(); bindPort = replyMesasge.getPort(); logger.debug("Bind at {}:{}", bindAddress, bindPort); }
/** * Constructs a datagram socket with a {@link SocksProxy}. <br> * <b>Notice:</b> The proxy must be {@link Socks5}, because only SOCKS5 protocol supports UDP * ASSOCIATE. It will throw {@link SocksException} if you give other {@link SocksProxy} * implementation which not supports SOCKS5 protocol. * * @param proxy {@link Socks5} instance. * @throws SocksException If any error about SOCKS protocol occurs. * @throws IOException If any I/O error occurs. * @see <a href="http://www.ietf.org/rfc/rfc1928.txt">SOCKS Protocol Version 5</a> */ public Socks5DatagramSocket(SocksProxy proxy) throws SocksException, IOException { super(); this.proxy = proxy.copy(); if (this.proxy.getChainProxy() != null) { throw new SocksException("Proxy chain not support UDP ASSOCIATE"); } if (!(this.proxy instanceof Socks5)) { throw new SocksException("Only SOCKS5 protocol support UDP ASSOCIATE"); } this.proxy.buildConnection(); CommandReplyMessage message = this.proxy.requestUdpAssociate(this.getLocalAddress(), this.getLocalPort()); logger.debug("build datagram socket at[{}:{}]", this.getLocalAddress(), this.getLocalPort()); this.relayServerInetAddress = message.getIp(); this.relayServerPort = message.getPort(); logger.info("relay server's address[{}:{}]", relayServerInetAddress, relayServerPort); }
/** * Constructs a datagram socket with a {@link SocksProxy}. <br> * <b>Notice:</b> The proxy must be {@link Socks5}, because only SOCKS5 protocol supports UDP * ASSOCIATE. It will throw {@link SocksException} if you give other {@link SocksProxy} * implementation which not supports SOCKS5 protocol. * * @param proxy {@link Socks5} instance. * @throws SocksException If any error about SOCKS protocol occurs. * @throws IOException If any I/O error occurs. * @see <a href="http://www.ietf.org/rfc/rfc1928.txt">SOCKS Protocol Version 5</a> */ public Socks5DatagramSocket(SocksProxy proxy) throws SocksException, IOException { super(); this.proxy = proxy.copy(); if (this.proxy.getChainProxy() != null) { throw new SocksException("Proxy chain not support UDP ASSOCIATE"); } if (!(this.proxy instanceof Socks5)) { throw new SocksException("Only SOCKS5 protocol support UDP ASSOCIATE"); } this.proxy.buildConnection(); CommandReplyMessage message = this.proxy.requestUdpAssociate(this.getLocalAddress(), this.getLocalPort()); logger.debug("build datagram socket at[{}:{}]", this.getLocalAddress(), this.getLocalPort()); this.relayServerInetAddress = message.getIp(); this.relayServerPort = message.getPort(); logger.info("relay server's address[{}:{}]", relayServerInetAddress, relayServerPort); }