Files
go-account-register/utils/proxy_handle.go
2025-09-13 10:22:49 +08:00

43 lines
857 B
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package utils
import (
"net/url"
"strings"
)
type ProxyHandle struct {
}
func (ProxyHandle) ParseProxy(proxyStr string) (protocol, username, password, ip, port string, err error) {
// 清理字符串中的多余字符(如末尾的 \n
proxyStr = strings.TrimSpace(proxyStr)
// 解析 URL
proxyURL, err := url.Parse(proxyStr)
if err != nil {
return "", "", "", "", "", err
}
// 提取协议http/https
protocol = proxyURL.Scheme
// 提取 IP 和端口
host := proxyURL.Host
if strings.Contains(host, ":") {
parts := strings.Split(host, ":")
ip = parts[0]
port = parts[1]
} else {
ip = host
port = "8080" // 默认端口
}
// 提取用户名和密码
if proxyURL.User != nil {
username = proxyURL.User.Username()
password, _ = proxyURL.User.Password()
}
return protocol, username, password, ip, port, nil
}