sing-box/experimental/clashapi.go

82 lines
2.5 KiB
Go
Raw Permalink Normal View History

2022-07-20 07:12:40 +08:00
package experimental
import (
2023-04-21 17:29:00 +08:00
"context"
2022-09-26 19:37:06 +08:00
"os"
2024-02-06 18:52:45 +08:00
"sort"
2022-09-26 19:37:06 +08:00
2022-07-20 07:12:40 +08:00
"github.com/sagernet/sing-box/adapter"
2023-11-28 23:47:32 +08:00
C "github.com/sagernet/sing-box/constant"
2022-07-20 07:12:40 +08:00
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option"
"github.com/sagernet/sing/common"
2022-07-20 07:12:40 +08:00
)
2023-04-21 17:29:00 +08:00
type ClashServerConstructor = func(ctx context.Context, router adapter.Router, logFactory log.ObservableFactory, options option.ClashAPIOptions) (adapter.ClashServer, error)
2022-09-26 19:37:06 +08:00
var clashServerConstructor ClashServerConstructor
func RegisterClashServerConstructor(constructor ClashServerConstructor) {
clashServerConstructor = constructor
}
2023-04-21 17:29:00 +08:00
func NewClashServer(ctx context.Context, router adapter.Router, logFactory log.ObservableFactory, options option.ClashAPIOptions) (adapter.ClashServer, error) {
2022-09-26 19:37:06 +08:00
if clashServerConstructor == nil {
return nil, os.ErrInvalid
}
2023-04-21 17:29:00 +08:00
return clashServerConstructor(ctx, router, logFactory, options)
2022-07-20 07:12:40 +08:00
}
func CalculateClashModeList(options option.Options) []string {
2024-02-06 18:52:45 +08:00
var clashModes []string
clashModes = append(clashModes, extraClashModeFromRule(common.PtrValueOrDefault(options.Route).Rules)...)
clashModes = append(clashModes, extraClashModeFromDNSRule(common.PtrValueOrDefault(options.DNS).Rules)...)
clashModes = common.FilterNotDefault(common.Uniq(clashModes))
predefinedOrder := []string{
"Rule", "Global", "Direct",
}
var newClashModes []string
for _, mode := range clashModes {
if !common.Contains(predefinedOrder, mode) {
newClashModes = append(newClashModes, mode)
}
}
sort.Strings(newClashModes)
for _, mode := range predefinedOrder {
if common.Contains(clashModes, mode) {
newClashModes = append(newClashModes, mode)
}
}
return newClashModes
2023-11-28 23:47:32 +08:00
}
func extraClashModeFromRule(rules []option.Rule) []string {
var clashMode []string
for _, rule := range rules {
switch rule.Type {
case C.RuleTypeDefault:
if rule.DefaultOptions.ClashMode != "" {
clashMode = append(clashMode, rule.DefaultOptions.ClashMode)
}
2023-11-28 23:47:32 +08:00
case C.RuleTypeLogical:
clashMode = append(clashMode, extraClashModeFromRule(rule.LogicalOptions.Rules)...)
}
}
2023-11-28 23:47:32 +08:00
return clashMode
}
func extraClashModeFromDNSRule(rules []option.DNSRule) []string {
var clashMode []string
for _, rule := range rules {
switch rule.Type {
case C.RuleTypeDefault:
if rule.DefaultOptions.ClashMode != "" {
clashMode = append(clashMode, rule.DefaultOptions.ClashMode)
}
2023-11-28 23:47:32 +08:00
case C.RuleTypeLogical:
clashMode = append(clashMode, extraClashModeFromDNSRule(rule.LogicalOptions.Rules)...)
}
}
return clashMode
}