annotate app/src/main/java/net/sourceforge/jsocks/UDPRelayServer.java @ 438:d29cce60f393

migrate from Eclipse to Android Studio
author Carl Byington <carl@five-ten-sg.com>
date Thu, 03 Dec 2015 11:23:55 -0800
parents src/net/sourceforge/jsocks/UDPRelayServer.java@205ee2873330
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 net.sourceforge.jsocks.server.*;
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
3 import java.net.*;
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
4 import java.io.*;
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 /**
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
7 UDP Relay server, used by ProxyServer to perform udp forwarding.
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
8 */
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
9 class UDPRelayServer implements Runnable{
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
10
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
11
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
12 DatagramSocket client_sock;
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
13 DatagramSocket remote_sock;
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
14
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
15 Socket controlConnection;
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
16
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
17 int relayPort;
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
18 InetAddress relayIP;
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
19
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
20 Thread pipe_thread1,pipe_thread2;
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
21 Thread master_thread;
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
22
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
23 ServerAuthenticator auth;
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
24
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
25 long lastReadTime;
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
26
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
27 static PrintStream log = null;
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
28 static CProxy proxy = null;
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
29 static int datagramSize = 0xFFFF;//64K, a bit more than max udp size
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
30 static int iddleTimeout = 180000;//3 minutes
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
31
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 Constructs UDP relay server to communicate with client
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
35 on given ip and port.
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
36 @param clientIP Address of the client from whom datagrams
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
37 will be recieved and to whom they will be forwarded.
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
38 @param clientPort Clients port.
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
39 @param master_thread Thread which will be interrupted, when
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
40 UDP relay server stoppes for some reason.
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
41 @param controlConnection Socket which will be closed, before
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
42 interrupting the master thread, it is introduced due to a bug
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
43 in windows JVM which does not throw InterruptedIOException in
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
44 threads which block in I/O operation.
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
45 */
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
46 public UDPRelayServer(InetAddress clientIP,int clientPort,
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
47 Thread master_thread,
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
48 Socket controlConnection,
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
49 ServerAuthenticator auth)
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
50 throws IOException{
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
51 this.master_thread = master_thread;
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
52 this.controlConnection = controlConnection;
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
53 this.auth = auth;
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 client_sock = new Socks5DatagramSocket(true,auth.getUdpEncapsulation(),
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
56 clientIP,clientPort);
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
57 relayPort = client_sock.getLocalPort();
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
58 relayIP = client_sock.getLocalAddress();
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
59
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
60 if(relayIP.getHostAddress().equals("0.0.0.0"))
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
61 relayIP = InetAddress.getLocalHost();
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 if(proxy == null)
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
64 remote_sock = new DatagramSocket();
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
65 else
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
66 remote_sock = new Socks5DatagramSocket(proxy,0,null);
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
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
69
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
70 //Public methods
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
71 /////////////////
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
72
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
73
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 Sets the timeout for UDPRelay server.<br>
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
76 Zero timeout implies infinity.<br>
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
77 Default timeout is 3 minutes.
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 static public void setTimeout(int timeout){
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
81 iddleTimeout = timeout;
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
82 }
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
83
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
84
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
85 /**
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
86 Sets the size of the datagrams used in the UDPRelayServer.<br>
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
87 Default size is 64K, a bit more than maximum possible size of the
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
88 datagram.
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
89 */
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
90 static public void setDatagramSize(int size){
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
91 datagramSize = size;
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
92 }
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
93
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 Port to which client should send datagram for association.
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
96 */
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
97 public int getRelayPort(){
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
98 return relayPort;
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
99 }
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
100 /**
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
101 IP address to which client should send datagrams for association.
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 public InetAddress getRelayIP(){
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
104 return relayIP;
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
105 }
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 /**
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
108 Starts udp relay server.
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
109 Spawns two threads of execution and returns.
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
110 */
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
111 public void start() throws IOException{
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
112 remote_sock.setSoTimeout(iddleTimeout);
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
113 client_sock.setSoTimeout(iddleTimeout);
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 log("Starting UDP relay server on "+relayIP+":"+relayPort);
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
116 log("Remote socket "+remote_sock.getLocalAddress()+":"+
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
117 remote_sock.getLocalPort());
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
118
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
119 pipe_thread1 = new Thread(this,"pipe1");
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
120 pipe_thread2 = new Thread(this,"pipe2");
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 lastReadTime = System.currentTimeMillis();
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 pipe_thread1.start();
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
125 pipe_thread2.start();
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
126 }
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
127
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
128 /**
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
129 Stops Relay server.
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
130 <p>
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
131 Does not close control connection, does not interrupt master_thread.
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
132 */
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
133 public synchronized void stop(){
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
134 master_thread = null;
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
135 controlConnection = null;
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
136 abort();
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
137 }
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 //Runnable interface
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
140 ////////////////////
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
141 public void run(){
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
142 try{
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
143 if(Thread.currentThread().getName().equals("pipe1"))
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
144 pipe(remote_sock,client_sock,false);
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
145 else
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
146 pipe(client_sock,remote_sock,true);
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
147 }catch(IOException ioe){
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
148 }finally{
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
149 abort();
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
150 log("UDP Pipe thread "+Thread.currentThread().getName()+" stopped.");
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
151 }
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
152
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
153 }
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 //Private methods
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
156 /////////////////
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
157 private synchronized void abort(){
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
158 if(pipe_thread1 == null) return;
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
159
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
160 log("Aborting UDP Relay Server");
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
161
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
162 remote_sock.close();
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
163 client_sock.close();
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
164
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
165 if(controlConnection != null)
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
166 try{ controlConnection.close();} catch(IOException ioe){}
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
167
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
168 if(master_thread!=null) master_thread.interrupt();
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 pipe_thread1.interrupt();
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
171 pipe_thread2.interrupt();
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
172
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
173 pipe_thread1 = null;
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
174 }
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
175
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
176
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
177 static private void log(String s){
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
178 if(log != null){
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
179 log.println(s);
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
180 log.flush();
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
181 }
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
182 }
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 private void pipe(DatagramSocket from,DatagramSocket to,boolean out)
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
185 throws IOException{
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
186 byte[] data = new byte[datagramSize];
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
187 DatagramPacket dp = new DatagramPacket(data,data.length);
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
188
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
189 while(true){
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
190 try{
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
191 from.receive(dp);
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
192 lastReadTime = System.currentTimeMillis();
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 if(auth.checkRequest(dp,out))
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
195 to.send(dp);
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
196
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
197 }catch(UnknownHostException uhe){
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
198 log("Dropping datagram for unknown host");
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
199 }catch(InterruptedIOException iioe){
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
200 //log("Interrupted: "+iioe);
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
201 //If we were interrupted by other thread.
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
202 if(iddleTimeout == 0) return;
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
203
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
204 //If last datagram was received, long time ago, return.
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
205 long timeSinceRead = System.currentTimeMillis() - lastReadTime;
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
206 if(timeSinceRead >= iddleTimeout -100) //-100 for adjustment
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
207 return;
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 dp.setLength(data.length);
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
210 }
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
211 }
205ee2873330 update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents: 0
diff changeset
212 }