/** * Returns a {@link SocksException} instance with a message "Protocol not supported". * * @return An instance of {@link SocksException}. */ public static SocksException protocolNotSupported() { return new SocksException("Protocol not supported"); }
/** * Returns a {@link SocksException} instance with a message "NO ACCEPTABLE METHODS". * * @return An instance of {@link SocksException}. */ public static SocksException noAcceptableMethods() { return new SocksException(NO_ACCEPTABLE_METHODS); }
/** * Returns a {@link SocksException} instance with a message "Protocol not supported". * * @return An instance of {@link SocksException}. */ public static SocksException protocolNotSupported() { return new SocksException("Protocol not supported"); }
/** * Returns a {@link SocksException} instance with a message "NO ACCEPTABLE METHODS". * * @return An instance of {@link SocksException}. */ public static SocksException noAcceptableMethods() { return new SocksException(NO_ACCEPTABLE_METHODS); }
/** * Returns a {@link SocksException} instance with a message of reply. * * @param reply Code of server's reply. * @return An instance of {@link SocksException}. */ public static SocksException serverReplyException(byte reply) { int code = reply; code = code & 0xff; if (code < 0 || code > 0x08) { return new SocksException("Unknown reply"); } code = code - 1; return new SocksException(serverReplyMessage[code]); }
/** * Returns a {@link SocksException} instance with a message of reply. * * @param reply Code of server's reply. * @return An instance of {@link SocksException}. */ public static SocksException serverReplyException(byte reply) { int code = reply; code = code & 0xff; if (code < 0 || code > 0x08) { return new SocksException("Unknown reply"); } code = code - 1; return new SocksException(serverReplyMessage[code]); }
@Override protected ServerSocket createServerSocket(int bindPort, InetAddress bindAddr) throws IOException { try { return createSSLServer(bindPort, bindAddr); } catch (Exception e) { throw new SocksException(e.getMessage()); } }
/** * Accepts a connection.<br> * <b>Notice:</b> This method can be called only once. It will throw SocksException if this method * is called more than once. */ @Override public synchronized Socket accept() throws SocksException, IOException { if (alreadyAccepted) { throw new SocksException("SOCKS4/SOCKS5 protocol only allows one income connection"); } alreadyAccepted = true; return proxy.accept(); }
@Override protected ServerSocket createServerSocket(int bindPort, InetAddress bindAddr) throws IOException { try { return createSSLServer(bindPort, bindAddr); } catch (Exception e) { throw new SocksException(e.getMessage()); } }
/** * Accepts a connection.<br> * <b>Notice:</b> This method can be called only once. It will throw SocksException if this method * is called more than once. */ @Override public synchronized Socket accept() throws SocksException, IOException { if (alreadyAccepted) { throw new SocksException("SOCKS4/SOCKS5 protocol only allows one income connection"); } alreadyAccepted = true; return proxy.accept(); }
@Override public Socket createProxySocket(InetAddress address, int port) throws IOException { try { return configuration.getSSLSocketFactory().createSocket(address, port); } catch (SSLConfigurationException e) { throw new SocksException(e.getMessage()); } }
@Override public Socket createProxySocket() throws IOException { try { return configuration.getSSLSocketFactory().createSocket(); } catch (SSLConfigurationException e) { throw new SocksException(e.getMessage()); } }
@Override public Socket createProxySocket() throws IOException { try { return configuration.getSSLSocketFactory().createSocket(); } catch (SSLConfigurationException e) { throw new SocksException(e.getMessage()); } }
@Override public Socket createProxySocket(InetAddress address, int port) throws IOException { try { return configuration.getSSLSocketFactory().createSocket(address, port); } catch (SSLConfigurationException e) { throw new SocksException(e.getMessage()); } }
@Override public SocksMethod doRequest(List<SocksMethod> acceptableMethods, Socket socket, int socksVersion) throws SocksException, IOException { InputStream inputStream = socket.getInputStream(); OutputStream outputStream = socket.getOutputStream(); byte[] bufferSent = new byte[2 + acceptableMethods.size()]; bufferSent[0] = (byte) socksVersion; bufferSent[1] = (byte) acceptableMethods.size(); for (int i = 0; i < acceptableMethods.size(); i++) { bufferSent[2 + i] = (byte) acceptableMethods.get(i).getByte(); } outputStream.write(bufferSent); outputStream.flush(); logger.debug("{}", LogMessageBuilder.build(bufferSent, MsgType.SEND)); // Received data. byte[] receivedData = StreamUtil.read(inputStream, 2); logger.debug("{}", LogMessageBuilder.build(receivedData, MsgType.RECEIVE)); if (receivedData[0] != socksVersion) { throw new SocksException("Remote server don't support SOCKS5"); } return SocksMethodRegistry.getByByte(receivedData[1]); }
@Override public SocksMethod doRequest(List<SocksMethod> acceptableMethods, Socket socket, int socksVersion) throws SocksException, IOException { InputStream inputStream = socket.getInputStream(); OutputStream outputStream = socket.getOutputStream(); byte[] bufferSent = new byte[2 + acceptableMethods.size()]; bufferSent[0] = (byte) socksVersion; bufferSent[1] = (byte) acceptableMethods.size(); for (int i = 0; i < acceptableMethods.size(); i++) { bufferSent[2 + i] = (byte) acceptableMethods.get(i).getByte(); } outputStream.write(bufferSent); outputStream.flush(); logger.debug("{}", LogMessageBuilder.build(bufferSent, MsgType.SEND)); // Received data. byte[] receivedData = StreamUtil.read(inputStream, 2); logger.debug("{}", LogMessageBuilder.build(receivedData, MsgType.RECEIVE)); if (receivedData[0] != socksVersion) { throw new SocksException("Remote server don't support SOCKS5"); } return SocksMethodRegistry.getByByte(receivedData[1]); }
bufferSent = new byte[6 + LENGTH_OF_IPV6]; } else { throw new SocksException("Address error");// TODO
bufferSent = new byte[6 + LENGTH_OF_IPV6]; } else { throw new SocksException("Address error");// TODO
/** * 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); }