diff --git a/adapter/outboundgroup/patch_android.go b/adapter/outboundgroup/patch_android.go index c9c1725b..c6566814 100644 --- a/adapter/outboundgroup/patch_android.go +++ b/adapter/outboundgroup/patch_android.go @@ -1,4 +1,4 @@ -// +build android,cmfa +//go:build android && cmfa package outboundgroup diff --git a/adapter/provider/parser.go b/adapter/provider/parser.go index f055e596..09ae4332 100644 --- a/adapter/provider/parser.go +++ b/adapter/provider/parser.go @@ -79,7 +79,7 @@ func ParseProxyProvider(name string, mapping map[string]any) (types.ProxyProvide case "http": if schema.Path != "" { path := C.Path.Resolve(schema.Path) - if !features.Contains("cmfa") && !C.Path.IsSafePath(path) { + if !features.CMFA && !C.Path.IsSafePath(path) { return nil, fmt.Errorf("%w: %s", errSubPath, path) } vehicle = resource.NewHTTPVehicle(schema.URL, path) diff --git a/adapter/provider/patch_android.go b/adapter/provider/patch_android.go index eba560eb..e9042bda 100644 --- a/adapter/provider/patch_android.go +++ b/adapter/provider/patch_android.go @@ -1,4 +1,4 @@ -// +build android,cmfa +//go:build android && cmfa package provider diff --git a/component/dialer/dialer.go b/component/dialer/dialer.go index 8d1f671f..985e2195 100644 --- a/component/dialer/dialer.go +++ b/component/dialer/dialer.go @@ -71,10 +71,10 @@ func DialContext(ctx context.Context, network, address string, options ...Option } func ListenPacket(ctx context.Context, network, address string, options ...Option) (net.PacketConn, error) { - if features.Contains("cmfa") && DefaultSocketHook != nil{ + if features.CMFA && DefaultSocketHook != nil { return listenPacketHooked(ctx, network, address) } - + cfg := applyOptions(options...) lc := &net.ListenConfig{} @@ -119,10 +119,10 @@ func GetTcpConcurrent() bool { } func dialContext(ctx context.Context, network string, destination netip.Addr, port string, opt *option) (net.Conn, error) { - if features.Contains("cmfa") && DefaultSocketHook != nil{ + if features.CMFA && DefaultSocketHook != nil { return dialContextHooked(ctx, network, destination, port) } - + address := net.JoinHostPort(destination.String(), port) netDialer := opt.netDialer diff --git a/component/dialer/patch_android.go b/component/dialer/patch_android.go index b1e80a5d..7c33a6c0 100644 --- a/component/dialer/patch_android.go +++ b/component/dialer/patch_android.go @@ -1,4 +1,4 @@ -// +build android,cmfa +//go:build android && cmfa package dialer diff --git a/component/dialer/patch_common.go b/component/dialer/patch_common.go index b1e8f2f9..bad0ef48 100644 --- a/component/dialer/patch_common.go +++ b/component/dialer/patch_common.go @@ -1,4 +1,4 @@ -// +build !cmfa +//go:build !(android && cmfa) package dialer diff --git a/component/mmdb/patch_android.go b/component/mmdb/patch_android.go index 9ded6566..a994b75e 100644 --- a/component/mmdb/patch_android.go +++ b/component/mmdb/patch_android.go @@ -1,4 +1,4 @@ -// +build android,cmfa +//go:build android && cmfa package mmdb diff --git a/component/process/process.go b/component/process/process.go index 5b49134e..76ec2c45 100644 --- a/component/process/process.go +++ b/component/process/process.go @@ -3,7 +3,6 @@ package process import ( "errors" "net/netip" - "github.com/metacubex/mihomo/constant" ) var ( @@ -20,7 +19,3 @@ const ( func FindProcessName(network string, srcIP netip.Addr, srcPort int) (uint32, string, error) { return findProcessName(network, srcIP, srcPort) } - -func FindPackageName(metadata *constant.Metadata) (string, error) { - return "", nil -} diff --git a/component/process/patch_android.go b/component/process/process_android.go similarity index 93% rename from component/process/patch_android.go rename to component/process/process_android.go index f9018557..fd5d3b6c 100644 --- a/component/process/patch_android.go +++ b/component/process/process_android.go @@ -1,4 +1,4 @@ -// +build android,cmfa +//go:build android && cmfa package process diff --git a/component/process/process_common.go b/component/process/process_common.go new file mode 100644 index 00000000..fa7eeb9f --- /dev/null +++ b/component/process/process_common.go @@ -0,0 +1,9 @@ +//go:build !(android && cmfa) + +package process + +import "github.com/metacubex/mihomo/constant" + +func FindPackageName(metadata *constant.Metadata) (string, error) { + return "", nil +} diff --git a/config/config.go b/config/config.go index c6c9aa55..c2b389c3 100644 --- a/config/config.go +++ b/config/config.go @@ -324,7 +324,7 @@ type RawConfig struct { RawTLS TLS `yaml:"tls"` Listeners []map[string]any `yaml:"listeners"` - ClashForAndroid RawClashForAndroid `yaml:"clash-for-android" json:"clash-for-android"` + ClashForAndroid RawClashForAndroid `yaml:"clash-for-android" json:"clash-for-android"` } type GeoXUrl struct { @@ -554,7 +554,7 @@ func ParseRawConfig(rawCfg *RawConfig) (*Config, error) { config.DNS = dnsCfg err = parseTun(rawCfg.Tun, config.General) - if !features.Contains("cmfa") && err != nil { + if !features.CMFA && err != nil { return nil, err } diff --git a/constant/features/cmfa.go b/constant/features/cmfa.go index e93ad625..9a81d82f 100644 --- a/constant/features/cmfa.go +++ b/constant/features/cmfa.go @@ -2,6 +2,4 @@ package features -func init() { - TAGS = append(TAGS, "cmfa") -} +const CMFA = true diff --git a/constant/features/cmfa_stub.go b/constant/features/cmfa_stub.go new file mode 100644 index 00000000..f7ef0827 --- /dev/null +++ b/constant/features/cmfa_stub.go @@ -0,0 +1,5 @@ +//go:build !cmfa + +package features + +const CMFA = false diff --git a/constant/features/low_memory.go b/constant/features/low_memory.go index ad52b555..2f0f4d50 100644 --- a/constant/features/low_memory.go +++ b/constant/features/low_memory.go @@ -2,6 +2,4 @@ package features -func init() { - TAGS = append(TAGS, "with_low_memory") -} +const WithLowMemory = true diff --git a/constant/features/low_memory_stub.go b/constant/features/low_memory_stub.go new file mode 100644 index 00000000..00b0b947 --- /dev/null +++ b/constant/features/low_memory_stub.go @@ -0,0 +1,5 @@ +//go:build !with_low_memory + +package features + +const WithLowMemory = false diff --git a/constant/features/no_fake_tcp.go b/constant/features/no_fake_tcp.go index f536a066..a9e09728 100644 --- a/constant/features/no_fake_tcp.go +++ b/constant/features/no_fake_tcp.go @@ -2,6 +2,4 @@ package features -func init() { - TAGS = append(TAGS, "no_fake_tcp") -} +const NoFakeTCP = true diff --git a/constant/features/no_fake_tcp_stub.go b/constant/features/no_fake_tcp_stub.go new file mode 100644 index 00000000..f1620a2b --- /dev/null +++ b/constant/features/no_fake_tcp_stub.go @@ -0,0 +1,5 @@ +//go:build !no_fake_tcp + +package features + +const NoFakeTCP = false diff --git a/constant/features/tags.go b/constant/features/tags.go index 8fe639a0..12a5fbdd 100644 --- a/constant/features/tags.go +++ b/constant/features/tags.go @@ -1,11 +1,17 @@ package features -import( - "golang.org/x/exp/slices" -) - -var TAGS = make([]string, 0, 0) - -func Contains(feat string) (bool) { - return slices.Contains(TAGS, feat) -} \ No newline at end of file +func Tags() (tags []string) { + if CMFA { + tags = append(tags, "cmfa") + } + if WithLowMemory { + tags = append(tags, "with_low_memory") + } + if NoFakeTCP { + tags = append(tags, "no_fake_tcp") + } + if WithGVisor { + tags = append(tags, "with_gvisor") + } + return +} diff --git a/constant/features/with_gvisor.go b/constant/features/with_gvisor.go index 1b3417b3..c00cc34e 100644 --- a/constant/features/with_gvisor.go +++ b/constant/features/with_gvisor.go @@ -2,6 +2,4 @@ package features -func init() { - TAGS = append(TAGS, "with_gvisor") -} +const WithGVisor = true diff --git a/constant/features/with_gvisor_stub.go b/constant/features/with_gvisor_stub.go new file mode 100644 index 00000000..5684c1b1 --- /dev/null +++ b/constant/features/with_gvisor_stub.go @@ -0,0 +1,5 @@ +//go:build !with_gvisor + +package features + +const WithGVisor = false diff --git a/dns/dhcp.go b/dns/dhcp.go index 09aec799..d4944a96 100644 --- a/dns/dhcp.go +++ b/dns/dhcp.go @@ -1,4 +1,4 @@ -// +build !cmfa +//go:build !(android && cmfa) package dns diff --git a/dns/patch_android.go b/dns/patch_android.go index 8ffa5af6..8cb4286e 100644 --- a/dns/patch_android.go +++ b/dns/patch_android.go @@ -1,4 +1,4 @@ -// +build android,cmfa +//go:build android && cmfa package dns diff --git a/dns/patch_common.go b/dns/patch_common.go index afb60d6d..fae1e126 100644 --- a/dns/patch_common.go +++ b/dns/patch_common.go @@ -1,7 +1,6 @@ -// +build !cmfa +//go:build !(android && cmfa) package dns func UpdateIsolateHandler(resolver *Resolver, mapper *ResolverEnhancer) { - return -} \ No newline at end of file +} diff --git a/dns/server.go b/dns/server.go index 8371dbd8..d45fb5eb 100644 --- a/dns/server.go +++ b/dns/server.go @@ -50,10 +50,10 @@ func (s *Server) SetHandler(handler handler) { } func ReCreateServer(addr string, resolver *Resolver, mapper *ResolverEnhancer) { - if features.Contains("cmfa") { + if features.CMFA { UpdateIsolateHandler(resolver, mapper) } - + if addr == address && resolver != nil { handler := NewHandler(resolver, mapper) server.SetHandler(handler) diff --git a/hub/executor/executor.go b/hub/executor/executor.go index 88da9d5d..f8ba31a5 100644 --- a/hub/executor/executor.go +++ b/hub/executor/executor.go @@ -25,6 +25,7 @@ import ( "github.com/metacubex/mihomo/component/trie" "github.com/metacubex/mihomo/config" C "github.com/metacubex/mihomo/constant" + "github.com/metacubex/mihomo/constant/features" "github.com/metacubex/mihomo/constant/provider" "github.com/metacubex/mihomo/dns" "github.com/metacubex/mihomo/listener" @@ -35,7 +36,6 @@ import ( "github.com/metacubex/mihomo/log" "github.com/metacubex/mihomo/ntp" "github.com/metacubex/mihomo/tunnel" - "github.com/metacubex/mihomo/constant/features" ) var mux sync.Mutex @@ -171,7 +171,7 @@ func updateListeners(general *config.General, listeners map[string]C.InboundList listener.ReCreateHTTP(general.Port, tunnel.Tunnel) listener.ReCreateSocks(general.SocksPort, tunnel.Tunnel) listener.ReCreateRedir(general.RedirPort, tunnel.Tunnel) - if !features.Contains("cmfa") { + if !features.CMFA { listener.ReCreateAutoRedir(general.EBpf.AutoRedir, tunnel.Tunnel) } listener.ReCreateTProxy(general.TProxyPort, tunnel.Tunnel) diff --git a/listener/http/patch_android.go b/listener/http/patch_android.go index 46d2eee5..b1b8bfc7 100644 --- a/listener/http/patch_android.go +++ b/listener/http/patch_android.go @@ -1,4 +1,4 @@ -// +build android,cmfa +//go:build android && cmfa package http diff --git a/listener/http/server.go b/listener/http/server.go index 06797273..d5e20601 100644 --- a/listener/http/server.go +++ b/listener/http/server.go @@ -66,7 +66,7 @@ func NewWithAuthenticate(addr string, tunnel C.Tunnel, authenticate bool, additi } continue } - if features.Contains("cmfa") { + if features.CMFA { if t, ok := conn.(*net.TCPConn); ok { t.SetKeepAlive(false) } diff --git a/main.go b/main.go index fd1e065c..01edee9f 100644 --- a/main.go +++ b/main.go @@ -48,8 +48,8 @@ func main() { if version { fmt.Printf("Mihomo Meta %s %s %s with %s %s\n", C.Version, runtime.GOOS, runtime.GOARCH, runtime.Version(), C.BuildTime) - if len(features.TAGS) != 0 { - fmt.Printf("Use tags: %s\n", strings.Join(features.TAGS, ", ")) + if tags := features.Tags(); len(tags) != 0 { + fmt.Printf("Use tags: %s\n", strings.Join(tags, ", ")) } return diff --git a/rules/provider/parse.go b/rules/provider/parse.go index 8319b737..a867d570 100644 --- a/rules/provider/parse.go +++ b/rules/provider/parse.go @@ -63,7 +63,7 @@ func ParseRuleProvider(name string, mapping map[string]interface{}, parse func(t case "http": if schema.Path != "" { path := C.Path.Resolve(schema.Path) - if !features.Contains("cmfa") && !C.Path.IsSafePath(path) { + if !features.CMFA && !C.Path.IsSafePath(path) { return nil, fmt.Errorf("%w: %s", errSubPath, path) } vehicle = resource.NewHTTPVehicle(schema.URL, path) diff --git a/rules/provider/patch_android.go b/rules/provider/patch_android.go index 28a0d47d..2bd5ffc8 100644 --- a/rules/provider/patch_android.go +++ b/rules/provider/patch_android.go @@ -1,4 +1,4 @@ -// +build android,cmfa +//go:build android && cmfa package provider diff --git a/tunnel/statistic/patch_android.go b/tunnel/statistic/patch_android.go index 235ab701..f1eee346 100644 --- a/tunnel/statistic/patch_android.go +++ b/tunnel/statistic/patch_android.go @@ -1,4 +1,4 @@ -// +build android,cmfa +//go:build android && cmfa package statistic diff --git a/tunnel/tunnel.go b/tunnel/tunnel.go index bbc2d1a1..c37b0c7d 100644 --- a/tunnel/tunnel.go +++ b/tunnel/tunnel.go @@ -621,7 +621,7 @@ func match(metadata *C.Metadata) (C.Proxy, C.Rule, error) { if attemptProcessLookup && !findProcessMode.Off() && (findProcessMode.Always() || rule.ShouldFindProcess()) { attemptProcessLookup = false - if !features.Contains("cmfa") { + if !features.CMFA { // normal check for process uid, path, err := P.FindProcessName(metadata.NetWork.String(), metadata.SrcIP, int(metadata.SrcPort)) if err != nil {