2019-05-09 13:00:29 +00:00
|
|
|
package rules
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Port struct {
|
|
|
|
adapter string
|
|
|
|
port string
|
|
|
|
isSource bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Port) RuleType() C.RuleType {
|
|
|
|
if p.isSource {
|
|
|
|
return C.SrcPort
|
|
|
|
}
|
|
|
|
return C.DstPort
|
|
|
|
}
|
|
|
|
|
2019-10-27 16:02:23 +00:00
|
|
|
func (p *Port) Match(metadata *C.Metadata) bool {
|
2019-05-09 13:00:29 +00: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
|
|
|
|
}
|
|
|
|
|
2020-07-27 03:57:55 +00:00
|
|
|
func (p *Port) ShouldResolveIP() bool {
|
|
|
|
return false
|
2019-10-27 16:02:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewPort(port string, adapter string, isSource bool) (*Port, error) {
|
2019-05-09 13:00:29 +00:00
|
|
|
_, err := strconv.Atoi(port)
|
|
|
|
if err != nil {
|
2019-10-27 16:02:23 +00:00
|
|
|
return nil, errPayload
|
2019-05-09 13:00:29 +00:00
|
|
|
}
|
|
|
|
return &Port{
|
|
|
|
adapter: adapter,
|
|
|
|
port: port,
|
|
|
|
isSource: isSource,
|
2019-10-27 16:02:23 +00:00
|
|
|
}, nil
|
2019-05-09 13:00:29 +00:00
|
|
|
}
|