Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an HttpProxyClient for use with the various protocol clients #847

Closed
biasc opened this issue Apr 16, 2019 · 16 comments
Closed

Add an HttpProxyClient for use with the various protocol clients #847

biasc opened this issue Apr 16, 2019 · 16 comments
Labels
enhancement New feature or request

Comments

@biasc
Copy link

biasc commented Apr 16, 2019

Describe the bug
I have successfully tested the mailkit imap class with 2 differents imap account (outlook and gmail) without using proxy
I am not able to connect to our http proxy in any mode, i don't have a socks proxy.
The connection to the proxy is Ok but when i call the connect method i have the error posted below.
This is the code i have used.
What i am doing wrong?

To Reproduce

client = new ImapClient(new ProtocolLogger("imap.log"))
                {
                    ServerCertificateValidationCallback = (s, c, h, e) => true,
                    CheckCertificateRevocation = false
                };
string proxyServer = "192.168.16.19";
                int proxyPort = 8080;
                string proxyUsername = "admin";
                string proxyPassword = "password";
                string b64UserPwd = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(proxyUsername + ":" + proxyPassword));
                Socket mySocket = new Socket(SocketType.Stream, ProtocolType.Tcp);
                mySocket.Connect(proxyServer,proxyPort);
                string connectMsg = string.Format("CONNECT {0}:{1} HTTP/1.0\r\n" + "Proxy-Authorization: Basic {2}\r\n" + "\r\n", 
                    proxyServer, 
                    proxyPort,
                    b64UserPwd);
                byte[] msg = Encoding.UTF8.GetBytes(connectMsg);
                mySocket.Send(msg);
                string proxyConnectionResponseHeader = "";
                while (!proxyConnectionResponseHeader.Contains("\r\n\r\n"))
                {
                    byte[] buffer = new byte[1];
                    mySocket.Receive(buffer, 0, 1, 0);
                    proxyConnectionResponseHeader += Encoding.ASCII.GetString(buffer);
                }
                if (!proxyConnectionResponseHeader.Contains("200 Connection established"))
                {
                    throw new Exception("Connection to Proxy Failed: " + proxyConnectionResponseHeader);
                }

 var mode = _appModel.UseSSL ? SecureSocketOptions.SslOnConnect :                                                 SecureSocketOptions.None;
                    client.Connect(mySocket,
                        _appModel.Server,
                        _appModel.Port,
                        mode);

Screenshots
image

Desktop (please complete the following information):

  • OS: Windows 10
@jstedfast
Copy link
Owner

Is mode = SecureSocketOptions.SslOnConnect? And what port are you trying to connect to? Is it 993? Or 143? Looks like you are trying to connect to port 143.

SslOnConnect is for port 993 (assuming the server uses standard ports). Try specifying StartTlsWhenAvailable instead.

@jstedfast jstedfast added the question A question about how to do something label Apr 16, 2019
@biasc
Copy link
Author

biasc commented Apr 16, 2019

The port is 993. Without the proxy configuration it works fine

@jstedfast
Copy link
Owner

After connecting your socket, try just creating and using an SslStream on top of it like this:

var network = new NetworkStream (mySocket, false);
var ssl = new SslStream (network, true);
ssl.AuthenticateAsClient (_appModel.Server);

Does that throw any exceptions?

@jstedfast
Copy link
Owner

I'm not sure how HTTP proxies work, but are you sure you're supposed to use HTTP/1.0 instead of HTTP/1.1?

@biasc
Copy link
Author

biasc commented Apr 16, 2019

I'have tried HTTP/1.1 and HTTP/1.0 but there are no diff.
Using

var network = new NetworkStream (mySocket, false); var ssl = new SslStream (network, true); ssl.AuthenticateAsClient (_appModel.Server);

Throws an errror
image

@jstedfast
Copy link
Owner

Unexpected packet format suggests to me that the port you are actually connecting to doesn't actually support SSL.

@Safirion
Copy link

I have the same problem. Is there a simple option to just use a WebProxy with MailKit ?

@jstedfast
Copy link
Owner

@Safirion I would recommend starting with what @biasc has and trying to figure out why SslStream.AUthenticateAsClient() is failing because once you figure that out, then using an HTTP proxy is solved.

The WebProxy API doesn't actually implement a way of connecting through a proxy, it only holds information about a proxy URI and the credentials it would need. In other words, it looks useless for solving your problem.

@Safirion
Copy link

@jstedfast Yes of course, but when a solution will be find for this problem, then it would be possible to implement a support of WebProxy directly in the library, no ?

@biasc
Copy link
Author

biasc commented Apr 19, 2019

@Safirion I have used this code
https://github.com/poma/ProxySocket/blob/master/src/Org.Mentalis.Network.ProxySocket/ProxySocket.cs
and it works.
@jstedfast maybe it can be used to create a standard implementation in mailkit?

jstedfast added a commit that referenced this issue Apr 19, 2019
@jstedfast
Copy link
Owner

Try the MyGet package once it's published by the build bots (next 30 minutes or so?) and use MailKit.Net.Proxy.HttpProxyClient.

(Note: the main MailKit github page provides download links to official NuGet release packages as well as MyGet CI builds)

@biasc
Copy link
Author

biasc commented Apr 19, 2019

@jstedfast I have already tested the MailKit.Net.Proxy.HttpProxyClient and it works!
GREAT!
This will be included in 2.1.5 ?

@jstedfast
Copy link
Owner

yes

@biasc
Copy link
Author

biasc commented Apr 19, 2019

Fantastic, thanks!

@jstedfast jstedfast added enhancement New feature or request and removed question A question about how to do something labels Apr 19, 2019
@jstedfast jstedfast changed the title Problem with http Proxy Add an HttpProxyClient for use with the various protocol clients Apr 19, 2019
@biasc
Copy link
Author

biasc commented Apr 23, 2019

@jstedfast When is the 2.1.5 release scheduled?

@jstedfast
Copy link
Owner

Hoping to fix a few more issues first. Making releases chews up a lot of my free time, so I like to spread them out and collect as many fixes as possible before rolling the next release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants