Skip to content

Commit

Permalink
Merge pull request #1 from 83hd4d/dev
Browse files Browse the repository at this point in the history
Fix issues and Add features
  • Loading branch information
83hd4d authored Jan 22, 2024
2 parents 1b0ed4a + bccb34c commit fdbd268
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 50 deletions.
14 changes: 8 additions & 6 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ func BuildConfig(configOpt ConfigOptions, input option.Options) (*option.Options
options.Experimental = &option.ExperimentalOptions{
ClashAPI: &option.ClashAPIOptions{
ExternalController: fmt.Sprintf("%s:%d", "127.0.0.1", configOpt.ClashApiPort),
StoreSelected: true,
CacheFile: "clash.db",
},
CacheFile: &option.CacheFileOptions{
Enabled: true,
Path: "clash.db",
},
}
}
Expand Down Expand Up @@ -354,9 +356,10 @@ func BuildConfig(configOpt ConfigOptions, input option.Options) (*option.Options
Type: C.TypeURLTest,
Tag: "auto",
URLTestOptions: option.URLTestOutboundOptions{
Outbounds: tags,
URL: configOpt.ConnectionTestUrl,
Interval: configOpt.URLTestInterval,
Outbounds: tags,
URL: configOpt.ConnectionTestUrl,
Interval: configOpt.URLTestInterval,
IdleTimeout: configOpt.URLTestIdleTimeout,
},
}

Expand Down Expand Up @@ -408,7 +411,6 @@ func applyOverrides(overrides ConfigOptions, options option.Options) *option.Opt
if overrides.EnableClashApi {
options.Experimental.ClashAPI = &option.ClashAPIOptions{
ExternalController: fmt.Sprintf("%s:%d", "127.0.0.1", overrides.ClashApiPort),
StoreSelected: true,
}
}

Expand Down
2 changes: 2 additions & 0 deletions config/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type ConfigOptions struct {
TUNStack string `json:"tun-stack"`
ConnectionTestUrl string `json:"connection-test-url"`
URLTestInterval option.Duration `json:"url-test-interval"`
URLTestIdleTimeout option.Duration `json:"url-test-idle-timeout"`
EnableClashApi bool `json:"enable-clash-api"`
ClashApiPort uint16 `json:"clash-api-port"`
EnableTun bool `json:"enable-tun"`
Expand Down Expand Up @@ -72,6 +73,7 @@ func DefaultConfigOptions() *ConfigOptions {
TUNStack: "mixed",
ConnectionTestUrl: "https://cp.cloudflare.com/",
URLTestInterval: option.Duration(10 * time.Minute),
URLTestIdleTimeout: option.Duration(100 * time.Minute),
EnableClashApi: true,
ClashApiPort: 6756,
EnableTun: true,
Expand Down
108 changes: 67 additions & 41 deletions config/outbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,70 @@ import (

type outboundMap map[string]interface{}

func patchOutboundMux(base option.Outbound, configOpt ConfigOptions, obj outboundMap) outboundMap {
if configOpt.EnableMux {
multiplex := option.OutboundMultiplexOptions{
Enabled: true,
Padding: configOpt.MuxPadding,
MaxStreams: configOpt.MaxStreams,
Protocol: configOpt.MuxProtocol,
}
obj["multiplex"] = multiplex
} else {
delete(obj, "multiplex")
}
return obj
}

func patchOutboundTLSTricks(base option.Outbound, configOpt ConfigOptions, obj outboundMap) outboundMap {

obj = patchOutboundFragment(base, configOpt, obj)
if tls, ok := obj["tls"].(map[string]interface{}); ok {
tlsTricks := option.TLSTricksOptions{
MixedCaseSNI: configOpt.TLSTricks.EnableMixedSNICase,
}

if configOpt.TLSTricks.EnablePadding {
tlsTricks.PaddingMode = "random"
tlsTricks.PaddingSize = configOpt.TLSTricks.PaddingSize
}

if tlsTricks.MixedCaseSNI || tlsTricks.PaddingMode != "" {
tls["tls_tricks"] = tlsTricks
} else {
tls["tls_tricks"] = nil
}
}
return obj
}

func patchOutboundFragment(base option.Outbound, configOpt ConfigOptions, obj outboundMap) outboundMap {
if configOpt.EnableFragment {
tlsFragment := option.TLSFragmentOptions{
Enabled: configOpt.TLSTricks.EnableFragment,
Size: configOpt.TLSTricks.FragmentSize,
Sleep: configOpt.TLSTricks.FragmentSleep,
}
obj["tls_fragment"] = tlsFragment
} else {
obj["tls_fragment"] = nil
}
return obj
}

func isOutboundReality(base option.Outbound) bool {
// this function checks reality status ONLY FOR VLESS.
// Some other protocols can also use reality, but it's discouraged as stated in the reality document
isReality := false
switch base.Type {
case C.TypeVLESS:
if base.VLESSOptions.TLS.Reality != nil {
isReality = base.VLESSOptions.TLS.Reality.Enabled
}
}
return isReality
}

func patchOutbound(base option.Outbound, configOpt ConfigOptions) (*option.Outbound, string, error) {
var serverDomain string
var outbound option.Outbound
Expand All @@ -35,50 +99,12 @@ func patchOutbound(base option.Outbound, configOpt ConfigOptions) (*option.Outbo
serverDomain = fmt.Sprintf("full:%s", server)
}
}

if !(base.Type == C.TypeSelector || base.Type == C.TypeURLTest || base.Type == C.TypeBlock || base.Type == C.TypeDNS) {
if configOpt.EnableFragment {
tlsFragment := option.TLSFragmentOptions{
Enabled: configOpt.TLSTricks.EnableFragment,
Size: configOpt.TLSTricks.FragmentSize,
Sleep: configOpt.TLSTricks.FragmentSleep,
}
obj["tls_fragment"] = tlsFragment
} else {
obj["tls_fragment"] = nil
}

if tls, ok := obj["tls"].(map[string]interface{}); ok {
tlsTricks := option.TLSTricksOptions{
MixedCaseSNI: configOpt.TLSTricks.EnableMixedSNICase,
}

if configOpt.TLSTricks.EnablePadding {
tlsTricks.PaddingMode = "random"
tlsTricks.PaddingSize = configOpt.TLSTricks.PaddingSize
}

if tlsTricks.MixedCaseSNI || tlsTricks.PaddingMode != "" {
tls["tls_tricks"] = tlsTricks
} else {
tls["tls_tricks"] = nil
}
}
if !(base.Type == C.TypeSelector || base.Type == C.TypeURLTest || base.Type == C.TypeBlock || base.Type == C.TypeDNS || isOutboundReality(base)) {
obj = patchOutboundTLSTricks(base, configOpt, obj)
}

switch base.Type {
case C.TypeVMess, C.TypeVLESS, C.TypeTrojan, C.TypeShadowsocks:
if configOpt.EnableMux {
multiplex := option.OutboundMultiplexOptions{
Enabled: true,
Padding: configOpt.MuxPadding,
MaxStreams: configOpt.MaxStreams,
Protocol: configOpt.MuxProtocol,
}
obj["multiplex"] = multiplex
} else {
delete(obj, "multiplex")
}
obj = patchOutboundMux(base, configOpt, obj)
}

modifiedJson, err := json.Marshal(obj)
Expand Down
4 changes: 1 addition & 3 deletions custom/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ func NewService(options option.Options) (*libbox.BoxService, error) {
ctx = filemanager.WithDefault(ctx, sWorkingPath, sTempPath, sUserID, sGroupID)
urlTestHistoryStorage := urltest.NewHistoryStorage()
ctx = service.ContextWithPtr(ctx, urlTestHistoryStorage)
pauseManager := pause.WithDefaultManager(ctx)
// ctx = pause.ContextWithManager(ctx, pauseManager)
instance, err := B.New(B.Options{
Context: ctx,
Options: options,
Expand All @@ -51,7 +49,7 @@ func NewService(options option.Options) (*libbox.BoxService, error) {
ctx,
cancel,
instance,
pauseManager,
service.FromContext[pause.Manager](ctx),
urlTestHistoryStorage,
)
return &service, nil
Expand Down

0 comments on commit fdbd268

Please sign in to comment.