Skip to content

Commit

Permalink
socks5支持用户名和密码
Browse files Browse the repository at this point in the history
  • Loading branch information
guonaihong committed May 17, 2024
1 parent 012ba1a commit 5236214
Showing 1 changed file with 23 additions and 2 deletions.
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

0 comments on commit 5236214

Please sign in to comment.