1 | /* |
---|
2 | * To change this template, choose Tools | Templates |
---|
3 | * and open the template in the editor. |
---|
4 | */ |
---|
5 | package portforwarder; |
---|
6 | |
---|
7 | import java.io.IOException; |
---|
8 | import java.io.InputStream; |
---|
9 | import java.io.OutputStream; |
---|
10 | import java.net.ServerSocket; |
---|
11 | import java.net.Socket; |
---|
12 | import java.util.logging.Level; |
---|
13 | import java.util.logging.Logger; |
---|
14 | |
---|
15 | /** |
---|
16 | * |
---|
17 | * @author fmguler |
---|
18 | */ |
---|
19 | public class Main { |
---|
20 | /** |
---|
21 | * @param args the command line arguments |
---|
22 | */ |
---|
23 | public static void main(String[] args) { |
---|
24 | // TODO code application logic here |
---|
25 | //arg0 -L or -R |
---|
26 | //arg1 port |
---|
27 | //arg2 host |
---|
28 | //arg3 hostport |
---|
29 | |
---|
30 | if (args.length == 0) { |
---|
31 | System.out.println("Requires arguments"); |
---|
32 | return; |
---|
33 | } |
---|
34 | |
---|
35 | if (args[0].equals("-L")) { |
---|
36 | |
---|
37 | try { |
---|
38 | int port = Integer.parseInt(args[1]); |
---|
39 | ServerSocket serverSocket = new ServerSocket(port); |
---|
40 | while (true) { |
---|
41 | Socket ourclient = serverSocket.accept(); |
---|
42 | //yeni baðlantý oldu, biz de baðlanalým |
---|
43 | String host = args[2]; |
---|
44 | int hostPort = Integer.parseInt(args[3]); |
---|
45 | Socket toHost = new Socket(host, hostPort); |
---|
46 | |
---|
47 | pipe(ourclient.getInputStream(), toHost.getOutputStream(), toHost.getInputStream(), ourclient.getOutputStream()); |
---|
48 | } |
---|
49 | |
---|
50 | } catch (IOException ex) { |
---|
51 | System.out.println("Error forwarding"); |
---|
52 | ex.printStackTrace(); |
---|
53 | } |
---|
54 | |
---|
55 | } else if (args[0].equals("-R")) { |
---|
56 | } else { |
---|
57 | System.out.println("Invalid switch"); |
---|
58 | } |
---|
59 | } |
---|
60 | |
---|
61 | public static void pipe(final InputStream is1, final OutputStream os1, final InputStream is2, final OutputStream os2) { |
---|
62 | |
---|
63 | new Thread(new Runnable() { |
---|
64 | public void run() { |
---|
65 | try { |
---|
66 | byte[] buf = new byte[1024]; |
---|
67 | int c = 0; |
---|
68 | while ((c = is1.read(buf)) != -1) { |
---|
69 | os1.write(buf, 0, c); |
---|
70 | } |
---|
71 | System.out.println("one pipe finished-1"); |
---|
72 | } catch (IOException ex) { |
---|
73 | System.out.println("Error piping is1 to os2"); |
---|
74 | ex.printStackTrace(); |
---|
75 | } |
---|
76 | } |
---|
77 | }).start(); |
---|
78 | |
---|
79 | new Thread(new Runnable() { |
---|
80 | public void run() { |
---|
81 | try { |
---|
82 | byte[] buf = new byte[1024]; |
---|
83 | int c = 0; |
---|
84 | while ((c = is2.read(buf)) != -1) { |
---|
85 | os2.write(buf, 0, c); |
---|
86 | } |
---|
87 | System.out.println("one pipe finished-2"); |
---|
88 | } catch (IOException ex) { |
---|
89 | System.out.println("Error piping is1 to os2"); |
---|
90 | ex.printStackTrace(); |
---|
91 | } |
---|
92 | } |
---|
93 | }).start(); |
---|
94 | } |
---|
95 | } |
---|