From 05cd9086dc55327b7fe77327d13505b7c6beca92 Mon Sep 17 00:00:00 2001 From: wuhanqing <554553400@qq.com> Date: Thu, 11 Apr 2024 17:49:02 +0800 Subject: [PATCH] UPDATE Inbounds. Support http, socks, socks5 --- conf/conf_val.go | 8 ++++++-- main/conf.go | 1 + proxy_node.go | 8 ++++++-- v2ray_config.go | 3 +++ v2raypool.go | 21 +++++++++++++-------- 5 files changed, 29 insertions(+), 12 deletions(-) diff --git a/conf/conf_val.go b/conf/conf_val.go index 98891a2..45c7a7b 100644 --- a/conf/conf_val.go +++ b/conf/conf_val.go @@ -52,6 +52,7 @@ func (cf Conf) GetSubscribeData() string { } return strings.TrimSpace(string(b)) } + func (cf Conf) HttpProxySplit() []string { spt := strings.Split(cf.HttpProxy, ":") lensp := len(spt) @@ -59,12 +60,13 @@ func (cf Conf) HttpProxySplit() []string { panic(fmt.Errorf("HttpProxy(%s)设置不正确.例:%s", cf.HttpProxy, DEFAULT_HTTP_PROXY)) } protcl := spt[0] - okprotcls := []string{"http"} // , "socks" + okprotcls := []string{"http", "socks", "socks5"} if miniutils.GetIndexOf(protcl, okprotcls) == -1 { panic(fmt.Errorf("HttpProxy(%s)设置不正确. Protocol Only Support: %v", cf.HttpProxy, okprotcls)) } return spt } + func (cf Conf) GetHttpProxyPort() int { spt := cf.HttpProxySplit() portStr := spt[2] @@ -76,8 +78,10 @@ func (cf Conf) GetHttpProxyPort() int { } // GetHttpProxyProtocol. get SystemProxy Inbound Protocol By HttpProxy. Only Support http and socks +// curl --proxy socks5://127.0.0.1:30000 https://httpbin.org/get -v +// curl --proxy http://127.0.0.1:30000 https://httpbin.org/get -v func (cf Conf) GetHttpProxyProtocol() string { - spt := strings.Split(cf.HttpProxy, ":") + spt := cf.HttpProxySplit() return spt[0] } diff --git a/main/conf.go b/main/conf.go index 314aa8b..73f5b5a 100644 --- a/main/conf.go +++ b/main/conf.go @@ -148,6 +148,7 @@ VP_SUBSCRIBE_URL = "%s" VP_SUBSCRIBE_DATA_FILE = "%s" # 设置HTTP代理,代理池每个节点的本地端口号,往后开始累加。为防止与常用端口冲突,尽量设大点。 +# 支持http和socks5入站协议 VP_HTTP_PROXY = "%s" # 节点测速的URL diff --git a/proxy_node.go b/proxy_node.go index 17e09fb..c72ac19 100644 --- a/proxy_node.go +++ b/proxy_node.go @@ -46,8 +46,12 @@ func (p *ProxyNode) SetV2ray(n V2rayNode) *ProxyNode { func (p *ProxyNode) AddToPool(c *V2rayApiClient) error { tag := getProxyNodeTag(p.Index) - // 本地入站协议一律使用http - err := c.AddInbound(net.Port(p.LocalPort), tag, "http") + cf := getConf() + protcl := cf.GetHttpProxyProtocol() + if protcl == "socks5" { + protcl = "socks" + } + err := c.AddInbound(net.Port(p.LocalPort), tag, protcl) if err != nil { return err } diff --git a/v2ray_config.go b/v2ray_config.go index f775c4e..a6d75da 100644 --- a/v2ray_config.go +++ b/v2ray_config.go @@ -241,6 +241,9 @@ func setV2rayConfigV4Inbounds(confv4 *V2rayConfigV4, inPort int, cf conf.Conf) { // https://www.v2fly.org/config/protocols/http.html#inboundconfigurationobject inset1 := `{"allowTransparent":false,"timeout":30}` protcl := cf.GetHttpProxyProtocol() + if protcl == "socks5" { + protcl = "socks" + } if protcl == "socks" { inset1 = `{"auth":"noauth","ip":"127.0.0.1","udp":true}` } diff --git a/v2raypool.go b/v2raypool.go index e48726c..5078a16 100644 --- a/v2raypool.go +++ b/v2raypool.go @@ -26,7 +26,6 @@ func GetProxyPool() *ProxyPool { } type ProxyPool struct { - cf conf.Conf serverMap map[int]*V2rayServer startAt time.Time activeCmd *exec.Cmd @@ -48,7 +47,7 @@ func (p ProxyPool) GetLocalPortRange() string { return fmt.Sprintf("%d-%d", p.nodes[0].LocalPort, p.nodes[len(p.nodes)-1].LocalPort) } func (p ProxyPool) GetLocalPortList() (dl []int, err error) { - cf := conf.GetConf() + cf := getConf() for _, v := range p.serverMap { conf := V2rayConfigV4{} err = v.jconf.Decode(&conf) @@ -403,8 +402,11 @@ func (p *ProxyPool) SetLocalAddr(n *ProxyNode, port int) string { } else { n.LocalPort = port } - // 本地入站协议一律使用http - n.localAddr = fmt.Sprintf("http://127.0.0.1:%d", n.LocalPort) + protcl := getConf().GetHttpProxyProtocol() + if protcl == "socks" { + protcl = "socks5" + } + n.localAddr = fmt.Sprintf("%s://127.0.0.1:%d", protcl, n.LocalPort) return n.localAddr } @@ -428,7 +430,7 @@ func (p *ProxyPool) testOneNode(n *ProxyNode, i int) bool { func (p *ProxyPool) TestAllForce() { p.IsLock = true runcount := 0 - logger := p.cf.GetLogger() + logger := getConf().GetLogger() for i, n := range p.nodes { if n.IsRunning() { runcount++ @@ -516,7 +518,7 @@ func (p *ProxyPool) StartAll() error { if !n.IsRunning() { err = n.AddToPool(c) if err != nil { - logger := p.cf.GetLogger() + logger := getConf().GetLogger() logger.Errorf("------StartAll--err--addr(%s)--AddToPool(%v)", n.RemoteAddr, err) break } @@ -693,11 +695,15 @@ func (p *ProxyPool) ActiveNode(n ProxyNode, globalProxy bool) error { return err } +func getConf() conf.Conf { + return conf.GetConf() +} + // proxyPoolInit 初始化代理池 // 在schedule中更新订阅 // https://cloud.tencent.com/developer/article/1564128 func proxyPoolInit() { - cf := conf.GetConf() + cf := getConf() subscribeRawData := cf.GetSubscribeData() if cf.SubscribeUrl == "" { // subscribeRawData == "" @@ -706,7 +712,6 @@ func proxyPoolInit() { port := cf.GetHttpProxyPort() pp := GetProxyPool() - pp.cf = cf pp.SetV2rayPath(cf.V2rayPath). SetTestUrl(cf.TestUrl). SetSubscribeRawData(subscribeRawData).