1 package socks; 2 3 6 public class UserPasswordAuthentication implements Authentication{ 7 8 9 public final static int METHOD_ID = 2; 10 11 String userName, password; 12 byte[] request; 13 14 19 public UserPasswordAuthentication(String userName,String password){ 20 this.userName = userName; 21 this.password = password; 22 formRequest(); 23 } 24 27 public String getUser(){ 28 return userName; 29 } 30 33 public String getPassword(){ 34 return password; 35 } 36 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; int status = in.read(); 54 if(status != 0) return null; 56 return new Object [] {in,out}; 57 } 58 59 62 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 } 76 | Popular Tags |