github.com/yaling888/clash@v1.53.0/adapter/outboundgroup/util.go (about)

     1  package outboundgroup
     2  
     3  import (
     4  	"fmt"
     5  	"net"
     6  	"net/netip"
     7  	"strconv"
     8  	"time"
     9  
    10  	C "github.com/yaling888/clash/constant"
    11  )
    12  
    13  func addrToMetadata(rawAddress string) (addr *C.Metadata, err error) {
    14  	host, port, err := net.SplitHostPort(rawAddress)
    15  	if err != nil {
    16  		err = fmt.Errorf("addrToMetadata failed: %w", err)
    17  		return
    18  	}
    19  
    20  	ip, err := netip.ParseAddr(host)
    21  	p, _ := strconv.ParseUint(port, 10, 16)
    22  	if err != nil {
    23  		addr = &C.Metadata{
    24  			Host:    host,
    25  			DstIP:   netip.Addr{},
    26  			DstPort: C.Port(p),
    27  		}
    28  		return addr, nil
    29  	} else if ip.Is4() {
    30  		addr = &C.Metadata{
    31  			Host:    "",
    32  			DstIP:   ip,
    33  			DstPort: C.Port(p),
    34  		}
    35  		return
    36  	}
    37  
    38  	addr = &C.Metadata{
    39  		Host:    "",
    40  		DstIP:   ip,
    41  		DstPort: C.Port(p),
    42  	}
    43  	return
    44  }
    45  
    46  func tcpKeepAlive(c net.Conn) {
    47  	if tcp, ok := c.(*net.TCPConn); ok {
    48  		_ = tcp.SetKeepAlive(true)
    49  		_ = tcp.SetKeepAlivePeriod(30 * time.Second)
    50  	}
    51  }