Skip to content

Commit

Permalink
UPDATE support socks5
Browse files Browse the repository at this point in the history
  • Loading branch information
wuhanqing committed Apr 13, 2024
1 parent 35f3bd3 commit 6c1ce6c
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 48 deletions.
2 changes: 1 addition & 1 deletion main/resource/add_v2ray_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
</div>

<div class="layui-tab-item">
指定节点
开发中
</div>

</div>
Expand Down
9 changes: 8 additions & 1 deletion main/resource/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ <h2 style="padding: 16px;text-align: center;">
V2rayPool控制面板
</h2>

<div class="layui-tab layui-tab-card">
<div class="layui-tab layui-tab-card" lay-filter="v2raytab">
<ul class="layui-tab-title">
<li class="layui-this">动态代理池</li>
<li>v2ray列表</li>
Expand Down Expand Up @@ -111,6 +111,13 @@ <h2 style="padding: 16px;text-align: center;">
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; // 获得被选中的值
Expand Down
2 changes: 1 addition & 1 deletion main/resource/v2raylist.html
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
]],
Expand Down
21 changes: 13 additions & 8 deletions v2ray.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
70 changes: 38 additions & 32 deletions v2ray_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
}
Expand Down Expand Up @@ -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"}`),
Expand All @@ -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版本
Expand Down
13 changes: 8 additions & 5 deletions webserver/v2ray_ctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6c1ce6c

Please sign in to comment.