0
|
1 package net.sourceforge.jsocks;
|
|
2
|
|
3 import java.net.*;
|
|
4 import java.io.*;
|
|
5
|
|
6 /**
|
|
7 * SocksSocket tryies to look very similar to normal Socket,
|
|
8 * while allowing connections through the SOCKS4 or 5 proxy.
|
|
9 * To use this class you will have to identify proxy you need
|
|
10 * to use, Proxy class allows you to set default proxy, which
|
|
11 * will be used by all Socks aware sockets. You can also create
|
|
12 * either Socks4Proxy or Socks5Proxy, and use them by passing to the
|
|
13 * appropriate constructors.
|
|
14 * <P>
|
|
15 * Using Socks package can be as easy as that:
|
|
16 *
|
|
17 * <pre><tt>
|
|
18 *
|
|
19 * import Socks.*;
|
|
20 * ....
|
|
21 *
|
|
22 * try{
|
|
23 * //Specify SOCKS5 proxy
|
|
24 * Proxy.setDefaultProxy("socks-proxy",1080);
|
|
25 *
|
|
26 * //OR you still use SOCKS4
|
|
27 * //Code below uses SOCKS4 proxy
|
|
28 * //Proxy.setDefaultProxy("socks-proxy",1080,userName);
|
|
29 *
|
|
30 * Socket s = SocksSocket("some.host.of.mine",13);
|
|
31 * readTimeFromSock(s);
|
|
32 * }catch(SocksException sock_ex){
|
|
33 * //Usually it will turn in more or less meaningfull message
|
|
34 * System.err.println("SocksException:"+sock_ex);
|
|
35 * }
|
|
36 *
|
|
37 * </tt></pre>
|
|
38 *<P>
|
|
39 * However if the need exist for more control, like resolving addresses
|
|
40 * remotely, or using some non-trivial authentication schemes, it can be done.
|
|
41 */
|
|
42
|
|
43 public class SocksSocket extends Socket {
|
|
44 //Data members
|
|
45 protected Proxy proxy;
|
|
46 protected String localHost, remoteHost;
|
|
47 protected InetAddress localIP, remoteIP;
|
|
48 protected int localPort, remotePort;
|
|
49
|
|
50 private Socket directSock = null;
|
|
51
|
|
52 /**
|
|
53 * Tryies to connect to given host and port
|
|
54 * using default proxy. If no default proxy speciefied
|
|
55 * it throws SocksException with error code SOCKS_NO_PROXY.
|
|
56 @param host Machine to connect to.
|
|
57 @param port Port to which to connect.
|
|
58 * @see SocksSocket#SocksSocket(Proxy,String,int)
|
|
59 * @see Socks5Proxy#resolveAddrLocally
|
|
60 */
|
|
61 public SocksSocket(String host, int port)
|
|
62 throws SocksException, UnknownHostException {
|
|
63 this(Proxy.defaultProxy, host, port);
|
|
64 }
|
|
65 /**
|
|
66 * Connects to host port using given proxy server.
|
|
67 @param p Proxy to use.
|
|
68 @param host Machine to connect to.
|
|
69 @param port Port to which to connect.
|
|
70 @throws UnknownHostException
|
|
71 If one of the following happens:
|
|
72 <ol>
|
|
73
|
|
74 <li> Proxy settings say that address should be resolved locally, but
|
|
75 this fails.
|
|
76 <li> Proxy settings say that the host should be contacted directly but
|
|
77 host name can't be resolved.
|
|
78 </ol>
|
|
79 @throws SocksException
|
|
80 If one of the following happens:
|
|
81 <ul>
|
|
82 <li> Proxy is is null.
|
|
83 <li> Proxy settings say that the host should be contacted directly but
|
|
84 this fails.
|
|
85 <li> Socks Server can't be contacted.
|
|
86 <li> Authentication fails.
|
|
87 <li> Connection is not allowed by the SOCKS proxy.
|
|
88 <li> SOCKS proxy can't establish the connection.
|
|
89 <li> Any IO error occured.
|
|
90 <li> Any protocol error occured.
|
|
91 </ul>
|
|
92 @throws IOexception if anything is wrong with I/O.
|
|
93 @see Socks5Proxy#resolveAddrLocally
|
|
94 */
|
|
95 public SocksSocket(Proxy p, String host, int port) throws SocksException,
|
|
96 UnknownHostException {
|
|
97 remoteHost = host;
|
|
98 remotePort = port;
|
|
99 remoteIP = InetAddress.getByName(host);
|
|
100 doDirect();
|
|
101 }
|
|
102
|
|
103 /**
|
|
104 Connects to given ip and port using given Proxy server.
|
|
105 @param p Proxy to use.
|
|
106 @param ip Machine to connect to.
|
|
107 @param port Port to which to connect.
|
|
108
|
|
109 */
|
|
110 public SocksSocket(InetAddress ip, int port) throws SocksException {
|
|
111 this.remoteIP = ip;
|
|
112 this.remotePort = port;
|
|
113 this.remoteHost = ip.getHostName();
|
|
114 doDirect();
|
|
115 }
|
|
116
|
|
117
|
|
118 /**
|
|
119 * These 2 constructors are used by the SocksServerSocket.
|
|
120 * This socket simply overrides remoteHost, remotePort
|
|
121 */
|
|
122 protected SocksSocket(String host, int port, Proxy proxy) {
|
|
123 this.remotePort = port;
|
|
124 this.proxy = proxy;
|
|
125 this.localIP = proxy.proxySocket.getLocalAddress();
|
|
126 this.localPort = proxy.proxySocket.getLocalPort();
|
|
127 this.remoteHost = host;
|
|
128 }
|
|
129 protected SocksSocket(InetAddress ip, int port, Proxy proxy) {
|
|
130 remoteIP = ip;
|
|
131 remotePort = port;
|
|
132 this.proxy = proxy;
|
|
133 this.localIP = proxy.proxySocket.getLocalAddress();
|
|
134 this.localPort = proxy.proxySocket.getLocalPort();
|
|
135 remoteHost = remoteIP.getHostName();
|
|
136 }
|
|
137
|
|
138 /**
|
|
139 * Same as Socket
|
|
140 */
|
|
141 public void close() throws IOException {
|
|
142 if (proxy != null)proxy.endSession();
|
|
143
|
|
144 proxy = null;
|
|
145 }
|
|
146 /**
|
|
147 * Same as Socket
|
|
148 */
|
|
149 public InputStream getInputStream() {
|
|
150 return proxy.in;
|
|
151 }
|
|
152 /**
|
|
153 * Same as Socket
|
|
154 */
|
|
155 public OutputStream getOutputStream() {
|
|
156 return proxy.out;
|
|
157 }
|
|
158 /**
|
|
159 * Same as Socket
|
|
160 */
|
|
161 public int getPort() {
|
|
162 return remotePort;
|
|
163 }
|
|
164 /**
|
|
165 * Returns remote host name, it is usefull in cases when addresses
|
|
166 * are resolved by proxy, and we can't create InetAddress object.
|
|
167 @return The name of the host this socket is connected to.
|
|
168 */
|
|
169 public String getHost() {
|
|
170 return remoteHost;
|
|
171 }
|
|
172 /**
|
|
173 * Get remote host as InetAddress object, might return null if
|
|
174 * addresses are resolved by proxy, and it is not possible to resolve
|
|
175 * it locally
|
|
176 @return Ip address of the host this socket is connected to, or null
|
|
177 if address was returned by the proxy as DOMAINNAME and can't be
|
|
178 resolved locally.
|
|
179 */
|
|
180 public InetAddress getInetAddress() {
|
|
181 if (remoteIP == null) {
|
|
182 try {
|
|
183 remoteIP = InetAddress.getByName(remoteHost);
|
|
184 }
|
|
185 catch (UnknownHostException e) {
|
|
186 return null;
|
|
187 }
|
|
188 }
|
|
189
|
|
190 return remoteIP;
|
|
191 }
|
|
192
|
|
193 /**
|
|
194 * Get the port assigned by the proxy for the socket, not
|
|
195 * the port on locall machine as in Socket.
|
|
196 @return Port of the socket used on the proxy server.
|
|
197 */
|
|
198 public int getLocalPort() {
|
|
199 return localPort;
|
|
200 }
|
|
201
|
|
202 /**
|
|
203 * Get address assigned by proxy to make a remote connection,
|
|
204 * it might be different from the host specified for the proxy.
|
|
205 * Can return null if socks server returned this address as hostname
|
|
206 * and it can't be resolved locally, use getLocalHost() then.
|
|
207 @return Address proxy is using to make a connection.
|
|
208 */
|
|
209 public InetAddress getLocalAddress() {
|
|
210 if (localIP == null) {
|
|
211 try {
|
|
212 localIP = InetAddress.getByName(localHost);
|
|
213 }
|
|
214 catch (UnknownHostException e) {
|
|
215 return null;
|
|
216 }
|
|
217 }
|
|
218
|
|
219 return localIP;
|
|
220 }
|
|
221 /**
|
|
222 Get name of the host, proxy has assigned to make a remote connection
|
|
223 for this socket. This method is usefull when proxy have returned
|
|
224 address as hostname, and we can't resolve it on this machine.
|
|
225 @return The name of the host proxy is using to make a connection.
|
|
226 */
|
|
227 public String getLocalHost() {
|
|
228 return localHost;
|
|
229 }
|
|
230
|
|
231 /**
|
|
232 Same as socket.
|
|
233 */
|
|
234 public void setSoLinger(boolean on, int val) throws SocketException {
|
|
235 proxy.proxySocket.setSoLinger(on, val);
|
|
236 }
|
|
237 /**
|
|
238 Same as socket.
|
|
239 */
|
|
240 public int getSoLinger(int timeout) throws SocketException {
|
|
241 return proxy.proxySocket.getSoLinger();
|
|
242 }
|
|
243 /**
|
|
244 Same as socket.
|
|
245 */
|
|
246 public void setSoTimeout(int timeout) throws SocketException {
|
|
247 proxy.proxySocket.setSoTimeout(timeout);
|
|
248 }
|
|
249 /**
|
|
250 Same as socket.
|
|
251 */
|
|
252 public int getSoTimeout(int timeout) throws SocketException {
|
|
253 return proxy.proxySocket.getSoTimeout();
|
|
254 }
|
|
255 /**
|
|
256 Same as socket.
|
|
257 */
|
|
258 public void setTcpNoDelay(boolean on) throws SocketException {
|
|
259 proxy.proxySocket.setTcpNoDelay(on);
|
|
260 }
|
|
261 /**
|
|
262 Same as socket.
|
|
263 */
|
|
264 public boolean getTcpNoDelay() throws SocketException {
|
|
265 return proxy.proxySocket.getTcpNoDelay();
|
|
266 }
|
|
267
|
|
268 /**
|
|
269 Get string representation of the socket.
|
|
270 */
|
|
271 public String toString() {
|
|
272 if (directSock != null) return "Direct connection:" + directSock;
|
|
273
|
|
274 return ("Proxy:" + proxy + ";" + "addr:" + remoteHost + ",port:" + remotePort
|
|
275 + ",localport:" + localPort);
|
|
276 }
|
|
277
|
|
278 //Private Methods
|
|
279 //////////////////
|
|
280
|
|
281 private void doDirect()throws SocksException {
|
|
282 try {
|
|
283 //System.out.println("IP:"+remoteIP+":"+remotePort);
|
|
284 directSock = new Socket(remoteIP, remotePort);
|
|
285 proxy.out = directSock.getOutputStream();
|
|
286 proxy.in = directSock.getInputStream();
|
|
287 proxy.proxySocket = directSock;
|
|
288 localIP = directSock.getLocalAddress();
|
|
289 localPort = directSock.getLocalPort();
|
|
290 }
|
|
291 catch (IOException io_ex) {
|
|
292 throw new SocksException(Proxy.SOCKS_DIRECT_FAILED,
|
|
293 "Direct connect failed:" + io_ex);
|
|
294 }
|
|
295 }
|
|
296
|
|
297 }
|