feat: Support user specify udp packet size in config (#1794)

This commit is contained in:
Tank 2020-05-07 17:47:36 +08:00 committed by GitHub
parent 7266154d54
commit d193519329
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 43 additions and 8 deletions

View File

@ -543,7 +543,7 @@ func (pxy *UdpProxy) InWorkConn(conn net.Conn, m *msg.StartWorkConn) {
go workConnSenderFn(pxy.workConn, pxy.sendCh) go workConnSenderFn(pxy.workConn, pxy.sendCh)
go workConnReaderFn(pxy.workConn, pxy.readCh) go workConnReaderFn(pxy.workConn, pxy.readCh)
go heartbeatFn(pxy.workConn, pxy.sendCh) go heartbeatFn(pxy.workConn, pxy.sendCh)
udp.Forwarder(pxy.localAddr, pxy.readCh, pxy.sendCh) udp.Forwarder(pxy.localAddr, pxy.readCh, pxy.sendCh, int(pxy.clientCfg.UdpPacketSize))
} }
type SudpProxy struct { type SudpProxy struct {
@ -688,7 +688,7 @@ func (pxy *SudpProxy) InWorkConn(conn net.Conn, m *msg.StartWorkConn) {
go workConnReaderFn(workConn, readCh) go workConnReaderFn(workConn, readCh)
go heartbeatFn(workConn, sendCh) go heartbeatFn(workConn, sendCh)
udp.Forwarder(pxy.localAddr, readCh, sendCh) udp.Forwarder(pxy.localAddr, readCh, sendCh, int(pxy.clientCfg.UdpPacketSize))
} }
// Common handler for tcp work connections. // Common handler for tcp work connections.

View File

@ -369,7 +369,7 @@ func (sv *SudpVisitor) Run() (err error) {
xl.Info("sudp start to work") xl.Info("sudp start to work")
go sv.dispatcher() go sv.dispatcher()
go udp.ForwardUserConn(sv.udpConn, sv.readCh, sv.sendCh) go udp.ForwardUserConn(sv.udpConn, sv.readCh, sv.sendCh, int(sv.ctl.clientCfg.UdpPacketSize))
return return
} }

View File

@ -68,6 +68,11 @@ tls_enable = true
meta_var1 = 123 meta_var1 = 123
meta_var2 = 234 meta_var2 = 234
# specify udp packet size, unit is byte. If not set, the default value is 1500.
# This parameter should be same between client and server.
# It affects the udp and sudp proxy.
udp_packet_size = 1500
# 'ssh' is the unique proxy name # 'ssh' is the unique proxy name
# if user in [common] section is not empty, it will be changed to {user}.{proxy} such as 'your_name.ssh' # if user in [common] section is not empty, it will be changed to {user}.{proxy} such as 'your_name.ssh'
[ssh] [ssh]

View File

@ -113,6 +113,11 @@ tcp_mux = true
# custom 404 page for HTTP requests # custom 404 page for HTTP requests
# custom_404_page = /path/to/404.html # custom_404_page = /path/to/404.html
# specify udp packet size, unit is byte. If not set, the default value is 1500.
# This parameter should be same between client and server.
# It affects the udp and sudp proxy.
udp_packet_size = 1500
[plugin.user-manager] [plugin.user-manager]
addr = 127.0.0.1:9000 addr = 127.0.0.1:9000
path = /handler path = /handler

View File

@ -116,6 +116,9 @@ type ClientCommonConf struct {
HeartBeatTimeout int64 `json:"heartbeat_timeout"` HeartBeatTimeout int64 `json:"heartbeat_timeout"`
// Client meta info // Client meta info
Metas map[string]string `json:"metas"` Metas map[string]string `json:"metas"`
// UdpPacketSize specifies the udp packet size
// By default, this value is 1500
UdpPacketSize int64 `json:"udp_packet_size"`
} }
// GetDefaultClientConf returns a client configuration with default values. // GetDefaultClientConf returns a client configuration with default values.
@ -145,6 +148,7 @@ func GetDefaultClientConf() ClientCommonConf {
HeartBeatInterval: 30, HeartBeatInterval: 30,
HeartBeatTimeout: 90, HeartBeatTimeout: 90,
Metas: make(map[string]string), Metas: make(map[string]string),
UdpPacketSize: 1500,
} }
} }
@ -298,6 +302,14 @@ func UnmarshalClientConfFromIni(content string) (cfg ClientCommonConf, err error
cfg.Metas[strings.TrimPrefix(k, "meta_")] = v cfg.Metas[strings.TrimPrefix(k, "meta_")] = v
} }
} }
if tmpStr, ok = conf.Get("common", "udp_packet_size"); ok {
if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
err = fmt.Errorf("Parse conf error: invalid udp_packet_size")
return
} else {
cfg.UdpPacketSize = v
}
}
return return
} }

View File

@ -145,6 +145,9 @@ type ServerCommonConf struct {
UserConnTimeout int64 `json:"user_conn_timeout"` UserConnTimeout int64 `json:"user_conn_timeout"`
// HTTPPlugins specify the server plugins support HTTP protocol. // HTTPPlugins specify the server plugins support HTTP protocol.
HTTPPlugins map[string]plugin.HTTPPluginOptions `json:"http_plugins"` HTTPPlugins map[string]plugin.HTTPPluginOptions `json:"http_plugins"`
// UdpPacketSize specifies the udp packet size
// By default, this value is 1500
UdpPacketSize int64 `json:"udp_packet_size"`
} }
// GetDefaultServerConf returns a server configuration with reasonable // GetDefaultServerConf returns a server configuration with reasonable
@ -182,6 +185,7 @@ func GetDefaultServerConf() ServerCommonConf {
UserConnTimeout: 10, UserConnTimeout: 10,
Custom404Page: "", Custom404Page: "",
HTTPPlugins: make(map[string]plugin.HTTPPluginOptions), HTTPPlugins: make(map[string]plugin.HTTPPluginOptions),
UdpPacketSize: 1500,
} }
} }
@ -416,6 +420,15 @@ func UnmarshalServerConfFromIni(content string) (cfg ServerCommonConf, err error
} else { } else {
cfg.TlsOnly = false cfg.TlsOnly = false
} }
if tmpStr, ok = conf.Get("common", "udp_packet_size"); ok {
if v, err = strconv.ParseInt(tmpStr, 10, 64); err != nil {
err = fmt.Errorf("Parse conf error: invalid udp_packet_size")
return
} else {
cfg.UdpPacketSize = v
}
}
return return
} }

View File

@ -39,7 +39,7 @@ func GetContent(m *msg.UdpPacket) (buf []byte, err error) {
return return
} }
func ForwardUserConn(udpConn *net.UDPConn, readCh <-chan *msg.UdpPacket, sendCh chan<- *msg.UdpPacket) { func ForwardUserConn(udpConn *net.UDPConn, readCh <-chan *msg.UdpPacket, sendCh chan<- *msg.UdpPacket, bufSize int) {
// read // read
go func() { go func() {
for udpMsg := range readCh { for udpMsg := range readCh {
@ -52,7 +52,7 @@ func ForwardUserConn(udpConn *net.UDPConn, readCh <-chan *msg.UdpPacket, sendCh
}() }()
// write // write
buf := pool.GetBuf(1500) buf := pool.GetBuf(bufSize)
defer pool.PutBuf(buf) defer pool.PutBuf(buf)
for { for {
n, remoteAddr, err := udpConn.ReadFromUDP(buf) n, remoteAddr, err := udpConn.ReadFromUDP(buf)
@ -69,7 +69,7 @@ func ForwardUserConn(udpConn *net.UDPConn, readCh <-chan *msg.UdpPacket, sendCh
} }
} }
func Forwarder(dstAddr *net.UDPAddr, readCh <-chan *msg.UdpPacket, sendCh chan<- msg.Message) { func Forwarder(dstAddr *net.UDPAddr, readCh <-chan *msg.UdpPacket, sendCh chan<- msg.Message, bufSize int) {
var ( var (
mu sync.RWMutex mu sync.RWMutex
) )
@ -85,7 +85,7 @@ func Forwarder(dstAddr *net.UDPAddr, readCh <-chan *msg.UdpPacket, sendCh chan<-
udpConn.Close() udpConn.Close()
}() }()
buf := pool.GetBuf(1500) buf := pool.GetBuf(bufSize)
for { for {
udpConn.SetReadDeadline(time.Now().Add(30 * time.Second)) udpConn.SetReadDeadline(time.Now().Add(30 * time.Second))
n, _, err := udpConn.ReadFromUDP(buf) n, _, err := udpConn.ReadFromUDP(buf)

View File

@ -196,7 +196,7 @@ func (pxy *UdpProxy) Run() (remoteAddr string, err error) {
// Response will be wrapped to be forwarded by work connection to server. // Response will be wrapped to be forwarded by work connection to server.
// Close readCh and sendCh at the end. // Close readCh and sendCh at the end.
go func() { go func() {
udp.ForwardUserConn(udpConn, pxy.readCh, pxy.sendCh) udp.ForwardUserConn(udpConn, pxy.readCh, pxy.sendCh, int(pxy.serverCfg.UdpPacketSize))
pxy.Close() pxy.Close()
}() }()
return remoteAddr, nil return remoteAddr, nil