修订代码,文档; 修改SelfListen签名;添加手动auto route选项

SelfListen返回一个bool和两个int; windows auto route中sleep4秒
This commit is contained in:
e1732a364fed
2000-01-01 00:00:00 +00:00
parent 237ee90c66
commit 7ed04c07ff
9 changed files with 90 additions and 76 deletions

View File

@@ -55,23 +55,19 @@
[[listen]]
protocol = "tun"
# tun Server使用 host 配置作为 tun device name (macos不设此项)
# tun Server使用 host 配置作为 tun device name
# 使用 ip 配置作为 gateway 的ip , 若不给出,默认为 10.1.0.20
# 使用 extra.tun_selfip 作为 tun向外拨号的ip, 若不给出, 默认为 10.1.0.10
# 使用 extra.tun_mask 作为 子网掩码, 若不给出, 默认为 255.255.255.0
# 使用 extra.tun_selfip 作为 tun向外拨号的ip, 若不给出, 默认为 10.1.0.10 windows上不配置该项
# 如果 extra.tun_auto_route 给出vs_gui会试图自动配置路由表.
# 此时必须额外给出需要 直连的ip列表, 比如你的 代理服务器的ip地址; 如果不给出, 则不会自动配置路由表
# 在windows上还可给出 tun_dns_list 否则windows上,vs默认会使用 114.114.114.114
# 目前的自动配置逻辑 完全仿照上面的路由配置指导。
extra.tun_auto_route = true
extra.tun_auto_route_direct_list = [ "127.0.0.1" ]
extra.tun_dns_list = [ "223.5.5.5" ] #阿里
# extra.tun_auto_route_manual = true # 若这项开启,则生成路由命令但不执行,由你自行拷贝到终端并执行。
[[dial]]
protocol = "vlesss"

16
main.go
View File

@@ -74,7 +74,8 @@ non-blocking. closer used to stop listening. It means listening failed if closer
func ListenSer(inServer proxy.Server, defaultOutClient proxy.Client, env *proxy.RoutingEnv, gi *GlobalInfo) (closer io.Closer) {
var extraCloser io.Closer
var is, tcp, udp bool
var is bool
var tcp, udp int
//tproxy,tun/tap 和 shadowsocks(udp) 都用到了 SelfListen
if is, tcp, udp = inServer.SelfListen(); is {
var chantcp chan netLayer.TCPRequestInfo
@@ -91,7 +92,7 @@ func ListenSer(inServer proxy.Server, defaultOutClient proxy.Client, env *proxy.
)
}
if tcp {
if tcp == 1 {
chantcp = make(chan netLayer.TCPRequestInfo, 2)
go func() {
for tcpInfo := range chantcp {
@@ -107,7 +108,7 @@ func ListenSer(inServer proxy.Server, defaultOutClient proxy.Client, env *proxy.
}()
}
if udp {
if udp == 1 {
chanudp = make(chan netLayer.UDPRequestInfo, 2)
go func() {
@@ -125,7 +126,9 @@ func ListenSer(inServer proxy.Server, defaultOutClient proxy.Client, env *proxy.
}
closer = inServer.(proxy.ListenerServer).StartListen(chantcp, chanudp)
if tcp && udp {
//可以直接return的值: 1,1 1,-1, -1,1 ;
//还需继续监听的值: 1,0 0,1
if tcp+udp != 1 {
return
} else {
@@ -197,8 +200,9 @@ func ListenSer(inServer proxy.Server, defaultOutClient proxy.Client, env *proxy.
network := inServer.Network()
if network == netLayer.DualNetworkName && is {
if is != tcp && udp {
if tcp {
//设置这里实际监听的传输层协议
if is != (tcp == 1 && udp == 1) {
if tcp == 1 {
network = "udp"
} else {
network = "tcp"

View File

@@ -492,6 +492,6 @@ func (d *Base) DialUDP(target netLayer.Addr) (mc *netLayer.UDPMsgConn, err error
return
}
func (d *Base) SelfListen() (is, tcp, udp bool) {
func (d *Base) SelfListen() (is bool, tcp, udp int) {
return
}

View File

@@ -91,7 +91,7 @@ type Server interface {
//get/listen a useable inner mux
GetServerInnerMuxSession(wlc io.ReadWriteCloser) *smux.Session
SelfListen() (is, tcp, udp bool)
SelfListen() (is bool, tcp, udp int) //is表示开启自监听; 此时若 tcp=1, 表示监听tcp, 若tcp=0, 表示自己不监听tcp, 但需要vs进行监听; 若tcp<0, 则表示自己不监听, 也不要vs监听; udp同理
}
type ListenerServer interface {

View File

@@ -98,18 +98,18 @@ func (s *Server) Network() string {
}
}
func (s *Server) SelfListen() (is, _, udp bool) {
func (s *Server) SelfListen() (is bool, _, udp int) {
switch n := s.Network(); n {
case "", netLayer.DualNetworkName:
udp = true
udp = 1
case "tcp":
case "udp":
udp = true
udp = 1
}
is = udp
is = udp > 0
return
}

View File

@@ -69,9 +69,12 @@ func NewServer() (proxy.Server, error) {
}
func (*Server) Name() string { return name }
func (s *Server) SelfListen() (is, tcp, udp bool) {
return true, true, true //darwin的tproxy不支持udp但是如果传入false则v2ray_simple包会试图自行监听udp。
//所以暂时的解决方案是欺骗它告诉他我们都监听
func (s *Server) SelfListen() (is bool, tcp, udp int) {
udp = -1
tcp = 1
is = true
return
}
func (s *Server) Close() error {
@@ -96,7 +99,7 @@ func (s *Server) StartListen(infoChan chan<- netLayer.TCPRequestInfo, udpInfoCha
tm := new(tproxy.Machine)
_, lt, _ := s.SelfListen()
if lt {
if lt > 0 {
s.infoChan = infoChan
lis, err := netLayer.ListenAndAccept("tcp", s.Addr, s.Sockopt, 0, func(conn net.Conn) {

View File

@@ -72,19 +72,21 @@ func NewServer() (proxy.Server, error) {
}
func (*Server) Name() string { return name }
func (s *Server) SelfListen() (is, tcp, udp bool) {
func (s *Server) SelfListen() (is bool, tcp, udp int) {
switch n := s.Network(); n {
case "", netLayer.DualNetworkName:
tcp = true
udp = true
tcp = 1
udp = 1
case "tcp":
tcp = true
tcp = 1
udp = -1
case "udp":
udp = true
udp = 1
tcp = -1
}
is = tcp || udp
is = true
return
}
@@ -115,7 +117,7 @@ func (s *Server) StartListen(infoChan chan<- netLayer.TCPRequestInfo, udpInfoCha
_, lt, lu := s.SelfListen()
if lt {
if lt > 0 {
s.infoChan = infoChan
lis, err := netLayer.ListenAndAccept("tcp", s.Addr, s.Sockopt, 0, func(conn net.Conn) {
@@ -144,7 +146,7 @@ func (s *Server) StartListen(infoChan chan<- netLayer.TCPRequestInfo, udpInfoCha
}
if lu {
if lu > 0 {
s.udpInfoChan = udpInfoChan
ad, err := netLayer.NewAddr(s.Addr)

View File

@@ -14,13 +14,17 @@ import (
var rememberedRouterIP string
func init() {
//经过测试发现,完全一样的路由命令,自动执行和 手动在控制台输入执行,效果竟然不一样; 手动的能正常运行, 自动的就不行, 怪
//似乎是需要等待几秒钟
/*
netsh interface ip set address name="vs_wintun" source=static addr=192.168.123.1 mask=255.255.255.0 gateway=none
经过测试发现,完全一样的路由命令,自动执行和 手动在控制台输入执行,效果竟然不一样; 手动的能正常运行, 自动的就不行, 怪
后发现是需要等待4秒钟3秒都不够
route add vps_ip router_ip
route add 0.0.0.0 mask 0.0.0.0 vps_ip metric 5
要确保wintun的 Gateway显示为 On-link, Interface显示为 设置好的地址;
错误时显示的是 Geteway 是 设置好的地址Interface为原始路由器的地址
netsh interface ip set address name="vs_wintun" source=static addr=192.168.123.1 mask=255.255.255.0 gateway=none
route add vps_ip router_ip
route add 0.0.0.0 mask 0.0.0.0 vps_ip metric 5
而且wintun的自动执行行为 和 go-tun2socks 的 tap的行为还是不一样。
@@ -82,27 +86,30 @@ func init() {
strs = append(strs, fmt.Sprintf("route add 0.0.0.0 mask 0.0.0.0 %s metric 6", tunGateway))
// utils.Warn("Please try run these commands manually(Administrator):")
// for _, s := range strs {
// utils.Warn(s)
// }
if manualRoute {
utils.Warn("Please try run these commands manually(Administrator):")
for _, s := range strs {
utils.Warn(s)
}
// if AddManualRunCmdsListFunc != nil {
// AddManualRunCmdsListFunc(strs)
// }
if AddManualRunCmdsListFunc != nil {
AddManualRunCmdsListFunc(strs)
}
} else {
if e := utils.ExecCmdList(strs[:len(strs)-1]); e != nil {
if ce := utils.CanLogErr("recover auto route failed"); ce != nil {
ce.Write(zap.Error(e))
}
}
if e := utils.ExecCmdList(strs[:len(strs)-1]); e != nil {
if ce := utils.CanLogErr("recover auto route failed"); ce != nil {
ce.Write(zap.Error(e))
time.Sleep(time.Second * 4)
if e := utils.ExecCmd(strs[len(strs)-1]); e != nil {
if ce := utils.CanLogErr("recover auto route failed"); ce != nil {
ce.Write(zap.Error(e))
}
}
}
time.Sleep(time.Second * 2)
if e := utils.ExecCmd(strs[len(strs)-1]); e != nil {
if ce := utils.CanLogErr("recover auto route failed"); ce != nil {
ce.Write(zap.Error(e))
}
}
}
autoRouteDownFunc = func(tunDevName, tunGateway, tunIP string, directList []string) {

View File

@@ -3,7 +3,9 @@
tun Server使用 host 配置作为 tun device name
使用 ip 配置作为 gateway 的ip
使用 extra.tun_selfip 作为 tun向外拨号的ip
使用 extra.tun_mask 作为 子网掩码
mac默认 utun5, windows 默认 vs_wintun
*/
package tun
@@ -21,6 +23,7 @@ import (
)
var AddManualRunCmdsListFunc func([]string)
var manualRoute bool
const name = "tun"
@@ -54,11 +57,6 @@ func (ServerCreator) NewServer(lc *proxy.ListenConf) (proxy.Server, error) {
s.selfip = str
}
}
if thing := lc.Extra["tun_mask"]; thing != nil {
if str, ok := thing.(string); ok {
s.mask = str
}
}
if thing := lc.Extra["tun_auto_route"]; thing != nil {
if auto, autoOk := utils.AnyToBool(thing); autoOk && auto {
@@ -81,14 +79,10 @@ func (ServerCreator) NewServer(lc *proxy.ListenConf) (proxy.Server, error) {
}
if thing := lc.Extra["tun_dns_list"]; thing != nil {
if thing := lc.Extra["tun_auto_route_manual"]; thing != nil {
if list, ok := thing.([]any); ok {
for _, v := range list {
if str, ok := v.(string); ok && str != "" {
s.tun_dnsList = append(s.tun_dnsList, str)
}
}
if manual, ok := utils.AnyToBool(thing); ok && manual {
manualRoute = true
}
}
}
@@ -113,9 +107,6 @@ func (ServerCreator) AfterCommonConfServer(ps proxy.Server) (err error) {
if s.selfip == "" {
s.selfip = defaultSelfIP
}
if s.mask == "" {
s.mask = defaultMask
}
return
}
@@ -129,17 +120,29 @@ type Server struct {
udpRequestChan chan<- netLayer.UDPRequestInfo
lwipCloser io.Closer
devName, realIP, selfip, mask string
autoRoute bool
autoRouteDirectList, tun_dnsList []string
devName, realIP, selfip string
autoRoute bool
autoRouteDirectList []string
}
func (*Server) Name() string { return name }
func (s *Server) SelfListen() (is, tcp, udp bool) {
func (s *Server) SelfListen() (is bool, tcp, udp int) {
switch n := s.Network(); n {
case "", netLayer.DualNetworkName:
tcp = 1
udp = 1
case "tcp":
tcp = 1
udp = -1
case "udp":
udp = 1
tcp = -1
}
is = true
tcp = true
udp = true
return
}
@@ -167,7 +170,7 @@ func (s *Server) Stop() {
func (s *Server) StartListen(tcpRequestChan chan<- netLayer.TCPRequestInfo, udpRequestChan chan<- netLayer.UDPRequestInfo) io.Closer {
s.stopped = false
//log.Println(s.devName, s.selfip, s.realIP, s.mask)
if s.devName == "" {
switch runtime.GOOS {
case "darwin":
@@ -202,7 +205,6 @@ func (s *Server) StartListen(tcpRequestChan chan<- netLayer.TCPRequestInfo, udpR
return nil
}
//newTchan, newUchan, lwipcloser := tun.Listen(tunDev)
go func() {
for tr := range newTchan {
if s.stopped {