github.com/metacubex/mihomo@v1.18.5/common/net/tcpip.go (about) 1 package net 2 3 import ( 4 "fmt" 5 "net" 6 "strings" 7 "time" 8 ) 9 10 var KeepAliveInterval = 15 * time.Second 11 12 func SplitNetworkType(s string) (string, string, error) { 13 var ( 14 shecme string 15 hostPort string 16 ) 17 result := strings.Split(s, "://") 18 if len(result) == 2 { 19 shecme = result[0] 20 hostPort = result[1] 21 } else if len(result) == 1 { 22 hostPort = result[0] 23 } else { 24 return "", "", fmt.Errorf("tcp/udp style error") 25 } 26 27 if len(shecme) == 0 { 28 shecme = "udp" 29 } 30 31 if shecme != "tcp" && shecme != "udp" { 32 return "", "", fmt.Errorf("scheme should be tcp:// or udp://") 33 } else { 34 return shecme, hostPort, nil 35 } 36 } 37 38 func SplitHostPort(s string) (host, port string, hasPort bool, err error) { 39 temp := s 40 hasPort = true 41 42 if !strings.Contains(s, ":") && !strings.Contains(s, "]:") { 43 temp += ":0" 44 hasPort = false 45 } 46 47 host, port, err = net.SplitHostPort(temp) 48 return 49 } 50 51 func TCPKeepAlive(c net.Conn) { 52 if tcp, ok := c.(*net.TCPConn); ok { 53 _ = tcp.SetKeepAlive(true) 54 _ = tcp.SetKeepAlivePeriod(KeepAliveInterval) 55 } 56 }