Feature: add path
to script config
script: path: ./script.star
This commit is contained in:
parent
663017a775
commit
3610d3dd84
@ -252,6 +252,7 @@ Script enables users to programmatically select a policy for the packets with mo
|
|||||||
mode: script
|
mode: script
|
||||||
|
|
||||||
script:
|
script:
|
||||||
|
# path: ./script.star
|
||||||
code: |
|
code: |
|
||||||
def main(ctx, metadata):
|
def main(ctx, metadata):
|
||||||
if metadata["process_name"] == 'apsd':
|
if metadata["process_name"] == 'apsd':
|
||||||
|
@ -112,6 +112,7 @@ type Tun struct {
|
|||||||
// Script config
|
// Script config
|
||||||
type Script struct {
|
type Script struct {
|
||||||
MainCode string `yaml:"code" json:"code"`
|
MainCode string `yaml:"code" json:"code"`
|
||||||
|
MainPath string `yaml:"path" json:"path"`
|
||||||
ShortcutsCode map[string]string `yaml:"shortcuts" json:"shortcuts"`
|
ShortcutsCode map[string]string `yaml:"shortcuts" json:"shortcuts"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -853,8 +854,26 @@ func parseAuthentication(rawRecords []string) []auth.AuthUser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func parseScript(script Script, rawRules []string) ([]string, error) {
|
func parseScript(script Script, rawRules []string) ([]string, error) {
|
||||||
mainCode := script.MainCode
|
var (
|
||||||
shortcutsCode := script.ShortcutsCode
|
path = script.MainPath
|
||||||
|
mainCode = script.MainCode
|
||||||
|
shortcutsCode = script.ShortcutsCode
|
||||||
|
)
|
||||||
|
|
||||||
|
if path != "" {
|
||||||
|
if !strings.HasSuffix(path, ".star") {
|
||||||
|
return nil, fmt.Errorf("initialized script file failure, script path [%s] invalid", path)
|
||||||
|
}
|
||||||
|
path = C.Path.Resolve(path)
|
||||||
|
if _, err := os.Stat(path); os.IsNotExist(err) {
|
||||||
|
return nil, fmt.Errorf("initialized script file failure, script path invalid: %w", err)
|
||||||
|
}
|
||||||
|
data, err := os.ReadFile(path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("initialized script file failure, read file error: %w", err)
|
||||||
|
}
|
||||||
|
mainCode = string(data)
|
||||||
|
}
|
||||||
|
|
||||||
if strings.TrimSpace(mainCode) == "" {
|
if strings.TrimSpace(mainCode) == "" {
|
||||||
mainCode = `
|
mainCode = `
|
||||||
@ -865,6 +884,10 @@ def main(ctx, metadata):
|
|||||||
mainCode = cleanPyKeywords(mainCode)
|
mainCode = cleanPyKeywords(mainCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !strings.Contains(mainCode, "def main(ctx, metadata):") {
|
||||||
|
return nil, fmt.Errorf(`initialized script code failure, the function 'def main(ctx, metadata):' is required`)
|
||||||
|
}
|
||||||
|
|
||||||
content := `# -*- coding: UTF-8 -*-
|
content := `# -*- coding: UTF-8 -*-
|
||||||
|
|
||||||
from datetime import datetime as whatever
|
from datetime import datetime as whatever
|
||||||
|
Reference in New Issue
Block a user