Files
2025-01-08 14:41:07 +08:00

28 lines
710 B
Go

package core
import (
"fmt"
"httppp/common"
"net/url"
)
// Node2ProxyURL 将 Node 结构体转为代理 URL
func Node2ProxyURL(node *common.Node) (string, error) {
// 如果代理需要认证,构造用户名:密码@部分
var authPart string
if node.Username != "" && node.Password != "" {
authPart = fmt.Sprintf("%s:%s@", node.Username, node.Password)
}
// 使用 "protocol://username:password@addr:port" 格式构建代理 URL
proxyURL := fmt.Sprintf("%s://%s%s:%d", node.Protocol, authPart, node.Addr, node.Port)
// 解析代理 URL
parsedURL, err := url.Parse(proxyURL)
if err != nil {
return "", fmt.Errorf("failed to parse proxy URL: %v", err)
}
return parsedURL.String(), nil
}