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

socks5支持用户名和密码 #387

Merged
merged 1 commit into from
May 17, 2024
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
25 changes: 23 additions & 2 deletions hcutil/hcutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,29 @@ func ModifyURL(url string) string {
return fmt.Sprintf("http://%s", url)
}

func SetSOCKS5(c *http.Client, addr string) error {
dialer, err := proxy.SOCKS5("tcp", addr, nil, proxy.Direct)
// socks5://username:password@localhost:7890
// socks5://localhost:7890
// localhost:7890
func SetSOCKS5(c *http.Client, socks5URL string) error {
host := socks5URL
var auth *proxy.Auth
if strings.HasPrefix(socks5URL, "socks5://") {
proxyURL, err := url.Parse(socks5URL)
if err != nil {
return err
}

if proxyURL.User != nil {
password, _ := proxyURL.User.Password()
auth = &proxy.Auth{
User: proxyURL.User.Username(),
Password: password,
}
}

host = proxyURL.Host
}
dialer, err := proxy.SOCKS5("tcp", host, auth, proxy.Direct)
if err != nil {
return err
}
Expand Down
Loading