diff --git a/QuickFIXn/SocketSettings.cs b/QuickFIXn/SocketSettings.cs index 8bb11b7eb..925dad6a0 100644 --- a/QuickFIXn/SocketSettings.cs +++ b/QuickFIXn/SocketSettings.cs @@ -14,6 +14,14 @@ public class SocketSettings : ICloneable { #region Socket Settings + /// + /// Store the socket host name so we can use it to connect via proxy if required. + /// + /// + /// The host name (without port number) to send to the proxy server for acl matching. + /// + public string SocketConnectHost { get; internal set; } + /// /// Gets a value that specifies whether the is using the Nagle algorithm. /// diff --git a/QuickFIXn/Transport/SocketInitiator.cs b/QuickFIXn/Transport/SocketInitiator.cs index bc6e061a3..d2628dfa2 100755 --- a/QuickFIXn/Transport/SocketInitiator.cs +++ b/QuickFIXn/Transport/SocketInitiator.cs @@ -173,7 +173,7 @@ private IPEndPoint GetNextSocketEndPoint(SessionID sessionID, QuickFix.Dictionar int port = System.Convert.ToInt32(settings.GetLong(portKey)); sessionToHostNum_[sessionID] = ++num; - socketSettings_.ServerCommonName = hostName; + socketSettings_.SocketConnectHost = socketSettings_.ServerCommonName = hostName; return new IPEndPoint(addrs.First(a => a.AddressFamily == AddressFamily.InterNetwork), port); } catch (System.Exception e) diff --git a/QuickFIXn/Transport/StreamFactory.cs b/QuickFIXn/Transport/StreamFactory.cs index 39a79e7b5..d7008d901 100644 --- a/QuickFIXn/Transport/StreamFactory.cs +++ b/QuickFIXn/Transport/StreamFactory.cs @@ -17,12 +17,12 @@ namespace QuickFix.Transport /// public static class StreamFactory { - private static Socket CreateTunnelThruProxy(string destIP, int destPort) + private static Socket CreateTunnelThruProxy(string destIP, int destPort, string destHostName) { string destUriWithPort = $"{destIP}:{destPort}"; UriBuilder uriBuilder = new UriBuilder(destUriWithPort); Uri destUri = uriBuilder.Uri; - IWebProxy webProxy = WebRequest.GetSystemWebProxy(); + IWebProxy webProxy = WebRequest.DefaultWebProxy; try { @@ -47,18 +47,19 @@ private static Socket CreateTunnelThruProxy(string destIP, int destPort) Socket socketThruProxy = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socketThruProxy.Connect(proxyEndPoint); - string proxyMsg = $"CONNECT {destIP}:{destPort} HTTP/1.1 \n\n"; + string proxyMsg = !string.IsNullOrWhiteSpace(destHostName) + ? $"CONNECT {destHostName}:{destPort} HTTP/1.1\nHost: {destHostName}:{destPort}\n\n" + : $"CONNECT {destIP}:{destPort} HTTP/1.1\n\n"; byte[] buffer = Encoding.ASCII.GetBytes(proxyMsg); byte[] buffer12 = new byte[500]; socketThruProxy.Send(buffer, buffer.Length, 0); - int msg = socketThruProxy.Receive(buffer12, 500, 0); - string data; - data = Encoding.ASCII.GetString(buffer12); - int index = data.IndexOf("200"); + socketThruProxy.Receive(buffer12, 500, 0); + string data = Encoding.ASCII.GetString(buffer12); + int index = data.IndexOf("200", StringComparison.Ordinal); if (index < 0) throw new ApplicationException( - $"Connection failed to {destUriWithPort} through proxy server {proxyUri.ToString()}."); + $"Connection failed to {destUriWithPort} through proxy server {proxyUri}."); return socketThruProxy; } @@ -73,7 +74,7 @@ private static Socket CreateTunnelThruProxy(string destIP, int destPort) public static Stream CreateClientStream(IPEndPoint endpoint, SocketSettings settings, ILog logger) { // If system has configured a proxy for this config, use it. - Socket socket = CreateTunnelThruProxy(endpoint.Address.ToString(), endpoint.Port); + Socket socket = CreateTunnelThruProxy(endpoint.Address.ToString(), endpoint.Port, settings.SocketConnectHost); // No proxy. Set up a regular socket. if (socket == null)