Mercurial > 510Connectbot
comparison src/net/sourceforge/jsocks/server/Ident.java @ 349:205ee2873330
update jsocks to 2011-03-19
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Fri, 01 Aug 2014 11:23:10 -0700 |
parents | |
children | 72de889ecfe7 |
comparison
equal
deleted
inserted
replaced
348:29076621bab0 | 349:205ee2873330 |
---|---|
1 package socks.server; | |
2 import net.sourceforge.jsocks.*; | |
3 import java.net.*; | |
4 import java.io.*; | |
5 import java.util.StringTokenizer; | |
6 | |
7 /** | |
8 Class Ident provides means to obtain user name of the owner of the socket | |
9 on remote machine, providing remote machine runs identd daemon. | |
10 <p> | |
11 To use it: | |
12 <tt><pre> | |
13 Socket s = ss.accept(); | |
14 Ident id = new Ident(s); | |
15 if(id.successful) goUseUser(id.userName); | |
16 else handleIdentError(id.errorCode,id.errorMessage) | |
17 </pre></tt> | |
18 */ | |
19 public class Ident{ | |
20 | |
21 /** Error Message can be null.*/ | |
22 public String errorMessage; | |
23 /** Host type as returned by daemon, can be null, if error happened*/ | |
24 public String hostType; | |
25 /** User name as returned by the identd daemon, or null, if it failed*/ | |
26 public String userName; | |
27 | |
28 /** If this is true then userName and hostType contain valid values. | |
29 Else errorCode contain the error code, and errorMessage contains | |
30 the corresponding message. | |
31 */ | |
32 public boolean successful; | |
33 /** Error code*/ | |
34 public int errorCode; | |
35 /** Identd on port 113 can't be contacted*/ | |
36 public static final int ERR_NO_CONNECT = 1; | |
37 /** Connection timed out*/ | |
38 public static final int ERR_TIMEOUT = 2; | |
39 /** Identd daemon responded with ERROR, in this case errorMessage | |
40 contains the string explanation, as send by the daemon. | |
41 */ | |
42 public static final int ERR_PROTOCOL = 3; | |
43 /** | |
44 When parsing server response protocol error happened. | |
45 */ | |
46 public static final int ERR_PROTOCOL_INCORRECT = 4; | |
47 | |
48 | |
49 /** Maximum amount of time we should wait before dropping the | |
50 connection to identd server.Setting it to 0 implies infinit | |
51 timeout. | |
52 */ | |
53 public static final int connectionTimeout = 10000; | |
54 | |
55 | |
56 /** | |
57 Constructor tries to connect to Identd daemon on the host of the | |
58 given socket, and retrieve user name of the owner of given socket | |
59 connection on remote machine. After constructor returns public | |
60 fields are initialised to whatever the server returned. | |
61 <p> | |
62 If user name was successfully retrieved successful is set to true, | |
63 and userName and hostType are set to whatever server returned. If | |
64 however for some reason user name was not obtained, successful is set | |
65 to false and errorCode contains the code explaining the reason of | |
66 failure, and errorMessage contains human readable explanation. | |
67 <p> | |
68 Constructor may block, for a while. | |
69 @param s Socket whose ownership on remote end should be obtained. | |
70 */ | |
71 public Ident(Socket s ){ | |
72 Socket sock = null; | |
73 successful = false; //We are pessimistic | |
74 | |
75 try{ | |
76 sock = new Socket(s.getInetAddress(),113); | |
77 sock.setSoTimeout(connectionTimeout); | |
78 byte[] request = (""+s.getPort()+" , "+ | |
79 s.getLocalPort()+"\r\n").getBytes(); | |
80 | |
81 sock.getOutputStream().write(request); | |
82 | |
83 BufferedReader in = new BufferedReader( | |
84 new InputStreamReader(sock.getInputStream())); | |
85 | |
86 parseResponse(in.readLine()); | |
87 | |
88 }catch(InterruptedIOException iioe){ | |
89 errorCode = ERR_TIMEOUT; | |
90 errorMessage = "Connection to identd timed out."; | |
91 }catch(ConnectException ce){ | |
92 errorCode = ERR_NO_CONNECT; | |
93 errorMessage = "Connection to identd server failed."; | |
94 | |
95 }catch(IOException ioe){ | |
96 errorCode = ERR_NO_CONNECT; | |
97 errorMessage = ""+ioe; | |
98 }finally{ | |
99 try{ if(sock!=null) sock.close();}catch(IOException ioe){}; | |
100 } | |
101 } | |
102 | |
103 private void parseResponse(String response){ | |
104 if(response == null){ | |
105 errorCode = ERR_PROTOCOL_INCORRECT; | |
106 errorMessage = "Identd server closed connection."; | |
107 return; | |
108 } | |
109 | |
110 StringTokenizer st = new StringTokenizer(response,":"); | |
111 if(st.countTokens() < 3){ | |
112 errorCode = ERR_PROTOCOL_INCORRECT; | |
113 errorMessage = "Can't parse server response."; | |
114 return; | |
115 } | |
116 | |
117 st.nextToken(); //Discard first token, it's basically what we have send | |
118 String command = st.nextToken().trim().toUpperCase(); | |
119 | |
120 if(command.equals("USERID") && st.countTokens() >= 2){ | |
121 successful = true; | |
122 hostType = st.nextToken().trim(); | |
123 userName = st.nextToken("").substring(1);//Get all that is left | |
124 }else if(command.equals("ERROR")){ | |
125 errorCode = ERR_PROTOCOL; | |
126 errorMessage = st.nextToken(); | |
127 }else{ | |
128 errorCode = ERR_PROTOCOL_INCORRECT; | |
129 System.out.println("Opa!"); | |
130 errorMessage = "Can't parse server response."; | |
131 } | |
132 | |
133 | |
134 } | |
135 | |
136 /////////////////////////////////////////////// | |
137 //USED for Testing | |
138 /* | |
139 public static void main(String[] args) throws IOException{ | |
140 | |
141 Socket s = null; | |
142 s = new Socket("gp101-16", 1391); | |
143 | |
144 Ident id = new Ident(s); | |
145 if(id.successful){ | |
146 System.out.println("User: "+id.userName); | |
147 System.out.println("HostType: "+id.hostType); | |
148 }else{ | |
149 System.out.println("ErrorCode: "+id.errorCode); | |
150 System.out.println("ErrorMessage: "+id.errorMessage); | |
151 | |
152 } | |
153 | |
154 if(s!= null) s.close(); | |
155 } | |
156 //*/ | |
157 | |
158 } |