Skip to content

Commit f957f25

Browse files
committed
Add control.SetKeepAlivePeriod
1 parent 3f6c423 commit f957f25

2 files changed

Lines changed: 36 additions & 0 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package control
2+
3+
import (
4+
E "github.com/sagernet/sing/common/exceptions"
5+
"golang.org/x/sys/unix"
6+
"syscall"
7+
"time"
8+
9+
_ "unsafe"
10+
)
11+
12+
func SetKeepAlivePeriod(keepCnt int, keepIdle time.Duration, interval time.Duration) Func {
13+
return func(network, address string, conn syscall.RawConn) error {
14+
return Raw(conn, func(fd uintptr) error {
15+
return E.Errors(
16+
unix.SetsockoptInt(int(fd), unix.IPPROTO_TCP, unix.TCP_KEEPCNT, keepCnt),
17+
unix.SetsockoptInt(int(fd), unix.IPPROTO_TCP, unix.TCP_KEEPIDLE, int(roundDurationUp(keepIdle, time.Second))),
18+
unix.SetsockoptInt(int(fd), unix.IPPROTO_TCP, unix.TCP_KEEPINTVL, int(roundDurationUp(interval, time.Second))),
19+
)
20+
})
21+
}
22+
}
23+
24+
//go:linkname roundDurationUp net.roundDurationUp
25+
func roundDurationUp(d time.Duration, to time.Duration) time.Duration
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//go:build !linux
2+
3+
package control
4+
5+
import (
6+
"time"
7+
)
8+
9+
func SetKeepAlivePeriod(keepCnt int, keepIdle time.Duration, interval time.Duration) Func {
10+
return nil
11+
}

0 commit comments

Comments
 (0)