1 2 29 30 package com.jcraft.jsch; 31 32 import java.io.*; 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(("CONNECT "+host+":"+port+" HTTP/1.0\r\n").getBytes()); 86 87 if(user!=null && passwd!=null){ 88 byte[] code=(user+":"+passwd).getBytes(); 89 code=Util.toBase64(code, 0, code.length); 90 out.write("Proxy-Authorization: Basic ".getBytes()); 91 out.write(code); 92 out.write("\r\n".getBytes()); 93 } 94 95 out.write("\r\n".getBytes()); 96 out.flush(); 97 98 int foo=0; 99 100 StringBuffer sb=new StringBuffer (); 101 while(foo>=0){ 102 foo=in.read(); if(foo!=13){sb.append((char)foo); continue;} 103 foo=in.read(); if(foo!=10){continue;} 104 break; 105 } 106 if(foo<0){ 107 throw new IOException(); 108 } 109 110 String response=sb.toString(); 111 String reason="Unknow reason"; 112 int code=-1; 113 try{ 114 foo=response.indexOf(' '); 115 int bar=response.indexOf(' ', foo+1); 116 code=Integer.parseInt(response.substring(foo+1, bar)); 117 reason=response.substring(bar+1); 118 } 119 catch(Exception e){ 120 } 121 if(code!=200){ 122 throw new IOException("proxy error: "+reason); 123 } 124 125 134 135 int count=0; 136 while(true){ 137 count=0; 138 while(foo>=0){ 139 foo=in.read(); if(foo!=13){count++; continue;} 140 foo=in.read(); if(foo!=10){continue;} 141 break; 142 } 143 if(foo<0){ 144 throw new IOException(); 145 } 146 if(count==0)break; 147 } 148 } 149 catch(RuntimeException e){ 150 throw e; 151 } 152 catch(Exception e){ 153 try{ if(socket!=null)socket.close(); } 154 catch(Exception eee){ 155 } 156 String message="ProxyHTTP: "+e.toString(); 157 if(e instanceof Throwable ) 158 throw new JSchException(message, (Throwable )e); 159 throw new JSchException(message); 160 } 161 } 162 public InputStream getInputStream(){ return in; } 163 public OutputStream getOutputStream(){ return out; } 164 public Socket getSocket(){ return socket; } 165 public void close(){ 166 try{ 167 if(in!=null)in.close(); 168 if(out!=null)out.close(); 169 if(socket!=null)socket.close(); 170 } 171 catch(Exception e){ 172 } 173 in=null; 174 out=null; 175 socket=null; 176 } 177 public static int getDefaultPort(){ 178 return DEFAULTPORT; 179 } 180 } 181 | Popular Tags |