1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
package com.vaadin.tests.tb3;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
public class SimpleProxy extends Thread {
private final ThreadGroup proxyThreads;
private final Queue<Socket> sockets = new ConcurrentLinkedQueue<>();
private final ServerSocket serverSocket;
private final String remoteHost;
private final int remotePort;
public SimpleProxy(int localPort, String remoteHost, int remotePort)
throws IOException {
super(new ThreadGroup("proxy " + localPort), "server");
this.remoteHost = remoteHost;
this.remotePort = remotePort;
proxyThreads = getThreadGroup();
serverSocket = new ServerSocket(localPort, 100,
InetAddress.getByName("0.0.0.0"));
setDaemon(true);
}
@Override
public void run() {
try {
while (!isInterrupted() && !serverSocket.isClosed()) {
try {
Socket proxySocket = serverSocket.accept();
sockets.add(proxySocket);
Socket remoteSocket = new Socket(remoteHost, remotePort);
sockets.add(remoteSocket);
new CopySocket(proxyThreads, proxySocket, remoteSocket)
.start();
new CopySocket(proxyThreads, remoteSocket, proxySocket)
.start();
} catch (SocketException e) {
if (!serverSocket.isClosed()) {
throw new RuntimeException(e);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
} finally {
disconnect();
}
}
public void disconnect() {
proxyThreads.interrupt();
for (Socket socket : sockets) {
try {
socket.close();
} catch (IOException ignored) {
}
}
try {
serverSocket.close();
} catch (IOException ignored) {
}
}
private class CopySocket extends Thread {
private final InputStream inputStream;
private final OutputStream outputStream;
private CopySocket(ThreadGroup proxyThreads, Socket srcSocket,
Socket dstSocket) throws IOException {
super(proxyThreads, "proxy worker");
setDaemon(true);
inputStream = srcSocket.getInputStream();
outputStream = dstSocket.getOutputStream();
}
@Override
public void run() {
try {
for (int b; (b = inputStream.read()) >= 0;) {
outputStream.write(b);
}
} catch (SocketException ignored) {
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
inputStream.close();
} catch (IOException ignored) {
}
try {
outputStream.close();
} catch (IOException ignored) {
}
}
}
}
}
|