Https connection using IP address

0 votes
asked Oct 28 by lanopk (1,530 points)

Hello.
Please make a basic inquiry about the use of HTTPS.

The connecting server has 5 servers (IP address) in one host name.
I want to connect to the server with the fastest IP among them.

For example, the hostname is "g.api.mega.co.nz", and I can find multiple IP addresses by calling GetHostAddresses.

Among them, the server with the fastest response is found through the ping test.

And I'm trying to make an Http connection to this server.
But the url has a name, not an ip.
For example, string url = "https://g.api.mega.co.nz/..." .

Where and how can I set it up so that I can connect to the IP I want?

Rebex.Net.HttpRequestCreator creator = new Rebex.Net.HttpRequestCreator();
creator.Settings.SslAllowedVersions = Rebex.Net.TlsVersion.TLS10 | Rebex.Net.TlsVersion.TLS11 | Rebex.Net.TlsVersion.TLS12 | Rebex.Net.TlsVersion.TLS13;
creator.Settings.SslAcceptAllCertificates = true;

string url = "https://g.api.mega.co.nz/...";

// find fastest ip
// ...
//

var request = creator.Create(url);
request.Method = "GET";
request.KeepAlive = true;

// set ip address
// request.??? = fastestip;

using (var response = request.GetResponse())
{
...
}
Applies to: Rebex HTTPS
commented Oct 29 by lanopk (1,530 points)
I succeeded by doing the following in .Net Http.
I'm not sure if this is the right way to do it.

string url = "https://{fastestip}/{parameter}";
var request = (HttpWebRequest) WebRequest.Create(url);
request.Host = "abc.com";
request.Response();

But Rebex.Net.HttpRequest doesn't have a "Host" setting.

1 Answer

0 votes
answered Oct 29 by Lukas Pokorny (126,870 points)
selected Nov 3 by lanopk
 
Best answer

You can implement a simple custom socket factory that makes it possible to choose the IP you need. Try this code to get started:

public class CustomSocket : ProxySocket, ISocket
{
    private static readonly ISocketFactory _factory = new CustomSocketFactory();

    public static ISocketFactory Factory { get { return _factory; } }

    private class CustomSocketFactory : ISocketFactory
    {
        ISocket ISocketFactory.CreateSocket()
        {
            return new CustomSocket();
        }
    }

    void ISocket.Connect(string serverName, int serverPort)
    {
        // choose the desired IP address here
        IPHostEntry entry = Dns.GetHostEntry(serverName);
        IPAddress address = entry.AddressList[0];

        // connect to it
        Connect(new IPEndPoint(address, serverPort));
    }
}

To utilize this in your code, use SetSocketFactory method:

var creator = new HttpRequestCreator();
creator.SetSocketFactory(CustomSocket.Factory);
...
commented Nov 4 by lanopk (1,530 points)
Thanks for the reply.
I'm going to try the method you suggested.

Is there any way to change the "Host" header when making a request?
commented Nov 4 by Lukas Pokorny (126,870 points)
The "Host" header corresponds to the host name in the specified URL. To use a different "Host" header, just specify a different URL.
...