sancarn / stdVBA

VBA Standard Library - A Collection of libraries to form a common standard layer for modern VBA applications.
MIT License
288 stars 60 forks source link

stdHTTP - bug using proxy only for stdHTTP #87

Closed 6DiegoDiego9 closed 9 months ago

6DiegoDiego9 commented 9 months ago

I manually set stdHTTP to use a proxy, with the following instructions: options("ProxyServer") = proxyString '(ip:port) options("ProxyUser") = username options("ProxyPass") = password

and I don't use a proxy outside stdHTTP (no proxy for browser).

However: A) if I set options("AutoProxy") = False, the proxy is simply not used at all by stdHTTP B) if I set options("AutoProxy") = True, protInit calls LoadAutoProxy, that calls GetProxyCurrentUser, that eventually calls WinHttpGetProxyForUrl which fails with Err.LastDllError = ERROR_WINHTTP_AUTODETECTION_FAILED (12180) (maybe because I don't use a proxy except for stdHTTP?)

sancarn commented 9 months ago

Thanks for reporting the issue! You're right, I don't set proxy information unless it's coming from the autoproxy!

I'll need to add:

If options.exists("ProxyServer") then request.SetProxy ProxySetting.Proxy, options("ProxyServer"), options("ProxyBypass")
If options.exists("ProxyUser") then request.SetCredentials options("ProxyUser"), options("ProxyPass"), SetCredentialsType.ForProxy

to protInit as a work around for now you can use an authenticator:

Public Sub BindProxy(ByVal pHTTP As Object, ByVal RequestMethod As String, ByVal sURL As String, ByVal ThreadingStyle As Long, ByVal options As Object)
  const ProxySetting_Proxy as Long = 2 
  const SetCredentialsType_ForProxy as Long = 1
  pHTTP.SetProxy ProxySetting_Proxy, options("ProxyServer"), options("ProxyBypass")
  pHTTP.SetCredentials options("ProxyUser"), options("ProxyPass"), SetCredentialsType_ForProxy
End Sub
sancarn commented 9 months ago

Think this should be completed now :) Let me know if it doesn't work

6DiegoDiego9 commented 9 months ago

Thanks for answering so fast! :)

There are a few issues: 1) ProxyUser and ProxyPass are both created by default when CreateOptions is called, even if for other reasons 2) "request" must likely be replaced with "pHTTP"

To fix 1), I think that we should: A) test for options("ProxyUser") <> vbNullString instead of options.exists("ProxyUser") B) create the key "ProxyServer" in CreateOptions by default, and test for options("ProxyServer") <> vbNullString for the first line, and subtest for options("ProxyUser") <> vbNullString for the second line

I tend for B), what do you think?

sancarn commented 9 months ago

Hi there, in the works for building the feature up again. But to fix this we'll be moving away from an explicit AutoProxy param in CreateOptions() and instead moving to a ProxyInfo param instead. ProxyInfo can be created from CreateProxy()

stdHTTP.Create(sURL, options:=stdHTTP.CreateOptions(Proxy:= stdHTTP.CreateProxy(ProxyTypeManual, myServer, myBypass, myUser, myPass))

By default proxy option will be CreateProxy(ProxyTypeAuto) and you can also specify CreateProxy(ProxyTypeNone) too. ProxyTypeAuto isn't fully complete yet though, just seen an issue! Will endeavor to fix :)

6DiegoDiego9 commented 9 months ago

The current version fails to compile:

image

sancarn commented 9 months ago

Hi @6DiegoDiego9, I've just pushed a fix.

6DiegoDiego9 commented 9 months ago

No more compile error, however I'm totally new to stdHTTP. Could you please write an "hello world" example using proxy with username/password?

sancarn commented 9 months ago

Are you sure you want a proxy?

It's hard to give you an example without first having a proxy to use, which is why this feature hasn't really been tested. Personally I always get by with what autoproxy is giving... As far as I know:

const sURL as string = "http://google.com"
const ProxyServer as string = "http://myproxy.whatever:port"
const ProxyUser as string = "MyUsername"
const ProxyPass as string = "MyPassword"
Dim resp as stdHTTP
set resp = stdHTTP.Create(sURL, options:=stdHTTP.CreateOptions(Proxy:= stdHTTP.CreateProxy(ProxyTypeManual, ProxyServer, "", ProxyUser, ProxyPass))

should work... Unless you don't actually want a proxy, and are looking for something else.

6DiegoDiego9 commented 9 months ago

Thanks, it worked!

ProxyServer can also be simply "IP:port"