00001 using Tamir.SharpSsh.java;
00002 using Tamir.SharpSsh.java.io;
00003 using Tamir.SharpSsh.java.util;
00004 using Tamir.SharpSsh.java.net;
00005 using Tamir.SharpSsh.java.lang;
00006 using Tamir.SharpSsh.jsch;
00007 using System.IO;
00008
00009 namespace Tamir.SharpSsh.jsch
00010 {
00011 public class ProxyHTTP : Proxy
00012 {
00013 private static int DEFAULTPORT=80;
00014 private String proxy_host;
00015 private int proxy_port;
00016 private JStream ins;
00017 private JStream outs;
00018 private Socket socket;
00019
00020 private String user;
00021 private String passwd;
00022
00023 public ProxyHTTP(String proxy_host)
00024 {
00025 int port=DEFAULTPORT;
00026 String host=proxy_host;
00027 if(proxy_host.indexOf(':')!=-1)
00028 {
00029 try
00030 {
00031 host=proxy_host.substring(0, proxy_host.indexOf(':'));
00032 port=Integer.parseInt(proxy_host.substring(proxy_host.indexOf(':')+1));
00033 }
00034 catch(Exception e)
00035 {
00036 }
00037 }
00038 this.proxy_host=host;
00039 this.proxy_port=port;
00040 }
00041 public ProxyHTTP(String proxy_host, int proxy_port)
00042 {
00043 this.proxy_host=proxy_host;
00044 this.proxy_port=proxy_port;
00045 }
00046 public void setUserPasswd(String user, String passwd)
00047 {
00048 this.user=user;
00049 this.passwd=passwd;
00050 }
00051 public void connect(SocketFactory socket_factory, String host, int port, int timeout)
00052 {
00053 try
00054 {
00055 if(socket_factory==null)
00056 {
00057 socket=Util.createSocket(proxy_host, proxy_port, timeout);
00058 ins= new JStream(socket.getInputStream());
00059 outs=new JStream(socket.getOutputStream());
00060 }
00061 else
00062 {
00063 socket=socket_factory.createSocket(proxy_host, proxy_port);
00064 ins=new JStream(socket_factory.getInputStream(socket));
00065 outs=new JStream(socket_factory.getOutputStream(socket));
00066 }
00067 if(timeout>0)
00068 {
00069 socket.setSoTimeout(timeout);
00070 }
00071 socket.setTcpNoDelay(true);
00072
00073 outs.write(new String("CONNECT "+host+":"+port+" HTTP/1.0\r\n").getBytes());
00074
00075 if(user!=null && passwd!=null)
00076 {
00077 byte[] _code=(user+":"+passwd).getBytes();
00078 _code=Util.toBase64(_code, 0, _code.Length);
00079 outs.write(new String("Proxy-Authorization: Basic ").getBytes());
00080 outs.write(_code);
00081 outs.write(new String("\r\n").getBytes());
00082 }
00083
00084 outs.write(new String("\r\n").getBytes());
00085 outs.flush();
00086
00087 int foo=0;
00088
00089 StringBuffer sb=new StringBuffer();
00090 while(foo>=0)
00091 {
00092 foo=ins.read(); if(foo!=13){sb.append((char)foo); continue;}
00093 foo=ins.read(); if(foo!=10){continue;}
00094 break;
00095 }
00096 if(foo<0)
00097 {
00098 throw new System.IO.IOException();
00099 }
00100
00101 String response=sb.toString();
00102 String reason="Unknow reason";
00103 int code=-1;
00104 try
00105 {
00106 foo=response.indexOf(' ');
00107 int bar=response.indexOf(' ', foo+1);
00108 code=Integer.parseInt(response.substring(foo+1, bar));
00109 reason=response.substring(bar+1);
00110 }
00111 catch(Exception e)
00112 {
00113 }
00114 if(code!=200)
00115 {
00116 throw new System.IO.IOException("proxy error: "+reason);
00117 }
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129 int count=0;
00130 while(true)
00131 {
00132 count=0;
00133 while(foo>=0)
00134 {
00135 foo=ins.read(); if(foo!=13){count++; continue;}
00136 foo=ins.read(); if(foo!=10){continue;}
00137 break;
00138 }
00139 if(foo<0)
00140 {
00141 throw new System.IO.IOException();
00142 }
00143 if(count==0)break;
00144 }
00145 }
00146 catch(RuntimeException e)
00147 {
00148 throw e;
00149 }
00150 catch(Exception e)
00151 {
00152 try{ if(socket!=null)socket.close(); }
00153 catch(Exception eee)
00154 {
00155 }
00156 String message="ProxyHTTP: "+e.toString();
00157 throw e;
00158 }
00159 }
00160 public Stream getInputStream(){ return ins.s; }
00161 public Stream getOutputStream(){ return outs.s; }
00162 public Socket getSocket(){ return socket; }
00163 public void close()
00164 {
00165 try
00166 {
00167 if(ins!=null)ins.close();
00168 if(outs!=null)outs.close();
00169 if(socket!=null)socket.close();
00170 }
00171 catch(Exception e)
00172 {
00173 }
00174 ins=null;
00175 outs=null;
00176 socket=null;
00177 }
00178 public static int getDefaultPort()
00179 {
00180 return DEFAULTPORT;
00181 }
00182 }
00183 }