github.com/chwjbn/xclash@v0.2.0/adapter/outboundgroup/util.go (about)

     1  package outboundgroup
     2  
     3  import (
     4  	"fmt"
     5  	"net"
     6  	"time"
     7  
     8  	C "github.com/chwjbn/xclash/constant"
     9  )
    10  
    11  func addrToMetadata(rawAddress string) (addr *C.Metadata, err error) {
    12  	host, port, err := net.SplitHostPort(rawAddress)
    13  	if err != nil {
    14  		err = fmt.Errorf("addrToMetadata failed: %w", err)
    15  		return
    16  	}
    17  
    18  	ip := net.ParseIP(host)
    19  	if ip == nil {
    20  		addr = &C.Metadata{
    21  			AddrType: C.AtypDomainName,
    22  			Host:     host,
    23  			DstIP:    nil,
    24  			DstPort:  port,
    25  		}
    26  		return
    27  	} else if ip4 := ip.To4(); ip4 != nil {
    28  		addr = &C.Metadata{
    29  			AddrType: C.AtypIPv4,
    30  			Host:     "",
    31  			DstIP:    ip4,
    32  			DstPort:  port,
    33  		}
    34  		return
    35  	}
    36  
    37  	addr = &C.Metadata{
    38  		AddrType: C.AtypIPv6,
    39  		Host:     "",
    40  		DstIP:    ip,
    41  		DstPort:  port,
    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  }