Feature: add mode script
This commit is contained in:
163
config/config.go
163
config/config.go
@ -6,6 +6,7 @@ import (
|
||||
"net"
|
||||
"net/url"
|
||||
"os"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
@ -15,6 +16,7 @@ import (
|
||||
"github.com/Dreamacro/clash/adapter/provider"
|
||||
"github.com/Dreamacro/clash/component/auth"
|
||||
"github.com/Dreamacro/clash/component/fakeip"
|
||||
S "github.com/Dreamacro/clash/component/script"
|
||||
"github.com/Dreamacro/clash/component/trie"
|
||||
C "github.com/Dreamacro/clash/constant"
|
||||
providerTypes "github.com/Dreamacro/clash/constant/provider"
|
||||
@ -92,21 +94,28 @@ type Tun struct {
|
||||
AutoRoute bool `yaml:"auto-route" json:"auto-route"`
|
||||
}
|
||||
|
||||
// Script config
|
||||
type Script struct {
|
||||
MainCode string `yaml:"code" json:"code"`
|
||||
ShortcutsCode map[string]string `yaml:"shortcuts" json:"shortcuts"`
|
||||
}
|
||||
|
||||
// Experimental config
|
||||
type Experimental struct{}
|
||||
|
||||
// Config is clash config manager
|
||||
type Config struct {
|
||||
General *General
|
||||
Tun *Tun
|
||||
DNS *DNS
|
||||
Experimental *Experimental
|
||||
Hosts *trie.DomainTrie
|
||||
Profile *Profile
|
||||
Rules []C.Rule
|
||||
Users []auth.AuthUser
|
||||
Proxies map[string]C.Proxy
|
||||
Providers map[string]providerTypes.ProxyProvider
|
||||
General *General
|
||||
Tun *Tun
|
||||
DNS *DNS
|
||||
Experimental *Experimental
|
||||
Hosts *trie.DomainTrie
|
||||
Profile *Profile
|
||||
Rules []C.Rule
|
||||
RuleProviders map[string]C.Rule
|
||||
Users []auth.AuthUser
|
||||
Proxies map[string]C.Proxy
|
||||
Providers map[string]providerTypes.ProxyProvider
|
||||
}
|
||||
|
||||
type RawDNS struct {
|
||||
@ -157,6 +166,7 @@ type RawConfig struct {
|
||||
Proxy []map[string]interface{} `yaml:"proxies"`
|
||||
ProxyGroup []map[string]interface{} `yaml:"proxy-groups"`
|
||||
Rule []string `yaml:"rules"`
|
||||
Script Script `yaml:"script"`
|
||||
}
|
||||
|
||||
// Parse config
|
||||
@ -204,6 +214,10 @@ func UnmarshalRawConfig(buf []byte) (*RawConfig, error) {
|
||||
Profile: Profile{
|
||||
StoreSelected: true,
|
||||
},
|
||||
Script: Script{
|
||||
MainCode: "",
|
||||
ShortcutsCode: map[string]string{},
|
||||
},
|
||||
}
|
||||
|
||||
if err := yaml.Unmarshal(buf, rawCfg); err != nil {
|
||||
@ -232,11 +246,17 @@ func ParseRawConfig(rawCfg *RawConfig) (*Config, error) {
|
||||
config.Proxies = proxies
|
||||
config.Providers = providers
|
||||
|
||||
rules, err := parseRules(rawCfg, proxies)
|
||||
err = parseScript(rawCfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rules, ruleProviders, err := parseRules(rawCfg, proxies)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
config.Rules = rules
|
||||
config.RuleProviders = ruleProviders
|
||||
|
||||
hosts, err := parseHosts(rawCfg)
|
||||
if err != nil {
|
||||
@ -396,9 +416,80 @@ func parseProxies(cfg *RawConfig) (proxies map[string]C.Proxy, providersMap map[
|
||||
return proxies, providersMap, nil
|
||||
}
|
||||
|
||||
func parseRules(cfg *RawConfig, proxies map[string]C.Proxy) ([]C.Rule, error) {
|
||||
func parseScript(cfg *RawConfig) error {
|
||||
mode := cfg.Mode
|
||||
script := cfg.Script
|
||||
mainCode := cleanPyKeywords(script.MainCode)
|
||||
shortcutsCode := script.ShortcutsCode
|
||||
|
||||
if mode != T.Script && len(shortcutsCode) == 0 {
|
||||
return nil
|
||||
} else if mode == T.Script && len(mainCode) == 0 {
|
||||
return fmt.Errorf("initialized script module failure, can't find script code in the config file")
|
||||
}
|
||||
|
||||
content :=
|
||||
`# -*- coding: UTF-8 -*-
|
||||
|
||||
from datetime import datetime as whatever
|
||||
|
||||
class ClashTime:
|
||||
def now(self):
|
||||
return whatever.now()
|
||||
|
||||
def unix(self):
|
||||
return int(whatever.now().timestamp())
|
||||
|
||||
def unix_nano(self):
|
||||
return int(round(whatever.now().timestamp() * 1000))
|
||||
|
||||
time = ClashTime()
|
||||
|
||||
`
|
||||
|
||||
var shouldInitPy bool
|
||||
if mode == T.Script {
|
||||
content += mainCode + "\n\n"
|
||||
shouldInitPy = true
|
||||
}
|
||||
|
||||
for k, v := range shortcutsCode {
|
||||
v = cleanPyKeywords(v)
|
||||
v = strings.TrimSpace(v)
|
||||
if len(v) == 0 {
|
||||
return fmt.Errorf("initialized rule SCRIPT failure, shortcut [%s] code invalid syntax", k)
|
||||
}
|
||||
|
||||
content += "def " + strings.ToLower(k) + "(ctx, network, process_name, host, src_ip, src_port, dst_ip, dst_port):\n return " + v + "\n\n"
|
||||
shouldInitPy = true
|
||||
}
|
||||
|
||||
if !shouldInitPy {
|
||||
return nil
|
||||
}
|
||||
|
||||
err := os.WriteFile(C.Path.Script(), []byte(content), 0644)
|
||||
if err != nil {
|
||||
return fmt.Errorf("initialized script module failure, %s", err.Error())
|
||||
}
|
||||
|
||||
if err = S.Py_Initialize(C.Path.ScriptDir()); err != nil {
|
||||
return fmt.Errorf("initialized script module failure, %s", err.Error())
|
||||
} else {
|
||||
log.Infoln("Start initial script module successful")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func parseRules(cfg *RawConfig, proxies map[string]C.Proxy) ([]C.Rule, map[string]C.Rule, error) {
|
||||
rules := []C.Rule{}
|
||||
ruleProviders := map[string]C.Rule{}
|
||||
rulesConfig := cfg.Rule
|
||||
mode := cfg.Mode
|
||||
|
||||
providerNames := []string{}
|
||||
isPyInit := S.Py_IsInitialized()
|
||||
|
||||
// parse rules
|
||||
for idx, line := range rulesConfig {
|
||||
@ -410,6 +501,10 @@ func parseRules(cfg *RawConfig, proxies map[string]C.Proxy) ([]C.Rule, error) {
|
||||
ruleName = strings.ToUpper(rule[0])
|
||||
)
|
||||
|
||||
if mode == T.Script && ruleName != "GEOSITE" {
|
||||
continue
|
||||
}
|
||||
|
||||
switch l := len(rule); {
|
||||
case l == 2:
|
||||
target = rule[1]
|
||||
@ -427,11 +522,11 @@ func parseRules(cfg *RawConfig, proxies map[string]C.Proxy) ([]C.Rule, error) {
|
||||
target = rule[2]
|
||||
params = rule[3:]
|
||||
default:
|
||||
return nil, fmt.Errorf("rules[%d] [%s] error: format invalid", idx, line)
|
||||
return nil, nil, fmt.Errorf("rules[%d] [%s] error: format invalid", idx, line)
|
||||
}
|
||||
|
||||
if _, ok := proxies[target]; !ok {
|
||||
return nil, fmt.Errorf("rules[%d] [%s] error: proxy [%s] not found", idx, line, target)
|
||||
if _, ok := proxies[target]; mode != T.Script && !ok {
|
||||
return nil, nil, fmt.Errorf("rules[%d] [%s] error: proxy [%s] not found", idx, line, target)
|
||||
}
|
||||
|
||||
//rule = trimArr(rule)
|
||||
@ -439,15 +534,34 @@ func parseRules(cfg *RawConfig, proxies map[string]C.Proxy) ([]C.Rule, error) {
|
||||
|
||||
parsed, parseErr := R.ParseRule(ruleName, payload, target, params)
|
||||
if parseErr != nil {
|
||||
return nil, fmt.Errorf("rules[%d] [%s] error: %s", idx, line, parseErr.Error())
|
||||
return nil, nil, fmt.Errorf("rules[%d] [%s] error: %s", idx, line, parseErr.Error())
|
||||
}
|
||||
|
||||
rules = append(rules, parsed)
|
||||
if isPyInit {
|
||||
if ruleName == "GEOSITE" {
|
||||
pvName := "geosite:" + strings.ToLower(payload)
|
||||
providerNames = append(providerNames, pvName)
|
||||
ruleProviders[pvName] = parsed
|
||||
}
|
||||
}
|
||||
|
||||
if mode != T.Script {
|
||||
rules = append(rules, parsed)
|
||||
}
|
||||
}
|
||||
|
||||
runtime.GC()
|
||||
|
||||
return rules, nil
|
||||
if isPyInit {
|
||||
err := S.NewClashPyContext(providerNames)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
} else {
|
||||
log.Infoln("Start initial script context successful")
|
||||
}
|
||||
}
|
||||
|
||||
return rules, ruleProviders, nil
|
||||
}
|
||||
|
||||
func parseHosts(cfg *RawConfig) (*trie.DomainTrie, error) {
|
||||
@ -657,3 +771,16 @@ func parseAuthentication(rawRecords []string) []auth.AuthUser {
|
||||
}
|
||||
return users
|
||||
}
|
||||
|
||||
func cleanPyKeywords(code string) string {
|
||||
if len(code) == 0 {
|
||||
return code
|
||||
}
|
||||
keywords := []string{"import", "print"}
|
||||
|
||||
for _, kw := range keywords {
|
||||
reg := regexp.MustCompile("(?m)[\r\n]+^.*" + kw + ".*$")
|
||||
code = reg.ReplaceAllString(code, "")
|
||||
}
|
||||
return code
|
||||
}
|
||||
|
@ -6,18 +6,19 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/Dreamacro/clash/component/mmdb"
|
||||
C "github.com/Dreamacro/clash/constant"
|
||||
"github.com/Dreamacro/clash/log"
|
||||
)
|
||||
|
||||
func downloadGeoIP(path string) (err error) {
|
||||
resp, err := http.Get("https://cdn.jsdelivr.net/gh/Loyalsoldier/v2ray-rules-dat@release/geoip.dat")
|
||||
func downloadMMDB(path string) (err error) {
|
||||
resp, err := http.Get("https://cdn.jsdelivr.net/gh/Loyalsoldier/geoip@release/Country.mmdb")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0644)
|
||||
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0o644)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -27,6 +28,45 @@ func downloadGeoIP(path string) (err error) {
|
||||
return err
|
||||
}
|
||||
|
||||
func initMMDB() error {
|
||||
if _, err := os.Stat(C.Path.MMDB()); os.IsNotExist(err) {
|
||||
log.Infoln("Can't find MMDB, start download")
|
||||
if err := downloadMMDB(C.Path.MMDB()); err != nil {
|
||||
return fmt.Errorf("can't download MMDB: %s", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
if !mmdb.Verify() {
|
||||
log.Warnln("MMDB invalid, remove and download")
|
||||
if err := os.Remove(C.Path.MMDB()); err != nil {
|
||||
return fmt.Errorf("can't remove invalid MMDB: %s", err.Error())
|
||||
}
|
||||
|
||||
if err := downloadMMDB(C.Path.MMDB()); err != nil {
|
||||
return fmt.Errorf("can't download MMDB: %s", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
//func downloadGeoIP(path string) (err error) {
|
||||
// resp, err := http.Get("https://cdn.jsdelivr.net/gh/Loyalsoldier/v2ray-rules-dat@release/geoip.dat")
|
||||
// if err != nil {
|
||||
// return
|
||||
// }
|
||||
// defer resp.Body.Close()
|
||||
//
|
||||
// f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0644)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// defer f.Close()
|
||||
// _, err = io.Copy(f, resp.Body)
|
||||
//
|
||||
// return err
|
||||
//}
|
||||
|
||||
func downloadGeoSite(path string) (err error) {
|
||||
resp, err := http.Get("https://cdn.jsdelivr.net/gh/Loyalsoldier/v2ray-rules-dat@release/geosite.dat")
|
||||
if err != nil {
|
||||
@ -44,17 +84,18 @@ func downloadGeoSite(path string) (err error) {
|
||||
return err
|
||||
}
|
||||
|
||||
func initGeoIP() error {
|
||||
if _, err := os.Stat(C.Path.GeoIP()); os.IsNotExist(err) {
|
||||
log.Infoln("Can't find GeoIP.dat, start download")
|
||||
if err := downloadGeoIP(C.Path.GeoIP()); err != nil {
|
||||
return fmt.Errorf("can't download GeoIP.dat: %s", err.Error())
|
||||
}
|
||||
log.Infoln("Download GeoIP.dat finish")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
//
|
||||
//func initGeoIP() error {
|
||||
// if _, err := os.Stat(C.Path.GeoIP()); os.IsNotExist(err) {
|
||||
// log.Infoln("Can't find GeoIP.dat, start download")
|
||||
// if err := downloadGeoIP(C.Path.GeoIP()); err != nil {
|
||||
// return fmt.Errorf("can't download GeoIP.dat: %s", err.Error())
|
||||
// }
|
||||
// log.Infoln("Download GeoIP.dat finish")
|
||||
// }
|
||||
//
|
||||
// return nil
|
||||
//}
|
||||
|
||||
func initGeoSite() error {
|
||||
if _, err := os.Stat(C.Path.GeoSite()); os.IsNotExist(err) {
|
||||
@ -88,9 +129,14 @@ func Init(dir string) error {
|
||||
f.Close()
|
||||
}
|
||||
|
||||
// initial GeoIP
|
||||
if err := initGeoIP(); err != nil {
|
||||
return fmt.Errorf("can't initial GeoIP: %w", err)
|
||||
//// initial GeoIP
|
||||
//if err := initGeoIP(); err != nil {
|
||||
// return fmt.Errorf("can't initial GeoIP: %w", err)
|
||||
//}
|
||||
|
||||
// initial mmdb
|
||||
if err := initMMDB(); err != nil {
|
||||
return fmt.Errorf("can't initial MMDB: %w", err)
|
||||
}
|
||||
|
||||
// initial GeoSite
|
||||
|
Reference in New Issue
Block a user