github.com/igoogolx/clash@v1.19.8/adapter/outboundgroup/util.go (about)

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