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