2022-07-19 23:12:40 +00:00
|
|
|
package experimental
|
|
|
|
|
|
|
|
import (
|
2023-04-21 09:29:00 +00:00
|
|
|
"context"
|
2022-09-26 11:37:06 +00:00
|
|
|
"os"
|
|
|
|
|
2022-07-19 23:12:40 +00:00
|
|
|
"github.com/sagernet/sing-box/adapter"
|
2023-11-28 15:47:32 +00:00
|
|
|
C "github.com/sagernet/sing-box/constant"
|
2022-07-19 23:12:40 +00:00
|
|
|
"github.com/sagernet/sing-box/log"
|
|
|
|
"github.com/sagernet/sing-box/option"
|
2023-08-24 13:52:38 +00:00
|
|
|
"github.com/sagernet/sing/common"
|
2022-07-19 23:12:40 +00:00
|
|
|
)
|
|
|
|
|
2023-04-21 09:29:00 +00:00
|
|
|
type ClashServerConstructor = func(ctx context.Context, router adapter.Router, logFactory log.ObservableFactory, options option.ClashAPIOptions) (adapter.ClashServer, error)
|
2022-09-26 11:37:06 +00:00
|
|
|
|
|
|
|
var clashServerConstructor ClashServerConstructor
|
|
|
|
|
|
|
|
func RegisterClashServerConstructor(constructor ClashServerConstructor) {
|
|
|
|
clashServerConstructor = constructor
|
|
|
|
}
|
|
|
|
|
2023-04-21 09:29:00 +00:00
|
|
|
func NewClashServer(ctx context.Context, router adapter.Router, logFactory log.ObservableFactory, options option.ClashAPIOptions) (adapter.ClashServer, error) {
|
2022-09-26 11:37:06 +00:00
|
|
|
if clashServerConstructor == nil {
|
|
|
|
return nil, os.ErrInvalid
|
|
|
|
}
|
2023-04-21 09:29:00 +00:00
|
|
|
return clashServerConstructor(ctx, router, logFactory, options)
|
2022-07-19 23:12:40 +00:00
|
|
|
}
|
2023-08-24 13:52:38 +00:00
|
|
|
|
|
|
|
func CalculateClashModeList(options option.Options) []string {
|
|
|
|
var clashMode []string
|
2023-11-28 15:47:32 +00:00
|
|
|
clashMode = append(clashMode, extraClashModeFromRule(common.PtrValueOrDefault(options.Route).Rules)...)
|
|
|
|
clashMode = append(clashMode, extraClashModeFromDNSRule(common.PtrValueOrDefault(options.DNS).Rules)...)
|
|
|
|
clashMode = common.FilterNotDefault(common.Uniq(clashMode))
|
|
|
|
return clashMode
|
|
|
|
}
|
|
|
|
|
|
|
|
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-08-24 13:52:38 +00:00
|
|
|
}
|
2023-11-28 15:47:32 +00:00
|
|
|
case C.RuleTypeLogical:
|
|
|
|
clashMode = append(clashMode, extraClashModeFromRule(rule.LogicalOptions.Rules)...)
|
2023-08-24 13:52:38 +00:00
|
|
|
}
|
|
|
|
}
|
2023-11-28 15:47:32 +00: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-08-24 13:52:38 +00:00
|
|
|
}
|
2023-11-28 15:47:32 +00:00
|
|
|
case C.RuleTypeLogical:
|
|
|
|
clashMode = append(clashMode, extraClashModeFromDNSRule(rule.LogicalOptions.Rules)...)
|
2023-08-24 13:52:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return clashMode
|
|
|
|
}
|