Skip to content

Commit

Permalink
feat: add http & socks5 proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
stulzq committed Sep 26, 2023
1 parent a02b32f commit 2abc77d
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 2 deletions.
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
5 changes: 4 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
54 changes: 54 additions & 0 deletions util/http_proxy.go
Original file line number Diff line number Diff line change
@@ -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:[email protected]: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
}
41 changes: 41 additions & 0 deletions util/http_proxy_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 2abc77d

Please sign in to comment.