Skip to content
This repository has been archived by the owner on Mar 17, 2024. It is now read-only.

Commit

Permalink
整理代码,文档,示例
Browse files Browse the repository at this point in the history
  • Loading branch information
e1732a364fed committed Dec 4, 2022
1 parent b517f9d commit c92219d
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 24 deletions.
9 changes: 9 additions & 0 deletions examples/multi.server.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,12 @@ protocol = "reject"
[[route]]
toTag = "my_special_tag_for_this_guy"
user = ["a684455c-b14f-11ea-bf0d-42010aaa0004"] #通过 listen 所得到 的 user的不同 来分流


[[dial]]
tag = "my_special_direct"
protocol = "direct"
network = "dual"
sendThrough = "tcp:127.0.0.1:80\nudp:127.0.0.1:12345"
# 这里展示了一种特殊用法,direct设置sendThrough, 就能限制它发送数据的端口, 而且tcp和udp可以设为不同的地址
# 注意这个写法 "tcp:ip:port\nudp:ip:port"
5 changes: 2 additions & 3 deletions examples/vless_tproxy.client.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@

# 我们不像v2ray/xray一样 使用 dokodemo的额外配置来处理tproxy,而是单独使用 tproxy作为一个协议.

# tproxy无法在 交互模式/apiServer中配置 或 热加载, 只能用 标准toml配置文件启用. (因为涉及到 iptables)

# tproxy只支持客户端, 且只支持linux系统, 一般用于软路由/树莓派等.
# tproxy只支持客户端, 且目前只支持linux系统, 一般用于软路由/树莓派等.

[[listen]]
protocol = "tproxy"
Expand All @@ -16,6 +14,7 @@ port = 12345
# sockopt.tproxy = true # 不需要明示指明 tproxy这个 sockopt, 因为protocol指出tproxy后就会自动配置该项.

# 不用指明network, 只要指明了tproxy, 就会同时自动监听 tcp和 dup.
# 但是如果你指明了 network = tcp, 就不会转发udp, 你指明了 network = udp 的话,就不会转发 tcp.

#extra = { auto_iptables = true } # 如果给出了 auto_iptables, 且 【protocol 为 tproxy 的listen】【只有一个】, 则程序会自动在开始监听前配置好iptables,并在程序退出前 清除iptables中被程序改动的部分。
# auto_iptables 会一字不差地运行 上面 给出的 toutyrater 的教程中的 iptables命令。
Expand Down
8 changes: 4 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ func ListenSer(inServer proxy.Server, defaultOutClient proxy.Client, env *proxy.
var is, tcp, udp bool
//tproxy 和 shadowsocks 都用到了 SelfListen
if is, tcp, udp = inServer.SelfListen(); is {
var chantcp chan proxy.IncomeTCPInfo
var chanudp chan proxy.IncomeUDPInfo
var chantcp chan proxy.TCPRequestInfo
var chanudp chan proxy.UDPRequestInfo

if tcp {
chantcp = make(chan proxy.IncomeTCPInfo, 2)
chantcp = make(chan proxy.TCPRequestInfo, 2)
go func() {
for tcpInfo := range chantcp {
go passToOutClient(incomingInserverConnState{
Expand All @@ -95,7 +95,7 @@ func ListenSer(inServer proxy.Server, defaultOutClient proxy.Client, env *proxy.

}
if udp {
chanudp = make(chan proxy.IncomeUDPInfo, 2)
chanudp = make(chan proxy.UDPRequestInfo, 2)

go func() {
for udpInfo := range chanudp {
Expand Down
16 changes: 9 additions & 7 deletions proxy/config_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/e1732a364fed/v2ray_simple/utils"
)

//用于 tproxy 或 tun/tap 这种 只有 网络层 和传输层的情况
// 用于 tproxy 或 tun/tap 这种 只有 网络层 和传输层的情况
type LesserConf struct {
Addr string
Tag string
Expand Down Expand Up @@ -42,7 +42,7 @@ type CommonConf struct {

Xver int `toml:"xver"` //可选,只能为0/1/2. 若不为0, 则使用 PROXY protocol 协议头.

Fullcone bool `toml:"fullcone"` //在direct会用到, fullcone的话因为不能关闭udp连接, 所以 时间长后, 可能会导致too many open files. fullcone 的话一般人是用不到的, 所以 有需要的人自行手动打开 即可
Fullcone bool `toml:"fullcone"` //在udp会用到, fullcone的话因为不能关闭udp连接, 所以 时间长后, 可能会导致too many open files. fullcone 的话一般人是用不到的, 所以 有需要的人自行手动打开 即可

/////////////////// tls层 ///////////////////

Expand Down Expand Up @@ -75,7 +75,7 @@ type CommonConf struct {

}

//和 GetAddrStrForListenOrDial 的区别是,它优先使用host,其次再使用ip
// 和 GetAddrStrForListenOrDial 的区别是,它优先使用host,其次再使用ip
func (cc *CommonConf) GetAddrStr() string {
switch cc.Network {
case "unix":
Expand All @@ -94,7 +94,7 @@ func (cc *CommonConf) GetAddrStr() string {

}

//if network is unix domain socket, return Host,or return ip:port / host:port; 和 GetAddr的区别是,它优先使用ip,其次再使用host
// if network is unix domain socket, return Host,or return ip:port / host:port; 和 GetAddr的区别是,它优先使用ip,其次再使用host
func (cc *CommonConf) GetAddrStrForListenOrDial() string {
switch cc.Network {
case "unix":
Expand All @@ -114,7 +114,8 @@ func (cc *CommonConf) GetAddrStrForListenOrDial() string {
}

// config for listening, the user can be called as listener or inServer.
// CommonConf.Host , CommonConf.IP, CommonConf.Port is the addr and port for listening
//
// CommonConf.Host , CommonConf.IP, CommonConf.Port is the addr and port for listening
type ListenConf struct {
CommonConf

Expand All @@ -135,11 +136,12 @@ type ListenConf struct {
}

// config for dialing, user can be called dialer or outClient.
// CommonConf.Host , CommonConf.IP, CommonConf.Port are the addr and port for dialing.
//
// CommonConf.Host , CommonConf.IP, CommonConf.Port are the addr and port for dialing.
type DialConf struct {
CommonConf

SendThrough string `toml:"sendThrough"` //可选,用于发送数据的 IP 地址
SendThrough string `toml:"sendThrough"` //可选,用于发送数据的 IP 地址, 可以是ip:port, 或者 tcp:ip:port\nudp:ip:port

Utls bool `toml:"utls"` //是否使用 uTls 库 替换 go官方tls库

Expand Down
6 changes: 3 additions & 3 deletions proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,19 @@ type Server interface {
SelfListen() (is, tcp, udp bool)
}

type IncomeTCPInfo struct {
type TCPRequestInfo struct {
net.Conn
Target netLayer.Addr
}

type IncomeUDPInfo struct {
type UDPRequestInfo struct {
netLayer.MsgConn
Target netLayer.Addr
}

type ListenerServer interface {
Server
StartListen(chan<- IncomeTCPInfo, chan<- IncomeUDPInfo) io.Closer
StartListen(chan<- TCPRequestInfo, chan<- UDPRequestInfo) io.Closer
}

type UserServer interface {
Expand Down
4 changes: 2 additions & 2 deletions proxy/shadowsocks/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func (m *Server) removeUDPByHash(hash netLayer.HashableAddr) {
m.Unlock()
}

func (s *Server) StartListen(_ chan<- proxy.IncomeTCPInfo, udpInfoChan chan<- proxy.IncomeUDPInfo) io.Closer {
func (s *Server) StartListen(_ chan<- proxy.TCPRequestInfo, udpInfoChan chan<- proxy.UDPRequestInfo) io.Closer {
uc, err := net.ListenUDP("udp", s.LUA)
if err != nil {
log.Panicln("shadowsocks listen udp failed", err)
Expand Down Expand Up @@ -226,7 +226,7 @@ func (s *Server) StartListen(_ chan<- proxy.IncomeTCPInfo, udpInfoChan chan<- pr
conn.readChan <- netLayer.AddrData{Data: readbuf.Bytes(), Addr: destAddr}

if !found {
udpInfoChan <- proxy.IncomeUDPInfo{
udpInfoChan <- proxy.UDPRequestInfo{
MsgConn: conn, Target: destAddr,
}
}
Expand Down
10 changes: 5 additions & 5 deletions proxy/tproxy/tproxy_linux.go → proxy/tproxy/server_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ type Server struct {

shouldSetIPTable bool

infoChan chan<- proxy.IncomeTCPInfo
udpInfoChan chan<- proxy.IncomeUDPInfo
infoChan chan<- proxy.TCPRequestInfo
udpInfoChan chan<- proxy.UDPRequestInfo
tm *tproxy.Machine
sync.Once
}
Expand Down Expand Up @@ -111,7 +111,7 @@ func (s *Server) Stop() {

}

func (s *Server) StartListen(infoChan chan<- proxy.IncomeTCPInfo, udpInfoChan chan<- proxy.IncomeUDPInfo) io.Closer {
func (s *Server) StartListen(infoChan chan<- proxy.TCPRequestInfo, udpInfoChan chan<- proxy.UDPRequestInfo) io.Closer {

tm := new(tproxy.Machine)

Expand All @@ -124,7 +124,7 @@ func (s *Server) StartListen(infoChan chan<- proxy.IncomeTCPInfo, udpInfoChan ch
tcpconn := conn.(*net.TCPConn)
targetAddr := tproxy.HandshakeTCP(tcpconn)

info := proxy.IncomeTCPInfo{
info := proxy.TCPRequestInfo{
Conn: tcpconn,
Target: targetAddr,
}
Expand Down Expand Up @@ -186,7 +186,7 @@ func (s *Server) StartListen(infoChan chan<- proxy.IncomeTCPInfo, udpInfoChan ch
return
}

udpInfoChan <- proxy.IncomeUDPInfo{MsgConn: msgConn, Target: raddr}
udpInfoChan <- proxy.UDPRequestInfo{MsgConn: msgConn, Target: raddr}

}
}()
Expand Down

0 comments on commit c92219d

Please sign in to comment.