Mercurial > 510Connectbot
comparison src/net/sourceforge/jsocks/SocksServerSocket.java @ 0:0ce5cc452d02
initial version
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Thu, 22 May 2014 10:41:19 -0700 |
parents | |
children | 205ee2873330 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:0ce5cc452d02 |
---|---|
1 package net.sourceforge.jsocks; | |
2 | |
3 import java.net.*; | |
4 import java.io.*; | |
5 | |
6 /** | |
7 SocksServerSocket allows to accept connections from one particular | |
8 host through the SOCKS4 or SOCKS5 proxy. | |
9 */ | |
10 public class SocksServerSocket extends ServerSocket { | |
11 //Data members | |
12 protected Proxy proxy; | |
13 protected String localHost; | |
14 protected InetAddress localIP; | |
15 protected int localPort; | |
16 | |
17 boolean doing_direct = false; | |
18 InetAddress remoteAddr; | |
19 | |
20 /** | |
21 *Creates ServerSocket capable of accepting one connection | |
22 *through the firewall, uses given proxy. | |
23 *@param host Host from which the connection should be recieved. | |
24 *@param port Port number of the primary connection. | |
25 */ | |
26 public SocksServerSocket(String host, int port) throws SocksException, | |
27 UnknownHostException, IOException { | |
28 super(0); | |
29 remoteAddr = InetAddress.getByName(host); | |
30 doDirect(); | |
31 } | |
32 | |
33 /** | |
34 * Creates ServerSocket capable of accepting one connection | |
35 * through the firewall, uses default Proxy. | |
36 *@param ip Host from which the connection should be recieved. | |
37 *@param port Port number of the primary connection. | |
38 */ | |
39 public SocksServerSocket(InetAddress ip, int port) throws SocksException, | |
40 IOException { | |
41 this(Proxy.defaultProxy, ip, port); | |
42 } | |
43 | |
44 /** | |
45 *Creates ServerSocket capable of accepting one connection | |
46 *through the firewall, uses given proxy. | |
47 *@param ip Host from which the connection should be recieved. | |
48 *@param port Port number of the primary connection. | |
49 */ | |
50 public SocksServerSocket(Proxy p, InetAddress ip, int port) | |
51 throws SocksException, IOException { | |
52 super(0); | |
53 remoteAddr = ip; | |
54 doDirect(); | |
55 } | |
56 | |
57 | |
58 /** | |
59 * Accepts the incoming connection. | |
60 */ | |
61 public Socket accept() throws IOException { | |
62 Socket s; | |
63 | |
64 if (!doing_direct) { | |
65 if (proxy == null) return null; | |
66 | |
67 ProxyMessage msg = proxy.accept(); | |
68 s = msg.ip == null ? new SocksSocket(msg.host, msg.port, proxy) | |
69 : new SocksSocket(msg.ip, msg.port, proxy); | |
70 //Set timeout back to 0 | |
71 proxy.proxySocket.setSoTimeout(0); | |
72 } | |
73 else { //Direct Connection | |
74 | |
75 //Mimic the proxy behaviour, | |
76 //only accept connections from the speciefed host. | |
77 while (true) { | |
78 s = super.accept(); | |
79 | |
80 if (s.getInetAddress().equals(remoteAddr)) { | |
81 //got the connection from the right host | |
82 //Close listenning socket. | |
83 break; | |
84 } | |
85 else | |
86 s.close(); //Drop all connections from other hosts | |
87 } | |
88 } | |
89 | |
90 proxy = null; | |
91 //Return accepted socket | |
92 return s; | |
93 } | |
94 | |
95 /** | |
96 * Closes the connection to proxy if socket have not been accepted, if | |
97 * the direct connection is used, closes direct ServerSocket. If the | |
98 * client socket have been allready accepted, does nothing. | |
99 */ | |
100 public void close() throws IOException { | |
101 super.close(); | |
102 | |
103 if (proxy != null) proxy.endSession(); | |
104 | |
105 proxy = null; | |
106 } | |
107 | |
108 /** | |
109 Get the name of the host proxy is using to listen for incoming | |
110 connection. | |
111 <P> | |
112 Usefull when address is returned by proxy as the hostname. | |
113 @return the hostname of the address proxy is using to listen | |
114 for incoming connection. | |
115 */ | |
116 public String getHost() { | |
117 return localHost; | |
118 } | |
119 | |
120 /** | |
121 * Get address assigned by proxy to listen for incomming | |
122 * connections, or the local machine address if doing direct | |
123 * connection. | |
124 */ | |
125 public InetAddress getInetAddress() { | |
126 if (localIP == null) { | |
127 try { | |
128 localIP = InetAddress.getByName(localHost); | |
129 } | |
130 catch (UnknownHostException e) { | |
131 return null; | |
132 } | |
133 } | |
134 | |
135 return localIP; | |
136 } | |
137 | |
138 /** | |
139 * Get port assigned by proxy to listen for incoming connections, or | |
140 the port chosen by local system, if accepting directly. | |
141 */ | |
142 public int getLocalPort() { | |
143 return localPort; | |
144 } | |
145 | |
146 /** | |
147 Set Timeout. | |
148 | |
149 @param timeout Amount of time in milliseconds, accept should wait for | |
150 incoming connection before failing with exception. | |
151 Zero timeout implies infinity. | |
152 */ | |
153 public void setSoTimeout(int timeout) throws SocketException { | |
154 super.setSoTimeout(timeout); | |
155 | |
156 if (!doing_direct) proxy.proxySocket.setSoTimeout(timeout); | |
157 } | |
158 | |
159 | |
160 //Private Methods | |
161 ////////////////// | |
162 | |
163 private void doDirect() { | |
164 doing_direct = true; | |
165 localPort = super.getLocalPort(); | |
166 localIP = super.getInetAddress(); | |
167 localHost = localIP.getHostName(); | |
168 } | |
169 | |
170 } |