From 6c1ce6cf616ae06af259fc71782e6acc2558d901 Mon Sep 17 00:00:00 2001 From: wuhanqing Date: Sun, 14 Apr 2024 01:49:49 +0800 Subject: [PATCH] UPDATE support socks5 --- main/resource/add_v2ray_form.html | 2 +- main/resource/index.html | 9 +++- main/resource/v2raylist.html | 2 +- v2ray.go | 21 ++++++---- v2ray_config.go | 70 +++++++++++++++++-------------- webserver/v2ray_ctl.go | 13 +++--- 6 files changed, 69 insertions(+), 48 deletions(-) diff --git a/main/resource/add_v2ray_form.html b/main/resource/add_v2ray_form.html index 66ea987..1c68780 100644 --- a/main/resource/add_v2ray_form.html +++ b/main/resource/add_v2ray_form.html @@ -21,7 +21,7 @@
- 指定节点 + 开发中
diff --git a/main/resource/index.html b/main/resource/index.html index 36e978e..51c2b03 100644 --- a/main/resource/index.html +++ b/main/resource/index.html @@ -37,7 +37,7 @@

V2rayPool控制面板

-
+
  • 动态代理池
  • v2ray列表
  • @@ -111,6 +111,13 @@

    var util = layui.util; var form = layui.form; var $ = layui.$; + var element = layui.element; + // tab 切换事件 + element.on('tab(v2raytab)', function(data){ + if (data.index == 1){ + table.reload('v2raylist') + } + }); form.on('select(test_domain)', function (data) { // var value = data.value; // 获得被选中的值 diff --git a/main/resource/v2raylist.html b/main/resource/v2raylist.html index e4fe9cb..ead3f7f 100644 --- a/main/resource/v2raylist.html +++ b/main/resource/v2raylist.html @@ -71,7 +71,7 @@ { type: "numbers", fixed: 'left' }, // field: "layid", title: "序号", { field: 'pid', title: 'PID', width: 100 }, { field: 'run_mode', title: '类型', width: 100 }, - { field: 'local_ports', title: '本地端口', width: 160 }, + { field: 'local_ports', title: '本地端口', width: 260 }, { field: 'config_file', title: '配置文件', width: 280 }, { fixed: 'right', title: '操作', width: 180, minWidth: 80, toolbar: '#barV2ray' } ]], diff --git a/v2ray.go b/v2ray.go index a99dce2..4440ffa 100644 --- a/v2ray.go +++ b/v2ray.go @@ -44,14 +44,6 @@ func (v V2rayServer) GetV2rayConfigV4() V2rayConfigV4 { v.jconf.Decode(&vconf) return vconf } -func (v V2rayServer) GetLocalPortList() []int { - var result []int - vconf := v.GetV2rayConfigV4() - for _, in := range vconf.Inbounds { - result = append(result, in.Port) - } - return result -} func (v V2rayServer) GetExeCmd() *exec.Cmd { return v.cmd } @@ -62,6 +54,19 @@ func (v *V2rayServer) setExeCmd(configFile string) { v.jconf = getV2rayConfigV4(v.selectNode, v.localPort) if v.localPort > 0 { configFile = V2RAY_CONFIG_FILE + // 再额外添加一个socks端口或http端口 + vconf := v.GetV2rayConfigV4() + inbd1 := vconf.Inbounds[0] + proto2 := "socks" + if inbd1.Protocol == "socks" { + proto2 = "http" + } + inbd2 := newV2rayInboundV4(proto2, v.localPort-1) + vconf.Inbounds = []V2rayInbound{inbd1, inbd2} + err := v.jconf.SetContent(vconf) + if err != nil { + panic(err) + } } else { configFile = V2RAYPOOL_CONFIG_FILE } diff --git a/v2ray_config.go b/v2ray_config.go index b1e85c8..b6967d5 100644 --- a/v2ray_config.go +++ b/v2ray_config.go @@ -92,9 +92,9 @@ func getRoutingRules(cf conf.Conf, inPort int) string { if inPort == 0 { // 启用 v2ray 内置的基于 gRPC 协议的 API rules = append(rules, V2rayRouteRule{Type: "field", InboundTag: []string{TAG_INBOUND_API}, OutboundTag: TAG_OUTBOUND_API}) - // IP代理池模式时,启用每个出站和入站一对一映射规则。池子上线为100个 - for i := 0; i < 100; i++ { - tag := getProxyNodeTag(i) + // IP代理池模式时,启用每个出站和入站一对一映射规则。 + for _, nd := range GetProxyPool().GetNodes("") { + tag := getProxyNodeTag(nd.Index) // 添加路由规则,相同标签的每一个出站和入站一一对应 rules = append(rules, V2rayRouteRule{Type: "field", InboundTag: []string{tag}, OutboundTag: tag}) } @@ -221,15 +221,46 @@ func setV2rayConfigV4Outbounds(confv4 *V2rayConfigV4, n V2rayNode) error { return nil } +func newV2rayInboundV4(proto string, inPort int) V2rayInbound { + inAddr := "0.0.0.0" // "127.0.0.1" 仅允许本地访问 + if proto == "socks5" { + proto = "socks" + } + intag := "http_IN" + inset1 := `{"allowTransparent":false,"timeout":30}` + if proto == "socks" { + // inset1 = `{"auth":"noauth","ip":"127.0.0.1","udp":true}` + intag = "socks_IN" + inset1 = `{"auth":"noauth","udp":true}` + } + return V2rayInbound{ + Protocol: proto, + Port: inPort, + Listen: inAddr, + Settings: json.RawMessage(inset1), + Tag: intag, + } + // ip: address: SOCKS5 通过 UDP ASSOCIATE 命令建立 UDP 会话。服务端在对客户端发来的该命令的回复中,指定客户端发包的目标地址。 + // v4.34.0+: 默认值为空,此时对于通过本地回环 IPv4/IPv6 连接的客户端,回复对应的回环 IPv4/IPv6 地址;对于非本机的客户端,回复当前入站的监听地址。 + // v4.33.0 及更早版本: 默认值 127.0.0.1。 + // inSet := fmt.Sprintf(`{"auth":"noauth","udp":true,"ip":"%s"}`, inAddr) + // inbd2 := V2rayInbound{ + // Protocol: "socks", + // Port: inPort, // 端口号相同会冲突 + // Listen: inAddr, + // Settings: json.RawMessage(inSet), + // Tag: "socks_IN", + // } +} + // setV2rayConfigV4Inbounds 入站配置 func setV2rayConfigV4Inbounds(confv4 *V2rayConfigV4, inPort int, cf conf.Conf) { - inAddr := "0.0.0.0" // "127.0.0.1" 仅允许本地访问 logger := cf.GetLogger() if inPort == 0 { - // inPort == 0时,启用gRPC,允许多个代理节点组成代理池。 + // inPort == 0 时,启用gRPC,允许多个代理节点组成代理池。 inbdapi := V2rayInbound{ - Listen: "127.0.0.1", + Listen: "0.0.0.0", // "127.0.0.1" 仅允许本地访问 Port: cf.V2rayApiPort, Protocol: "dokodemo-door", Settings: json.RawMessage(`{"address": "127.0.0.1"}`), @@ -239,37 +270,12 @@ func setV2rayConfigV4Inbounds(confv4 *V2rayConfigV4, inPort int, cf conf.Conf) { logger.Debugf("-----setV2rayConfigV4Inbounds--inPort(%d)--inbdapi--V2rayApiPort(%d)", inPort, inbdapi.Port) } else { // 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}` - inset1 = `{"auth":"noauth","udp":true}` - } - inbd1 := V2rayInbound{ - Protocol: protcl, - Port: inPort, - Listen: inAddr, - Settings: json.RawMessage(inset1), - Tag: "http_IN", - } + inbd1 := newV2rayInboundV4(protcl, inPort) logger.Debugf("-----setV2rayConfigV4Inbounds--inPort(%d)--inbd1--", inPort) confv4.Inbounds = []V2rayInbound{inbd1} } - // ip: address: SOCKS5 通过 UDP ASSOCIATE 命令建立 UDP 会话。服务端在对客户端发来的该命令的回复中,指定客户端发包的目标地址。 - // v4.34.0+: 默认值为空,此时对于通过本地回环 IPv4/IPv6 连接的客户端,回复对应的回环 IPv4/IPv6 地址;对于非本机的客户端,回复当前入站的监听地址。 - // v4.33.0 及更早版本: 默认值 127.0.0.1。 - // inSet := fmt.Sprintf(`{"auth":"noauth","udp":true,"ip":"%s"}`, inAddr) - // inbd2 := V2rayInbound{ - // Protocol: "socks", - // Port: inPort, // 端口号相同会冲突 - // Listen: inAddr, - // Settings: json.RawMessage(inSet), - // Tag: "socks_IN", - // } } // getV2rayConfigV4 v2ray官方配置v4版本 diff --git a/webserver/v2ray_ctl.go b/webserver/v2ray_ctl.go index e8b4886..d74711e 100644 --- a/webserver/v2ray_ctl.go +++ b/webserver/v2ray_ctl.go @@ -117,25 +117,28 @@ func GetV2rayList() []byte { confile := jconf.GetFilepath() localPorts := "" runmode := "个性配置" - vports := vs.GetLocalPortList() vapiport := cf.V2rayApiPort sysport := cf.GetHttpProxyPort() - for i, port := range vports { + + for i, inbd := range vs.GetV2rayConfigV4().Inbounds { + port := inbd.Port + proto := inbd.Protocol if port == vapiport { + proto = "grpc" runmode = "动态代理池" } + addStr := fmt.Sprintf("%s:%d", proto, port) if port == sysport { runmode = "系统代理" } - addStr := fmt.Sprintf("%d", port) if i > 0 { - addStr = "," + addStr + addStr = ", " + addStr } localPorts += addStr } if runmode == "动态代理池" { - localPorts += "," + pp.GetLocalPortRange() + localPorts += fmt.Sprintf(", %s:%s", cf.GetHttpProxyProtocol(), pp.GetLocalPortRange()) } if runmode == "系统代理" { confile = vp.ROUTING_RULES_FILE + " -> " + confile