1 | package nu.fw.jeti.plugins.filetransfer.socks5.jsocks; |
---|
2 | |
---|
3 | import java.io.IOException; |
---|
4 | import java.io.InputStream; |
---|
5 | import java.io.OutputStream; |
---|
6 | import java.io.DataInputStream; |
---|
7 | import java.net.InetAddress; |
---|
8 | import java.net.UnknownHostException; |
---|
9 | |
---|
10 | /** |
---|
11 | Abstract class which describes SOCKS4/5 response/request. |
---|
12 | */ |
---|
13 | public abstract class ProxyMessage{ |
---|
14 | /** Host as an IP address */ |
---|
15 | public InetAddress ip=null; |
---|
16 | /** SOCKS version, or version of the response for SOCKS4*/ |
---|
17 | public int version; |
---|
18 | /** Port field of the request/response*/ |
---|
19 | public int port; |
---|
20 | /** Request/response code as an int*/ |
---|
21 | public int command; |
---|
22 | /** Host as string.*/ |
---|
23 | public String host=null; |
---|
24 | /** User field for SOCKS4 request messages*/ |
---|
25 | public String user=null; |
---|
26 | |
---|
27 | ProxyMessage(int command,InetAddress ip,int port){ |
---|
28 | this.command = command; |
---|
29 | this.ip = ip; |
---|
30 | this.port = port; |
---|
31 | } |
---|
32 | |
---|
33 | ProxyMessage(){ |
---|
34 | } |
---|
35 | |
---|
36 | |
---|
37 | /** |
---|
38 | Initialises Message from the stream. Reads server response from |
---|
39 | given stream. |
---|
40 | @param in Input stream to read response from. |
---|
41 | @throws SocksException If server response code is not SOCKS_SUCCESS(0), or |
---|
42 | if any error with protocol occurs. |
---|
43 | @throws IOException If any error happens with I/O. |
---|
44 | */ |
---|
45 | public abstract void read(InputStream in) |
---|
46 | throws SocksException, |
---|
47 | IOException; |
---|
48 | |
---|
49 | |
---|
50 | /** |
---|
51 | Initialises Message from the stream. Reads server response or client |
---|
52 | request from given stream. |
---|
53 | |
---|
54 | @param in Input stream to read response from. |
---|
55 | @param clinetMode If true read server response, else read client request. |
---|
56 | @throws SocksException If server response code is not SOCKS_SUCCESS(0) and |
---|
57 | reading in client mode, or if any error with protocol occurs. |
---|
58 | @throws IOException If any error happens with I/O. |
---|
59 | */ |
---|
60 | public abstract void read(InputStream in,boolean client_mode) |
---|
61 | throws SocksException, |
---|
62 | IOException; |
---|
63 | |
---|
64 | |
---|
65 | /** |
---|
66 | Writes the message to the stream. |
---|
67 | @param out Output stream to which message should be written. |
---|
68 | */ |
---|
69 | public abstract void write(OutputStream out)throws SocksException, |
---|
70 | IOException; |
---|
71 | |
---|
72 | /** |
---|
73 | Get the Address field of this message as InetAddress object. |
---|
74 | @return Host address or null, if one can't be determined. |
---|
75 | */ |
---|
76 | public InetAddress getInetAddress() throws UnknownHostException{ |
---|
77 | return ip; |
---|
78 | } |
---|
79 | |
---|
80 | |
---|
81 | /** |
---|
82 | Get string representaion of this message. |
---|
83 | @return string representation of this message. |
---|
84 | */ |
---|
85 | public String toString(){ |
---|
86 | return |
---|
87 | "Proxy Message:\n"+ |
---|
88 | "Version:"+ version+"\n"+ |
---|
89 | "Command:"+ command+"\n"+ |
---|
90 | "IP: "+ ip+"\n"+ |
---|
91 | "Port: "+ port+"\n"+ |
---|
92 | "User: "+ user+"\n" ; |
---|
93 | } |
---|
94 | |
---|
95 | //Package methods |
---|
96 | ////////////////// |
---|
97 | |
---|
98 | static final String bytes2IPV4(byte[] addr,int offset){ |
---|
99 | String hostName = ""+(addr[offset] & 0xFF); |
---|
100 | for(int i = offset+1;i<offset+4;++i) |
---|
101 | hostName+="."+(addr[i] & 0xFF); |
---|
102 | return hostName; |
---|
103 | } |
---|
104 | |
---|
105 | static final String bytes2IPV6(byte[] addr,int offset){ |
---|
106 | //Have no idea how they look like! |
---|
107 | return null; |
---|
108 | } |
---|
109 | |
---|
110 | } |
---|