forked from staktrace/moz-scripts
/
PortForward.java
59 lines (55 loc) · 1.8 KB
/
PortForward.java
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
import java.io.*;
import java.net.*;
public class PortForward {
public static void main( String[] args ) throws Exception {
if (args.length == 0) {
System.err.println( "Usage: java PortForward <local-port> <listen-port>" );
return;
}
int localPort = Integer.parseInt( args[0] );
int listenPort = Integer.parseInt( args[1] );
ServerSocket ss = new ServerSocket( listenPort );
final Socket in = ss.accept();
final Socket out = new Socket( "127.0.0.1", localPort );
Thread in2out = new Thread() {
public void run() {
try {
InputStream i = in.getInputStream();
OutputStream o = out.getOutputStream();
int b = i.read();
while (b >= 0) {
o.write( (byte)b );
b = i.read();
}
o.close();
i.close();
} catch (Exception e) {
e.printStackTrace();
}
}
};
in2out.start();
Thread out2in = new Thread() {
public void run() {
try {
InputStream i = out.getInputStream();
OutputStream o = in.getOutputStream();
int b = i.read();
while (b >= 0) {
o.write( (byte)b );
b = i.read();
}
o.close();
i.close();
} catch (Exception e) {
e.printStackTrace();
}
}
};
out2in.start();
in2out.join();
out2in.join();
in.close();
out.close();
}
}