Skip to content

Commit c307a2f

Browse files
authored
feat: implement NAT mapping keep-alive mechanism in hybridStart function
1 parent ad555c9 commit c307a2f

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

internal/client.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,21 @@ func (c *Client) hybridStart() error {
260260
return fmt.Errorf("hybridStart: invalid STUN response")
261261
}
262262

263+
// 保活NAT映射
264+
go func() {
265+
dummy := []byte{0}
266+
ticker := time.NewTicker(time.Second)
267+
defer ticker.Stop()
268+
for {
269+
select {
270+
case <-c.ctx.Done():
271+
return
272+
case <-ticker.C:
273+
udpConn.Write(dummy)
274+
}
275+
}
276+
}()
277+
263278
// 查找映射地址
264279
var extAddr string
265280
for pos := 20; pos+4 <= n; pos += 4 + int(uint16(resp[pos+2])<<8|uint16(resp[pos+3])) + (4 - int((uint16(resp[pos+2])<<8|uint16(resp[pos+3]))%4)) {

0 commit comments

Comments
 (0)