mirror of
https://github.com/lwch/natpass.git
synced 2024-04-21 12:41:54 +00:00
增加keepalive
This commit is contained in:
@@ -39,6 +39,8 @@ func (c *Client) Run() {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
go c.keepalive(ctx)
|
||||
|
||||
for _, t := range c.cfg.Tunnels {
|
||||
if t.Type == "tcp" {
|
||||
go c.handleTcpTunnel(ctx, t)
|
||||
@@ -110,6 +112,18 @@ func (c *Client) handleTcpTunnel(ctx context.Context, t global.Tunnel) {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) keepalive(ctx context.Context) {
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
default:
|
||||
}
|
||||
time.Sleep(10 * time.Second)
|
||||
c.sendKeepalive()
|
||||
}
|
||||
}
|
||||
|
||||
// handleUdpTunnel local listen to udp tunnel
|
||||
func (c *Client) handleUdpTunnel(ctx context.Context, t global.Tunnel) {
|
||||
// TODO
|
||||
|
||||
@@ -98,3 +98,11 @@ func (c *Client) disconnect(id, to string) {
|
||||
delete(c.tunnels, id)
|
||||
c.Unlock()
|
||||
}
|
||||
|
||||
func (c *Client) sendKeepalive() {
|
||||
var msg network.Msg
|
||||
msg.From = c.cfg.ID
|
||||
msg.To = "server"
|
||||
msg.XType = network.Msg_keepalive
|
||||
c.conn.WriteMessage(&msg, time.Second)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user