]>
Commit | Line | Data |
---|---|---|
1 | /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */ | |
2 | /* | |
3 | Copyright (c) 2002-2010 ymnk, JCraft,Inc. All rights reserved. | |
4 | ||
5 | Redistribution and use in source and binary forms, with or without | |
6 | modification, are permitted provided that the following conditions are met: | |
7 | ||
8 | 1. Redistributions of source code must retain the above copyright notice, | |
9 | this list of conditions and the following disclaimer. | |
10 | ||
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. | |
14 | ||
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. | |
17 | ||
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. | |
28 | */ | |
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(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")); | |
93 | } | |
94 | ||
95 | out.write(Util.str2byte("\r\n")); | |
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 | /* | |
126 | while(foo>=0){ | |
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; | |
131 | break; | |
132 | } | |
133 | */ | |
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 | } |