Skip to content

Commit

Permalink
UPDATE Inbounds. Support http, socks, socks5
Browse files Browse the repository at this point in the history
  • Loading branch information
iotames committed Apr 11, 2024
1 parent 536d1ca commit 05cd908
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 12 deletions.
8 changes: 6 additions & 2 deletions conf/conf_val.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,21 @@ func (cf Conf) GetSubscribeData() string {
}
return strings.TrimSpace(string(b))
}

func (cf Conf) HttpProxySplit() []string {
spt := strings.Split(cf.HttpProxy, ":")
lensp := len(spt)
if lensp != 3 {
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]
Expand All @@ -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]
}

Expand Down
1 change: 1 addition & 0 deletions main/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ VP_SUBSCRIBE_URL = "%s"
VP_SUBSCRIBE_DATA_FILE = "%s"
# 设置HTTP代理,代理池每个节点的本地端口号,往后开始累加。为防止与常用端口冲突,尽量设大点。
# 支持http和socks5入站协议
VP_HTTP_PROXY = "%s"
# 节点测速的URL
Expand Down
8 changes: 6 additions & 2 deletions proxy_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 3 additions & 0 deletions v2ray_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
}
Expand Down
21 changes: 13 additions & 8 deletions v2raypool.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ func GetProxyPool() *ProxyPool {
}

type ProxyPool struct {
cf conf.Conf
serverMap map[int]*V2rayServer
startAt time.Time
activeCmd *exec.Cmd
Expand All @@ -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)
Expand Down Expand Up @@ -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
}

Expand All @@ -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++
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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 == ""
Expand All @@ -706,7 +712,6 @@ func proxyPoolInit() {
port := cf.GetHttpProxyPort()

pp := GetProxyPool()
pp.cf = cf
pp.SetV2rayPath(cf.V2rayPath).
SetTestUrl(cf.TestUrl).
SetSubscribeRawData(subscribeRawData).
Expand Down

0 comments on commit 05cd908

Please sign in to comment.