Change builder
This commit is contained in:
@@ -15,12 +15,15 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func BuildConfigJson(configOpt ConfigOptions, input option.Options) (string, error) {
|
func BuildConfigJson(configOpt ConfigOptions, input option.Options) (string, error) {
|
||||||
options := BuildConfig(configOpt, input)
|
options, err := BuildConfig(configOpt, input)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
var buffer bytes.Buffer
|
var buffer bytes.Buffer
|
||||||
json.NewEncoder(&buffer)
|
json.NewEncoder(&buffer)
|
||||||
encoder := json.NewEncoder(&buffer)
|
encoder := json.NewEncoder(&buffer)
|
||||||
encoder.SetIndent("", " ")
|
encoder.SetIndent("", " ")
|
||||||
err := encoder.Encode(options)
|
err = encoder.Encode(options)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
@@ -28,9 +31,9 @@ func BuildConfigJson(configOpt ConfigOptions, input option.Options) (string, err
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO include selectors
|
// TODO include selectors
|
||||||
func BuildConfig(configOpt ConfigOptions, input option.Options) option.Options {
|
func BuildConfig(configOpt ConfigOptions, input option.Options) (*option.Options, error) {
|
||||||
if configOpt.ExecuteAsIs {
|
if configOpt.ExecuteAsIs {
|
||||||
return applyOverrides(configOpt, input)
|
return applyOverrides(configOpt, input), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("config options: %+v\n", configOpt)
|
fmt.Printf("config options: %+v\n", configOpt)
|
||||||
@@ -326,6 +329,9 @@ func BuildConfig(configOpt ConfigOptions, input option.Options) option.Options {
|
|||||||
var tags []string
|
var tags []string
|
||||||
for _, out := range input.Outbounds {
|
for _, out := range input.Outbounds {
|
||||||
outbound, serverDomain, err := patchOutbound(out, configOpt)
|
outbound, serverDomain, err := patchOutbound(out, configOpt)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if serverDomain != "" {
|
if serverDomain != "" {
|
||||||
directDNSDomains = append(directDNSDomains, serverDomain)
|
directDNSDomains = append(directDNSDomains, serverDomain)
|
||||||
@@ -395,10 +401,10 @@ func BuildConfig(configOpt ConfigOptions, input option.Options) option.Options {
|
|||||||
options.DNS.Rules = append([]option.DNSRule{{Type: C.RuleTypeDefault, DefaultOptions: dnsRule}}, options.DNS.Rules...)
|
options.DNS.Rules = append([]option.DNSRule{{Type: C.RuleTypeDefault, DefaultOptions: dnsRule}}, options.DNS.Rules...)
|
||||||
}
|
}
|
||||||
|
|
||||||
return options
|
return &options, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func applyOverrides(overrides ConfigOptions, options option.Options) option.Options {
|
func applyOverrides(overrides ConfigOptions, options option.Options) *option.Options {
|
||||||
if overrides.EnableClashApi {
|
if overrides.EnableClashApi {
|
||||||
options.Experimental.ClashAPI = &option.ClashAPIOptions{
|
options.Experimental.ClashAPI = &option.ClashAPIOptions{
|
||||||
ExternalController: fmt.Sprintf("%s:%d", "127.0.0.1", overrides.ClashApiPort),
|
ExternalController: fmt.Sprintf("%s:%d", "127.0.0.1", overrides.ClashApiPort),
|
||||||
@@ -421,7 +427,7 @@ func applyOverrides(overrides ConfigOptions, options option.Options) option.Opti
|
|||||||
}
|
}
|
||||||
options.Inbounds = inbounds
|
options.Inbounds = inbounds
|
||||||
|
|
||||||
return options
|
return &options
|
||||||
}
|
}
|
||||||
|
|
||||||
func removeDuplicateStr(strSlice []string) []string {
|
func removeDuplicateStr(strSlice []string) []string {
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ package main
|
|||||||
import "C"
|
import "C"
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
@@ -15,6 +16,7 @@ import (
|
|||||||
"github.com/hiddify/libcore/config"
|
"github.com/hiddify/libcore/config"
|
||||||
"github.com/sagernet/sing-box/experimental/libbox"
|
"github.com/sagernet/sing-box/experimental/libbox"
|
||||||
"github.com/sagernet/sing-box/log"
|
"github.com/sagernet/sing-box/log"
|
||||||
|
"github.com/sagernet/sing-box/option"
|
||||||
)
|
)
|
||||||
|
|
||||||
var box *libbox.BoxService
|
var box *libbox.BoxService
|
||||||
@@ -144,16 +146,20 @@ func startService(delayStart bool) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return stopAndAlert(EmptyConfiguration, err)
|
return stopAndAlert(EmptyConfiguration, err)
|
||||||
}
|
}
|
||||||
options = config.BuildConfig(*configOptions, options)
|
var patchedOptions *option.Options
|
||||||
|
patchedOptions, err = config.BuildConfig(*configOptions, options)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error building config: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
config.SaveCurrentConfig(sWorkingPath, options)
|
config.SaveCurrentConfig(sWorkingPath, *patchedOptions)
|
||||||
|
|
||||||
err = startCommandServer(*logFactory)
|
err = startCommandServer(*logFactory)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return stopAndAlert(StartCommandServer, err)
|
return stopAndAlert(StartCommandServer, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
instance, err := NewService(options)
|
instance, err := NewService(*patchedOptions)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return stopAndAlert(CreateService, err)
|
return stopAndAlert(CreateService, err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user