1 /*
2  * @(#) $Id: ProxyThread.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.*;
15import javax.net.SocketFactory;
16import javax.net.ServerSocketFactory;
17import javax.net.ssl.SSLServerSocketFactory;
18import javax.net.ssl.SSLServerSocket;
19import javax.net.ssl.SSLSocket;
20import javax.net.ssl.SSLSession;
21import javax.net.ssl.SSLPeerUnverifiedException;
22import java.security.cert.Certificate;
23import java.security.cert.X509Certificate;
24import org.jstk.JSTKArgs;
25
26public class ProxyThread extends Thread {
27    public class Forwarder extends Thread {
28        private JSTKSocket inSock;
29        private JSTKSocket outSock;
30        private JSTKBuffer buf;
31        String id;
32        private Vector paVec = new Vector();
33        public Forwarder(String id, JSTKSocket inSock, JSTKSocket outSock, JSTKBuffer buf){
34            super(id);
35            this.id = id;
36            this.inSock = inSock;
37            this.outSock = outSock;
38            this.buf = buf;
39        }
40
41        public void addProtocolAnalyzer(ProtocolAnalyzer pa){
42            this.paVec.add(pa);
43        }
44
45        public void run() {
46            try {
47                int n;
48                while ((n = inSock.read(buf)) != -1){
49                    for (int i = 0; i < paVec.size(); i++){
50                        ProtocolAnalyzer pa = (ProtocolAnalyzer)paVec.elementAt(i);
51                        pa.analyze(buf);
52                    }
53                    outSock.write(buf);
54                }
55                inSock.close();
56                outSock.close();
57            } catch (Exception e) {
58                try {
59                    inSock.close();
60                    outSock.close();
61                } catch (Exception exc){
62                }
63            }
64        }
65    }
66
67    private JSTKSocket socket1 = null;
68    private boolean verbose = false;
69    private boolean showdata = false;
70    private String threadId = null;
71    private int bufsize = 0;
72    private JSTKArgs args = null;
73    private int thdIndex;
74    String host;
75    int port;
76
77    public ProxyThread(JSTKSocket socket, int thdIndex, JSTKArgs args) throws IOException {
78        super("ProxyThread");
79        this.socket1 = socket;
80        this.thdIndex = thdIndex;
81        showdata = Boolean.valueOf(args.get("showdata")).booleanValue();
82        verbose = Boolean.valueOf(args.get("verbose")).booleanValue();
83        bufsize = Integer.parseInt(args.get("bufsize"));
84        this.args = args;
85
86        this.socket1.getSocket().setTcpNoDelay(true);
87    }
88
89    public void run() {
90        threadId = Thread.currentThread().toString();
91        JSTKSocket socket2 = null;
92        try {
93            socket2 = JSTKSocketUtil.connect(args);
94        } catch (Exception e) {
95            String remoteHost = args.get("host") + ":" + args.get("port");
96            System.err.println("Connection failed: " + remoteHost + ". Exception: " + e);
97            socket1.close();
98            return;
99        }
00
01        if (verbose){
02            System.out.println("[" + thdIndex + "] Established Connection ...");
03            JSTKSocketUtil.print(socket2, " --> ");
04        }
05        JSTKBuffer fbuf = JSTKBuffer.getInstance(bufsize, args);
06        JSTKBuffer rbuf = JSTKBuffer.getInstance(bufsize, args);
07
08        Forwarder cin2cout = new Forwarder("FowardTyhread", socket1, socket2, fbuf);
09        Forwarder cout2cin = new Forwarder("ReverseThread", socket2, socket1, rbuf);
10        String patype = args.get("patype");
11        if (patype != null){
12            String[] patypes = patype.split(",");
13            for (int i = 0; i < patypes.length; i++){
14                String pt = patypes[i].trim();
15                ProtocolAnalyzer fpa = ProtocolAnalyzerFactory.getInstance(pt,"-->");
16                ProtocolAnalyzer rpa = ProtocolAnalyzerFactory.getInstance(pt,"<--");
17                if (fpa != null && rpa != null){
18                    cin2cout.addProtocolAnalyzer(fpa);
19                    cout2cin.addProtocolAnalyzer(rpa);
20                    if (verbose)
21                        System.out.println("Analyzer added for PA Type: " + pt);
22                } else {
23                    if (verbose)
24                        System.out.println("Analyzer not found for PA Type: " + pt);
25                }
26            }
27        }
28        try {
29            cin2cout.start();
30            cout2cin.start();
31            cin2cout.join();
32            if (verbose){
33                System.out.println("[" + thdIndex + "] ... Connection Over.");
34            }
35        } catch (Exception exc){
36            System.err.println("Exception: " + exc);
37        }
38    }
39}
40