chg: HiddifyOptions

This commit is contained in:
x
2024-09-26 23:27:04 +02:00
parent 0f9f6689b1
commit 1f485e1193
17 changed files with 148 additions and 169 deletions

View File

@@ -27,7 +27,8 @@ var tunnelServiceRunning = false
func isSupportedOS() bool {
return runtime.GOOS == "windows" || runtime.GOOS == "linux"
}
func ActivateTunnelService(opt ConfigOptions) (bool, error) {
func ActivateTunnelService(opt HiddifyOptions) (bool, error) {
tunnelServiceRunning = true
// if !isSupportedOS() {
// return false, E.New("Unsupported OS: " + runtime.GOOS)
@@ -36,11 +37,12 @@ func ActivateTunnelService(opt ConfigOptions) (bool, error) {
go startTunnelRequestWithFailover(opt, true)
return true, nil
}
func DeactivateTunnelServiceForce() (bool, error) {
return stopTunnelRequest()
}
func DeactivateTunnelService() (bool, error) {
func DeactivateTunnelService() (bool, error) {
// if !isSupportedOS() {
// return true, nil
// }
@@ -58,16 +60,15 @@ func DeactivateTunnelService() (bool, error) {
return true, nil
}
func startTunnelRequestWithFailover(opt ConfigOptions, installService bool) {
func startTunnelRequestWithFailover(opt HiddifyOptions, installService bool) {
res, err := startTunnelRequest(opt, installService)
fmt.Printf("Start Tunnel Result: %v\n", res)
if err != nil {
fmt.Printf("Start Tunnel Failed! Stopping core... err=%v\n", err)
// StopAndAlert(pb.MessageType.MessageType_UNEXPECTED_ERROR, "Start Tunnel Failed! Stopping...")
}
}
func isPortInUse(port string) bool {
listener, err := net.Listen("tcp", "127.0.0.1:"+port)
if err != nil {
@@ -76,7 +77,8 @@ func isPortInUse(port string) bool {
defer listener.Close()
return false // Port is available
}
func startTunnelRequest(opt ConfigOptions, installService bool) (bool, error) {
func startTunnelRequest(opt HiddifyOptions, installService bool) (bool, error) {
if !isPortInUse("18020") {
if installService {
return runTunnelService(opt)
@@ -153,7 +155,7 @@ func ExitTunnelService() (bool, error) {
return true, nil
}
func runTunnelService(opt ConfigOptions) (bool, error) {
func runTunnelService(opt HiddifyOptions) (bool, error) {
executablePath := getTunnelServicePath()
fmt.Printf("Executable path is %s", executablePath)
out, err := ExecuteCmd(executablePath, false, "tunnel", "install")
@@ -163,7 +165,7 @@ func runTunnelService(opt ConfigOptions) (bool, error) {
fmt.Println("Shell command executed without flag:", out, err)
}
if err == nil {
<-time.After(1 * time.Second) //wait until service loaded completely
<-time.After(1 * time.Second) // wait until service loaded completely
}
return startTunnelRequest(opt, false)
}

View File

@@ -41,8 +41,7 @@ const (
var OutboundMainProxyTag = OutboundSelectTag
func BuildConfigJson(configOpt ConfigOptions, input option.Options) (string, error) {
func BuildConfigJson(configOpt HiddifyOptions, input option.Options) (string, error) {
options, err := BuildConfig(configOpt, input)
if err != nil {
return "", err
@@ -59,7 +58,7 @@ func BuildConfigJson(configOpt ConfigOptions, input option.Options) (string, err
}
// TODO include selectors
func BuildConfig(opt ConfigOptions, input option.Options) (*option.Options, error) {
func BuildConfig(opt HiddifyOptions, input option.Options) (*option.Options, error) {
fmt.Printf("config options: %++v\n", opt)
var options option.Options
@@ -82,7 +81,8 @@ func BuildConfig(opt ConfigOptions, input option.Options) (*option.Options, erro
return &options, nil
}
func addForceDirect(options *option.Options, opt *ConfigOptions, directDNSDomains map[string]bool) {
func addForceDirect(options *option.Options, opt *HiddifyOptions, directDNSDomains map[string]bool) {
remoteDNSAddress := opt.RemoteDnsAddress
if strings.Contains(remoteDNSAddress, "://") {
remoteDNSAddress = strings.SplitAfter(remoteDNSAddress, "://")[1]
@@ -124,16 +124,15 @@ func addForceDirect(options *option.Options, opt *ConfigOptions, directDNSDomain
dnsRule.Server = DNSDirectTag
options.DNS.Rules = append([]option.DNSRule{{Type: C.RuleTypeDefault, DefaultOptions: dnsRule}}, options.DNS.Rules...)
}
}
func setOutbounds(options *option.Options, input *option.Options, opt *ConfigOptions) error {
func setOutbounds(options *option.Options, input *option.Options, opt *HiddifyOptions) error {
directDNSDomains := make(map[string]bool)
var outbounds []option.Outbound
var tags []string
OutboundMainProxyTag = OutboundSelectTag
//inbound==warp over proxies
//outbound==proxies over warp
// inbound==warp over proxies
// outbound==proxies over warp
if opt.Warp.EnableWarp {
for _, out := range input.Outbounds {
if out.Type == C.TypeCustom {
@@ -266,7 +265,7 @@ func setOutbounds(options *option.Options, input *option.Options, opt *ConfigOpt
return nil
}
func setClashAPI(options *option.Options, opt *ConfigOptions) {
func setClashAPI(options *option.Options, opt *HiddifyOptions) {
if opt.EnableClashApi {
if opt.ClashApiSecret == "" {
opt.ClashApiSecret = generateRandomString(16)
@@ -285,7 +284,7 @@ func setClashAPI(options *option.Options, opt *ConfigOptions) {
}
}
func setLog(options *option.Options, opt *ConfigOptions) {
func setLog(options *option.Options, opt *HiddifyOptions) {
options.Log = &option.LogOptions{
Level: opt.LogLevel,
Output: "box.log",
@@ -293,11 +292,9 @@ func setLog(options *option.Options, opt *ConfigOptions) {
Timestamp: true,
DisableColor: true,
}
}
func setInbound(options *option.Options, opt *ConfigOptions) {
func setInbound(options *option.Options, opt *HiddifyOptions) {
var inboundDomainStrategy option.DomainStrategy
if !opt.ResolveDestination {
inboundDomainStrategy = option.DomainStrategy(dns.DomainStrategyAsIS)
@@ -312,7 +309,6 @@ func setInbound(options *option.Options, opt *ConfigOptions) {
Tag: InboundTUNTag,
TunOptions: option.TunInboundOptions{
Stack: opt.TUNStack,
MTU: opt.MTU,
AutoRoute: true,
@@ -391,7 +387,7 @@ func setInbound(options *option.Options, opt *ConfigOptions) {
)
}
func setDns(options *option.Options, opt *ConfigOptions) {
func setDns(options *option.Options, opt *HiddifyOptions) {
options.DNS = &option.DNSOptions{
StaticIPs: map[string][]string{},
DNSClientOptions: option.DNSClientOptions{
@@ -436,7 +432,7 @@ func setDns(options *option.Options, opt *ConfigOptions) {
}
}
func setFakeDns(options *option.Options, opt *ConfigOptions) {
func setFakeDns(options *option.Options, opt *HiddifyOptions) {
if opt.EnableFakeDNS {
inet4Range := netip.MustParsePrefix("198.18.0.0/15")
inet6Range := netip.MustParsePrefix("fc00::/18")
@@ -468,7 +464,7 @@ func setFakeDns(options *option.Options, opt *ConfigOptions) {
}
}
func setRoutingOptions(options *option.Options, opt *ConfigOptions) {
func setRoutingOptions(options *option.Options, opt *HiddifyOptions) {
dnsRules := []option.DefaultDNSRule{}
routeRules := []option.Rule{}
rulesets := []option.RuleSet{}
@@ -505,7 +501,6 @@ func setRoutingOptions(options *option.Options, opt *ConfigOptions) {
},
})
routeRules = append(routeRules, option.Rule{
Type: C.RuleTypeDefault,
DefaultOptions: option.DefaultRule{
Port: []uint16{53},
@@ -664,12 +659,14 @@ func setRoutingOptions(options *option.Options, opt *ConfigOptions) {
},
})
dnsRules = append(dnsRules, option.DefaultDNSRule{
RuleSet: []string{"geosite-ads",
RuleSet: []string{
"geosite-ads",
"geosite-malware",
"geosite-phishing",
"geosite-cryptominers",
"geoip-malware",
"geoip-phishing"},
"geoip-phishing",
},
Server: DNSBlockTag,
// DisableCache: true,
})
@@ -752,10 +749,9 @@ func setRoutingOptions(options *option.Options, opt *ConfigOptions) {
}
}
}
}
func patchHiddifyWarpFromConfig(out option.Outbound, opt ConfigOptions) option.Outbound {
func patchHiddifyWarpFromConfig(out option.Outbound, opt HiddifyOptions) option.Outbound {
if opt.Warp.EnableWarp && opt.Warp.Mode == "proxy_over_warp" {
if out.DirectOptions.Detour == "" {
out.DirectOptions.Detour = "Hiddify Warp ✅"

View File

@@ -294,16 +294,16 @@ func file_core_proto_rawDescGZIP() []byte {
var file_core_proto_msgTypes = make([]protoimpl.MessageInfo, 4)
var file_core_proto_goTypes = []interface{}{
(*ParseConfigRequest)(nil), // 0: ConfigOptions.ParseConfigRequest
(*ParseConfigResponse)(nil), // 1: ConfigOptions.ParseConfigResponse
(*GenerateConfigRequest)(nil), // 2: ConfigOptions.GenerateConfigRequest
(*GenerateConfigResponse)(nil), // 3: ConfigOptions.GenerateConfigResponse
(*ParseConfigRequest)(nil), // 0: HiddifyOptions.ParseConfigRequest
(*ParseConfigResponse)(nil), // 1: HiddifyOptions.ParseConfigResponse
(*GenerateConfigRequest)(nil), // 2: HiddifyOptions.GenerateConfigRequest
(*GenerateConfigResponse)(nil), // 3: HiddifyOptions.GenerateConfigResponse
}
var file_core_proto_depIdxs = []int32{
0, // 0: ConfigOptions.CoreService.ParseConfig:input_type -> ConfigOptions.ParseConfigRequest
2, // 1: ConfigOptions.CoreService.GenerateFullConfig:input_type -> ConfigOptions.GenerateConfigRequest
1, // 2: ConfigOptions.CoreService.ParseConfig:output_type -> ConfigOptions.ParseConfigResponse
3, // 3: ConfigOptions.CoreService.GenerateFullConfig:output_type -> ConfigOptions.GenerateConfigResponse
0, // 0: HiddifyOptions.CoreService.ParseConfig:input_type -> HiddifyOptions.ParseConfigRequest
2, // 1: HiddifyOptions.CoreService.GenerateFullConfig:input_type -> HiddifyOptions.GenerateConfigRequest
1, // 2: HiddifyOptions.CoreService.ParseConfig:output_type -> HiddifyOptions.ParseConfigResponse
3, // 3: HiddifyOptions.CoreService.GenerateFullConfig:output_type -> HiddifyOptions.GenerateConfigResponse
2, // [2:4] is the sub-list for method output_type
0, // [0:2] is the sub-list for method input_type
0, // [0:0] is the sub-list for extension type_name

View File

@@ -19,8 +19,8 @@ import (
const _ = grpc.SupportPackageIsVersion7
const (
CoreService_ParseConfig_FullMethodName = "/ConfigOptions.CoreService/ParseConfig"
CoreService_GenerateFullConfig_FullMethodName = "/ConfigOptions.CoreService/GenerateFullConfig"
CoreService_ParseConfig_FullMethodName = "/HiddifyOptions.CoreService/ParseConfig"
CoreService_GenerateFullConfig_FullMethodName = "/HiddifyOptions.CoreService/GenerateFullConfig"
)
// CoreServiceClient is the client API for CoreService service.
@@ -129,7 +129,7 @@ func _CoreService_GenerateFullConfig_Handler(srv interface{}, ctx context.Contex
// It's only intended for direct use with grpc.RegisterService,
// and not to be introspected or modified (even as a copy)
var CoreService_ServiceDesc = grpc.ServiceDesc{
ServiceName: "ConfigOptions.CoreService",
ServiceName: "HiddifyOptions.CoreService",
HandlerType: (*CoreServiceServer)(nil),
Methods: []grpc.MethodDesc{
{

View File

@@ -5,7 +5,7 @@ import (
dns "github.com/sagernet/sing-dns"
)
type ConfigOptions struct {
type HiddifyOptions struct {
EnableFullConfig bool `json:"enable-full-config"`
LogLevel string `json:"log-level"`
EnableClashApi bool `json:"enable-clash-api"`
@@ -92,8 +92,8 @@ type WarpOptions struct {
Account WarpAccount
}
func DefaultConfigOptions() *ConfigOptions {
return &ConfigOptions{
func DefaultHiddifyOptions() *HiddifyOptions {
return &HiddifyOptions{
DNSOptions: DNSOptions{
RemoteDnsAddress: "1.1.1.1",
RemoteDnsDomainStrategy: option.DomainStrategy(dns.DomainStrategyAsIS),

View File

@@ -11,7 +11,7 @@ import (
type outboundMap map[string]interface{}
func patchOutboundMux(base option.Outbound, configOpt ConfigOptions, obj outboundMap) outboundMap {
func patchOutboundMux(base option.Outbound, configOpt HiddifyOptions, obj outboundMap) outboundMap {
if configOpt.Mux.Enable {
multiplex := option.OutboundMultiplexOptions{
Enabled: true,
@@ -26,8 +26,7 @@ func patchOutboundMux(base option.Outbound, configOpt ConfigOptions, obj outboun
return obj
}
func patchOutboundTLSTricks(base option.Outbound, configOpt ConfigOptions, obj outboundMap) outboundMap {
func patchOutboundTLSTricks(base option.Outbound, configOpt HiddifyOptions, obj outboundMap) outboundMap {
if base.Type == C.TypeSelector || base.Type == C.TypeURLTest || base.Type == C.TypeBlock || base.Type == C.TypeDNS {
return obj
}
@@ -56,9 +55,7 @@ func patchOutboundTLSTricks(base option.Outbound, configOpt ConfigOptions, obj o
"interval": configOpt.TLSTricks.FragmentSleep,
}
}
}
}
if base.Type == C.TypeDirect {
return patchOutboundFragment(base, configOpt, obj)
@@ -100,7 +97,7 @@ func patchOutboundTLSTricks(base option.Outbound, configOpt ConfigOptions, obj o
return obj
}
func patchOutboundFragment(base option.Outbound, configOpt ConfigOptions, obj outboundMap) outboundMap {
func patchOutboundFragment(base option.Outbound, configOpt HiddifyOptions, obj outboundMap) outboundMap {
if configOpt.TLSTricks.EnableFragment {
obj["tcp_fast_open"] = false
obj["tls_fragment"] = option.TLSFragmentOptions{
@@ -127,11 +124,9 @@ func isOutboundReality(base option.Outbound) bool {
return false
}
return base.VLESSOptions.OutboundTLSOptionsContainer.TLS.Reality.Enabled
}
func patchOutbound(base option.Outbound, configOpt ConfigOptions, staticIpsDns map[string][]string) (*option.Outbound, string, error) {
func patchOutbound(base option.Outbound, configOpt HiddifyOptions, staticIpsDns map[string][]string) (*option.Outbound, string, error) {
formatErr := func(err error) error {
return fmt.Errorf("error patching outbound[%s][%s]: %w", base.Tag, base.Type, err)
}

View File

@@ -6,9 +6,8 @@ import (
_ "embed"
"encoding/json"
"fmt"
"path/filepath"
"os"
"path/filepath"
"github.com/hiddify/ray2sing/ray2sing"
"github.com/sagernet/sing-box/experimental/libbox"
@@ -31,9 +30,10 @@ func ParseConfig(path string, debug bool) ([]byte, error) {
}
return ParseConfigContent(string(content), debug, nil, false)
}
func ParseConfigContent(contentstr string, debug bool, configOpt *ConfigOptions, fullConfig bool) ([]byte, error) {
func ParseConfigContent(contentstr string, debug bool, configOpt *HiddifyOptions, fullConfig bool) ([]byte, error) {
if configOpt == nil {
configOpt = DefaultConfigOptions()
configOpt = DefaultHiddifyOptions()
}
content := []byte(contentstr)
var jsonObj map[string]interface{} = make(map[string]interface{})
@@ -51,7 +51,6 @@ func ParseConfigContent(contentstr string, debug bool, configOpt *ConfigOptions,
} else {
jsonObj["outbounds"] = tmpJsonObj["outbounds"]
}
}
} else if jsonArray, ok := tmpJsonResult.([]map[string]interface{}); ok {
jsonObj["outbounds"] = jsonArray
@@ -89,7 +88,7 @@ func ParseConfigContent(contentstr string, debug bool, configOpt *ConfigOptions,
return nil, fmt.Errorf("unable to determine config format")
}
func patchConfig(content []byte, name string, configOpt *ConfigOptions) ([]byte, error) {
func patchConfig(content []byte, name string, configOpt *HiddifyOptions) ([]byte, error) {
options := option.Options{}
err := json.Unmarshal(content, &options)
if err != nil {
@@ -113,7 +112,6 @@ func patchConfig(content []byte, name string, configOpt *ConfigOptions) ([]byte,
for i, base := range options.Outbounds {
options.Outbounds[i] = *res[base.Tag].Value
}
}
content, _ = json.MarshalIndent(options, "", " ")
@@ -123,7 +121,6 @@ func patchConfig(content []byte, name string, configOpt *ConfigOptions) ([]byte,
}
func validateResult(content []byte, name string) ([]byte, error) {
err := libbox.CheckConfig(string(content))
if err != nil {
return nil, fmt.Errorf("[%s] invalid sing-box config: %w", name, err)

View File

@@ -22,11 +22,10 @@ func String(s string) *string {
func (s *server) ParseConfig(ctx context.Context, in *ParseConfigRequest) (*ParseConfigResponse, error) {
config, err := ParseConfig(in.TempPath, in.Debug)
if err != nil {
return &ParseConfigResponse{Error: String(err.Error())}, nil
}
err = os.WriteFile(in.Path, config, 0644)
err = os.WriteFile(in.Path, config, 0o644)
if err != nil {
return nil, err
}
@@ -47,7 +46,7 @@ func (s *server) GenerateFullConfig(ctx context.Context, in *GenerateConfigReque
if err != nil {
return nil, err
}
config, err := BuildConfigJson(*DefaultConfigOptions(), options)
config, err := BuildConfigJson(*DefaultHiddifyOptions(), options)
if err != nil {
return nil, err
}

View File

@@ -3,6 +3,7 @@ package config
import (
"encoding/base64"
"fmt"
"log/slog"
"net/netip"
"os"
"strings"
@@ -11,7 +12,6 @@ import (
C "github.com/sagernet/sing-box/constant"
// "github.com/bepass-org/wireguard-go/warp"
"log/slog"
"github.com/sagernet/sing-box/option"
T "github.com/sagernet/sing-box/option"
@@ -66,7 +66,6 @@ func wireGuardToSingbox(wgConfig WarpWireguardConfig, server string, port uint16
}
func getRandomIP() string {
ipPort, err := warp.RandomWarpEndpoint(true, true)
if err == nil {
return ipPort.Addr().String()
@@ -75,7 +74,6 @@ func getRandomIP() string {
}
func generateWarp(license string, host string, port uint16, fakePackets string, fakePacketsSize string, fakePacketsDelay string, fakePacketsMode string) (*T.Outbound, error) {
_, _, wgConfig, err := GenerateWarpInfo(license, "", "")
if err != nil {
return nil, err
@@ -142,10 +140,9 @@ func GenerateWarpInfo(license string, oldAccountId string, oldAccessToken string
}
return &identity, res, &warpcfg, err
}
func patchWarp(base *option.Outbound, configOpt *ConfigOptions, final bool, staticIpsDns map[string][]string) error {
func patchWarp(base *option.Outbound, configOpt *HiddifyOptions, final bool, staticIpsDns map[string][]string) error {
if base.Type == C.TypeCustom {
if warp, ok := base.CustomOptions["warp"].(map[string]interface{}); ok {
key, _ := warp["key"].(string)
@@ -180,7 +177,6 @@ func patchWarp(base *option.Outbound, configOpt *ConfigOptions, final bool, stat
base.WireGuardOptions = warpConfig.WireGuardOptions
}
}
if final && base.Type == C.TypeWireGuard {
@@ -202,7 +198,6 @@ func patchWarp(base *option.Outbound, configOpt *ConfigOptions, final bool, stat
}
base.WireGuardOptions.Server = rndDomain
}
}
if base.WireGuardOptions.ServerPort == 0 {
port := warp.RandomWarpPort()