Init: first commit 🎉

This commit is contained in:
Dreamacro
2018-06-10 22:50:03 +08:00
parent 8532718345
commit 4f192ef575
27 changed files with 1451 additions and 0 deletions

35
rules/domain_keyword.go Normal file
View File

@ -0,0 +1,35 @@
package rules
import (
"strings"
C "github.com/Dreamacro/clash/constant"
)
type DomainKeyword struct {
keyword string
adapter string
}
func (dk *DomainKeyword) RuleType() C.RuleType {
return C.DomainKeyword
}
func (dk *DomainKeyword) IsMatch(addr *C.Addr) bool {
if addr.AddrType != C.AtypDomainName {
return false
}
domain := addr.Host
return strings.Contains(domain, dk.keyword)
}
func (dk *DomainKeyword) Adapter() string {
return dk.adapter
}
func NewDomainKeyword(keyword string, adapter string) *DomainKeyword {
return &DomainKeyword{
keyword: keyword,
adapter: adapter,
}
}

35
rules/domain_suffix.go Normal file
View File

@ -0,0 +1,35 @@
package rules
import (
"strings"
C "github.com/Dreamacro/clash/constant"
)
type DomainSuffix struct {
suffix string
adapter string
}
func (ds *DomainSuffix) RuleType() C.RuleType {
return C.DomainSuffix
}
func (ds *DomainSuffix) IsMatch(addr *C.Addr) bool {
if addr.AddrType != C.AtypDomainName {
return false
}
domain := addr.Host
return strings.HasSuffix(domain, "."+ds.suffix) || domain == ds.suffix
}
func (ds *DomainSuffix) Adapter() string {
return ds.adapter
}
func NewDomainSuffix(suffix string, adapter string) *DomainSuffix {
return &DomainSuffix{
suffix: suffix,
adapter: adapter,
}
}

27
rules/final.go Normal file
View File

@ -0,0 +1,27 @@
package rules
import (
C "github.com/Dreamacro/clash/constant"
)
type Final struct {
adapter string
}
func (f *Final) RuleType() C.RuleType {
return C.FINAL
}
func (f *Final) IsMatch(addr *C.Addr) bool {
return true
}
func (f *Final) Adapter() string {
return f.adapter
}
func NewFinal(adapter string) *Final {
return &Final{
adapter: adapter,
}
}

52
rules/geoip.go Normal file
View File

@ -0,0 +1,52 @@
package rules
import (
"net"
C "github.com/Dreamacro/clash/constant"
"github.com/oschwald/geoip2-golang"
log "github.com/sirupsen/logrus"
)
var mmdb *geoip2.Reader
func init() {
var err error
mmdb, err = geoip2.Open(C.MMDBPath)
if err != nil {
log.Fatalf("Can't load mmdb: %s", err.Error())
}
}
type GEOIP struct {
country string
adapter string
}
func (g *GEOIP) RuleType() C.RuleType {
return C.GEOIP
}
func (g *GEOIP) IsMatch(addr *C.Addr) bool {
if addr.AddrType == C.AtypDomainName {
return false
}
dstIP := net.ParseIP(addr.Host)
if dstIP == nil {
return false
}
record, _ := mmdb.Country(dstIP)
return record.Country.IsoCode == g.country
}
func (g *GEOIP) Adapter() string {
return g.adapter
}
func NewGEOIP(country string, adapter string) *GEOIP {
return &GEOIP{
country: country,
adapter: adapter,
}
}

42
rules/ipcidr.go Normal file
View File

@ -0,0 +1,42 @@
package rules
import (
"net"
C "github.com/Dreamacro/clash/constant"
)
type IPCIDR struct {
ipnet *net.IPNet
adapter string
}
func (i *IPCIDR) RuleType() C.RuleType {
return C.IPCIDR
}
func (i *IPCIDR) IsMatch(addr *C.Addr) bool {
if addr.AddrType == C.AtypDomainName {
return false
}
ip := net.ParseIP(addr.Host)
if ip == nil {
return false
}
return i.ipnet.Contains(ip)
}
func (g *IPCIDR) Adapter() string {
return g.adapter
}
func NewIPCIDR(s string, adapter string) *IPCIDR {
_, ipnet, err := net.ParseCIDR(s)
if err != nil {
}
return &IPCIDR{
ipnet: ipnet,
adapter: adapter,
}
}