clash/rules/common/network_type.go

50 lines
848 B
Go
Raw Normal View History

package common
import (
"fmt"
C "github.com/Dreamacro/clash/constant"
"strings"
)
type NetworkType struct {
2022-03-17 15:24:07 +00:00
*Base
network C.NetWork
adapter string
}
func NewNetworkType(network, adapter string) (*NetworkType, error) {
2022-04-15 16:21:08 +00:00
ntType := NetworkType{
Base: &Base{},
}
ntType.adapter = adapter
switch strings.ToUpper(network) {
case "TCP":
2022-04-15 16:21:08 +00:00
ntType.network = C.TCP
break
case "UDP":
2022-04-15 16:21:08 +00:00
ntType.network = C.UDP
break
default:
return nil, fmt.Errorf("unsupported network type, only TCP/UDP")
}
2022-04-15 16:21:08 +00:00
return &ntType, nil
}
func (n *NetworkType) RuleType() C.RuleType {
return C.Network
}
func (n *NetworkType) Match(metadata *C.Metadata) (bool, string) {
return n.network == metadata.NetWork, n.adapter
}
func (n *NetworkType) Adapter() string {
return n.adapter
}
func (n *NetworkType) Payload() string {
return n.network.String()
}