Fix proxy connection for Cryville.Common.Network.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net.Sockets;
|
||||
@@ -18,12 +19,22 @@ namespace Cryville.Common.Network {
|
||||
readonly int origPort;
|
||||
|
||||
protected string Version = "HTTP/1.1";
|
||||
protected TcpClient TcpClient;
|
||||
protected TcpClient TcpClient { get; private set; }
|
||||
protected Stream RawTcpStream {
|
||||
get {
|
||||
return TcpClient.GetStream();
|
||||
}
|
||||
}
|
||||
protected virtual Stream Stream {
|
||||
get {
|
||||
return TcpClient.GetStream();
|
||||
}
|
||||
}
|
||||
protected virtual string WindowsProxyProtocolName {
|
||||
get {
|
||||
return "http";
|
||||
}
|
||||
}
|
||||
|
||||
private readonly bool _proxied = false;
|
||||
|
||||
@@ -42,7 +53,7 @@ namespace Cryville.Common.Network {
|
||||
|
||||
public virtual void Connect() {
|
||||
if (_proxied) {
|
||||
Request("CONNECT", _baseUri.Host + ":" + origPort.ToString());
|
||||
Request(RawTcpStream, "CONNECT", string.Format(CultureInfo.InvariantCulture, "{0}:{1}", _baseUri.Host, origPort));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,17 +63,13 @@ namespace Cryville.Common.Network {
|
||||
|
||||
public HttpResponse Request(string method, Uri uri, string body = null, Encoding encoding = null) {
|
||||
string struri = GetUri(uri).PathAndQuery;
|
||||
// if (_proxied) struri = GetUri(uri).AbsoluteUri;
|
||||
return Request(method, struri, body, encoding);
|
||||
return Request(Stream, method, struri, body, encoding);
|
||||
}
|
||||
|
||||
public HttpResponse Request(string method, string uri, string body = null, Encoding encoding = null) {
|
||||
public HttpResponse Request(Stream stream, string method, string uri, string body = null, Encoding encoding = null) {
|
||||
var headers = new Dictionary<string, string>();
|
||||
// if (Stream.CanTimeout) Stream.ReadTimeout = Stream.WriteTimeout = 5000;
|
||||
foreach (var h in Headers)
|
||||
headers.Add(h.Key, h.Value);
|
||||
// headers["Accept"] = "text/plain, */*";
|
||||
// headers["Connection"] = "close";
|
||||
headers["Host"] = _baseUri.Host;
|
||||
byte[] payload = new byte[0];
|
||||
if (body != null) {
|
||||
@@ -85,14 +92,14 @@ namespace Cryville.Common.Network {
|
||||
Array.Copy(buffer0, buffer1, buffer0.Length);
|
||||
Array.Copy(payload, 0, buffer1, buffer0.Length, payload.Length);
|
||||
Logger.Log("main", 0, "Network", Encoding.UTF8.GetString(buffer1));
|
||||
Stream.Write(buffer1, 0, buffer1.Length);
|
||||
Stream.Flush();
|
||||
var response = new HttpResponse(Stream);
|
||||
stream.Write(buffer1, 0, buffer1.Length);
|
||||
stream.Flush();
|
||||
var response = new HttpResponse(stream);
|
||||
Logger.Log("main", 0, "Network", "{0}", response);
|
||||
return response;
|
||||
}
|
||||
|
||||
protected virtual bool GetProxy(ref string host, ref int port) {
|
||||
protected bool GetProxy(ref string host, ref int port) {
|
||||
if (Environment.OSVersion.Platform == PlatformID.Win32NT) {
|
||||
var reg = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings");
|
||||
var proxyEnable = (int)reg.GetValue("ProxyEnable");
|
||||
@@ -101,7 +108,13 @@ namespace Cryville.Common.Network {
|
||||
if (!string.IsNullOrEmpty(proxyStr)) {
|
||||
string[] proxies = proxyStr.Split(';');
|
||||
foreach (var p in proxies) {
|
||||
if (p.StartsWith("http=")) {
|
||||
if (!p.Contains('=')) {
|
||||
string[] s = p.Split(':');
|
||||
host = s[0];
|
||||
port = int.Parse(s[1]);
|
||||
return true;
|
||||
}
|
||||
else if (p.StartsWith(WindowsProxyProtocolName + "=")) {
|
||||
string[] s = p.Split('=', ':');
|
||||
host = s[1];
|
||||
port = int.Parse(s[2]);
|
||||
|
@@ -1,7 +1,6 @@
|
||||
using Microsoft.Win32;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Net.Sockets;
|
||||
|
||||
namespace Cryville.Common.Network {
|
||||
public class HttpsClient : HttpClient {
|
||||
@@ -12,40 +11,24 @@ namespace Cryville.Common.Network {
|
||||
return _tlsTcpClient.Stream;
|
||||
}
|
||||
}
|
||||
protected override string WindowsProxyProtocolName {
|
||||
get {
|
||||
return "https";
|
||||
}
|
||||
}
|
||||
|
||||
public HttpsClient(Uri baseUri) : base(baseUri, 443) {
|
||||
_tlsTcpClient = new TlsTcpClient(DirectHost, DirectPort);
|
||||
_tlsTcpClient = new TlsTcpClient(TcpClient, baseUri.Host);
|
||||
}
|
||||
|
||||
public override void Connect() {
|
||||
_tlsTcpClient.Connect();
|
||||
base.Connect();
|
||||
_tlsTcpClient.Connect();
|
||||
}
|
||||
|
||||
public override void Close() {
|
||||
base.Close();
|
||||
_tlsTcpClient.Close();
|
||||
}
|
||||
|
||||
protected override bool GetProxy(ref string host, ref int port) {
|
||||
if (Environment.OSVersion.Platform == PlatformID.Win32NT) {
|
||||
var reg = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings");
|
||||
var proxyEnable = (int)reg.GetValue("ProxyEnable");
|
||||
if (proxyEnable == 0) return false;
|
||||
var proxyStr = (string)reg.GetValue("ProxyServer");
|
||||
if (!string.IsNullOrEmpty(proxyStr)) {
|
||||
string[] proxies = proxyStr.Split(';');
|
||||
foreach (var p in proxies) {
|
||||
if (p.StartsWith("https=")) {
|
||||
string[] s = p.Split('=', ':');
|
||||
host = s[1];
|
||||
port = int.Parse(s[2]);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
base.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -10,13 +10,11 @@ using System.Text;
|
||||
|
||||
namespace Cryville.Common.Network {
|
||||
public class TlsTcpClient {
|
||||
readonly TcpClient _tcpClient;
|
||||
readonly TlsClientProtocol _protocol;
|
||||
readonly TlsClient _tlsClient;
|
||||
public Stream Stream { get; private set; }
|
||||
public TlsTcpClient(string hostname, int port) {
|
||||
_tcpClient = new TcpClient(hostname, port);
|
||||
_protocol = new TlsClientProtocol(_tcpClient.GetStream());
|
||||
public TlsTcpClient(TcpClient tcpClient, string hostname) {
|
||||
_protocol = new TlsClientProtocol(tcpClient.GetStream());
|
||||
_tlsClient = new InternalTlsClient(hostname, new BcTlsCrypto(new SecureRandom()));
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user