1 /*
2  * @(#) $Id: ProxyCommand.java,v 1.6 2003/07/08 08:13:53 pankaj Exp $
3  *
4  * Copyright (c) 2002-03 by Pankaj Kumar (http://www.pankaj-k.net). 
5  * All rights reserved.
6  *
7  * The license governing the use of this file can be found in the 
8  * root directory of the containing software.
9  */
10package org.jstk.ssl;
11
12import java.net.*;
13import java.io.*;
14import java.util.HashMap;
15import javax.net.ServerSocketFactory;
16import javax.net.ssl.SSLServerSocketFactory;
17import javax.net.ssl.SSLServerSocket;
18import javax.net.ssl.SSLSocket;
19import javax.net.ssl.SSLSession;
20import javax.net.ssl.SSLPeerUnverifiedException;
21import java.security.cert.Certificate;
22import java.security.cert.X509Certificate;
23import org.jstk.*;
24
25public class ProxyCommand extends JSTKCommandAdapter {
26    private static HashMap defaults = new HashMap();
27    static {
28        defaults.put("inport", "8995");
29        defaults.put("host", "localhost");
30        defaults.put("port", "9000");
31        defaults.put("inproto", "TCP");
32        defaults.put("outproto", "TCP");
33        defaults.put("bufsize", "8192");
34    }
35
36    public String briefDescription(){
37        String briefDesc = "simple proxy for TCP or SSL connections";
38        return briefDesc;
39    }
40
41    public String optionsDescription(){
42        String optionsDesc =
43            "  -inport <inport>: Port No. to accept incoming connection.[" +
44            defaults.get("inport") + "]\n" +
45            "  -host <host>    : Remote host machine name or IP address.[" +
46            defaults.get("host") + "]\n" +
47            "  -port <port>    : Destination port on remote host.[" +
48            defaults.get("port") + "]\n" +
49            "  -patype <type>  : Protocol Analyzed Type (dd, http or ssl).\n" +
50            "  -bufsize <bufsz>: Receiving Buffer Size (in bytes).[" +
51            defaults.get("bufsize") + "]\n" +
52            "  -inproto <proto>: Incoming connection protocol(TCP or SSL).[" +
53            defaults.get("inproto") + "]\n" +
54            "  -outproto <proto>: Outgoing connection protocol(TCP or SSL).[" +
55            defaults.get("outproto") + "]\n" +
56            "  -verbose        : Print a message on receiving data.\n" +
57            "  -showdata       : Show the received data on stdout.\n" +
58            "  -inetaddr <addr>: Use this IP address (useful for multi-homed hosts).\n";
59        return optionsDesc;
60    }
61    public String[] useForms(){
62        String[] useForms = {
63            "[-inport <inport>] [-port <port>] [-patype <type>]"
64        };
65        return useForms;
66    }
67    public String[] sampleUses(){
68        String[] sampleUses = {
69            "",
70            "-inport 2950",
71            "-ssl -cauth"
72        };
73        return sampleUses;
74    }
75
76    public Object execute(JSTKArgs args) throws JSTKException{
77        try {
78            args.setDefaults(defaults);
79            String inport = args.get("inport");
80            String host = args.get("host");
81            int port = Integer.parseInt(args.get("port"));
82            String inetAddrVal = args.get("inetaddr");
83            boolean showData = Boolean.valueOf(args.get("showdata")).booleanValue();
84            boolean verbose = Boolean.valueOf(args.get("verbose")).booleanValue();
85            int bufsize = Integer.parseInt(args.get("bufsize"));
86            String inproto = args.get("inproto");
87            String outproto = args.get("outproto");
88
89            int lport = Integer.parseInt(inport);
90
91            InetAddress localHost = InetAddress.getLocalHost();
92            System.out.println("  Local Host   : " + localHost + ", Listen Port: " + lport);
93            System.out.println("  IN protocol  : " + inproto);
94            System.out.println("  I/O library  : " + JSTKSocketUtil.getIOLibrary(args, inproto));
95
96            System.out.println("  Remote Host  : " + host + ", Port: " + port);
97            System.out.println("  OUT protocol : " + outproto);
98            System.out.println("  I/O library  : " + JSTKSocketUtil.getIOLibrary(args, outproto));
99            System.out.println("  Buffer Size  : " + bufsize);
00            System.out.println("  -----------------------------------");
01            JSTKServerSocket jss = JSTKSocketUtil.createServerSocket(args);
02
03            int count = 0;
04            while (true){
05                JSTKSocket socket = jss.accept();
06                if (verbose){
07                    System.out.println("[" + count + "] Accepted Connection Request ...");
08                    JSTKSocketUtil.print(socket, " <-- ");
09                }
10                new ProxyThread(socket, count, args).start();
11                ++count;
12            }
13        } catch (Exception exc){
14            throw new JSTKException("ProxyCommand execution failed", exc);
15        }
16    }
17
18    public static void main(String[] args) throws Exception {
19        JSTKOptions opts = new JSTKOptions();
20        opts.parse(args, 0);
21        ProxyCommand proxyCmd = new ProxyCommand();
22        JSTKResult result = (JSTKResult)proxyCmd.execute(opts);
23        System.out.println(result.getText());
24        System.exit(result.isSuccess()? 0 : 1);
25    }
26}
27