first commit
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
package proxynet
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"xiawan/wx/clientsdk/baseutils"
|
||||
|
||||
"golang.org/x/net/proxy"
|
||||
)
|
||||
|
||||
// WXProxyInfo 代理信息
|
||||
type WXProxyInfo struct {
|
||||
ProxyUrl string
|
||||
// 代理城市
|
||||
CityName string
|
||||
// 代理IP
|
||||
ProxyIP string
|
||||
// 代理端口
|
||||
ProxyPort uint32
|
||||
// 用户名
|
||||
UserName string
|
||||
// 密码
|
||||
Password string
|
||||
}
|
||||
|
||||
func ParseWXProxyInfo(url string) *WXProxyInfo {
|
||||
// socks5://username:password@ipv4:port
|
||||
// 处理代理 ip 端口 用户名 密码
|
||||
if len(url) == 0 {
|
||||
return nil
|
||||
}
|
||||
newUrlStr := strings.Replace(url, "socks5://", "", 1)
|
||||
urlArr := strings.Split(newUrlStr, "@")
|
||||
if len(urlArr) == 2 {
|
||||
userPassArr := strings.Split(urlArr[0], ":")
|
||||
if len(userPassArr) == 2 {
|
||||
portArr := strings.Split(urlArr[1], ":")
|
||||
if len(portArr) == 2 {
|
||||
port, err := strconv.ParseUint(portArr[1], 10, 32)
|
||||
if err == nil {
|
||||
return &WXProxyInfo{
|
||||
ProxyUrl: url,
|
||||
UserName: userPassArr[0],
|
||||
Password: userPassArr[1],
|
||||
ProxyIP: portArr[0],
|
||||
ProxyPort: uint32(port),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return &WXProxyInfo{
|
||||
ProxyUrl: url,
|
||||
}
|
||||
}
|
||||
|
||||
// NewWXProxyInfo 新建代理信息
|
||||
func NewWXProxyInfo(cityName string, ip string, port uint32, userName string, password string) *WXProxyInfo {
|
||||
return &WXProxyInfo{
|
||||
CityName: cityName,
|
||||
ProxyIP: ip,
|
||||
ProxyPort: port,
|
||||
UserName: userName,
|
||||
Password: password,
|
||||
}
|
||||
}
|
||||
|
||||
// GetDialer 获取代理
|
||||
func (wxpi *WXProxyInfo) GetDialer() proxy.Dialer {
|
||||
if len(wxpi.ProxyUrl) > 0 {
|
||||
urlproxy, err := url.Parse(wxpi.ProxyUrl) //socks5://test:test@119.96.129.237:1080
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
dialer, err := proxy.FromURL(urlproxy, proxy.Direct)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return dialer
|
||||
}
|
||||
|
||||
auth := &proxy.Auth{
|
||||
User: wxpi.UserName,
|
||||
Password: wxpi.Password,
|
||||
}
|
||||
|
||||
// 创建拨号器
|
||||
proxyAddr := fmt.Sprintf("%s:%d", wxpi.ProxyIP, wxpi.ProxyPort)
|
||||
dialer, err := proxy.SOCKS5("tcp", proxyAddr, auth, proxy.Direct)
|
||||
if err != nil {
|
||||
baseutils.PrintLog("GetDialer err: " + err.Error())
|
||||
return nil
|
||||
}
|
||||
return dialer
|
||||
}
|
||||
Reference in New Issue
Block a user