实现tproxy,添加tproxy示例文件.未经测试,很可能有问题.

This commit is contained in:
hahafool
2022-04-20 13:22:10 +08:00
parent 9a1afe18c9
commit b8a27ab713
12 changed files with 218 additions and 42 deletions

View File

@@ -96,8 +96,7 @@ func loadCommonComponentsFromStandardConf() {
}
}
// set conf variable, or exit the program; 还会设置mainFallback
// 先检查configFileName是否存在存在就尝试加载文件否则尝试 -L参数
// 先检查configFileName是否存在存在就尝试加载文件到 standardConf 或者 simpleConf否则尝试 -L参数
func loadConfig() (err error) {
fpath := utils.GetFilePath(configFileName)

View File

@@ -0,0 +1,27 @@
# 本文件使用tproxy作为客户端监听协议. 服务端配置文件 直接使用 vlesss.server.toml 即可
# 相关配置和讲解 可以对照 https://toutyrater.github.io/app/tproxy.html
# 比如如何配置iptables, 要好好看上面教程. 原理都是一样的, 只是配置文件略有区别。
# 我们不像v2ray/xray一样 使用 dokodemo的额外配置来处理tproxy而是单独使用 tproxy作为一个协议.
# tproxy无法在 交互模式/apiServer中配置 或 热加载, 只能用 标准toml配置文件启用.
[[listen]]
protocol = "tproxy" # tproxy只支持客户端, 且只支持linux系统, 一般用于软路由/树莓派等.
host = "127.0.0.1"
port = 12345
# sockopt.tproxy = true # 不需要指明 tproxy这个 sockopt, 因为protocol指出trpxoxy后就会自动配置该项.
# 不用指明network, 只要指明了tproxy, 就会同时自动监听 tcp和 dup.
[[dial]]
protocol = "vlesss"
uuid = "a684455c-b14f-11ea-bf0d-42010aaa0003"
host = "127.0.0.1"
port = 4434
version = 0
insecure = true
utls = true
sockopt.mark = 255

20
main.go
View File

@@ -81,6 +81,8 @@ var (
startPProf bool
startMProf bool
tproxyListenCount int
)
func init() {
@@ -186,6 +188,10 @@ func main() {
for _, serverConf := range standardConf.Listen {
thisConf := serverConf
if thisConf.Protocol == "tproxy" {
listenTproxy(thisConf.GetAddrStrForListenOrDial())
continue
}
if thisConf.Uuid == "" && default_uuid != "" {
thisConf.Uuid = default_uuid
@@ -253,7 +259,7 @@ func main() {
configFileQualifiedToRun := false
if (defaultInServer != nil || len(allServers) > 0) && defaultOutClient != nil {
if (defaultInServer != nil || len(allServers) > 0 || tproxyListenCount != 0) && (defaultOutClient != nil) {
configFileQualifiedToRun = true
if confMode == simpleMode {
@@ -902,7 +908,10 @@ func passToOutClient(iics incomingInserverConnState, isfallback bool, wlc net.Co
if wlc == nil && udp_wlc == nil {
//无wlc证明 inServer 握手失败,且 没有任何回落可用, 直接退出。
utils.Debug("invalid request and no matched fallback, hung up")
if iics.wrappedConn != nil {
iics.wrappedConn.Close()
}
return
}
@@ -939,11 +948,13 @@ func passToOutClient(iics incomingInserverConnState, isfallback bool, wlc net.Co
inServer := iics.inServer
//尝试分流, 获取到真正要发向 的 outClient
if !iics.forbidDNS_orRoute && routePolicy != nil && !inServer.CantRoute() {
if !iics.forbidDNS_orRoute && routePolicy != nil && !(inServer != nil && inServer.CantRoute()) {
desc := &netLayer.TargetDescription{
Addr: targetAddr,
Tag: inServer.GetTag(),
}
if inServer != nil {
desc.Tag = inServer.GetTag()
}
if ce := utils.CanLogDebug("try routing"); ce != nil {
@@ -1027,6 +1038,7 @@ func passToOutClient(iics incomingInserverConnState, isfallback bool, wlc net.Co
if targetAddr.IsUDP() {
if inServer != nil {
switch inServer.Name() {
case "socks5":
// UDP Associate
@@ -1037,6 +1049,8 @@ func passToOutClient(iics incomingInserverConnState, isfallback bool, wlc net.Co
iics.shouldCloseInSerBaseConnWhenFinish = true
}
}
} else {
//lazy_encrypt情况比较特殊基础连接何时被关闭会在tlslazy相关代码中处理。

View File

@@ -3,6 +3,8 @@ package netLayer
import (
"crypto/tls"
"net"
"syscall"
"time"
)
func (addr *Addr) Dial() (net.Conn, error) {
@@ -90,3 +92,27 @@ dialedPart:
return resultConn, err
}
func (addr Addr) DialWithOpt(sockopt *Sockopt) (net.Conn, error) {
dialer := &net.Dialer{
Timeout: time.Second * 16,
}
dialer.Control = func(network, address string, c syscall.RawConn) error {
return c.Control(func(fd uintptr) {
if sockopt != nil {
if sockopt.Somark != 0 {
SetSomark(int(fd), sockopt.Somark)
}
if sockopt.TProxy {
SetTproxy(int(fd))
}
}
})
}
return dialer.Dial(addr.Network, addr.String())
}

View File

@@ -13,7 +13,6 @@ import (
"io"
"log"
"net"
"os"
"reflect"
"syscall"
@@ -129,19 +128,3 @@ func (c *ReadWrapper) WriteBuffers(buffers [][]byte) (int64, error) {
return int64(n), e
}
//用于 listen和 dial 配置一些底层参数.
type Sockopt struct {
TProxy bool `toml:"tproxy"`
Somark int `toml:"mark"`
}
type ListenerWithFile interface {
net.Listener
File() (f *os.File, err error)
}
type ConnWithFile interface {
net.Conn
File() (f *os.File, err error)
}

22
netLayer/sockopt.go Normal file
View File

@@ -0,0 +1,22 @@
package netLayer
import (
"net"
"os"
)
//用于 listen和 dial 配置一些底层参数.
type Sockopt struct {
TProxy bool `toml:"tproxy"`
Somark int `toml:"mark"`
}
type ListenerWithFile interface {
net.Listener
File() (f *os.File, err error)
}
type ConnWithFile interface {
net.Conn
File() (f *os.File, err error)
}

View File

@@ -4,6 +4,14 @@ import (
"syscall"
)
func SetTproxy(fd int) error {
return syscall.SetsockoptInt(fd, syscall.SOL_IP, syscall.IP_TRANSPARENT, 1)
}
func SetSomark(fd int, somark int) error {
return syscall.SetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_MARK, somark)
}
func SetTproxyFor(tcplistener ListenerWithFile) error {
fileDescriptorSource, err := tcplistener.File()
if err != nil {
@@ -11,7 +19,7 @@ func SetTproxyFor(tcplistener ListenerWithFile) error {
}
defer fileDescriptorSource.Close()
return syscall.SetsockoptInt(int(fileDescriptorSource.Fd()), syscall.SOL_IP, syscall.IP_TRANSPARENT, 1)
return SetTproxy(int(fileDescriptorSource.Fd()))
}
func SetSomarkForListener(tcplistener ListenerWithFile, somark int) error {
@@ -21,15 +29,15 @@ func SetSomarkForListener(tcplistener ListenerWithFile, somark int) error {
}
defer fileDescriptorSource.Close()
return syscall.SetsockoptInt(int(fileDescriptorSource.Fd()), syscall.SOL_SOCKET, syscall.SO_MARK, somark)
return SetSomark(int(fileDescriptorSource.Fd()), somark)
}
func SetSomarkForConn(c ConnWithFile) error {
func SetSomarkForConn(c ConnWithFile, somark int) error {
fileDescriptorSource, err := c.File()
if err != nil {
return err
}
defer fileDescriptorSource.Close()
return syscall.SetsockoptInt(int(fileDescriptorSource.Fd()), syscall.SOL_SOCKET, syscall.SO_MARK, int(config.Mark))
return SetSomark(int(fileDescriptorSource.Fd()), somark)
}

View File

@@ -14,3 +14,11 @@ func SetSomarkForListener(tcplistener ListenerWithFile) error {
func SetSomarkForConn(c ConnWithFile) error {
return nil
}
func SetTproxy(fd int) error {
return nil
}
func SetSomark(fd int, somark int) error {
return nil
}

View File

@@ -47,23 +47,23 @@ import (
"github.com/hahahrfool/v2ray_simple/utils"
)
func HandshakeTCP(tcpConn *net.TCPConn) (net.Conn, netLayer.Addr, error) {
func HandshakeTCP(tcpConn *net.TCPConn) netLayer.Addr {
targetTCPAddr := tcpConn.LocalAddr().(*net.TCPAddr)
return tcpConn, netLayer.Addr{
return netLayer.Addr{
IP: targetTCPAddr.IP,
Port: targetTCPAddr.Port,
}, nil
}
}
var udpMsgConnMap = make(map[netLayer.HashableAddr]*MsgConn)
func HandshakeUDP(underlay *net.UDPConn) (netLayer.MsgConn, error) {
func HandshakeUDP(underlay *net.UDPConn) (netLayer.MsgConn, netLayer.Addr, error) {
bs := utils.GetPacket()
n, src, dst, err := ReadFromUDP(underlay, bs)
if err != nil {
return nil, err
return nil, netLayer.Addr{}, err
}
ad := netLayer.NewAddrFromUDPAddr(src)
hash := ad.GetHashable()
@@ -80,7 +80,7 @@ func HandshakeUDP(underlay *net.UDPConn) (netLayer.MsgConn, error) {
conn.readChan <- netLayer.AddrData{Data: bs[:n], Addr: netLayer.NewAddrFromUDPAddr(dst)}
return conn, nil
return conn, netLayer.NewAddrFromUDPAddr(dst), nil
}
//implements netLayer.MsgConn

78
tproxy_linux.go Normal file
View File

@@ -0,0 +1,78 @@
package main
import (
"go.uber.org/zap"
"net"
"github.com/hahahrfool/v2ray_simple/netLayer"
"github.com/hahahrfool/v2ray_simple/netLayer/tproxy"
"github.com/hahahrfool/v2ray_simple/utils"
)
func listenTproxy(addr string) {
utils.Info("Start running Tproxy ")
ad, err := netLayer.NewAddr(addr)
if err != nil {
panic(err)
}
var tp TProxy = TProxy(ad)
go tp.StartLoopTCP()
go tp.StartLoopUDP()
tproxyListenCount++
}
var tproxyList []TProxy
//tproxy因为比较特殊, 不属于 proxy.Server, 需要独特的转发过程去处理.
type TProxy netLayer.Addr
func (tp TProxy) StartLoop() {
tp.StartLoopTCP()
}
func (tp TProxy) StartLoopTCP() {
ad := netLayer.Addr(tp)
netLayer.ListenAndAccept("tcp", ad.String(), &netLayer.Sockopt{TProxy: true}, func(conn net.Conn) {
tcpconn := conn.(*net.TCPConn)
targetAddr := tproxy.HandshakeTCP(tcpconn)
if ce := utils.CanLogErr("TProxy loop read got new tcp"); ce != nil {
ce.Write(zap.String("->", targetAddr.String()))
}
passToOutClient(incomingInserverConnState{
wrappedConn: tcpconn,
}, false, tcpconn, nil, targetAddr)
})
}
func (tp TProxy) StartLoopUDP() {
ad := netLayer.Addr(tp)
ad.Network = "udp"
conn, err := ad.DialWithOpt(&netLayer.Sockopt{TProxy: true})
if err != nil {
if ce := utils.CanLogErr("TProxy StartLoopUDP DialWithOpt failed"); ce != nil {
ce.Write(zap.Error(err))
}
return
}
udpConn := conn.(*net.UDPConn)
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.CanLogErr("TProxy loop read got new udp"); ce != nil {
ce.Write(zap.String("->", raddr.String()))
}
}
go passToOutClient(incomingInserverConnState{}, false, nil, msgConn, raddr)
}
}

12
tproxy_other.go Normal file
View File

@@ -0,0 +1,12 @@
//go:build !linux
// +build !linux
package main
import (
"github.com/hahahrfool/v2ray_simple/utils"
)
func listenTproxy(addr string) {
utils.Warn("Tproxy not possible on non-linux device")
}

1
udp.go
View File

@@ -1 +0,0 @@
package main