github.com/metacubex/sing-tun@v0.2.7-0.20240512075008-89e7c6208eec/network_name.go (about)

     1  package tun
     2  
     3  import (
     4  	"strconv"
     5  
     6  	"github.com/metacubex/sing-tun/internal/clashtcpip"
     7  	F "github.com/sagernet/sing/common/format"
     8  	N "github.com/sagernet/sing/common/network"
     9  )
    10  
    11  func NetworkName(network uint8) string {
    12  	switch network {
    13  	case clashtcpip.TCP:
    14  		return N.NetworkTCP
    15  	case clashtcpip.UDP:
    16  		return N.NetworkUDP
    17  	case clashtcpip.ICMP:
    18  		return N.NetworkICMPv4
    19  	case clashtcpip.ICMPv6:
    20  		return N.NetworkICMPv6
    21  	}
    22  	return F.ToString(network)
    23  }
    24  
    25  func NetworkFromName(name string) uint8 {
    26  	switch name {
    27  	case N.NetworkTCP:
    28  		return clashtcpip.TCP
    29  	case N.NetworkUDP:
    30  		return clashtcpip.UDP
    31  	case N.NetworkICMPv4:
    32  		return clashtcpip.ICMP
    33  	case N.NetworkICMPv6:
    34  		return clashtcpip.ICMPv6
    35  	}
    36  	parseNetwork, err := strconv.ParseUint(name, 10, 8)
    37  	if err != nil {
    38  		return 0
    39  	}
    40  	return uint8(parseNetwork)
    41  }