| 33 | import java.net.*; |
| 34 | |
| 35 | public class ProxyHTTP implements Proxy{ |
| 36 | private static int DEFAULTPORT=80; |
| 37 | private String proxy_host; |
| 38 | private int proxy_port; |
| 39 | private InputStream in; |
| 40 | private OutputStream out; |
| 41 | private Socket socket; |
| 42 | |
| 43 | private String user; |
| 44 | private String passwd; |
| 45 | |
| 46 | public ProxyHTTP(String proxy_host){ |
| 47 | int port=DEFAULTPORT; |
| 48 | String host=proxy_host; |
| 49 | if(proxy_host.indexOf(':')!=-1){ |
| 50 | try{ |
| 51 | host=proxy_host.substring(0, proxy_host.indexOf(':')); |
| 52 | port=Integer.parseInt(proxy_host.substring(proxy_host.indexOf(':')+1)); |
| 53 | } |
| 54 | catch(Exception e){ |
| 55 | } |
| 56 | } |
| 57 | this.proxy_host=host; |
| 58 | this.proxy_port=port; |
| 59 | } |
| 60 | public ProxyHTTP(String proxy_host, int proxy_port){ |
| 61 | this.proxy_host=proxy_host; |
| 62 | this.proxy_port=proxy_port; |
| 63 | } |
| 64 | public void setUserPasswd(String user, String passwd){ |
| 65 | this.user=user; |
| 66 | this.passwd=passwd; |
| 67 | } |
| 68 | public void connect(SocketFactory socket_factory, String host, int port, int timeout) throws JSchException{ |
| 69 | try{ |
| 70 | if(socket_factory==null){ |
| 71 | socket=Util.createSocket(proxy_host, proxy_port, timeout); |
| 72 | in=socket.getInputStream(); |
| 73 | out=socket.getOutputStream(); |
| 74 | } |
| 75 | else{ |
| 76 | socket=socket_factory.createSocket(proxy_host, proxy_port); |
| 77 | in=socket_factory.getInputStream(socket); |
| 78 | out=socket_factory.getOutputStream(socket); |
| 79 | } |
| 80 | if(timeout>0){ |
| 81 | socket.setSoTimeout(timeout); |
| 82 | } |
| 83 | socket.setTcpNoDelay(true); |
| 84 | |
| 85 | out.write(Util.str2byte("CONNECT "+host+":"+port+" HTTP/1.0\r\n")); |
| 86 | |
| 87 | if(user!=null && passwd!=null){ |
| 88 | byte[] code=Util.str2byte(user+":"+passwd); |
| 89 | code=Util.toBase64(code, 0, code.length); |
| 90 | out.write(Util.str2byte("Proxy-Authorization: Basic ")); |
| 91 | out.write(code); |
| 92 | out.write(Util.str2byte("\r\n")); |
nothing calls this directly
no outgoing calls
no test coverage detected