mirror of
https://github.com/e1732a364fed/v2ray_simple.git
synced 2025-10-07 17:51:14 +08:00
实现tproxy,添加tproxy示例文件.未经测试,很可能有问题.
This commit is contained in:
@@ -96,8 +96,7 @@ func loadCommonComponentsFromStandardConf() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// set conf variable, or exit the program; 还会设置mainFallback
|
// 先检查configFileName是否存在,存在就尝试加载文件到 standardConf 或者 simpleConf,否则尝试 -L参数
|
||||||
// 先检查configFileName是否存在,存在就尝试加载文件,否则尝试 -L参数
|
|
||||||
func loadConfig() (err error) {
|
func loadConfig() (err error) {
|
||||||
|
|
||||||
fpath := utils.GetFilePath(configFileName)
|
fpath := utils.GetFilePath(configFileName)
|
||||||
|
27
examples/vless_tproxy.client.toml
Normal file
27
examples/vless_tproxy.client.toml
Normal 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
|
38
main.go
38
main.go
@@ -81,6 +81,8 @@ var (
|
|||||||
|
|
||||||
startPProf bool
|
startPProf bool
|
||||||
startMProf bool
|
startMProf bool
|
||||||
|
|
||||||
|
tproxyListenCount int
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@@ -186,6 +188,10 @@ func main() {
|
|||||||
|
|
||||||
for _, serverConf := range standardConf.Listen {
|
for _, serverConf := range standardConf.Listen {
|
||||||
thisConf := serverConf
|
thisConf := serverConf
|
||||||
|
if thisConf.Protocol == "tproxy" {
|
||||||
|
listenTproxy(thisConf.GetAddrStrForListenOrDial())
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
if thisConf.Uuid == "" && default_uuid != "" {
|
if thisConf.Uuid == "" && default_uuid != "" {
|
||||||
thisConf.Uuid = default_uuid
|
thisConf.Uuid = default_uuid
|
||||||
@@ -253,7 +259,7 @@ func main() {
|
|||||||
|
|
||||||
configFileQualifiedToRun := false
|
configFileQualifiedToRun := false
|
||||||
|
|
||||||
if (defaultInServer != nil || len(allServers) > 0) && defaultOutClient != nil {
|
if (defaultInServer != nil || len(allServers) > 0 || tproxyListenCount != 0) && (defaultOutClient != nil) {
|
||||||
configFileQualifiedToRun = true
|
configFileQualifiedToRun = true
|
||||||
|
|
||||||
if confMode == simpleMode {
|
if confMode == simpleMode {
|
||||||
@@ -902,7 +908,10 @@ func passToOutClient(iics incomingInserverConnState, isfallback bool, wlc net.Co
|
|||||||
if wlc == nil && udp_wlc == nil {
|
if wlc == nil && udp_wlc == nil {
|
||||||
//无wlc证明 inServer 握手失败,且 没有任何回落可用, 直接退出。
|
//无wlc证明 inServer 握手失败,且 没有任何回落可用, 直接退出。
|
||||||
utils.Debug("invalid request and no matched fallback, hung up")
|
utils.Debug("invalid request and no matched fallback, hung up")
|
||||||
iics.wrappedConn.Close()
|
if iics.wrappedConn != nil {
|
||||||
|
iics.wrappedConn.Close()
|
||||||
|
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -939,11 +948,13 @@ func passToOutClient(iics incomingInserverConnState, isfallback bool, wlc net.Co
|
|||||||
inServer := iics.inServer
|
inServer := iics.inServer
|
||||||
|
|
||||||
//尝试分流, 获取到真正要发向 的 outClient
|
//尝试分流, 获取到真正要发向 的 outClient
|
||||||
if !iics.forbidDNS_orRoute && routePolicy != nil && !inServer.CantRoute() {
|
if !iics.forbidDNS_orRoute && routePolicy != nil && !(inServer != nil && inServer.CantRoute()) {
|
||||||
|
|
||||||
desc := &netLayer.TargetDescription{
|
desc := &netLayer.TargetDescription{
|
||||||
Addr: targetAddr,
|
Addr: targetAddr,
|
||||||
Tag: inServer.GetTag(),
|
}
|
||||||
|
if inServer != nil {
|
||||||
|
desc.Tag = inServer.GetTag()
|
||||||
}
|
}
|
||||||
|
|
||||||
if ce := utils.CanLogDebug("try routing"); ce != nil {
|
if ce := utils.CanLogDebug("try routing"); ce != nil {
|
||||||
@@ -1027,16 +1038,19 @@ func passToOutClient(iics incomingInserverConnState, isfallback bool, wlc net.Co
|
|||||||
|
|
||||||
if targetAddr.IsUDP() {
|
if targetAddr.IsUDP() {
|
||||||
|
|
||||||
switch inServer.Name() {
|
if inServer != nil {
|
||||||
case "socks5":
|
switch inServer.Name() {
|
||||||
// UDP Associate:
|
case "socks5":
|
||||||
// 因为socks5的 UDP Associate 办法是较为特殊的,不使用现有tcp而是新建立udp,所以此时该tcp连接已经没用了
|
// UDP Associate:
|
||||||
// 但是根据socks5标准,这个tcp链接同样是 keep alive的,否则客户端就会认为服务端挂掉了.
|
// 因为socks5的 UDP Associate 办法是较为特殊的,不使用现有tcp而是新建立udp,所以此时该tcp连接已经没用了
|
||||||
// 另外,此时 targetAddr.IsUDP 只是用于告知此链接是udp Associate,并不包含实际地址信息
|
// 但是根据socks5标准,这个tcp链接同样是 keep alive的,否则客户端就会认为服务端挂掉了.
|
||||||
default:
|
// 另外,此时 targetAddr.IsUDP 只是用于告知此链接是udp Associate,并不包含实际地址信息
|
||||||
iics.shouldCloseInSerBaseConnWhenFinish = true
|
default:
|
||||||
|
iics.shouldCloseInSerBaseConnWhenFinish = true
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
//lazy_encrypt情况比较特殊,基础连接何时被关闭会在tlslazy相关代码中处理。
|
//lazy_encrypt情况比较特殊,基础连接何时被关闭会在tlslazy相关代码中处理。
|
||||||
|
@@ -3,6 +3,8 @@ package netLayer
|
|||||||
import (
|
import (
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"net"
|
"net"
|
||||||
|
"syscall"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (addr *Addr) Dial() (net.Conn, error) {
|
func (addr *Addr) Dial() (net.Conn, error) {
|
||||||
@@ -90,3 +92,27 @@ dialedPart:
|
|||||||
return resultConn, err
|
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())
|
||||||
|
|
||||||
|
}
|
||||||
|
@@ -13,7 +13,6 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
@@ -129,19 +128,3 @@ func (c *ReadWrapper) WriteBuffers(buffers [][]byte) (int64, error) {
|
|||||||
return int64(n), e
|
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
22
netLayer/sockopt.go
Normal 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)
|
||||||
|
}
|
@@ -4,6 +4,14 @@ import (
|
|||||||
"syscall"
|
"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 {
|
func SetTproxyFor(tcplistener ListenerWithFile) error {
|
||||||
fileDescriptorSource, err := tcplistener.File()
|
fileDescriptorSource, err := tcplistener.File()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -11,7 +19,7 @@ func SetTproxyFor(tcplistener ListenerWithFile) error {
|
|||||||
}
|
}
|
||||||
defer fileDescriptorSource.Close()
|
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 {
|
func SetSomarkForListener(tcplistener ListenerWithFile, somark int) error {
|
||||||
@@ -21,15 +29,15 @@ func SetSomarkForListener(tcplistener ListenerWithFile, somark int) error {
|
|||||||
}
|
}
|
||||||
defer fileDescriptorSource.Close()
|
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()
|
fileDescriptorSource, err := c.File()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer fileDescriptorSource.Close()
|
defer fileDescriptorSource.Close()
|
||||||
|
|
||||||
return syscall.SetsockoptInt(int(fileDescriptorSource.Fd()), syscall.SOL_SOCKET, syscall.SO_MARK, int(config.Mark))
|
return SetSomark(int(fileDescriptorSource.Fd()), somark)
|
||||||
}
|
}
|
||||||
|
@@ -14,3 +14,11 @@ func SetSomarkForListener(tcplistener ListenerWithFile) error {
|
|||||||
func SetSomarkForConn(c ConnWithFile) error {
|
func SetSomarkForConn(c ConnWithFile) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func SetTproxy(fd int) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetSomark(fd int, somark int) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
@@ -47,23 +47,23 @@ import (
|
|||||||
"github.com/hahahrfool/v2ray_simple/utils"
|
"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)
|
targetTCPAddr := tcpConn.LocalAddr().(*net.TCPAddr)
|
||||||
|
|
||||||
return tcpConn, netLayer.Addr{
|
return netLayer.Addr{
|
||||||
IP: targetTCPAddr.IP,
|
IP: targetTCPAddr.IP,
|
||||||
Port: targetTCPAddr.Port,
|
Port: targetTCPAddr.Port,
|
||||||
}, nil
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var udpMsgConnMap = make(map[netLayer.HashableAddr]*MsgConn)
|
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()
|
bs := utils.GetPacket()
|
||||||
n, src, dst, err := ReadFromUDP(underlay, bs)
|
n, src, dst, err := ReadFromUDP(underlay, bs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, netLayer.Addr{}, err
|
||||||
}
|
}
|
||||||
ad := netLayer.NewAddrFromUDPAddr(src)
|
ad := netLayer.NewAddrFromUDPAddr(src)
|
||||||
hash := ad.GetHashable()
|
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)}
|
conn.readChan <- netLayer.AddrData{Data: bs[:n], Addr: netLayer.NewAddrFromUDPAddr(dst)}
|
||||||
|
|
||||||
return conn, nil
|
return conn, netLayer.NewAddrFromUDPAddr(dst), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
//implements netLayer.MsgConn
|
//implements netLayer.MsgConn
|
||||||
|
78
tproxy_linux.go
Normal file
78
tproxy_linux.go
Normal 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
12
tproxy_other.go
Normal 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")
|
||||||
|
}
|
Reference in New Issue
Block a user