diff --git a/code/client/global/conf.go b/code/client/global/conf.go index aec1256..29bd2de 100644 --- a/code/client/global/conf.go +++ b/code/client/global/conf.go @@ -4,6 +4,7 @@ import ( "crypto/md5" "natpass/code/utils" "os" + "time" "github.com/lwch/runtime" "gopkg.in/yaml.v2" @@ -22,14 +23,16 @@ type Tunnel struct { // Configure client configure type Configure struct { - ID string - Server string - Enc [md5.Size]byte - Links int - LogDir string - LogSize utils.Bytes - LogRotate int - Tunnels []Tunnel + ID string + Server string + Enc [md5.Size]byte + Links int + LogDir string + LogSize utils.Bytes + LogRotate int + ReadTimeout time.Duration + WriteTimeout time.Duration + Tunnels []Tunnel } // LoadConf load configure file @@ -38,8 +41,12 @@ func LoadConf(dir string) *Configure { ID string `yaml:"id"` Server string `yaml:"server"` Secret string `yaml:"secret"` - Links int `yaml:"links"` - Log struct { + Link struct { + Connections int `yaml:"connections"` + ReadTimeout time.Duration `yaml:"read_timeout"` + WriteTimeout time.Duration `yaml:"write_timeout"` + } `yaml:"link"` + Log struct { Dir string `yaml:"dir"` Size utils.Bytes `yaml:"size"` Rotate int `yaml:"rotate"` @@ -56,17 +63,25 @@ func LoadConf(dir string) *Configure { } cfg.Tunnel[i] = t } - if cfg.Links <= 0 { - cfg.Links = 3 + if cfg.Link.Connections <= 0 { + cfg.Link.Connections = 3 + } + if cfg.Link.ReadTimeout <= 0 { + cfg.Link.ReadTimeout = 5 * time.Second + } + if cfg.Link.WriteTimeout <= 0 { + cfg.Link.WriteTimeout = 5 * time.Second } return &Configure{ - ID: cfg.ID, - Server: cfg.Server, - Enc: md5.Sum([]byte(cfg.Secret)), - Links: cfg.Links, - LogDir: cfg.Log.Dir, - LogSize: cfg.Log.Size, - LogRotate: cfg.Log.Rotate, - Tunnels: cfg.Tunnel, + ID: cfg.ID, + Server: cfg.Server, + Enc: md5.Sum([]byte(cfg.Secret)), + Links: cfg.Link.Connections, + ReadTimeout: cfg.Link.ReadTimeout, + WriteTimeout: cfg.Link.WriteTimeout, + LogDir: cfg.Log.Dir, + LogSize: cfg.Log.Size, + LogRotate: cfg.Log.Rotate, + Tunnels: cfg.Tunnel, } } diff --git a/code/client/global/constants.go b/code/client/global/constants.go deleted file mode 100644 index 0a6b8af..0000000 --- a/code/client/global/constants.go +++ /dev/null @@ -1,8 +0,0 @@ -package global - -import "time" - -const ( - ReadTimeout = 5 * time.Second - WriteTimeout = 5 * time.Second -) diff --git a/code/client/pool/conn.go b/code/client/pool/conn.go index 468e101..17d786a 100644 --- a/code/client/pool/conn.go +++ b/code/client/pool/conn.go @@ -2,7 +2,6 @@ package pool import ( "context" - "natpass/code/client/global" "natpass/code/network" "strings" "sync" @@ -90,7 +89,7 @@ func (conn *Conn) loopRead(cancel context.CancelFunc) { defer conn.Close() defer cancel() for { - msg, err := conn.conn.ReadMessage(global.ReadTimeout) + msg, err := conn.conn.ReadMessage(conn.parent.cfg.ReadTimeout) if err != nil { if strings.Contains(err.Error(), "i/o timeout") { continue @@ -117,7 +116,7 @@ func (conn *Conn) loopRead(cancel context.CancelFunc) { } select { case ch <- msg: - case <-time.After(global.ReadTimeout): + case <-time.After(conn.parent.cfg.ReadTimeout): logging.Error("write read channel for link %s timeouted", linkID) if ch == conn.unknownRead { continue @@ -136,7 +135,7 @@ func (conn *Conn) loopWrite(cancel context.CancelFunc) { return } msg.From = conn.ID - err := conn.conn.WriteMessage(msg, global.WriteTimeout) + err := conn.conn.WriteMessage(msg, conn.parent.cfg.WriteTimeout) if err == nil { continue } diff --git a/code/client/pool/send.go b/code/client/pool/send.go index 6b99bfe..b753b5f 100644 --- a/code/client/pool/send.go +++ b/code/client/pool/send.go @@ -26,7 +26,7 @@ func (conn *Conn) SendConnectReq(id string, cfg global.Tunnel) { } select { case conn.write <- &msg: - case <-time.After(global.WriteTimeout): + case <-time.After(conn.parent.cfg.WriteTimeout): } } @@ -44,7 +44,7 @@ func (conn *Conn) SendConnectError(to, id, info string) { } select { case conn.write <- &msg: - case <-time.After(global.WriteTimeout): + case <-time.After(conn.parent.cfg.WriteTimeout): } } @@ -61,7 +61,7 @@ func (conn *Conn) SendConnectOK(to, id string) { } select { case conn.write <- &msg: - case <-time.After(global.WriteTimeout): + case <-time.After(conn.parent.cfg.WriteTimeout): } } @@ -77,7 +77,7 @@ func (conn *Conn) SendDisconnect(to, id string) { } select { case conn.write <- &msg: - case <-time.After(global.WriteTimeout): + case <-time.After(conn.parent.cfg.WriteTimeout): } } @@ -99,7 +99,7 @@ func (conn *Conn) SendData(to, id string, data []byte) { } select { case conn.write <- &msg: - case <-time.After(global.WriteTimeout): + case <-time.After(conn.parent.cfg.WriteTimeout): } } @@ -110,6 +110,6 @@ func (conn *Conn) SendKeepalive() { msg.XType = network.Msg_keepalive select { case conn.write <- &msg: - case <-time.After(global.WriteTimeout): + case <-time.After(conn.parent.cfg.WriteTimeout): } } diff --git a/code/server/global/conf.go b/code/server/global/conf.go index 7931908..ee25a5e 100644 --- a/code/server/global/conf.go +++ b/code/server/global/conf.go @@ -4,6 +4,7 @@ import ( "crypto/md5" "natpass/code/utils" "os" + "time" "github.com/lwch/runtime" "gopkg.in/yaml.v2" @@ -11,13 +12,15 @@ import ( // Configure server configure type Configure struct { - Listen uint16 - Enc [md5.Size]byte - TLSKey string - TLSCrt string - LogDir string - LogSize utils.Bytes - LogRotate int + Listen uint16 + Enc [md5.Size]byte + TLSKey string + TLSCrt string + ReadTimeout time.Duration + WriteTimeout time.Duration + LogDir string + LogSize utils.Bytes + LogRotate int } // LoadConf load configure file @@ -25,7 +28,11 @@ func LoadConf(dir string) *Configure { var cfg struct { Listen uint16 `yaml:"listen"` Secret string `yaml:"secret"` - Log struct { + Link struct { + ReadTimeout time.Duration `yaml:"read_timeout"` + WriteTimeout time.Duration `yaml:"write_timeout"` + } `yaml:"link"` + Log struct { Dir string `yaml:"dir"` Size utils.Bytes `yaml:"size"` Rotate int `yaml:"rotate"` @@ -40,12 +47,14 @@ func LoadConf(dir string) *Configure { defer f.Close() runtime.Assert(yaml.NewDecoder(f).Decode(&cfg)) return &Configure{ - Listen: cfg.Listen, - Enc: md5.Sum([]byte(cfg.Secret)), - TLSKey: cfg.TLS.Key, - TLSCrt: cfg.TLS.Crt, - LogDir: cfg.Log.Dir, - LogSize: cfg.Log.Size, - LogRotate: cfg.Log.Rotate, + Listen: cfg.Listen, + Enc: md5.Sum([]byte(cfg.Secret)), + TLSKey: cfg.TLS.Key, + TLSCrt: cfg.TLS.Crt, + ReadTimeout: cfg.Link.ReadTimeout, + WriteTimeout: cfg.Link.WriteTimeout, + LogDir: cfg.Log.Dir, + LogSize: cfg.Log.Size, + LogRotate: cfg.Log.Rotate, } } diff --git a/code/server/global/constants.go b/code/server/global/constants.go deleted file mode 100644 index 0a6b8af..0000000 --- a/code/server/global/constants.go +++ /dev/null @@ -1,8 +0,0 @@ -package global - -import "time" - -const ( - ReadTimeout = 5 * time.Second - WriteTimeout = 5 * time.Second -) diff --git a/code/server/handler/client.go b/code/server/handler/client.go index 0428328..0844d1a 100644 --- a/code/server/handler/client.go +++ b/code/server/handler/client.go @@ -1,7 +1,6 @@ package handler import ( - "natpass/code/client/global" "natpass/code/network" "strings" "sync" @@ -44,7 +43,7 @@ func (c *client) run() { c.parent.closeAll(c) return } - msg, err := c.c.ReadMessage(global.ReadTimeout) + msg, err := c.c.ReadMessage(c.parent.cfg.ReadTimeout) if err != nil { if strings.Contains(err.Error(), "i/o timeout") { continue @@ -58,7 +57,7 @@ func (c *client) run() { } func (c *client) writeMessage(msg *network.Msg) error { - return c.c.WriteMessage(msg, global.WriteTimeout) + return c.c.WriteMessage(msg, c.parent.cfg.WriteTimeout) } func (c *client) addLink(id string) { @@ -93,7 +92,7 @@ func (c *client) close(id string) { Id: id, }, } - c.c.WriteMessage(&msg, global.WriteTimeout) + c.c.WriteMessage(&msg, c.parent.cfg.WriteTimeout) c.Lock() delete(c.links, id) c.Unlock() diff --git a/conf/client.yaml b/conf/client.yaml index e221d3c..47e0d1d 100644 --- a/conf/client.yaml +++ b/conf/client.yaml @@ -1,7 +1,10 @@ id: this # 客户端ID server: 127.0.0.1:6154 # 服务器地址 -links: 3 # 与server的连接数 secret: 0123456789 # 预共享密钥,必须与server端相同,否则握手失败 +link: + connections: 3 # 与server的连接数 + read_timeout: 5s # 读取数据包超时时间 + write_timeout: 5s # 发送数据包超时时间 log: dir: ./logs # 路径 size: 50M # 单个文件大小 diff --git a/conf/server.yaml b/conf/server.yaml index 9c4b2e5..88c4fd0 100644 --- a/conf/server.yaml +++ b/conf/server.yaml @@ -1,5 +1,8 @@ listen: 6154 # 监听端口号 secret: 0123456789 # 预共享密钥 +link: + read_timeout: 5s # 读取数据包超时时间 + write_timeout: 5s # 发送数据包超时时间 log: dir: ./logs # 路径 size: 50M # 单个文件大小