Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SSLPoke should support Proxy - giving the solution in the comments #8

Open
kunalgithub opened this issue Apr 11, 2024 · 1 comment
Open
Assignees

Comments

@kunalgithub
Copy link

kunalgithub commented Apr 11, 2024

SSLPoke should support proxy

Here i am creating version of SSLPoke with proxy

import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import java.io.*;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Socket;
import java.util.Arrays;
import java.util.Optional;
import java.util.regex.Pattern;

/**
 * Establish a SSL connection to a host and port, writes a byte and
 * prints the response. See
 * http://confluence.atlassian.com/display/JIRA/Connecting+to+SSL+services
 */
public class SSLPoke {

    private static final Pattern PATTERN = Pattern.compile(
            "^(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d\\d?|2[0-4]\\d|25[0-5])$");

    public static void main(String[] args) throws IOException {
        if (args.length < 2) {
            System.out.println("Usage: " + SSLPoke.class.getName() + " <host> <port>");
            System.out.println("Usage: Additionally Supply Proxy : " + SSLPoke.class.getName() + "" +
                    "<host> <port> --proxy=proxyHost:proxyPort");
            System.exit(1);
        }

        InetSocketAddress inetSocketAddress = getProxy(args);
        boolean proxyEnabled = inetSocketAddress != null;
        Socket socket = null;

        if (proxyEnabled) {
            Proxy proxy = new Proxy(Proxy.Type.HTTP,inetSocketAddress);
            socket = new Socket(proxy);
            InetSocketAddress address = new InetSocketAddress(args[0], 443);
            socket.connect(address);
        }

        try {
            SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
            SSLSocket sslsocket = null;
            if (proxyEnabled) {
                sslsocket = (SSLSocket) sslsocketfactory.createSocket(socket, args[0], Integer.parseInt(args[1]),
                        true);
            } else {
                sslsocket = (SSLSocket) sslsocketfactory.createSocket(args[0], Integer.parseInt(args[1]));
            }
            InputStream in = sslsocket.getInputStream();
            OutputStream out = sslsocket.getOutputStream();
            out.write(1);
            while (in.available() > 0) {
                System.out.print(in.read());
            }
            System.out.println("Successfully connected");

        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    public static InetSocketAddress getProxy(String[] args) {
        Optional<String> optProxy = Arrays.stream(args).filter(arg -> arg.startsWith("--proxy")).findFirst();
        if (optProxy.isPresent() && optProxy.get().split("=").length > 1 &&
                optProxy.get().split("=")[1].split(":").length > 1) {
            String[] proxy = optProxy.get().split("=")[1].split(":");
            try {
                if (!validate(proxy[0])) {
                    System.out.println("Not a valid IP");
                    System.exit(1);
                }
                return new InetSocketAddress(proxy[0], Integer.valueOf(proxy[1]));
            } catch (Throwable e) {
                System.out.println("PROXY FORMAT --proxy=IP:PORT");
                e.printStackTrace();
                System.exit(1);
            }
        }
        System.out.println("PROXY NOT PROVIDED");
        return null;
    }

    public static boolean hasProxyFlag(String[] args) {
        Optional<String> optProxy = Arrays.stream(args).filter(arg -> arg.startsWith("--proxy")).findFirst();
        return optProxy.isPresent();
    }

    public static boolean validate(final String ip) {
        return PATTERN.matcher(ip).matches();
    }
}
@kunalgithub kunalgithub changed the title SSLPoke should support Proxy SSLPoke should support Proxy - giving the solution in the comments Apr 11, 2024
@MichalHecko
Copy link
Owner

MichalHecko commented Apr 11, 2024

Thank you a lot. I will do a review and merge your changes later. My old code needs a little polish

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants