add tls and ws listener

This commit is contained in:
ginuerzh
2021-03-31 23:24:51 +08:00
parent b74e4cc8a4
commit 4e04e7ed86
18 changed files with 518 additions and 42 deletions

32
utils/tcp.go Normal file
View File

@ -0,0 +1,32 @@
package utils
import (
"net"
"time"
)
const (
defaultKeepAlivePeriod = 180 * time.Second
)
// TCPKeepAliveListener is a TCP listener with keep alive enabled.
type TCPKeepAliveListener struct {
KeepAlivePeriod time.Duration
*net.TCPListener
}
func (l *TCPKeepAliveListener) Accept() (c net.Conn, err error) {
tc, err := l.AcceptTCP()
if err != nil {
return
}
tc.SetKeepAlive(true)
period := l.KeepAlivePeriod
if period <= 0 {
period = defaultKeepAlivePeriod
}
tc.SetKeepAlivePeriod(period)
return tc, nil
}