Search in sources :

Example 1 with SocksProxy

use of sockslib.client.SocksProxy in project nzbhydra2 by theotherp.

the class Socks5Server method initProxy.

private void initProxy(Arguments arguments, SocksServerBuilder builder) throws IllegalArgumentException, IOException {
    String proxyValue = arguments.getValue(Arrays.asList("-P", "--proxy"), null);
    String regex = "((\\w+):(\\w+)@)?([.\\w]+):(\\d+)";
    if (proxyValue != null) {
        if (proxyValue.matches(regex)) {
            SocksProxy proxy = null;
            String host = null;
            int port = 1080;
            Credentials credentials = null;
            String[] values = proxyValue.split("@");
            String[] address = null;
            String[] user = null;
            if (values.length == 1) {
                address = values[0].split(":");
            } else {
                user = values[0].split(":");
                address = values[1].split(":");
                credentials = new UsernamePasswordCredentials(user[0], user[1]);
            }
            host = address[0];
            port = Integer.parseInt(address[1]);
            String proxySslValue = arguments.getValue(Arrays.asList("-S", "--proxySsl"), null);
            if (proxySslValue != null) {
                proxy = new SSLSocks5(new InetSocketAddress(host, port), SSLConfiguration.load(proxySslValue));
            }
            SocksProxy tempProxy = initProxySSL(arguments, builder, new InetSocketAddress(host, port));
            if (tempProxy != null) {
                proxy = tempProxy;
            }
            if (proxy == null) {
                proxy = new Socks5(new InetSocketAddress(host, port));
            }
            if (credentials != null) {
                proxy.setCredentials(credentials);
            }
            builder.setProxy(proxy);
        } else {
            logger.error("[-P] or [--proxy] value: [username:password@]host:port");
            throw new IllegalArgumentException();
        }
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) SocksProxy(sockslib.client.SocksProxy) SSLSocks5(sockslib.client.SSLSocks5) Socks5(sockslib.client.Socks5) SSLSocks5(sockslib.client.SSLSocks5) UsernamePasswordCredentials(sockslib.common.UsernamePasswordCredentials) Credentials(sockslib.common.Credentials) UsernamePasswordCredentials(sockslib.common.UsernamePasswordCredentials)

Example 2 with SocksProxy

use of sockslib.client.SocksProxy in project nzbhydra2 by theotherp.

the class TCPTimeServer method run.

@Override
public void run() {
    int port = DEFAULT_PORT;
    SocksProxy socksProxy = null;
    String alwaysResponse = null;
    String proxyHost = null;
    String proxyUsername = null;
    String proxyPassword = null;
    if (args != null) {
        for (String arg : args) {
            if (arg.equals("-h") || arg.equals("--help")) {
                showHelp();
                System.exit(0);
            } else if (arg.startsWith("--port=")) {
                try {
                    port = Arguments.intValueOf(arg);
                } catch (NumberFormatException e) {
                    logger.error("Value of [--port] should be a number");
                    System.exit(-1);
                }
            } else if (arg.startsWith("--always-response=")) {
                alwaysResponse = Arguments.valueOf(arg);
            } else {
                logger.error("Unknown argument [{}]", arg);
                return;
            }
        }
    }
    try {
        server = new ServerSocket(port);
        logger.info("TCP time server created at {}", server.getInetAddress(), port);
        while (!stop) {
            Socket socket = server.accept();
            InputStream inputStream = socket.getInputStream();
            OutputStream outputStream = socket.getOutputStream();
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            int b;
            while ((b = (byte) inputStream.read()) != -1) {
                if (b == '\n') {
                    break;
                }
                byteArrayOutputStream.write(b);
            }
            byte[] buffer = byteArrayOutputStream.toByteArray();
            String receive = new String(buffer);
            logger.info("Client from {} send:{}", socket.getRemoteSocketAddress(), receive);
            if (receive.equals("shutdown")) {
                break;
            }
            String response = new Date().toString();
            if (alwaysResponse != null) {
                response = alwaysResponse;
            }
            response += "\n";
            outputStream.write(response.getBytes());
            outputStream.flush();
            inputStream.close();
            outputStream.close();
            socket.close();
        }
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
    }
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SocksProxy(sockslib.client.SocksProxy) ServerSocket(java.net.ServerSocket) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) Date(java.util.Date)

Aggregations

SocksProxy (sockslib.client.SocksProxy)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 InetSocketAddress (java.net.InetSocketAddress)1 ServerSocket (java.net.ServerSocket)1 Socket (java.net.Socket)1 Date (java.util.Date)1 SSLSocks5 (sockslib.client.SSLSocks5)1 Socks5 (sockslib.client.Socks5)1 Credentials (sockslib.common.Credentials)1 UsernamePasswordCredentials (sockslib.common.UsernamePasswordCredentials)1