Clash.Meta/rule/port.go

58 lines
911 B
Go
Raw Normal View History

2019-05-09 21:00:29 +08:00
package rules
import (
"strconv"
C "github.com/Dreamacro/clash/constant"
)
type Port struct {
adapter string
port string
isSource bool
2021-07-01 22:49:29 +08:00
network C.NetWork
2019-05-09 21:00:29 +08:00
}
func (p *Port) RuleType() C.RuleType {
if p.isSource {
return C.SrcPort
}
return C.DstPort
}
func (p *Port) Match(metadata *C.Metadata) bool {
2019-05-09 21:00:29 +08:00
if p.isSource {
return metadata.SrcPort == p.port
}
return metadata.DstPort == p.port
}
func (p *Port) Adapter() string {
return p.adapter
}
func (p *Port) Payload() string {
return p.port
}
func (p *Port) ShouldResolveIP() bool {
return false
}
2021-07-01 22:49:29 +08:00
func (p *Port) NetWork() C.NetWork {
return p.network
}
func NewPort(port string, adapter string, isSource bool, network C.NetWork) (*Port, error) {
2019-05-09 21:00:29 +08:00
_, err := strconv.Atoi(port)
if err != nil {
return nil, errPayload
2019-05-09 21:00:29 +08:00
}
return &Port{
adapter: adapter,
port: port,
isSource: isSource,
2021-07-01 22:49:29 +08:00
network: network,
}, nil
2019-05-09 21:00:29 +08:00
}