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

upstream proxy support https proxy mode #73

Merged
merged 2 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
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
19 changes: 18 additions & 1 deletion internal/helper/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package helper
import (
"bufio"
"context"
"crypto/tls"
"encoding/base64"
"errors"
"net"
Expand All @@ -14,11 +15,27 @@ import (

// GetProxyConn connect proxy
// ref: http/transport.go dialConn func
func GetProxyConn(ctx context.Context, proxyUrl *url.URL, address string) (net.Conn, error) {
func GetProxyConn(ctx context.Context, proxyUrl *url.URL, address string, sslInsecure bool) (net.Conn, error) {
conn, err := (&net.Dialer{}).DialContext(ctx, "tcp", proxyUrl.Host)
if err != nil {
return nil, err
}
// 如果代理URL是HTTPS,则进行TLS握手
if proxyUrl.Scheme == "https" {
tlsConfig := &tls.Config{
ServerName: proxyUrl.Hostname(), // 设置TLS握手的服务器名称
InsecureSkipVerify: sslInsecure,
// 可以在这里添加其他TLS配置
}
// 包装原始连接为TLS连接
tlsConn := tls.Client(conn, tlsConfig)
// 执行TLS握手
if err := tlsConn.HandshakeContext(ctx); err != nil {
conn.Close() // 握手失败,关闭连接
return nil, err
}
conn = tlsConn // 使用TLS连接替换原始连接
}
connectReq := &http.Request{
Method: "CONNECT",
URL: &url.URL{Opaque: address},
Expand Down
2 changes: 1 addition & 1 deletion proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (proxy *Proxy) getUpstreamConn(ctx context.Context, req *http.Request) (net
}
var conn net.Conn
if proxyUrl != nil {
conn, err = helper.GetProxyConn(ctx, proxyUrl, req.Host)
conn, err = helper.GetProxyConn(ctx, proxyUrl, req.Host, proxy.Opts.SslInsecure)
} else {
conn, err = (&net.Dialer{}).DialContext(ctx, "tcp", req.Host)
}
Expand Down