001: package test_util;
002:
003: import java.net.*;
004: import java.io.*;
005: import socks.*;
006:
007: /** SOCKS aware echo client*/
008:
009: public class SocksTest implements Runnable {
010:
011: private int port;
012: private InetAddress hostIP;
013:
014: private Socket ss;
015: private InputStream in;
016: private OutputStream out;
017:
018: private static final int BUF_SIZE = 1024;
019: static final int defaultProxyPort = 1080; //Default Port
020: static final String defaultProxyHost = "www-proxy"; //Default proxy
021:
022: public SocksTest(String host, int port) throws IOException,
023: UnknownHostException, SocksException {
024: this .port = port;
025:
026: ss = new SocksSocket(host, port);
027: out = ss.getOutputStream();
028: in = ss.getInputStream();
029: System.out.println("Connected...");
030: System.out.println("TO: " + host + ":" + port);
031: System.out.println("ViaProxy: "
032: + ss.getLocalAddress().getHostAddress() + ":"
033: + ss.getLocalPort());
034:
035: }
036:
037: public void close() throws IOException {
038: ss.close();
039: }
040:
041: public void send(String s) throws IOException {
042: out.write(s.getBytes());
043: }
044:
045: public void run() {
046: byte[] buf = new byte[1024];
047: int bytes_read;
048: try {
049: while ((bytes_read = in.read(buf)) > 0) {
050: System.out.write(buf, 0, bytes_read);
051: }
052: } catch (IOException io_ex) {
053: io_ex.printStackTrace();
054: }
055: }
056:
057: public static void usage() {
058: System.err
059: .print("Usage: java SocksTest host port [socksHost socksPort]\n");
060: }
061:
062: public static void main(String args[]) {
063: int port;
064: String host;
065: int proxyPort;
066: String proxyHost;
067:
068: if (args.length > 1 && args.length < 5) {
069: try {
070:
071: host = args[0];
072: port = Integer.parseInt(args[1]);
073:
074: proxyPort = (args.length > 3) ? Integer
075: .parseInt(args[3]) : defaultProxyPort;
076:
077: host = args[0];
078: proxyHost = (args.length > 2) ? args[2]
079: : defaultProxyHost;
080:
081: Proxy.setDefaultProxy(proxyHost, proxyPort, "KOUKY001");
082: //Proxy.setDefaultProxy(proxyHost,proxyPort);
083: Proxy.getDefaultProxy().setDirect(
084: InetAddress.getByName("localhost"));
085:
086: SocksTest st = new SocksTest(host, port);
087: Thread thread = new Thread(st);
088: thread.start();
089:
090: BufferedReader in = new BufferedReader(
091: new InputStreamReader(System.in));
092: String s;
093:
094: s = in.readLine();
095: while (s != null) {
096: st.send(s + "\r\n");
097: //try{
098: //Thread.currentThread().sleep(10);
099: //}catch(InterruptedException i_ex){
100: //}
101: s = in.readLine();
102: }
103: st.close();
104: System.exit(1);
105:
106: } catch (SocksException s_ex) {
107: System.err.println("SocksException:" + s_ex);
108: s_ex.printStackTrace();
109: System.exit(1);
110: } catch (IOException io_ex) {
111: io_ex.printStackTrace();
112: System.exit(1);
113: } catch (NumberFormatException num_ex) {
114: usage();
115: num_ex.printStackTrace();
116: System.exit(1);
117: }
118:
119: } else {
120: usage();
121: }
122: }
123:
124: }//End of class
|