From e2870eeea5083c2df28cd616b56596bf170666eb Mon Sep 17 00:00:00 2001 From: mzz2017 Date: Sun, 5 Jan 2020 02:21:58 +0800 Subject: [PATCH] fix: transparent mode supports tcp dns query now --- service/model/iptables/iptables.go | 123 +++++++++++++++-------------- service/model/v2ray/v2rayTmpl.go | 43 +++++++--- 2 files changed, 96 insertions(+), 70 deletions(-) diff --git a/service/model/iptables/iptables.go b/service/model/iptables/iptables.go index 5bc654e6..f628c5b4 100644 --- a/service/model/iptables/iptables.go +++ b/service/model/iptables/iptables.go @@ -3,6 +3,7 @@ package iptables import ( "V2RayA/global" "V2RayA/persistence/configure" + "V2RayA/tools" "errors" "io/ioutil" "os" @@ -97,19 +98,19 @@ iptables -t mangle -F LOG iptables -t mangle -F SETMARK iptables -t mangle -X SETMARK ` -// if tools.IsCommandValid("ip6tables") { -// commands += ` -//ip6tables -t mangle -F SSTP_OUT -//ip6tables -t mangle -D OUTPUT -j SSTP_OUT -//ip6tables -t mangle -X SSTP_OUT -//ip6tables -t mangle -F SSTP_PRE -//ip6tables -t mangle -D PREROUTING -j SSTP_PRE -//ip6tables -t mangle -X SSTP_PRE -//ip6tables -t mangle -F LOG -//ip6tables -t mangle -F SETMARK -//ip6tables -t mangle -X SETMARK -//` -// } + if tools.IsCommandValid("ip6tables") { + commands += ` +ip6tables -t mangle -F SSTP_OUT +ip6tables -t mangle -D OUTPUT -j SSTP_OUT +ip6tables -t mangle -X SSTP_OUT +ip6tables -t mangle -F SSTP_PRE +ip6tables -t mangle -D PREROUTING -j SSTP_PRE +ip6tables -t mangle -X SSTP_PRE +ip6tables -t mangle -F LOG +ip6tables -t mangle -F SETMARK +ip6tables -t mangle -X SETMARK +` + } if global.ServiceControlMode == global.DockerMode { commands = strings.ReplaceAll(commands, "iptables", "iptables-legacy") commands = strings.ReplaceAll(commands, "ip6tables", "ip6tables-legacy") @@ -134,6 +135,7 @@ iptables -t mangle -N SETMARK iptables -t mangle -A SETMARK -i docker+ -j RETURN #iptables -t mangle -A SETMARK -p udp --dport 53 -j MARK --set-mark 1 iptables -t mangle -A SETMARK -p udp --dport 53 -j RETURN +#iptables -t mangle -A SETMARK -p tcp --dport 53 -j MARK --set-mark 1 iptables -t mangle -A SETMARK -p tcp --dport 53 -j RETURN iptables -t mangle -A SETMARK -d 0.0.0.0/32 -j RETURN iptables -t mangle -A SETMARK -d 10.0.0.0/8 -j RETURN @@ -172,52 +174,55 @@ iptables -t mangle -A SSTP_PRE -p udp -m mark ! --mark 1 -j SETMARK iptables -t mangle -A SSTP_PRE -m mark --mark 1 -p tcp -j TPROXY --on-port 12345 --tproxy-mark 1 iptables -t mangle -A SSTP_PRE -m mark --mark 1 -p udp -j TPROXY --on-port 12345 --tproxy-mark 1 ` -// if tools.IsCommandValid("ip6tables") { -// commands += ` -//# 建链 -//ip6tables -t mangle -N SSTP_OUT -//ip6tables -t mangle -N SSTP_PRE -//ip6tables -t mangle -A OUTPUT -j SSTP_OUT -//ip6tables -t mangle -A PREROUTING -j SSTP_PRE -// -//# 打上 iptables 标记,mark 了的会走代理 -//ip6tables -t mangle -N SETMARK -//ip6tables -t mangle -A SETMARK -i docker+ -j RETURN -//#ip6tables -t mangle -A SETMARK -p udp --dport 53 -j MARK --set-mark 1 -//ip6tables -t mangle -A SETMARK -d ::/128 -j RETURN -//ip6tables -t mangle -A SETMARK -d ::1/128 -j RETURN -//ip6tables -t mangle -A SETMARK -d ::ffff:0:0/96 -j RETURN -//ip6tables -t mangle -A SETMARK -d ::ffff:0:0:0/96 -j RETURN -//ip6tables -t mangle -A SETMARK -d 64:ff9b::/96 -j RETURN -//ip6tables -t mangle -A SETMARK -d 100::/64 -j RETURN -//ip6tables -t mangle -A SETMARK -d 2001::/32 -j RETURN -//ip6tables -t mangle -A SETMARK -d 2001:20::/28 -j RETURN -//ip6tables -t mangle -A SETMARK -d 2001:db8::/32 -j RETURN -//ip6tables -t mangle -A SETMARK -d 2002::/16 -j RETURN -//ip6tables -t mangle -A SETMARK -d fc00::/7 -j RETURN -//ip6tables -t mangle -A SETMARK -d fe80::/10 -j RETURN -//ip6tables -t mangle -A SETMARK -p udp -j MARK --set-mark 1 -//ip6tables -t mangle -A SETMARK -p tcp -j MARK --set-mark 1 -// -//# 走过TPROXY的通行 -//ip6tables -t mangle -A SSTP_OUT -m mark --mark 0xff -j RETURN -//# 本机出方向规则,白名单端口 -//ip6tables -t mangle -A SSTP_OUT -p tcp -m multiport --sports {{TCP_PORTS}} -j RETURN -//ip6tables -t mangle -A SSTP_OUT -p udp -m multiport --sports {{UDP_PORTS}} -j RETURN -//# 本机发出去的 TCP 和 UDP 走一下 SETMARK 链 -//ip6tables -t mangle -A SSTP_OUT -p tcp -m mark ! --mark 1 -j SETMARK -//ip6tables -t mangle -A SSTP_OUT -p udp -m mark ! --mark 1 -j SETMARK -// -//# 走过TPROXY的通行 -//ip6tables -t mangle -A SSTP_PRE -m mark --mark 0xff -j RETURN -//# 让内网主机发出的 TCP 和 UDP 走一下 SETMARK 链 -//ip6tables -t mangle -A SSTP_PRE -p tcp -m mark ! --mark 1 -j SETMARK -//ip6tables -t mangle -A SSTP_PRE -p udp -m mark ! --mark 1 -j SETMARK -//# 将所有打了标记的 TCP 和 UDP 包透明地转发到代理的监听端口 -//ip6tables -t mangle -A SSTP_PRE -m mark --mark 1 -p tcp -j TPROXY --on-port 12345 --tproxy-mark 1 -//ip6tables -t mangle -A SSTP_PRE -m mark --mark 1 -p udp -j TPROXY --on-port 12345 --tproxy-mark 1 -//` -// } + if tools.IsCommandValid("ip6tables") { + commands += ` +# 建链 +ip6tables -t mangle -N SSTP_OUT +ip6tables -t mangle -N SSTP_PRE +ip6tables -t mangle -A OUTPUT -j SSTP_OUT +ip6tables -t mangle -A PREROUTING -j SSTP_PRE + +# 打上 iptables 标记,mark 了的会走代理 +ip6tables -t mangle -N SETMARK +ip6tables -t mangle -A SETMARK -i docker+ -j RETURN +#ip6tables -t mangle -A SETMARK -p udp --dport 53 -j MARK --set-mark 1 +ip6tables -t mangle -A SETMARK -p udp --dport 53 -j RETURN +#ip6tables -t mangle -A SETMARK -p tcp --dport 53 -j MARK --set-mark 1 +ip6tables -t mangle -A SETMARK -p tcp --dport 53 -j RETURN +ip6tables -t mangle -A SETMARK -d ::/128 -j RETURN +ip6tables -t mangle -A SETMARK -d ::1/128 -j RETURN +ip6tables -t mangle -A SETMARK -d ::ffff:0:0/96 -j RETURN +ip6tables -t mangle -A SETMARK -d ::ffff:0:0:0/96 -j RETURN +ip6tables -t mangle -A SETMARK -d 64:ff9b::/96 -j RETURN +ip6tables -t mangle -A SETMARK -d 100::/64 -j RETURN +ip6tables -t mangle -A SETMARK -d 2001::/32 -j RETURN +ip6tables -t mangle -A SETMARK -d 2001:20::/28 -j RETURN +ip6tables -t mangle -A SETMARK -d 2001:db8::/32 -j RETURN +ip6tables -t mangle -A SETMARK -d 2002::/16 -j RETURN +ip6tables -t mangle -A SETMARK -d fc00::/7 -j RETURN +ip6tables -t mangle -A SETMARK -d fe80::/10 -j RETURN +ip6tables -t mangle -A SETMARK -p udp -j MARK --set-mark 1 +ip6tables -t mangle -A SETMARK -p tcp -j MARK --set-mark 1 + +# 走过TPROXY的通行 +ip6tables -t mangle -A SSTP_OUT -m mark --mark 0xff -j RETURN +# 本机出方向规则,白名单端口 +ip6tables -t mangle -A SSTP_OUT -p tcp -m multiport --sports {{TCP_PORTS}} -j RETURN +ip6tables -t mangle -A SSTP_OUT -p udp -m multiport --sports {{UDP_PORTS}} -j RETURN +# 本机发出去的 TCP 和 UDP 走一下 SETMARK 链 +ip6tables -t mangle -A SSTP_OUT -p tcp -m mark ! --mark 1 -j SETMARK +ip6tables -t mangle -A SSTP_OUT -p udp -m mark ! --mark 1 -j SETMARK + +# 走过TPROXY的通行 +ip6tables -t mangle -A SSTP_PRE -m mark --mark 0xff -j RETURN +# 让内网主机发出的 TCP 和 UDP 走一下 SETMARK 链 +ip6tables -t mangle -A SSTP_PRE -p tcp -m mark ! --mark 1 -j SETMARK +ip6tables -t mangle -A SSTP_PRE -p udp -m mark ! --mark 1 -j SETMARK +# 将所有打了标记的 TCP 和 UDP 包透明地转发到代理的监听端口 +ip6tables -t mangle -A SSTP_PRE -m mark --mark 1 -p tcp -j TPROXY --on-port 12345 --tproxy-mark 1 +ip6tables -t mangle -A SSTP_PRE -m mark --mark 1 -p udp -j TPROXY --on-port 12345 --tproxy-mark 1 +` + } //参考http://briteming.hatenablog.com/entry/2019/06/18/175518 //先看要不要把自己的端口加进去 selfPort := strings.Split(global.GetEnvironmentConfig().Address, ":")[1] diff --git a/service/model/v2ray/v2rayTmpl.go b/service/model/v2ray/v2rayTmpl.go index 4a5d3422..0ccdf0b8 100644 --- a/service/model/v2ray/v2rayTmpl.go +++ b/service/model/v2ray/v2rayTmpl.go @@ -84,16 +84,20 @@ type Vnext struct { Users []User `json:"users"` } type Server struct { - Address string `json:"address"` + Network string `json:"network,omitempty"` + Address string `json:"address,omitempty"` Method string `json:"method,omitempty"` Ota bool `json:"ota,omitempty"` Password string `json:"password,omitempty"` - Port int `json:"port"` + Port int `json:"port,omitempty"` } type Settings struct { Vnext interface{} `json:"vnext,omitempty"` Servers interface{} `json:"servers,omitempty"` DomainStrategy string `json:"domainStrategy,omitempty"` + Port int `json:"port,omitempty"` + Address string `json:"address,omitempty"` + Network string `json:"network,omitempty"` } type TLSSettings struct { AllowInsecure bool `json:"allowInsecure"` @@ -132,7 +136,6 @@ type Outbound struct { Settings *Settings `json:"settings,omitempty"` StreamSettings *StreamSettings `json:"streamSettings,omitempty"` Mux *Mux `json:"mux,omitempty"` - Network string `json:"network,omitempty"` } type TCPSettings struct { ConnectionReuse bool `json:"connectionReuse"` @@ -265,6 +268,13 @@ func ResolveOutbound(v *vmessInfo.VmessInfo, tag string, ssrLocalPortIfNeed int) o.StreamSettings.TLSSettings = &tmplJson.TLSSettings } case "shadowsocks", "shadowsocksr": + //// 尝试将address解析成ip + //if net.ParseIP(v.Add) == nil { + // addrs, e := net.LookupHost(v.Add) + // if e == nil && len(addrs) > 0 { + // v.Add = addrs[0] + // } + //} v.Net = strings.ToLower(v.Net) switch v.Net { case "aes-128-cfb", "aes-192-cfb", "aes-256-cfb", "aes-128-ctr", "aes-192-ctr", "aes-256-ctr", "aes-128-ofb", "aes-192-ofb", "aes-256-ofb", "des-cfb", "bf-cfb", "cast5-cfb", "rc4-md5", "chacha20", "chacha20-ietf", "salsa20", "camellia-128-cfb", "camellia-192-cfb", "camellia-256-cfb", "idea-cfb", "rc2-cfb", "seed-cfb": @@ -309,8 +319,12 @@ func NewTemplateFromVmessInfo(v vmessInfo.VmessInfo) (t Template, err error) { if err != nil { return t, errors.New("读入模板json出错,请检查templateJson变量是否是正确的json格式") } - // 其中Template是基础配置,替换掉*t即可 + // 其中Template是基础配置,替换掉t即可 t = tmplJson.Template + // 调试模式 + if global.Version == "debug" { + t.Log.Loglevel = "debug" + } o, err := ResolveOutbound(&v, "proxy", global.GetEnvironmentConfig().SSRListenPort) if err != nil { return t, err @@ -367,10 +381,17 @@ func NewTemplateFromVmessInfo(v vmessInfo.VmessInfo) (t Template, err error) { } } t.DNS.Servers = []interface{}{ + "localhost", //如将iptables中的53端口RETURN改为mark,将本行localhost去除 + DnsServer{ + Address: "208.67.222.222", + Port: 5353, + }, + DnsServer{ + Address: "8.8.8.8", + Port: 5353, + }, + "114.114.114.114", ds, - "8.8.8.8", // 非中中国大陆域名使用 Google 的 DNS - "1.1.1.1", - "127.0.0.1", } //再修改inbounds tproxy := "tproxy" @@ -382,7 +403,7 @@ func NewTemplateFromVmessInfo(v vmessInfo.VmessInfo) (t Template, err error) { Enabled: true, DestOverride: []string{"http", "tls"}, }, - Settings: &InboundSettings{FollowRedirect: true}, + Settings: &InboundSettings{Network: "tcp,udp", FollowRedirect: true}, StreamSettings: StreamSettings{Sockopt: &Sockopt{Tproxy: &tproxy}}, Tag: "transparent", }) @@ -391,6 +412,7 @@ func NewTemplateFromVmessInfo(v vmessInfo.VmessInfo) (t Template, err error) { t.Outbounds = append(t.Outbounds, Outbound{ Tag: "dns-out", Protocol: "dns", + Settings: &Settings{Network: "tcp"}, }) for i := range t.Outbounds { if t.Outbounds[i].Protocol == "blackhole" { @@ -409,12 +431,11 @@ func NewTemplateFromVmessInfo(v vmessInfo.VmessInfo) (t Template, err error) { } //最后是routing t.Routing.Rules = append(t.Routing.Rules, - RoutingRule{ // 劫持 53 端口 UDP 流量,使用 V2Ray 的 DNS + RoutingRule{ // 劫持 53 端口流量,使用 V2Ray 的 DNS Type: "field", InboundTag: []string{"transparent"}, Port: "53", OutboundTag: "dns-out", - Network: "udp", }, RoutingRule{ // 直连 123 端口 UDP 流量(NTP 协议) Type: "field", @@ -431,7 +452,7 @@ func NewTemplateFromVmessInfo(v vmessInfo.VmessInfo) (t Template, err error) { RoutingRule{ // 设置 DNS 配置中的国外 DNS 服务器地址走代理,以达到 DNS 分流目的 Type: "field", OutboundTag: "proxy", - IP: []string{"8.8.8.8", "1.1.1.1"}, + IP: []string{"8.8.8.8", "1.1.1.1", "208.67.222.222"}, }, RoutingRule{ // BT 流量直连 Type: "field",