Mercurial > 510Connectbot
annotate app/src/main/java/net/sourceforge/jsocks/Socks5DatagramSocket.java @ 523:775a81a78a2b stable-1.9.4-7
add more debug code
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Fri, 31 May 2024 18:53:22 -0600 |
parents | d29cce60f393 |
children |
rev | line source |
---|---|
349
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
1 package net.sourceforge.jsocks; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
2 import java.net.*; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
3 import java.io.*; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
4 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
5 /** |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
6 Datagram socket to interract through the firewall.<BR> |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
7 Can be used same way as the normal DatagramSocket. One should |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
8 be carefull though with the datagram sizes used, as additional data |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
9 is present in both incomming and outgoing datagrams. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
10 <p> |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
11 SOCKS5 protocol allows to send host address as either: |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
12 <ul> |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
13 <li> IPV4, normal 4 byte address. (10 bytes header size) |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
14 <li> IPV6, version 6 ip address (not supported by Java as for now). |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
15 22 bytes header size. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
16 <li> Host name,(7+length of the host name bytes header size). |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
17 </ul> |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
18 As with other Socks equivalents, direct addresses are handled |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
19 transparently, that is data will be send directly when required |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
20 by the proxy settings. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
21 <p> |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
22 <b>NOTE:</b><br> |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
23 Unlike other SOCKS Sockets, it <b>does not</b> support proxy chaining, |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
24 and will throw an exception if proxy has a chain proxy attached. The |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
25 reason for that is not my laziness, but rather the restrictions of |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
26 the SOCKSv5 protocol. Basicaly SOCKSv5 proxy server, needs to know from |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
27 which host:port datagrams will be send for association, and returns address |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
28 to which datagrams should be send by the client, but it does not |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
29 inform client from which host:port it is going to send datagrams, in fact |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
30 there is even no guarantee they will be send at all and from the same address |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
31 each time. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
32 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
33 */ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
34 public class Socks5DatagramSocket extends DatagramSocket{ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
35 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
36 InetAddress relayIP; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
37 int relayPort; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
38 Socks5Proxy proxy; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
39 private boolean server_mode = false; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
40 UDPEncapsulation encapsulation; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
41 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
42 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
43 /** |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
44 Construct Datagram socket for communication over SOCKS5 proxy |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
45 server. This constructor uses default proxy, the one set with |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
46 CProxy.setDefaultProxy() method. If default proxy is not set or |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
47 it is set to version4 proxy, which does not support datagram |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
48 forwarding, throws SocksException. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
49 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
50 */ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
51 public Socks5DatagramSocket() throws SocksException, |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
52 IOException{ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
53 this(CProxy.defaultProxy,0,null); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
54 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
55 /** |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
56 Construct Datagram socket for communication over SOCKS5 proxy |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
57 server. And binds it to the specified local port. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
58 This constructor uses default proxy, the one set with |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
59 CProxy.setDefaultProxy() method. If default proxy is not set or |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
60 it is set to version4 proxy, which does not support datagram |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
61 forwarding, throws SocksException. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
62 */ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
63 public Socks5DatagramSocket(int port) throws SocksException, |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
64 IOException{ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
65 this(CProxy.defaultProxy,port,null); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
66 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
67 /** |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
68 Construct Datagram socket for communication over SOCKS5 proxy |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
69 server. And binds it to the specified local port and address. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
70 This constructor uses default proxy, the one set with |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
71 CProxy.setDefaultProxy() method. If default proxy is not set or |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
72 it is set to version4 proxy, which does not support datagram |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
73 forwarding, throws SocksException. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
74 */ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
75 public Socks5DatagramSocket(int port,InetAddress ip) throws SocksException, |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
76 IOException{ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
77 this(CProxy.defaultProxy,port,ip); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
78 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
79 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
80 /** |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
81 Constructs datagram socket for communication over specified proxy. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
82 And binds it to the given local address and port. Address of null |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
83 and port of 0, signify any availabale port/address. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
84 Might throw SocksException, if: |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
85 <ol> |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
86 <li> Given version of proxy does not support UDP_ASSOCIATE. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
87 <li> CProxy can't be reached. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
88 <li> Authorization fails. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
89 <li> CProxy does not want to perform udp forwarding, for any reason. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
90 </ol> |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
91 Might throw IOException if binding dtagram socket to given address/port |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
92 fails. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
93 See java.net.DatagramSocket for more details. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
94 */ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
95 public Socks5DatagramSocket(CProxy p,int port,InetAddress ip) |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
96 throws SocksException, |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
97 IOException{ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
98 super(port,ip); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
99 if(p == null) throw new SocksException(CProxy.SOCKS_NO_PROXY); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
100 if(!(p instanceof Socks5Proxy)) |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
101 throw new SocksException(-1,"Datagram Socket needs Proxy version 5"); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
102 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
103 if(p.chainProxy != null) |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
104 throw new SocksException(CProxy.SOCKS_JUST_ERROR, |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
105 "Datagram Sockets do not support proxy chaining."); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
106 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
107 proxy =(Socks5Proxy) p.copy(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
108 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
109 ProxyMessage msg = proxy.udpAssociate(super.getLocalAddress(), |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
110 super.getLocalPort()); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
111 relayIP = msg.ip; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
112 if(relayIP.getHostAddress().equals("0.0.0.0")) relayIP = proxy.proxyIP; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
113 relayPort = msg.port; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
114 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
115 encapsulation = proxy.udp_encapsulation; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
116 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
117 //debug("Datagram Socket:"+getLocalAddress()+":"+getLocalPort()+"\n"); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
118 //debug("Socks5Datagram: "+relayIP+":"+relayPort+"\n"); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
119 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
120 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
121 /** |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
122 Used by UDPRelayServer. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
123 */ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
124 Socks5DatagramSocket(boolean server_mode,UDPEncapsulation encapsulation, |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
125 InetAddress relayIP,int relayPort) |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
126 throws IOException{ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
127 super(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
128 this.server_mode = server_mode; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
129 this.relayIP = relayIP; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
130 this.relayPort = relayPort; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
131 this.encapsulation = encapsulation; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
132 this.proxy = null; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
133 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
134 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
135 /** |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
136 Sends the Datagram either through the proxy or directly depending |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
137 on current proxy settings and destination address. <BR> |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
138 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
139 <B> NOTE: </B> DatagramPacket size should be at least 10 bytes less |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
140 than the systems limit. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
141 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
142 <P> |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
143 See documentation on java.net.DatagramSocket |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
144 for full details on how to use this method. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
145 @param dp Datagram to send. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
146 @throws IOException If error happens with I/O. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
147 */ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
148 public void send(DatagramPacket dp) throws IOException{ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
149 //If the host should be accessed directly, send it as is. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
150 if(!server_mode && proxy.isDirect(dp.getAddress())){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
151 super.send(dp); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
152 //debug("Sending directly:"); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
153 return; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
154 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
155 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
156 byte[] head = formHeader(dp.getAddress(),dp.getPort()); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
157 byte[] buf = new byte[head.length + dp.getLength()]; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
158 byte[] data = dp.getData(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
159 //Merge head and data |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
160 System.arraycopy(head,0,buf,0,head.length); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
161 //System.arraycopy(data,dp.getOffset(),buf,head.length,dp.getLength()); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
162 System.arraycopy(data,0,buf,head.length,dp.getLength()); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
163 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
164 if(encapsulation != null) |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
165 buf = encapsulation.udpEncapsulate(buf,true); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
166 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
167 super.send(new DatagramPacket(buf,buf.length,relayIP,relayPort)); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
168 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
169 /** |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
170 This method allows to send datagram packets with address type DOMAINNAME. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
171 SOCKS5 allows to specify host as names rather than ip addresses.Using |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
172 this method one can send udp datagrams through the proxy, without having |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
173 to know the ip address of the destination host. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
174 <p> |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
175 If proxy specified for that socket has an option resolveAddrLocally set |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
176 to true host will be resolved, and the datagram will be send with address |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
177 type IPV4, if resolve fails, UnknownHostException is thrown. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
178 @param dp Datagram to send, it should contain valid port and data |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
179 @param host Host name to which datagram should be send. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
180 @throws IOException If error happens with I/O, or the host can't be |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
181 resolved when proxy settings say that hosts should be resolved locally. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
182 @see Socks5Proxy#resolveAddrLocally(boolean) |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
183 */ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
184 public void send(DatagramPacket dp, String host) throws IOException{ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
185 if(proxy.isDirect(host)){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
186 dp.setAddress(InetAddress.getByName(host)); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
187 super.send(dp); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
188 return; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
189 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
190 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
191 if(((Socks5Proxy)proxy).resolveAddrLocally){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
192 dp.setAddress(InetAddress.getByName(host)); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
193 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
194 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
195 byte[] head = formHeader(host,dp.getPort()); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
196 byte[] buf = new byte[head.length + dp.getLength()]; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
197 byte[] data = dp.getData(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
198 //Merge head and data |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
199 System.arraycopy(head,0,buf,0,head.length); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
200 //System.arraycopy(data,dp.getOffset(),buf,head.length,dp.getLength()); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
201 System.arraycopy(data,0,buf,head.length,dp.getLength()); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
202 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
203 if(encapsulation != null) |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
204 buf = encapsulation.udpEncapsulate(buf,true); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
205 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
206 super.send(new DatagramPacket(buf,buf.length,relayIP,relayPort)); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
207 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
208 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
209 /** |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
210 * Receives udp packet. If packet have arrived from the proxy relay server, |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
211 * it is processed and address and port of the packet are set to the |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
212 * address and port of sending host.<BR> |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
213 * If the packet arrived from anywhere else it is not changed.<br> |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
214 * <B> NOTE: </B> DatagramPacket size should be at least 10 bytes bigger |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
215 * than the largest packet you expect (this is for IPV4 addresses). |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
216 * For hostnames and IPV6 it is even more. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
217 @param dp Datagram in which all relevent information will be copied. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
218 */ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
219 public void receive(DatagramPacket dp) throws IOException{ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
220 super.receive(dp); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
221 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
222 if(server_mode){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
223 //Drop all datagrams not from relayIP/relayPort |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
224 int init_length = dp.getLength(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
225 int initTimeout = getSoTimeout(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
226 long startTime = System.currentTimeMillis(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
227 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
228 while(!relayIP.equals(dp.getAddress()) || |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
229 relayPort != dp.getPort()){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
230 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
231 //Restore datagram size |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
232 dp.setLength(init_length); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
233 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
234 //If there is a non-infinit timeout on this socket |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
235 //Make sure that it happens no matter how often unexpected |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
236 //packets arrive. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
237 if(initTimeout != 0){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
238 int newTimeout = initTimeout - (int)(System.currentTimeMillis() - |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
239 startTime); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
240 if(newTimeout <= 0) throw new InterruptedIOException( |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
241 "In Socks5DatagramSocket->receive()"); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
242 setSoTimeout(newTimeout); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
243 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
244 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
245 super.receive(dp); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
246 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
247 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
248 //Restore timeout settings |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
249 if(initTimeout != 0) setSoTimeout(initTimeout); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
250 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
251 }else if(!relayIP.equals(dp.getAddress()) || |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
252 relayPort != dp.getPort()) |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
253 return; // Recieved direct packet |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
254 //If the datagram is not from the relay server, return it it as is. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
255 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
256 byte[] data; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
257 data = dp.getData(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
258 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
259 if(encapsulation != null) |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
260 data = encapsulation.udpEncapsulate(data,false); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
261 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
262 int offset = 0; //Java 1.1 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
263 //int offset = dp.getOffset(); //Java 1.2 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
264 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
265 ByteArrayInputStream bIn = new ByteArrayInputStream(data,offset, |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
266 dp.getLength()); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
267 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
268 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
269 ProxyMessage msg = new Socks5Message(bIn); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
270 dp.setPort(msg.port); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
271 dp.setAddress(msg.getInetAddress()); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
272 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
273 //what wasn't read by the Message is the data |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
274 int data_length = bIn.available(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
275 //Shift data to the left |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
276 System.arraycopy(data,offset+dp.getLength()-data_length, |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
277 data,offset,data_length); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
278 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
279 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
280 dp.setLength(data_length); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
281 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
282 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
283 /** |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
284 * Returns port assigned by the proxy, to which datagrams are relayed. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
285 * It is not the same port to which other party should send datagrams. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
286 @return Port assigned by socks server to which datagrams are send |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
287 for association. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
288 */ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
289 public int getLocalPort(){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
290 if(server_mode) return super.getLocalPort(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
291 return relayPort; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
292 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
293 /** |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
294 * Address assigned by the proxy, to which datagrams are send for relay. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
295 * It is not necesseraly the same address, to which other party should send |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
296 * datagrams. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
297 @return Address to which datagrams are send for association. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
298 */ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
299 public InetAddress getLocalAddress(){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
300 if(server_mode) return super.getLocalAddress(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
301 return relayIP; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
302 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
303 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
304 /** |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
305 * Closes datagram socket, and proxy connection. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
306 */ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
307 public void close(){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
308 if(!server_mode) proxy.endSession(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
309 super.close(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
310 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
311 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
312 /** |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
313 This method checks wether proxy still runs udp forwarding service |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
314 for this socket. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
315 <p> |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
316 This methods checks wether the primary connection to proxy server |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
317 is active. If it is, chances are that proxy continues to forward |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
318 datagrams being send from this socket. If it was closed, most likely |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
319 datagrams are no longer being forwarded by the server. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
320 <p> |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
321 CProxy might decide to stop forwarding datagrams, in which case it |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
322 should close primary connection. This method allows to check, wether |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
323 this have been done. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
324 <p> |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
325 You can specify timeout for which we should be checking EOF condition |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
326 on the primary connection. Timeout is in milliseconds. Specifying 0 as |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
327 timeout implies infinity, in which case method will block, until |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
328 connection to proxy is closed or an error happens, and then return false. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
329 <p> |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
330 One possible scenario is to call isProxyactive(0) in separate thread, |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
331 and once it returned notify other threads about this event. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
332 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
333 @param timeout For how long this method should block, before returning. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
334 @return true if connection to proxy is active, false if eof or error |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
335 condition have been encountered on the connection. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
336 */ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
337 public boolean isProxyAlive(int timeout){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
338 if(server_mode) return false; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
339 if(proxy != null){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
340 try{ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
341 proxy.proxySocket.setSoTimeout(timeout); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
342 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
343 int eof = proxy.in.read(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
344 if(eof < 0) return false; // EOF encountered. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
345 else return true; // This really should not happen |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
346 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
347 }catch(InterruptedIOException iioe){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
348 return true; // read timed out. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
349 }catch(IOException ioe){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
350 return false; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
351 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
352 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
353 return false; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
354 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
355 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
356 //PRIVATE METHODS |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
357 ////////////////// |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
358 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
359 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
360 private byte[] formHeader(InetAddress ip, int port){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
361 Socks5Message request = new Socks5Message(0,ip,port); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
362 request.data[0] = 0; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
363 return request.data; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
364 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
365 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
366 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
367 private byte[] formHeader(String host,int port){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
368 Socks5Message request = new Socks5Message(0,host,port); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
369 request.data[0] = 0; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
370 return request.data; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
371 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
372 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
373 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
374 /*====================================================================== |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
375 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
376 //Mainly Test functions |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
377 ////////////////////// |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
378 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
379 private String bytes2String(byte[] b){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
380 String s=""; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
381 char[] hex_digit = { '0','1','2','3','4','5','6','7','8','9', |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
382 'A','B','C','D','E','F'}; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
383 for(int i=0;i<b.length;++i){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
384 int i1 = (b[i] & 0xF0) >> 4; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
385 int i2 = b[i] & 0xF; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
386 s+=hex_digit[i1]; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
387 s+=hex_digit[i2]; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
388 s+=" "; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
389 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
390 return s; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
391 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
392 private static final void debug(String s){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
393 if(DEBUG) |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
394 System.out.print(s); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
395 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
396 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
397 private static final boolean DEBUG = true; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
398 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
399 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
400 public static void usage(){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
401 System.err.print( |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
402 "Usage: java Socks.SocksDatagramSocket host port [socksHost socksPort]\n"); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
403 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
404 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
405 static final int defaultProxyPort = 1080; //Default Port |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
406 static final String defaultProxyHost = "www-proxy"; //Default proxy |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
407 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
408 public static void main(String args[]){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
409 int port; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
410 String host; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
411 int proxyPort; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
412 String proxyHost; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
413 InetAddress ip; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
414 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
415 if(args.length > 1 && args.length < 5){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
416 try{ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
417 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
418 host = args[0]; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
419 port = Integer.parseInt(args[1]); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
420 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
421 proxyPort =(args.length > 3)? Integer.parseInt(args[3]) |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
422 : defaultProxyPort; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
423 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
424 host = args[0]; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
425 ip = InetAddress.getByName(host); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
426 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
427 proxyHost =(args.length > 2)? args[2] |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
428 : defaultProxyHost; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
429 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
430 CProxy.setDefaultProxy(proxyHost,proxyPort); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
431 CProxy p = CProxy.getDefaultProxy(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
432 p.addDirect("lux"); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
433 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
434 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
435 DatagramSocket ds = new Socks5DatagramSocket(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
436 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
437 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
438 BufferedReader in = new BufferedReader( |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
439 new InputStreamReader(System.in)); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
440 String s; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
441 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
442 System.out.print("Enter line:"); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
443 s = in.readLine(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
444 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
445 while(s != null){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
446 byte[] data = (s+"\r\n").getBytes(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
447 DatagramPacket dp = new DatagramPacket(data,0,data.length, |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
448 ip,port); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
449 System.out.println("Sending to: "+ip+":"+port); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
450 ds.send(dp); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
451 dp = new DatagramPacket(new byte[1024],1024); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
452 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
453 System.out.println("Trying to recieve on port:"+ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
454 ds.getLocalPort()); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
455 ds.receive(dp); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
456 System.out.print("Recieved:\n"+ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
457 "From:"+dp.getAddress()+":"+dp.getPort()+ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
458 "\n\n"+ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
459 new String(dp.getData(),dp.getOffset(),dp.getLength())+"\n" |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
460 ); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
461 System.out.print("Enter line:"); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
462 s = in.readLine(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
463 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
464 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
465 ds.close(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
466 System.exit(1); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
467 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
468 }catch(SocksException s_ex){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
469 System.err.println("SocksException:"+s_ex); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
470 s_ex.printStackTrace(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
471 System.exit(1); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
472 }catch(IOException io_ex){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
473 io_ex.printStackTrace(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
474 System.exit(1); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
475 }catch(NumberFormatException num_ex){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
476 usage(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
477 num_ex.printStackTrace(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
478 System.exit(1); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
479 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
480 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
481 }else{ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
482 usage(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
483 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
484 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
485 */ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
486 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
0
diff
changeset
|
487 } |