chore: 暴露数据给前端

This commit is contained in:
gVisor bot 2022-05-17 16:47:21 +08:00
parent c260affd46
commit cae1e23735
5 changed files with 57 additions and 13 deletions

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"errors" "errors"
"github.com/gofrs/uuid"
"net" "net"
"github.com/Dreamacro/clash/component/dialer" "github.com/Dreamacro/clash/component/dialer"
@ -17,6 +18,7 @@ type Base struct {
tp C.AdapterType tp C.AdapterType
udp bool udp bool
rmark int rmark int
id string
} }
// Name implements C.ProxyAdapter // Name implements C.ProxyAdapter
@ -24,6 +26,20 @@ func (b *Base) Name() string {
return b.name return b.name
} }
// Id implements C.ProxyAdapter
func (b *Base) Id() string {
if b.id == "" {
id, err := uuid.NewV6()
if err != nil {
b.id = b.name
} else {
b.id = id.String()
}
}
return b.id
}
// Type implements C.ProxyAdapter // Type implements C.ProxyAdapter
func (b *Base) Type() C.AdapterType { func (b *Base) Type() C.AdapterType {
return b.tp return b.tp
@ -58,6 +74,7 @@ func (b *Base) SupportUDP() bool {
func (b *Base) MarshalJSON() ([]byte, error) { func (b *Base) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]string{ return json.Marshal(map[string]string{
"type": b.Type().String(), "type": b.Type().String(),
"id": b.Id(),
}) })
} }

View File

@ -46,3 +46,11 @@ func (re *RuleExtra) NotMatchProcessName(processName string) bool {
type RuleGeoSite interface { type RuleGeoSite interface {
GetDomainMatcher() *router.DomainMatcher GetDomainMatcher() *router.DomainMatcher
} }
type RuleGeoIP interface {
GetIPMatcher() *router.GeoIPMatcher
}
type RuleGroup interface {
GetRecodeSize() int
}

View File

@ -1,6 +1,7 @@
package route package route
import ( import (
"github.com/Dreamacro/clash/constant"
"net/http" "net/http"
"github.com/Dreamacro/clash/tunnel" "github.com/Dreamacro/clash/tunnel"
@ -19,17 +20,23 @@ type Rule struct {
Type string `json:"type"` Type string `json:"type"`
Payload string `json:"payload"` Payload string `json:"payload"`
Proxy string `json:"proxy"` Proxy string `json:"proxy"`
Size int `json:"Size"`
} }
func getRules(w http.ResponseWriter, r *http.Request) { func getRules(w http.ResponseWriter, r *http.Request) {
rawRules := tunnel.Rules() rawRules := tunnel.Rules()
rules := []Rule{} rules := []Rule{}
for _, rule := range rawRules { for _, rule := range rawRules {
rules = append(rules, Rule{ r := Rule{
Type: rule.RuleType().String(), Type: rule.RuleType().String(),
Payload: rule.Payload(), Payload: rule.Payload(),
Proxy: rule.Adapter(), Proxy: rule.Adapter(),
}) Size: -1,
}
if rule.RuleType() == constant.GEOIP || rule.RuleType() == constant.GEOSITE {
r.Size = rule.(constant.RuleGroup).GetRecodeSize()
}
rules = append(rules, r)
} }

View File

@ -18,6 +18,7 @@ type GEOIP struct {
adapter string adapter string
noResolveIP bool noResolveIP bool
geoIPMatcher *router.GeoIPMatcher geoIPMatcher *router.GeoIPMatcher
recodeSize int
} }
func (g *GEOIP) RuleType() C.RuleType { func (g *GEOIP) RuleType() C.RuleType {
@ -65,6 +66,10 @@ func (g *GEOIP) GetIPMatcher() *router.GeoIPMatcher {
return g.geoIPMatcher return g.geoIPMatcher
} }
func (g *GEOIP) GetRecodeSize() int {
return g.recodeSize
}
func NewGEOIP(country string, adapter string, noResolveIP bool) (*GEOIP, error) { func NewGEOIP(country string, adapter string, noResolveIP bool) (*GEOIP, error) {
if !C.GeodataMode { if !C.GeodataMode {
geoip := &GEOIP{ geoip := &GEOIP{
@ -76,18 +81,19 @@ func NewGEOIP(country string, adapter string, noResolveIP bool) (*GEOIP, error)
return geoip, nil return geoip, nil
} }
geoIPMatcher, recordsCount, err := geodata.LoadGeoIPMatcher(country) geoIPMatcher, size, err := geodata.LoadGeoIPMatcher(country)
if err != nil { if err != nil {
return nil, fmt.Errorf("[GeoIP] %s", err.Error()) return nil, fmt.Errorf("[GeoIP] %s", err.Error())
} }
log.Infoln("Start initial GeoIP rule %s => %s, records: %d", country, adapter, recordsCount) log.Infoln("Start initial GeoIP rule %s => %s, records: %d", country, adapter, size)
geoip := &GEOIP{ geoip := &GEOIP{
Base: &Base{}, Base: &Base{},
country: country, country: country,
adapter: adapter, adapter: adapter,
noResolveIP: noResolveIP, noResolveIP: noResolveIP,
geoIPMatcher: geoIPMatcher, geoIPMatcher: geoIPMatcher,
recodeSize: size,
} }
return geoip, nil return geoip, nil
} }

View File

@ -14,9 +14,10 @@ import (
type GEOSITE struct { type GEOSITE struct {
*Base *Base
country string country string
adapter string adapter string
matcher *router.DomainMatcher matcher *router.DomainMatcher
recodeSize int
} }
func (gs *GEOSITE) RuleType() C.RuleType { func (gs *GEOSITE) RuleType() C.RuleType {
@ -44,19 +45,24 @@ func (gs *GEOSITE) GetDomainMatcher() *router.DomainMatcher {
return gs.matcher return gs.matcher
} }
func (gs *GEOSITE) GetRecodeSize() int {
return gs.recodeSize
}
func NewGEOSITE(country string, adapter string) (*GEOSITE, error) { func NewGEOSITE(country string, adapter string) (*GEOSITE, error) {
matcher, recordsCount, err := geodata.LoadGeoSiteMatcher(country) matcher, size, err := geodata.LoadGeoSiteMatcher(country)
if err != nil { if err != nil {
return nil, fmt.Errorf("load GeoSite data error, %s", err.Error()) return nil, fmt.Errorf("load GeoSite data error, %s", err.Error())
} }
log.Infoln("Start initial GeoSite rule %s => %s, records: %d", country, adapter, recordsCount) log.Infoln("Start initial GeoSite rule %s => %s, records: %d", country, adapter, size)
geoSite := &GEOSITE{ geoSite := &GEOSITE{
Base: &Base{}, Base: &Base{},
country: country, country: country,
adapter: adapter, adapter: adapter,
matcher: matcher, matcher: matcher,
recodeSize: size,
} }
return geoSite, nil return geoSite, nil