http - Per-Proxy Authentication in Java -


i trying support authenticated proxies in java application. understanding java.net.proxy class not support authentication, , need handle authentication yourself.

i have created subclass of java.net.proxy class, takes 2 additional parameters, username , password.

implementing http proxy authentication quite easy, , method gethttpproxyauthenticationheader returns base64 encoded auth info, pass httpurlconnection or similar.

i'm having trouble sock proxies though. cannot find documentation on sending authentication socks server. i'm unsure if need implement socks authentication protocol in class using method such authenticatesocksproxy(outputstream stream), , call

authedproxy.authenticatesocksproxy(outputstream); 

before using socket like

outputstream.writebytes(mydata.getbytes()); 

another option return byte[] of authentication data , write data manually, instead of class writing authentication data.

i not think java.net.authenticator, or system.setproperty methods of use, since implementation needs work on per-connection basis , thread-safe.

any appreciated.

was taken jsocks project sources: https://code.google.com/p/jsocks-mirror/source/browse/trunk/src/java/net/sourceforge/jsocks/socks/userpasswordauthentication.java

i think it's clean enough understand full process:

 /**   socks5 user password authentication scheme. */ public class userpasswordauthentication implements authentication{     /**socks id user/password authentication method*/    public final static int method_id = 2;     string username, password;    byte[] request;     /**      create instance of userpasswordauthentication.      @param username user name send socks server.      @param password password send socks server.    */    public userpasswordauthentication(string username,string password){      this.username = username;      this.password = password;      formrequest();    }    /** user name.    @return user name.    */    public string getuser(){      return username;    }    /** password    @return password    */    public string getpassword(){      return password;    }    /**     user/password authentication defined in rfc1929.     @return array containnig in, out streams, or null if authentication     fails.    */    public object[] dosocksauthentication(int methodid,                                          java.net.socket proxysocket)                    throws java.io.ioexception{        if(methodid != method_id) return null;        java.io.inputstream in = proxysocket.getinputstream();       java.io.outputstream out = proxysocket.getoutputstream();        out.write(request);       int version = in.read();       if(version < 0) return null; //server closed connection       int status = in.read();       if(status != 0) return null; //server closed connection, or auth failed.        return new object[] {in,out};    }  //private methods //////////////////  /** convert username password in binary form, ready send server*/    private void formrequest(){       byte[] user_bytes = username.getbytes();       byte[] password_bytes = password.getbytes();        request = new byte[3+user_bytes.length+password_bytes.length];       request[0] = (byte) 1;        request[1] = (byte) user_bytes.length;       system.arraycopy(user_bytes,0,request,2,user_bytes.length);       request[2+user_bytes.length] = (byte) password_bytes.length;       system.arraycopy(password_bytes,0,                        request,3+user_bytes.length,password_bytes.length);    } } 

Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -