001: package test;
002:
003: import java.io.*;
004: import java.net.*;
005: import socks.*;
006:
007: public class TestClient extends TestService {
008: /** Proxy which should be used*/
009: Proxy proxy;
010: /** Host on which TestServer is running*/
011: String testHost;
012:
013: int timeout = 15000;
014: int acceptTimeout = 0;
015:
016: BufferedReader in;
017: Writer out;
018:
019: public TestClient(Proxy p, String testHost) {
020: this .proxy = p;
021: this .testHost = testHost;
022: if (log == null)
023: log = System.out;
024: }
025:
026: public void start() {
027: connectTests(true);
028: acceptTests(true);
029: udpTests(true);
030:
031: connectTests(false);
032: acceptTests(false);
033: udpTests(false);
034: }
035:
036: void connectTests(boolean useString) {
037: try {
038: open(ECHO, useString);
039: testEcho();
040: s.close();
041:
042: open(DISCARD, useString);
043: testDiscard();
044: s.close();
045:
046: open(CHARGEN, useString);
047:
048: for (int i = 0; i < 3;) {
049: try {
050: testChargen();
051: break;
052: } catch (InterruptedIOException ioe) {
053: log("IO interrupted:" + i);
054: i++;
055: }
056: }
057:
058: s.close();
059:
060: } catch (IOException ioe) {
061: ioe.printStackTrace();
062: }
063:
064: }
065:
066: void acceptTests(boolean useString) {
067: try {
068: testAccept(ECHO, useString);
069: testEcho();
070: s.close();
071:
072: testAccept(DISCARD, useString);
073: testDiscard();
074: s.close();
075:
076: testAccept(CHARGEN, useString);
077:
078: for (int i = 0; i < 3;) {
079: try {
080: testChargen();
081: break;
082: } catch (InterruptedIOException ioe) {
083: log("IO interrupted:" + i);
084: i++;
085: }
086: }
087: s.close();
088:
089: } catch (IOException ioe) {
090: ioe.printStackTrace();
091: }
092: }
093:
094: void udpTests(boolean useString) {
095: log("Udp tests are not yet implemented");
096: }
097:
098: void testEcho() throws IOException {
099: log("Testing echo.");
100: for (int i = 0; i < 5; ++i) {
101: out.write("String number " + i + "\r\n");
102: out.flush();
103: log("Echo:" + in.readLine());
104: ;
105: }
106: log("Echo finished");
107: }
108:
109: void testDiscard() throws IOException {
110: log("Testing discard");
111: for (int i = 0; i < 5; ++i) {
112: log("Sending discard message:" + i);
113: out.write("Discard message:" + i + "\r\n");
114: out.flush();
115: }
116: log("Discard finished");
117: }
118:
119: void testChargen() throws IOException {
120: log("Testing chargen");
121: String s;
122: s = in.readLine();
123: while (s != null) {
124: log("ChGen:" + s);
125: s = in.readLine();
126: }
127: log("Chargen finished.");
128: }
129:
130: void testAccept(int service, boolean useString) throws IOException {
131: open(CONNECT, useString);
132:
133: log("Testing accept");
134: ServerSocket ss;
135:
136: if (useString)
137: ss = new SocksServerSocket(proxy, testHost,
138: servicePorts[service]);
139: else
140: ss = new SocksServerSocket(proxy, InetAddress
141: .getByName(testHost), servicePorts[service]);
142: log("Listenning on " + ss.getInetAddress() + ":"
143: + ss.getLocalPort());
144: ss.setSoTimeout(acceptTimeout);
145:
146: out.write("" + ss.getLocalPort() + " " + service + "\r\n");
147: out.flush();
148:
149: String line = in.readLine();
150: if (line != null) {
151: log("Accept failed:" + line);
152: }
153:
154: s.close();
155:
156: s = ss.accept();
157: log("Accepted:" + s);
158:
159: s.setSoTimeout(timeout);
160:
161: out = new OutputStreamWriter(s.getOutputStream());
162: in = new BufferedReader(new InputStreamReader(s
163: .getInputStream()));
164:
165: ss.close();
166: }
167:
168: void open(int service, boolean useString) throws IOException {
169:
170: if (!useString) {
171: s = new SocksSocket(proxy, InetAddress.getByName(testHost),
172: servicePorts[service]);
173: } else {
174: s = new SocksSocket(proxy, testHost, servicePorts[service]);
175: }
176:
177: s.setSoTimeout(timeout);
178:
179: out = new OutputStreamWriter(s.getOutputStream());
180: in = new BufferedReader(new InputStreamReader(s
181: .getInputStream()));
182: }
183:
184: //Main function
185: ///////////////
186:
187: static void usage() {
188: System.err
189: .println("Usage: java Testclient testhost proxy [directhosts]");
190: }
191:
192: static Proxy initProxy(String ps) {
193: java.util.StringTokenizer st = new java.util.StringTokenizer(
194: ps, ",;");
195: Proxy proxy = null;
196: while (st.hasMoreElements()) {
197: String entry = st.nextToken();
198: Proxy p = Proxy.parseProxy(entry);
199: if (p == null) {
200: log("Proxy " + entry + " invalid.");
201: return null;
202: }
203: p.setChainProxy(proxy);
204: proxy = p;
205: }
206: return proxy;
207: }
208:
209: static void addDirectHosts(Proxy p, String directHosts) {
210: java.util.StringTokenizer st = new java.util.StringTokenizer(
211: directHosts, ",;");
212:
213: while (st.hasMoreElements()) {
214: String entry = st.nextToken();
215: log("Adding direct host:" + entry);
216: p.addDirect(entry);
217: }
218: }
219:
220: public static void main(String[] argv) {
221: if (argv.length < 2) {
222: usage();
223: return;
224: }
225:
226: log = System.out;
227:
228: String testHost = argv[0];
229: String proxyHost = argv[1];
230: String directHosts = argv.length > 2 ? argv[2] : null;
231:
232: Proxy p = initProxy(proxyHost);
233: if (p == null) {
234: log("Can't init proxy.");
235: return;
236: }
237: if (directHosts != null)
238: addDirectHosts(p, directHosts);
239:
240: if (p instanceof Socks5Proxy)
241: ((Socks5Proxy) p).resolveAddrLocally(false);
242:
243: TestClient tc = new TestClient(p, testHost);
244: tc.start();
245:
246: }
247: }
|