1 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
3 Copyright (c) 2002-2010 ymnk, JCraft,Inc. All rights reserved.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
8 1. Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
11 2. Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in
13 the documentation and/or other materials provided with the distribution.
15 3. The names of the authors may not be used to endorse or promote products
16 derived from this software without specific prior written permission.
18 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
19 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
21 INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
22 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 package com.jcraft.jsch;
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;
44 private String passwd;
46 public ProxyHTTP(String proxy_host){
48 String host=proxy_host;
49 if(proxy_host.indexOf(':')!=-1){
51 host=proxy_host.substring(0, proxy_host.indexOf(':'));
52 port=Integer.parseInt(proxy_host.substring(proxy_host.indexOf(':')+1));
60 public ProxyHTTP(String proxy_host, int proxy_port){
61 this.proxy_host=proxy_host;
62 this.proxy_port=proxy_port;
64 public void setUserPasswd(String user, String passwd){
68 public void connect(SocketFactory socket_factory, String host, int port, int timeout) throws JSchException{
70 if(socket_factory==null){
71 socket=Util.createSocket(proxy_host, proxy_port, timeout);
72 in=socket.getInputStream();
73 out=socket.getOutputStream();
76 socket=socket_factory.createSocket(proxy_host, proxy_port);
77 in=socket_factory.getInputStream(socket);
78 out=socket_factory.getOutputStream(socket);
81 socket.setSoTimeout(timeout);
83 socket.setTcpNoDelay(true);
85 out.write(Util.str2byte("CONNECT "+host+":"+port+" HTTP/1.0\r\n"));
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 "));
92 out.write(Util.str2byte("\r\n"));
95 out.write(Util.str2byte("\r\n"));
100 StringBuffer sb=new StringBuffer();
102 foo=in.read(); if(foo!=13){sb.append((char)foo); continue;}
103 foo=in.read(); if(foo!=10){continue;}
107 throw new IOException();
110 String response=sb.toString();
111 String reason="Unknow reason";
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);
122 throw new IOException("proxy error: "+reason);
127 foo=in.read(); if(foo!=13) continue;
128 foo=in.read(); if(foo!=10) continue;
129 foo=in.read(); if(foo!=13) continue;
130 foo=in.read(); if(foo!=10) continue;
139 foo=in.read(); if(foo!=13){count++; continue;}
140 foo=in.read(); if(foo!=10){continue;}
144 throw new IOException();
149 catch(RuntimeException e){
153 try{ if(socket!=null)socket.close(); }
154 catch(Exception eee){
156 String message="ProxyHTTP: "+e.toString();
157 if(e instanceof Throwable)
158 throw new JSchException(message, (Throwable)e);
159 throw new JSchException(message);
162 public InputStream getInputStream(){ return in; }
163 public OutputStream getOutputStream(){ return out; }
164 public Socket getSocket(){ return socket; }
167 if(in!=null)in.close();
168 if(out!=null)out.close();
169 if(socket!=null)socket.close();
177 public static int getDefaultPort(){