diff --git a/go.mod b/go.mod index 6586d3b..d682d07 100644 --- a/go.mod +++ b/go.mod @@ -6,11 +6,15 @@ require ( github.com/bytedance/sonic v1.8.6 github.com/gin-gonic/gin v1.7.7 github.com/pkg/errors v0.9.1 + github.com/spf13/pflag v1.0.5 github.com/spf13/viper v1.16.0 + github.com/stretchr/testify v1.8.4 + golang.org/x/net v0.10.0 ) require ( github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/fsnotify/fsnotify v1.6.0 // indirect github.com/gin-contrib/sse v0.1.0 // indirect github.com/go-playground/locales v0.14.1 // indirect @@ -27,10 +31,10 @@ require ( github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pelletier/go-toml/v2 v2.0.8 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/spf13/afero v1.9.5 // indirect github.com/spf13/cast v1.5.1 // indirect github.com/spf13/jwalterweatherman v1.1.0 // indirect - github.com/spf13/pflag v1.0.5 // indirect github.com/subosito/gotenv v1.4.2 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/ugorji/go/codec v1.2.11 // indirect diff --git a/go.sum b/go.sum index 2555c8d..c89ae04 100644 --- a/go.sum +++ b/go.sum @@ -212,8 +212,9 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= -github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY= github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/subosito/gotenv v1.4.2 h1:X1TuBLAMDFbaTAChgCBLu3DU3UPyELpnF2jjJ2cz/S8= github.com/subosito/gotenv v1.4.2/go.mod h1:ayKnFf/c6rvx/2iiLrJUk1e6plDbT3edrFNGqEflhK0= github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= @@ -309,6 +310,8 @@ golang.org/x/net v0.0.0-20201209123823-ac852fbbde11/go.mod h1:m0MpNAwzfU5UDzcl9v golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M= +golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= diff --git a/util/http_proxy.go b/util/http_proxy.go new file mode 100644 index 0000000..120891a --- /dev/null +++ b/util/http_proxy.go @@ -0,0 +1,54 @@ +package util + +import ( + "context" + "encoding/base64" + "fmt" + "net" + "net/http" + "net/url" + + "golang.org/x/net/proxy" +) + +func NewHttpProxy(proxyAddress string) (*http.Transport, error) { + proxyURL, err := url.Parse(proxyAddress) + if err != nil { + return nil, fmt.Errorf("error parsing proxy URL: %v", err) + } + + transport := &http.Transport{ + Proxy: http.ProxyURL(proxyURL), + } + + if proxyURL.User != nil { + proxyAuth := "Basic " + base64.StdEncoding.EncodeToString([]byte(proxyURL.User.String())) + + transport.ProxyConnectHeader = http.Header{ + "Proxy-Authorization": []string{proxyAuth}, + } + } + + return transport, nil +} + +func NewSocksProxy(proxyAddress string) (*http.Transport, error) { + // proxyAddress: socks5://user:password@127.0.0.1:1080 + proxyURL, err := url.Parse(proxyAddress) + if err != nil { + return nil, fmt.Errorf("error parsing proxy URL: %v", err) + } + + dialer, err := proxy.FromURL(proxyURL, proxy.Direct) + if err != nil { + return nil, fmt.Errorf("error creating proxy dialer: %v", err) + } + + transport := &http.Transport{ + DialContext: func(ctx context.Context, network, address string) (net.Conn, error) { + return dialer.Dial(network, address) + }, + } + + return transport, nil +} diff --git a/util/http_proxy_test.go b/util/http_proxy_test.go new file mode 100644 index 0000000..83a4ed7 --- /dev/null +++ b/util/http_proxy_test.go @@ -0,0 +1,41 @@ +package util + +import ( + "github.com/stretchr/testify/assert" + "net/http" + "testing" +) + +func TestHttpProxy(t *testing.T) { + proxyAddress := "http://127.0.0.1:1087" + transport, err := NewHttpProxy(proxyAddress) + + assert.NoError(t, err) + assert.NotNil(t, transport) + + client := &http.Client{ + Transport: transport, + } + + resp, err := client.Get("https://www.google.com") + assert.NoError(t, err) + assert.NotNil(t, resp) + assert.Equal(t, 200, resp.StatusCode) +} + +func TestSocksProxy(t *testing.T) { + proxyAddress := "socks5://127.0.0.1:1080" + transport, err := NewSocksProxy(proxyAddress) + + assert.NoError(t, err) + assert.NotNil(t, transport) + + client := &http.Client{ + Transport: transport, + } + + resp, err := client.Get("https://www.google.com") + assert.NoError(t, err) + assert.NotNil(t, resp) + assert.Equal(t, 200, resp.StatusCode) +}