comparison app/src/main/java/net/sourceforge/jsocks/UserPasswordAuthentication.java @ 438:d29cce60f393

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