mirror of
https://github.com/XTLS/Xray-core.git
synced 2024-05-18 09:59:19 +03:00
Compare commits
4 Commits
f2778f49f0
...
d73620970e
Author | SHA1 | Date | |
---|---|---|---|
A1lo | d73620970e | ||
dependabot[bot] | c1ad35fba8 | ||
nobody | 447a49d16a | ||
Allo | 6b25c7e500 |
|
@ -21,12 +21,14 @@ type Commander struct {
|
|||
services []Service
|
||||
ohm outbound.Manager
|
||||
tag string
|
||||
listen string
|
||||
}
|
||||
|
||||
// NewCommander creates a new Commander based on the given config.
|
||||
func NewCommander(ctx context.Context, config *Config) (*Commander, error) {
|
||||
c := &Commander{
|
||||
tag: config.Tag,
|
||||
tag: config.Tag,
|
||||
listen: config.Listen,
|
||||
}
|
||||
|
||||
common.Must(core.RequireFeatures(ctx, func(om outbound.Manager) {
|
||||
|
@ -66,16 +68,29 @@ func (c *Commander) Start() error {
|
|||
}
|
||||
c.Unlock()
|
||||
|
||||
var listen = func(listener net.Listener) {
|
||||
if err := c.server.Serve(listener); err != nil {
|
||||
newError("failed to start grpc server").Base(err).AtError().WriteToLog()
|
||||
}
|
||||
}
|
||||
|
||||
if len(c.listen) > 0 {
|
||||
if l, err := net.Listen("tcp", c.listen); err != nil {
|
||||
newError("API server failed to listen on ", c.listen).Base(err).AtError().WriteToLog()
|
||||
return err
|
||||
} else {
|
||||
newError("API server listening on ", l.Addr()).AtInfo().WriteToLog()
|
||||
go listen(l)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
listener := &OutboundListener{
|
||||
buffer: make(chan net.Conn, 4),
|
||||
done: done.New(),
|
||||
}
|
||||
|
||||
go func() {
|
||||
if err := c.server.Serve(listener); err != nil {
|
||||
newError("failed to start grpc server").Base(err).AtError().WriteToLog()
|
||||
}
|
||||
}()
|
||||
go listen(listener)
|
||||
|
||||
if err := c.ohm.RemoveHandler(context.Background(), c.tag); err != nil {
|
||||
newError("failed to remove existing handler").WriteToLog()
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// versions:
|
||||
// protoc-gen-go v1.33.0
|
||||
// protoc v4.23.1
|
||||
// protoc-gen-go v1.28.1
|
||||
// protoc v4.25.3
|
||||
// source: app/commander/config.proto
|
||||
|
||||
package commander
|
||||
|
@ -29,6 +29,8 @@ type Config struct {
|
|||
|
||||
// Tag of the outbound handler that handles grpc connections.
|
||||
Tag string `protobuf:"bytes,1,opt,name=tag,proto3" json:"tag,omitempty"`
|
||||
// Network address of commander grpc service.
|
||||
Listen string `protobuf:"bytes,3,opt,name=listen,proto3" json:"listen,omitempty"`
|
||||
// Services that supported by this server. All services must implement Service
|
||||
// interface.
|
||||
Service []*serial.TypedMessage `protobuf:"bytes,2,rep,name=service,proto3" json:"service,omitempty"`
|
||||
|
@ -73,6 +75,13 @@ func (x *Config) GetTag() string {
|
|||
return ""
|
||||
}
|
||||
|
||||
func (x *Config) GetListen() string {
|
||||
if x != nil {
|
||||
return x.Listen
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *Config) GetService() []*serial.TypedMessage {
|
||||
if x != nil {
|
||||
return x.Service
|
||||
|
@ -127,20 +136,21 @@ var file_app_commander_config_proto_rawDesc = []byte{
|
|||
0x61, 0x79, 0x2e, 0x61, 0x70, 0x70, 0x2e, 0x63, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x65, 0x72,
|
||||
0x1a, 0x21, 0x63, 0x6f, 0x6d, 0x6d, 0x6f, 0x6e, 0x2f, 0x73, 0x65, 0x72, 0x69, 0x61, 0x6c, 0x2f,
|
||||
0x74, 0x79, 0x70, 0x65, 0x64, 0x5f, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x2e, 0x70, 0x72,
|
||||
0x6f, 0x74, 0x6f, 0x22, 0x56, 0x0a, 0x06, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x12, 0x10, 0x0a,
|
||||
0x6f, 0x74, 0x6f, 0x22, 0x6e, 0x0a, 0x06, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x12, 0x10, 0x0a,
|
||||
0x03, 0x74, 0x61, 0x67, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x74, 0x61, 0x67, 0x12,
|
||||
0x3a, 0x0a, 0x07, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0b,
|
||||
0x32, 0x20, 0x2e, 0x78, 0x72, 0x61, 0x79, 0x2e, 0x63, 0x6f, 0x6d, 0x6d, 0x6f, 0x6e, 0x2e, 0x73,
|
||||
0x65, 0x72, 0x69, 0x61, 0x6c, 0x2e, 0x54, 0x79, 0x70, 0x65, 0x64, 0x4d, 0x65, 0x73, 0x73, 0x61,
|
||||
0x67, 0x65, 0x52, 0x07, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x22, 0x12, 0x0a, 0x10, 0x52,
|
||||
0x65, 0x66, 0x6c, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x42,
|
||||
0x58, 0x0a, 0x16, 0x63, 0x6f, 0x6d, 0x2e, 0x78, 0x72, 0x61, 0x79, 0x2e, 0x61, 0x70, 0x70, 0x2e,
|
||||
0x63, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x65, 0x72, 0x50, 0x01, 0x5a, 0x27, 0x67, 0x69, 0x74,
|
||||
0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x78, 0x74, 0x6c, 0x73, 0x2f, 0x78, 0x72, 0x61,
|
||||
0x79, 0x2d, 0x63, 0x6f, 0x72, 0x65, 0x2f, 0x61, 0x70, 0x70, 0x2f, 0x63, 0x6f, 0x6d, 0x6d, 0x61,
|
||||
0x6e, 0x64, 0x65, 0x72, 0xaa, 0x02, 0x12, 0x58, 0x72, 0x61, 0x79, 0x2e, 0x41, 0x70, 0x70, 0x2e,
|
||||
0x43, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x65, 0x72, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f,
|
||||
0x33,
|
||||
0x16, 0x0a, 0x06, 0x6c, 0x69, 0x73, 0x74, 0x65, 0x6e, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52,
|
||||
0x06, 0x6c, 0x69, 0x73, 0x74, 0x65, 0x6e, 0x12, 0x3a, 0x0a, 0x07, 0x73, 0x65, 0x72, 0x76, 0x69,
|
||||
0x63, 0x65, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x20, 0x2e, 0x78, 0x72, 0x61, 0x79, 0x2e,
|
||||
0x63, 0x6f, 0x6d, 0x6d, 0x6f, 0x6e, 0x2e, 0x73, 0x65, 0x72, 0x69, 0x61, 0x6c, 0x2e, 0x54, 0x79,
|
||||
0x70, 0x65, 0x64, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x52, 0x07, 0x73, 0x65, 0x72, 0x76,
|
||||
0x69, 0x63, 0x65, 0x22, 0x12, 0x0a, 0x10, 0x52, 0x65, 0x66, 0x6c, 0x65, 0x63, 0x74, 0x69, 0x6f,
|
||||
0x6e, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x42, 0x58, 0x0a, 0x16, 0x63, 0x6f, 0x6d, 0x2e, 0x78,
|
||||
0x72, 0x61, 0x79, 0x2e, 0x61, 0x70, 0x70, 0x2e, 0x63, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x65,
|
||||
0x72, 0x50, 0x01, 0x5a, 0x27, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f,
|
||||
0x78, 0x74, 0x6c, 0x73, 0x2f, 0x78, 0x72, 0x61, 0x79, 0x2d, 0x63, 0x6f, 0x72, 0x65, 0x2f, 0x61,
|
||||
0x70, 0x70, 0x2f, 0x63, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x65, 0x72, 0xaa, 0x02, 0x12, 0x58,
|
||||
0x72, 0x61, 0x79, 0x2e, 0x41, 0x70, 0x70, 0x2e, 0x43, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x65,
|
||||
0x72, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
|
||||
}
|
||||
|
||||
var (
|
||||
|
|
|
@ -12,6 +12,10 @@ import "common/serial/typed_message.proto";
|
|||
message Config {
|
||||
// Tag of the outbound handler that handles grpc connections.
|
||||
string tag = 1;
|
||||
|
||||
// Network address of commander grpc service.
|
||||
string listen = 3;
|
||||
|
||||
// Services that supported by this server. All services must implement Service
|
||||
// interface.
|
||||
repeated xray.common.serial.TypedMessage service = 2;
|
||||
|
|
2
go.mod
2
go.mod
|
@ -28,7 +28,7 @@ require (
|
|||
golang.org/x/sys v0.19.0
|
||||
golang.zx2c4.com/wireguard v0.0.0-20231211153847-12269c276173
|
||||
google.golang.org/grpc v1.63.2
|
||||
google.golang.org/protobuf v1.33.0
|
||||
google.golang.org/protobuf v1.34.0
|
||||
gvisor.dev/gvisor v0.0.0-20231202080848-1f7806d17489
|
||||
h12.io/socks v1.0.3
|
||||
lukechampine.com/blake3 v1.2.2
|
||||
|
|
4
go.sum
4
go.sum
|
@ -279,8 +279,8 @@ google.golang.org/grpc v1.17.0/go.mod h1:6QZJwpn2B+Zp71q/5VxRsJ6NXXVCE5NRUHRo+f3
|
|||
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
|
||||
google.golang.org/grpc v1.63.2 h1:MUeiw1B2maTVZthpU5xvASfTh3LDbxHd6IJ6QQVU+xM=
|
||||
google.golang.org/grpc v1.63.2/go.mod h1:WAX/8DgncnokcFUldAxq7GeB5DXHDbMF+lLvDomNkRA=
|
||||
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
|
||||
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
|
||||
google.golang.org/protobuf v1.34.0 h1:Qo/qEd2RZPCf2nKuorzksSknv0d3ERwp1vFG38gSmH4=
|
||||
google.golang.org/protobuf v1.34.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
|
|
|
@ -14,6 +14,7 @@ import (
|
|||
|
||||
type APIConfig struct {
|
||||
Tag string `json:"tag"`
|
||||
Listen string `json:"listen"`
|
||||
Services []string `json:"services"`
|
||||
}
|
||||
|
||||
|
@ -42,6 +43,7 @@ func (c *APIConfig) Build() (*commander.Config, error) {
|
|||
|
||||
return &commander.Config{
|
||||
Tag: c.Tag,
|
||||
Listen: c.Listen,
|
||||
Service: services,
|
||||
}, nil
|
||||
}
|
||||
|
|
|
@ -73,8 +73,11 @@ func (c *NameServerConfig) Build() (*dns.NameServer, error) {
|
|||
var domains []*dns.NameServer_PriorityDomain
|
||||
var originalRules []*dns.NameServer_OriginalRule
|
||||
|
||||
cache := NewGeoCache()
|
||||
defer cache.Clear()
|
||||
|
||||
for _, rule := range c.Domains {
|
||||
parsedDomain, err := parseDomainRule(rule)
|
||||
parsedDomain, err := cache.ParseDomainRule(rule)
|
||||
if err != nil {
|
||||
return nil, newError("invalid domain rule: ", rule).Base(err)
|
||||
}
|
||||
|
@ -91,7 +94,7 @@ func (c *NameServerConfig) Build() (*dns.NameServer, error) {
|
|||
})
|
||||
}
|
||||
|
||||
geoipList, err := ToCidrList(c.ExpectIPs)
|
||||
geoipList, err := cache.ToCidrList(c.ExpectIPs)
|
||||
if err != nil {
|
||||
return nil, newError("invalid IP rule: ", c.ExpectIPs).Base(err)
|
||||
}
|
||||
|
@ -209,6 +212,9 @@ func (m *HostsWrapper) Build() ([]*dns.Config_HostMapping, error) {
|
|||
}
|
||||
sort.Strings(domains)
|
||||
|
||||
cache := newSiteCache()
|
||||
defer cache.reset()
|
||||
|
||||
for _, domain := range domains {
|
||||
switch {
|
||||
case strings.HasPrefix(domain, "domain:"):
|
||||
|
@ -226,7 +232,7 @@ func (m *HostsWrapper) Build() ([]*dns.Config_HostMapping, error) {
|
|||
if len(listName) == 0 {
|
||||
return nil, newError("empty geosite rule: ", domain)
|
||||
}
|
||||
geositeList, err := loadGeositeWithAttr("geosite.dat", listName)
|
||||
geositeList, err := cache.loadGeositeWithAttr("geosite.dat", listName)
|
||||
if err != nil {
|
||||
return nil, newError("failed to load geosite: ", listName).Base(err)
|
||||
}
|
||||
|
@ -287,7 +293,7 @@ func (m *HostsWrapper) Build() ([]*dns.Config_HostMapping, error) {
|
|||
}
|
||||
filename := kv[0]
|
||||
list := kv[1]
|
||||
geositeList, err := loadGeositeWithAttr(filename, list)
|
||||
geositeList, err := cache.loadGeositeWithAttr(filename, list)
|
||||
if err != nil {
|
||||
return nil, newError("failed to load domain list: ", list, " from ", filename).Base(err)
|
||||
}
|
||||
|
|
|
@ -116,8 +116,11 @@ func (c *RouterConfig) Build() (*router.Config, error) {
|
|||
}
|
||||
}
|
||||
|
||||
cache := NewGeoCache()
|
||||
defer cache.Clear()
|
||||
|
||||
for _, rawRule := range rawRuleList {
|
||||
rule, err := ParseRule(rawRule)
|
||||
rule, err := cache.ParseRule(rawRule)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -195,39 +198,80 @@ func ParseIP(s string) (*router.CIDR, error) {
|
|||
}
|
||||
}
|
||||
|
||||
func loadGeoIP(code string) ([]*router.CIDR, error) {
|
||||
return loadIP("geoip.dat", code)
|
||||
}
|
||||
|
||||
var (
|
||||
FileCache = make(map[string][]byte)
|
||||
IPCache = make(map[string]*router.GeoIP)
|
||||
SiteCache = make(map[string]*router.GeoSite)
|
||||
)
|
||||
|
||||
func loadFile(file string) ([]byte, error) {
|
||||
if FileCache[file] == nil {
|
||||
bs, err := filesystem.ReadAsset(file)
|
||||
if err != nil {
|
||||
return nil, newError("failed to open file: ", file).Base(err)
|
||||
}
|
||||
if len(bs) == 0 {
|
||||
return nil, newError("empty file: ", file)
|
||||
}
|
||||
// Do not cache file, may save RAM when there
|
||||
// are many files, but consume CPU each time.
|
||||
return bs, nil
|
||||
FileCache[file] = bs
|
||||
bs, err := filesystem.ReadAsset(file)
|
||||
if err != nil {
|
||||
return nil, newError("failed to open file: ", file).Base(err)
|
||||
}
|
||||
return FileCache[file], nil
|
||||
if len(bs) == 0 {
|
||||
return nil, newError("empty file: ", file)
|
||||
}
|
||||
return bs, nil
|
||||
}
|
||||
|
||||
func loadIP(file, code string) ([]*router.CIDR, error) {
|
||||
type ipCache struct {
|
||||
fileContent map[string][]byte
|
||||
geoIPs map[string]*router.GeoIP
|
||||
}
|
||||
|
||||
type siteCache struct {
|
||||
fileContent map[string][]byte
|
||||
geoSites map[string]*router.GeoSite
|
||||
}
|
||||
|
||||
// GeoCache is a cache for GeoIP and GeoSite.
|
||||
type GeoCache struct {
|
||||
ipCache *ipCache
|
||||
siteCache *siteCache
|
||||
}
|
||||
|
||||
// NewGeoCache creates a new GeoCache.
|
||||
func NewGeoCache() *GeoCache {
|
||||
return &GeoCache{
|
||||
ipCache: newIPCache(),
|
||||
siteCache: newSiteCache(),
|
||||
}
|
||||
}
|
||||
|
||||
// Clear clears the cache.
|
||||
func (c *GeoCache) Clear() {
|
||||
if c.ipCache.reset() || c.siteCache.reset() {
|
||||
// only trigger GC if there is something to release
|
||||
runtime.GC()
|
||||
}
|
||||
}
|
||||
|
||||
// newIPCache creates a new ipCache.
|
||||
// after use, call close() to release memory.
|
||||
func newIPCache() *ipCache {
|
||||
return &ipCache{
|
||||
fileContent: make(map[string][]byte),
|
||||
geoIPs: make(map[string]*router.GeoIP),
|
||||
}
|
||||
}
|
||||
|
||||
// reset remove the content of ipCache.
|
||||
// returns true if the cache is not empty.
|
||||
func (c *ipCache) reset() bool {
|
||||
if len(c.fileContent) == 0 && len(c.geoIPs) == 0 {
|
||||
return false
|
||||
}
|
||||
c.fileContent = make(map[string][]byte)
|
||||
c.geoIPs = make(map[string]*router.GeoIP)
|
||||
return true
|
||||
}
|
||||
|
||||
func (c *ipCache) loadIP(file, code string) ([]*router.CIDR, error) {
|
||||
index := file + ":" + code
|
||||
if IPCache[index] == nil {
|
||||
bs, err := loadFile(file)
|
||||
if err != nil {
|
||||
return nil, newError("failed to load file: ", file).Base(err)
|
||||
if c.geoIPs[index] == nil {
|
||||
bs := c.fileContent[file]
|
||||
if bs == nil {
|
||||
var err error
|
||||
bs, err = loadFile(file)
|
||||
if err != nil {
|
||||
return nil, newError("failed to load file: ", file).Base(err)
|
||||
}
|
||||
c.fileContent[file] = bs
|
||||
}
|
||||
bs = find(bs, []byte(code))
|
||||
if bs == nil {
|
||||
|
@ -237,19 +281,43 @@ func loadIP(file, code string) ([]*router.CIDR, error) {
|
|||
if err := proto.Unmarshal(bs, &geoip); err != nil {
|
||||
return nil, newError("error unmarshal IP in ", file, ": ", code).Base(err)
|
||||
}
|
||||
defer runtime.GC() // or debug.FreeOSMemory()
|
||||
return geoip.Cidr, nil // do not cache geoip
|
||||
IPCache[index] = &geoip
|
||||
c.geoIPs[index] = &geoip
|
||||
return geoip.Cidr, nil
|
||||
}
|
||||
return IPCache[index].Cidr, nil
|
||||
return c.geoIPs[index].Cidr, nil
|
||||
}
|
||||
|
||||
func loadSite(file, code string) ([]*router.Domain, error) {
|
||||
// newSiteCache creates a new siteCache.
|
||||
// after use, call close() to release memory.
|
||||
func newSiteCache() *siteCache {
|
||||
return &siteCache{
|
||||
fileContent: make(map[string][]byte),
|
||||
geoSites: make(map[string]*router.GeoSite),
|
||||
}
|
||||
}
|
||||
|
||||
// reset remove the content of siteCache.
|
||||
// returns true if the cache is not empty.
|
||||
func (c *siteCache) reset() bool {
|
||||
if len(c.fileContent) == 0 && len(c.geoSites) == 0 {
|
||||
return false
|
||||
}
|
||||
c.fileContent = make(map[string][]byte)
|
||||
c.geoSites = make(map[string]*router.GeoSite)
|
||||
return true
|
||||
}
|
||||
|
||||
func (c *siteCache) loadSite(file, code string) ([]*router.Domain, error) {
|
||||
index := file + ":" + code
|
||||
if SiteCache[index] == nil {
|
||||
bs, err := loadFile(file)
|
||||
if err != nil {
|
||||
return nil, newError("failed to load file: ", file).Base(err)
|
||||
if c.geoSites[index] == nil {
|
||||
bs := c.fileContent[file]
|
||||
if bs == nil {
|
||||
var err error
|
||||
bs, err = loadFile(file)
|
||||
if err != nil {
|
||||
return nil, newError("failed to load file: ", file).Base(err)
|
||||
}
|
||||
c.fileContent[file] = bs
|
||||
}
|
||||
bs = find(bs, []byte(code))
|
||||
if bs == nil {
|
||||
|
@ -259,11 +327,10 @@ func loadSite(file, code string) ([]*router.Domain, error) {
|
|||
if err := proto.Unmarshal(bs, &geosite); err != nil {
|
||||
return nil, newError("error unmarshal Site in ", file, ": ", code).Base(err)
|
||||
}
|
||||
defer runtime.GC() // or debug.FreeOSMemory()
|
||||
return geosite.Domain, nil // do not cache geosite
|
||||
SiteCache[index] = &geosite
|
||||
c.geoSites[index] = &geosite
|
||||
return geosite.Domain, nil
|
||||
}
|
||||
return SiteCache[index].Domain, nil
|
||||
return c.geoSites[index].Domain, nil
|
||||
}
|
||||
|
||||
func DecodeVarint(buf []byte) (x uint64, n int) {
|
||||
|
@ -358,14 +425,14 @@ func parseAttrs(attrs []string) *AttributeList {
|
|||
return al
|
||||
}
|
||||
|
||||
func loadGeositeWithAttr(file string, siteWithAttr string) ([]*router.Domain, error) {
|
||||
func (c *siteCache) loadGeositeWithAttr(file string, siteWithAttr string) ([]*router.Domain, error) {
|
||||
parts := strings.Split(siteWithAttr, "@")
|
||||
if len(parts) == 0 {
|
||||
return nil, newError("empty site")
|
||||
}
|
||||
country := strings.ToUpper(parts[0])
|
||||
attrs := parseAttrs(parts[1:])
|
||||
domains, err := loadSite(file, country)
|
||||
domains, err := c.loadSite(file, country)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -384,10 +451,10 @@ func loadGeositeWithAttr(file string, siteWithAttr string) ([]*router.Domain, er
|
|||
return filteredDomains, nil
|
||||
}
|
||||
|
||||
func parseDomainRule(domain string) ([]*router.Domain, error) {
|
||||
func (c *GeoCache) ParseDomainRule(domain string) ([]*router.Domain, error) {
|
||||
if strings.HasPrefix(domain, "geosite:") {
|
||||
country := strings.ToUpper(domain[8:])
|
||||
domains, err := loadGeositeWithAttr("geosite.dat", country)
|
||||
domains, err := c.siteCache.loadGeositeWithAttr("geosite.dat", country)
|
||||
if err != nil {
|
||||
return nil, newError("failed to load geosite: ", country).Base(err)
|
||||
}
|
||||
|
@ -411,7 +478,7 @@ func parseDomainRule(domain string) ([]*router.Domain, error) {
|
|||
}
|
||||
filename := kv[0]
|
||||
country := kv[1]
|
||||
domains, err := loadGeositeWithAttr(filename, country)
|
||||
domains, err := c.siteCache.loadGeositeWithAttr(filename, country)
|
||||
if err != nil {
|
||||
return nil, newError("failed to load external sites: ", country, " from ", filename).Base(err)
|
||||
}
|
||||
|
@ -454,7 +521,7 @@ func parseDomainRule(domain string) ([]*router.Domain, error) {
|
|||
return []*router.Domain{domainRule}, nil
|
||||
}
|
||||
|
||||
func ToCidrList(ips StringList) ([]*router.GeoIP, error) {
|
||||
func (c *GeoCache) ToCidrList(ips StringList) ([]*router.GeoIP, error) {
|
||||
var geoipList []*router.GeoIP
|
||||
var customCidrs []*router.CIDR
|
||||
|
||||
|
@ -469,7 +536,7 @@ func ToCidrList(ips StringList) ([]*router.GeoIP, error) {
|
|||
if len(country) == 0 {
|
||||
return nil, newError("empty country name in rule")
|
||||
}
|
||||
geoip, err := loadGeoIP(strings.ToUpper(country))
|
||||
geoip, err := c.ipCache.loadIP("geoip.dat", strings.ToUpper(country))
|
||||
if err != nil {
|
||||
return nil, newError("failed to load GeoIP: ", country).Base(err)
|
||||
}
|
||||
|
@ -509,7 +576,7 @@ func ToCidrList(ips StringList) ([]*router.GeoIP, error) {
|
|||
country = country[1:]
|
||||
isReverseMatch = true
|
||||
}
|
||||
geoip, err := loadIP(filename, strings.ToUpper(country))
|
||||
geoip, err := c.ipCache.loadIP(filename, strings.ToUpper(country))
|
||||
if err != nil {
|
||||
return nil, newError("failed to load IPs: ", country, " from ", filename).Base(err)
|
||||
}
|
||||
|
@ -539,7 +606,7 @@ func ToCidrList(ips StringList) ([]*router.GeoIP, error) {
|
|||
return geoipList, nil
|
||||
}
|
||||
|
||||
func parseFieldRule(msg json.RawMessage) (*router.RoutingRule, error) {
|
||||
func (c *GeoCache) parseFieldRule(msg json.RawMessage) (*router.RoutingRule, error) {
|
||||
type RawFieldRule struct {
|
||||
RouterRule
|
||||
Domain *StringList `json:"domain"`
|
||||
|
@ -581,7 +648,7 @@ func parseFieldRule(msg json.RawMessage) (*router.RoutingRule, error) {
|
|||
|
||||
if rawFieldRule.Domain != nil {
|
||||
for _, domain := range *rawFieldRule.Domain {
|
||||
rules, err := parseDomainRule(domain)
|
||||
rules, err := c.ParseDomainRule(domain)
|
||||
if err != nil {
|
||||
return nil, newError("failed to parse domain rule: ", domain).Base(err)
|
||||
}
|
||||
|
@ -591,7 +658,7 @@ func parseFieldRule(msg json.RawMessage) (*router.RoutingRule, error) {
|
|||
|
||||
if rawFieldRule.Domains != nil {
|
||||
for _, domain := range *rawFieldRule.Domains {
|
||||
rules, err := parseDomainRule(domain)
|
||||
rules, err := c.ParseDomainRule(domain)
|
||||
if err != nil {
|
||||
return nil, newError("failed to parse domain rule: ", domain).Base(err)
|
||||
}
|
||||
|
@ -600,7 +667,7 @@ func parseFieldRule(msg json.RawMessage) (*router.RoutingRule, error) {
|
|||
}
|
||||
|
||||
if rawFieldRule.IP != nil {
|
||||
geoipList, err := ToCidrList(*rawFieldRule.IP)
|
||||
geoipList, err := c.ToCidrList(*rawFieldRule.IP)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -616,7 +683,7 @@ func parseFieldRule(msg json.RawMessage) (*router.RoutingRule, error) {
|
|||
}
|
||||
|
||||
if rawFieldRule.SourceIP != nil {
|
||||
geoipList, err := ToCidrList(*rawFieldRule.SourceIP)
|
||||
geoipList, err := c.ToCidrList(*rawFieldRule.SourceIP)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -652,14 +719,14 @@ func parseFieldRule(msg json.RawMessage) (*router.RoutingRule, error) {
|
|||
return rule, nil
|
||||
}
|
||||
|
||||
func ParseRule(msg json.RawMessage) (*router.RoutingRule, error) {
|
||||
func (c *GeoCache) ParseRule(msg json.RawMessage) (*router.RoutingRule, error) {
|
||||
rawRule := new(RouterRule)
|
||||
err := json.Unmarshal(msg, rawRule)
|
||||
if err != nil {
|
||||
return nil, newError("invalid router rule").Base(err)
|
||||
}
|
||||
if rawRule.Type == "" || strings.EqualFold(rawRule.Type, "field") {
|
||||
fieldrule, err := parseFieldRule(msg)
|
||||
fieldrule, err := c.parseFieldRule(msg)
|
||||
if err != nil {
|
||||
return nil, newError("invalid field rule").Base(err)
|
||||
}
|
||||
|
|
|
@ -44,7 +44,10 @@ func TestToCidrList(t *testing.T) {
|
|||
"ext-ip:geoiptestrouter.dat:!ca",
|
||||
})
|
||||
|
||||
_, err := ToCidrList(ips)
|
||||
cache := NewGeoCache()
|
||||
defer cache.Clear()
|
||||
|
||||
_, err := cache.ToCidrList(ips)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to parse geoip list, got %s", err)
|
||||
}
|
||||
|
|
|
@ -33,6 +33,80 @@ import (
|
|||
"google.golang.org/grpc/credentials/insecure"
|
||||
)
|
||||
|
||||
func TestCommanderListenConfigurationItem(t *testing.T) {
|
||||
tcpServer := tcp.Server{
|
||||
MsgProcessor: xor,
|
||||
}
|
||||
dest, err := tcpServer.Start()
|
||||
common.Must(err)
|
||||
defer tcpServer.Close()
|
||||
|
||||
clientPort := tcp.PickPort()
|
||||
cmdPort := tcp.PickPort()
|
||||
clientConfig := &core.Config{
|
||||
App: []*serial.TypedMessage{
|
||||
serial.ToTypedMessage(&commander.Config{
|
||||
Tag: "api",
|
||||
Listen: fmt.Sprintf("127.0.0.1:%d", cmdPort),
|
||||
Service: []*serial.TypedMessage{
|
||||
serial.ToTypedMessage(&command.Config{}),
|
||||
},
|
||||
}),
|
||||
},
|
||||
Inbound: []*core.InboundHandlerConfig{
|
||||
{
|
||||
Tag: "d",
|
||||
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
|
||||
PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(clientPort)}},
|
||||
Listen: net.NewIPOrDomain(net.LocalHostIP),
|
||||
}),
|
||||
ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
|
||||
Address: net.NewIPOrDomain(dest.Address),
|
||||
Port: uint32(dest.Port),
|
||||
Networks: []net.Network{net.Network_TCP},
|
||||
}),
|
||||
},
|
||||
},
|
||||
Outbound: []*core.OutboundHandlerConfig{
|
||||
{
|
||||
Tag: "default-outbound",
|
||||
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
servers, err := InitializeServerConfigs(clientConfig)
|
||||
common.Must(err)
|
||||
defer CloseAllServers(servers)
|
||||
|
||||
if err := testTCPConn(clientPort, 1024, time.Second*5)(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
cmdConn, err := grpc.Dial(fmt.Sprintf("127.0.0.1:%d", cmdPort), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithBlock())
|
||||
common.Must(err)
|
||||
defer cmdConn.Close()
|
||||
|
||||
hsClient := command.NewHandlerServiceClient(cmdConn)
|
||||
resp, err := hsClient.RemoveInbound(context.Background(), &command.RemoveInboundRequest{
|
||||
Tag: "d",
|
||||
})
|
||||
common.Must(err)
|
||||
if resp == nil {
|
||||
t.Error("unexpected nil response")
|
||||
}
|
||||
|
||||
{
|
||||
_, err := net.DialTCP("tcp", nil, &net.TCPAddr{
|
||||
IP: []byte{127, 0, 0, 1},
|
||||
Port: int(clientPort),
|
||||
})
|
||||
if err == nil {
|
||||
t.Error("unexpected nil error")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCommanderRemoveHandler(t *testing.T) {
|
||||
tcpServer := tcp.Server{
|
||||
MsgProcessor: xor,
|
||||
|
|
Loading…
Reference in New Issue