Mercurial > 510Connectbot
annotate app/src/main/java/net/sourceforge/jsocks/UserPasswordAuthentication.java @ 444:5a74f1b7c1db
migrate from Eclipse to Android Studio
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Thu, 03 Dec 2015 12:33:34 -0800 |
parents | d29cce60f393 |
children |
rev | line source |
---|---|
349
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
1 package net.sourceforge.jsocks; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
2 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
3 /** |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
4 SOCKS5 User Password authentication scheme. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
5 */ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
6 public class UserPasswordAuthentication implements Authentication{ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
7 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
8 /**SOCKS ID for User/Password authentication method*/ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
9 public final static int METHOD_ID = 2; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
10 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
11 String userName, password; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
12 byte[] request; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
13 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
14 /** |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
15 Create an instance of UserPasswordAuthentication. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
16 @param userName User Name to send to SOCKS server. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
17 @param password Password to send to SOCKS server. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
18 */ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
19 public UserPasswordAuthentication(String userName,String password){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
20 this.userName = userName; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
21 this.password = password; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
22 formRequest(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
23 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
24 /** Get the user name. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
25 @return User name. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
26 */ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
27 public String getUser(){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
28 return userName; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
29 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
30 /** Get password |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
31 @return Password |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
32 */ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
33 public String getPassword(){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
34 return password; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
35 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
36 /** |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
37 Does User/Password authentication as defined in rfc1929. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
38 @return An array containnig in, out streams, or null if authentication |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
39 fails. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
40 */ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
41 public Object[] doSocksAuthentication(int methodId, |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
42 java.net.Socket proxySocket) |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
43 throws java.io.IOException{ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
44 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
45 if(methodId != METHOD_ID) return null; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
46 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
47 java.io.InputStream in = proxySocket.getInputStream(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
48 java.io.OutputStream out = proxySocket.getOutputStream(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
49 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
50 out.write(request); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
51 int version = in.read(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
52 if(version < 0) return null; //Server closed connection |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
53 int status = in.read(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
54 if(status != 0) return null; //Server closed connection, or auth failed. |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
55 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
56 return new Object[] {in,out}; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
57 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
58 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
59 //Private methods |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
60 ////////////////// |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
61 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
62 /** Convert UserName password in to binary form, ready to be send to server*/ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
63 private void formRequest(){ |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
64 byte[] user_bytes = userName.getBytes(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
65 byte[] password_bytes = password.getBytes(); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
66 |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
67 request = new byte[3+user_bytes.length+password_bytes.length]; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
68 request[0] = (byte) 1; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
69 request[1] = (byte) user_bytes.length; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
70 System.arraycopy(user_bytes,0,request,2,user_bytes.length); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
71 request[2+user_bytes.length] = (byte) password_bytes.length; |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
72 System.arraycopy(password_bytes,0, |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
73 request,3+user_bytes.length,password_bytes.length); |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
74 } |
205ee2873330
update jsocks to 2011-03-19
Carl Byington <carl@five-ten-sg.com>
parents:
diff
changeset
|
75 } |