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

Couple of proxy fixes #833

Merged
merged 2 commits into from
May 26, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions src/ConnectionRequest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ using Base64: base64encode
import ..@debug, ..DEBUG_LEVEL
import ..Streams: Stream

islocalhost(host) = host == "localhost" || host == "127.0.0.1" || host == "127.0.0.1"

# hasdotsuffix reports whether s ends in "."+suffix.
hasdotsuffix(s, suffix) = endswith(s, "." * suffix)

Expand All @@ -32,7 +34,7 @@ function __init__()
end

function getproxy(scheme, host)
isnoproxy(host) && return nothing
(isnoproxy(host) || islocalhost(host)) && return nothing
if scheme == "http" && (p = get(ENV, "http_proxy", ""); !isempty(p))
return p
elseif scheme == "http" && (p = get(ENV, "HTTP_PROXY", ""); !isempty(p))
Expand Down Expand Up @@ -85,22 +87,28 @@ function connectionlayer(handler)
end

try
if proxy !== nothing && target_url.scheme == "https"
if proxy !== nothing && target_url.scheme in ("https", "wss", "ws")
# tunnel request
target_url = URI(target_url, port=443)
if target_url.scheme in ("https", "wss")
target_url = merge(target_url, port=443)
elseif target_url.scheme in ("ws", ) && target_url.port == ""
target_url = merge(target_url, port=80) # if there is no port info, connect_tunnel will fail
end
r = connect_tunnel(io, target_url, req)
if r.status != 200
close(io)
return r
end
io = ConnectionPool.sslupgrade(io, target_url.host; kw...)
if target_url.scheme in ("https", "wss")
io = ConnectionPool.sslupgrade(io, target_url.host; kw...)
end
req.headers = filter(x->x.first != "Proxy-Authorization", req.headers)
end

stream = Stream(req.response, io)
resp = handler(stream; kw...)

if proxy !== nothing && target_url.scheme == "https"
if proxy !== nothing && target_url.scheme in ("https", "wss")
close(io)
end

Expand Down