diff --git a/custom/custom.go b/custom/custom.go index 425320f..5d0cd9a 100644 --- a/custom/custom.go +++ b/custom/custom.go @@ -12,8 +12,6 @@ import ( var box *BoxService -var templateOptions = shared.ConfigTemplateOptions{IncludeTunInbound: false, IncludeMixedInbound: true, IncludeLogOutput: true} - //export setup func setup(baseDir *C.char, workingDir *C.char, tempDir *C.char) { Setup(C.GoString(baseDir), C.GoString(workingDir), C.GoString(tempDir)) diff --git a/shared/config.json.template b/shared/config.json.template new file mode 100644 index 0000000..3c4bdf4 --- /dev/null +++ b/shared/config.json.template @@ -0,0 +1,127 @@ +{ + "log": {}, + "dns": { + "servers": [ + { + "tag": "remote", + "address_resolver": "local", + "address": "tcp://1.1.1.1", + "strategy": "prefer_ipv4", + "detour": "select" + }, + { + "tag": "local", + "address": "local", + "detour": "direct" + } + ], + "rules": [ + { + "clash_mode": "global", + "server": "remote" + }, + { + "clash_mode": "direct", + "server": "local" + }, + { + "outbound": [ + "any" + ], + "server": "local" + }, + { + "geosite": "ir", + "server": "local" + } + ], + "strategy": "ipv4_only" + }, + "inbounds": [ + { + "type": "tun", + "inet4_address": "172.19.0.1/30", + "sniff": true, + "sniff_override_destination": true, + "domain_strategy": "ipv4_only", + "strict_route": true, + "mtu": 9000, + "endpoint_independent_nat": true, + "auto_route": true + }, + { + "type": "socks", + "tag": "socks-in", + "listen": "127.0.0.1", + "sniff": true, + "sniff_override_destination": true, + "domain_strategy": "ipv4_only", + "listen_port": 2333, + "users": [] + }, + { + "type": "mixed", + "tag": "mixed-in", + "sniff": true, + "sniff_override_destination": true, + "domain_strategy": "ipv4_only", + "listen": "127.0.0.1", + "listen_port": 2334, + "set_system_proxy": true, + "users": [] + } + ], + "outbounds": [ + { + "type": "direct", + "tag": "direct" + }, + { + "type": "block", + "tag": "block" + }, + { + "type": "dns", + "tag": "dns-out" + } + ], + "route": { + "rules": [ + { + "geosite": "category-ads-all", + "outbound": "block" + }, + { + "protocol": "dns", + "outbound": "dns-out" + }, + { + "clash_mode": "direct", + "outbound": "direct" + }, + { + "clash_mode": "global", + "outbound": "select" + }, + { + "geoip": [ + "ir", + "private" + ], + "outbound": "direct" + }, + { + "geosite": "ir", + "outbound": "direct" + } + ], + "auto_detect_interface": true + }, + "experimental": { + "clash_api": { + "external_controller": "127.0.0.1:9090", + "store_selected": true, + "secret": "" + } + } +} \ No newline at end of file diff --git a/shared/parser.go b/shared/parser.go index 7b4310b..aa68964 100644 --- a/shared/parser.go +++ b/shared/parser.go @@ -1,6 +1,7 @@ package shared import ( + _ "embed" "fmt" "os" @@ -10,6 +11,9 @@ import ( "gopkg.in/yaml.v3" ) +//go:embed config.json.template +var configByte []byte + func ParseConfig(path string) error { content, err := os.ReadFile(path) if err != nil { @@ -43,7 +47,7 @@ func parseClash(content []byte) ([]byte, error) { return nil, err } - output := defaultTemplate(ConfigTemplateOptions{}) + output := configByte output, err = convert.Patch(output, sbConfig, "", "", nil) if err != nil { fmt.Printf("patch error %s", err) diff --git a/shared/template.go b/shared/template.go deleted file mode 100644 index d985a7a..0000000 --- a/shared/template.go +++ /dev/null @@ -1,163 +0,0 @@ -package shared - -import ( - "bytes" - "text/template" -) - -const base = ` - { - "log": { - {{if .IncludeLogOutput}} - "disabled": false, - "level": "info", - "output": "box.log", - {{end}} - }, - "dns": { - "servers": [ - { - "tag": "remote", - "address_resolver": "local", - "address": "tcp://1.1.1.1", - "strategy": "prefer_ipv4", - "detour": "select" - }, - { - "tag": "local", - "address": "local", - "detour": "direct" - } - ], - "rules": [ - { - "clash_mode": "global", - "server": "remote" - }, - { - "clash_mode": "direct", - "server": "local" - }, - { - "outbound": [ - "any" - ], - "server": "local" - }, - { - "geosite": "ir", - "server": "local" - } - ], - "strategy": "ipv4_only" - }, - "inbounds": [ - {{if .IncludeTunInbound}} - { - "type": "tun", - "inet4_address": "172.19.0.1/30", - "sniff": true, - "sniff_override_destination": true, - "domain_strategy": "ipv4_only", - "strict_route": true, - "mtu": 9000, - "endpoint_independent_nat": true, - "auto_route": true - }, - {{end}} - { - "type": "socks", - "tag": "socks-in", - "listen": "127.0.0.1", - "sniff": true, - "sniff_override_destination": true, - "domain_strategy": "ipv4_only", - "listen_port": 2333, - "users": [] - } - {{if .IncludeMixedInbound}} - ,{ - "type": "mixed", - "tag": "mixed-in", - "sniff": true, - "sniff_override_destination": true, - "domain_strategy": "ipv4_only", - "listen": "127.0.0.1", - "listen_port": 2334, - "set_system_proxy": true, - "users": [] - } - {{end}} - ], - "outbounds": [ - { - "type": "direct", - "tag": "direct" - }, - { - "type": "block", - "tag": "block" - }, - { - "type": "dns", - "tag": "dns-out" - } - ], - "route": { - "rules": [ - { - "geosite": "category-ads-all", - "outbound": "block" - }, - { - "protocol": "dns", - "outbound": "dns-out" - }, - { - "clash_mode": "direct", - "outbound": "direct" - }, - { - "clash_mode": "global", - "outbound": "select" - }, - { - "geoip": [ - "ir", - "private" - ], - "outbound": "direct" - }, - // { - // "geosite": "geolocation-!ir", - // "outbound": "select" - // }, - { - "geosite": "ir", - "outbound": "direct" - } - ], - "auto_detect_interface": true - }, - "experimental": { - "clash_api": { - "external_controller": "127.0.0.1:9090", - "store_selected": true, - "secret": "" - } - } - }` - -type ConfigTemplateOptions struct { - IncludeTunInbound bool - IncludeMixedInbound bool - IncludeLogOutput bool -} - -func defaultTemplate(options ConfigTemplateOptions) []byte { - var buffer bytes.Buffer - t := template.New("baseConfig") - t, _ = t.Parse(base) - t.Execute(&buffer, options) - return buffer.Bytes() -}