github.com/metacubex/mihomo@v1.18.5/rules/common/network_type.go (about)

     1  package common
     2  
     3  import (
     4  	"fmt"
     5  	C "github.com/metacubex/mihomo/constant"
     6  	"strings"
     7  )
     8  
     9  type NetworkType struct {
    10  	*Base
    11  	network C.NetWork
    12  	adapter string
    13  }
    14  
    15  func NewNetworkType(network, adapter string) (*NetworkType, error) {
    16  	ntType := NetworkType{
    17  		Base: &Base{},
    18  	}
    19  
    20  	ntType.adapter = adapter
    21  	switch strings.ToUpper(network) {
    22  	case "TCP":
    23  		ntType.network = C.TCP
    24  	case "UDP":
    25  		ntType.network = C.UDP
    26  	default:
    27  		return nil, fmt.Errorf("unsupported network type, only TCP/UDP")
    28  	}
    29  
    30  	return &ntType, nil
    31  }
    32  
    33  func (n *NetworkType) RuleType() C.RuleType {
    34  	return C.Network
    35  }
    36  
    37  func (n *NetworkType) Match(metadata *C.Metadata) (bool, string) {
    38  	return n.network == metadata.NetWork, n.adapter
    39  }
    40  
    41  func (n *NetworkType) Adapter() string {
    42  	return n.adapter
    43  }
    44  
    45  func (n *NetworkType) Payload() string {
    46  	return n.network.String()
    47  }