2
Copyright (c) 2006-2010 ymnk, JCraft,Inc. All rights reserved.
4
Redistribution and use in source and binary forms, with or without
5
modification, are permitted provided that the following conditions are met:
7
1. Redistributions of source code must retain the above copyright notice,
8
this list of conditions and the following disclaimer.
10
2. Redistributions in binary form must reproduce the above copyright
11
notice, this list of conditions and the following disclaimer in
12
the documentation and/or other materials provided with the distribution.
14
3. The names of the authors may not be used to endorse or promote products
15
derived from this software without specific prior written permission.
17
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
18
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
19
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
20
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
21
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
26
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
This code is based on jsch (http://www.jcraft.com/jsch).
29
All credit should go to the authors of jsch.
34
using System.Net.Sockets;
41
public class ProxyHTTP : Proxy
43
private static int DEFAULTPORT = 80;
45
private string proxy_host;
47
private int proxy_port;
49
private InputStream @in;
51
private OutputStream @out;
53
private Socket socket;
57
private string passwd;
59
public ProxyHTTP(string proxy_host)
61
int port = DEFAULTPORT;
62
string host = proxy_host;
63
if (proxy_host.IndexOf(':') != -1)
67
host = Sharpen.Runtime.Substring(proxy_host, 0, proxy_host.IndexOf(':'));
68
port = System.Convert.ToInt32(Sharpen.Runtime.Substring(proxy_host, proxy_host.IndexOf
75
this.proxy_host = host;
76
this.proxy_port = port;
79
public ProxyHTTP(string proxy_host, int proxy_port)
81
this.proxy_host = proxy_host;
82
this.proxy_port = proxy_port;
85
public virtual void SetUserPasswd(string user, string passwd)
91
/// <exception cref="NSch.JSchException"></exception>
92
public virtual void Connect(SocketFactory socket_factory, string host, int port,
97
if (socket_factory == null)
99
socket = Util.CreateSocket(proxy_host, proxy_port, timeout);
100
@in = socket.GetInputStream();
101
@out = socket.GetOutputStream();
105
socket = socket_factory.CreateSocket(proxy_host, proxy_port);
106
@in = socket_factory.GetInputStream(socket);
107
@out = socket_factory.GetOutputStream(socket);
111
socket.ReceiveTimeout = timeout;
113
socket.NoDelay = true;
114
@out.Write(Util.Str2byte("CONNECT " + host + ":" + port + " HTTP/1.0\r\n"));
115
if (user != null && passwd != null)
117
byte[] code = Util.Str2byte(user + ":" + passwd);
118
code = Util.ToBase64(code, 0, code.Length);
119
@out.Write(Util.Str2byte("Proxy-Authorization: Basic "));
121
@out.Write(Util.Str2byte("\r\n"));
123
@out.Write(Util.Str2byte("\r\n"));
126
StringBuilder sb = new StringBuilder();
132
sb.Append((char)foo);
144
throw new IOException();
146
string response = sb.ToString();
147
string reason = "Unknow reason";
151
foo = response.IndexOf(' ');
152
int bar = response.IndexOf(' ', foo + 1);
153
code_1 = System.Convert.ToInt32(Sharpen.Runtime.Substring(response, foo + 1, bar)
155
reason = Sharpen.Runtime.Substring(response, bar + 1);
162
throw new IOException("proxy error: " + reason);
185
throw new IOException();
193
catch (RuntimeException e)
209
string message = "ProxyHTTP: " + e.ToString();
212
throw new JSchException(message, (Exception)e);
214
throw new JSchException(message);
218
public virtual InputStream GetInputStream()
223
public virtual OutputStream GetOutputStream()
228
public virtual Socket GetSocket()
233
public virtual void Close()
258
public static int GetDefaultPort()