Files
v2ray_simple/tproxy_linux.go
e1732a364fed f28f0d0bee 修订代码, 默认loglevel 改为 Log_info.
对一般用户而言,还是需要使用Info等级 来了解一下 一般的 日志情况,等到使用熟练之后,且确认运行没有错误后, 可以自行调为 warning 来提升性能

发现 bubble包 还自己引入了 命令行参数,这十分不可取,所以我们还是直接使用其代码。

将其它包中 的 命令行参数 统一 移动 到 cmd/verysimple 中;tls lazy 特性因为还在 调试阶段,所以 命令行参数 仍然放到 v2ray_simple 包中。
2022-04-26 13:22:18 +08:00

88 lines
2.1 KiB
Go

package v2ray_simple
import (
"go.uber.org/zap"
"net"
"github.com/e1732a364fed/v2ray_simple/netLayer"
"github.com/e1732a364fed/v2ray_simple/netLayer/tproxy"
"github.com/e1732a364fed/v2ray_simple/utils"
)
//监听透明代理, 返回一个 值 用于 关闭.
func ListenTproxy(addr string) (tm *tproxy.Machine) {
utils.Info("Start running Tproxy")
ad, err := netLayer.NewAddr(addr)
if err != nil {
panic(err)
}
//因为 tproxy比较特殊, 不属于 proxy.Server, 所以 需要 独立的 转发过程去处理.
lis, err := startLoopTCP(ad)
if err != nil {
if ce := utils.CanLogErr("TProxy startLoopTCP failed"); ce != nil {
ce.Write(zap.Error(err))
}
return
}
udpConn := startLoopUDP(ad)
tm = &tproxy.Machine{Addr: ad, Listener: lis, UDPConn: udpConn})
return
}
//非阻塞
func startLoopTCP(ad netLayer.Addr) (net.Listener, error) {
return netLayer.ListenAndAccept("tcp", ad.String(), &netLayer.Sockopt{TProxy: true}, func(conn net.Conn) {
tcpconn := conn.(*net.TCPConn)
targetAddr := tproxy.HandshakeTCP(tcpconn)
if ce := utils.CanLogInfo("TProxy loop read got new tcp"); ce != nil {
ce.Write(zap.String("->", targetAddr.String()))
}
passToOutClient(incomingInserverConnState{
wrappedConn: tcpconn,
defaultClient: DefaultOutClient,
}, false, tcpconn, nil, targetAddr)
})
}
//非阻塞
func startLoopUDP(ad netLayer.Addr) *net.UDPConn {
ad.Network = "udp"
conn, err := ad.ListenUDP_withOpt(&netLayer.Sockopt{TProxy: true})
if err != nil {
if ce := utils.CanLogErr("TProxy startLoopUDP DialWithOpt failed"); ce != nil {
ce.Write(zap.Error(err))
}
return nil
}
udpConn := conn.(*net.UDPConn)
go func() {
for {
msgConn, raddr, err := tproxy.HandshakeUDP(udpConn)
if err != nil {
if ce := utils.CanLogErr("TProxy startLoopUDP loop read failed"); ce != nil {
ce.Write(zap.Error(err))
}
break
} else {
if ce := utils.CanLogInfo("TProxy loop read got new udp"); ce != nil {
ce.Write(zap.String("->", raddr.String()))
}
}
go passToOutClient(incomingInserverConnState{
defaultClient: DefaultOutClient,
}, false, nil, msgConn, raddr)
}
}()
return udpConn
}