From a9b10acac20ed4270df02c97870284f070fcb16f Mon Sep 17 00:00:00 2001 From: wwqgtxx Date: Tue, 27 Aug 2024 11:02:45 +0800 Subject: [PATCH] cleanup code --- .../src/main/golang/native/config/provider.go | 8 +- .../golang/native/{log_open.go => log.go} | 2 - core/src/main/golang/native/log_premium.go | 75 --------------- .../tun/{metadata_open.go => metadata.go} | 2 - .../golang/native/tun/metadata_premium.go | 29 ------ .../tunnel/{loopback_open.go => loopback.go} | 2 - .../golang/native/tunnel/loopback_premium.go | 7 -- .../{providers_open.go => providers.go} | 2 - .../golang/native/tunnel/providers_premium.go | 92 ------------------- 9 files changed, 6 insertions(+), 213 deletions(-) rename core/src/main/golang/native/{log_open.go => log.go} (98%) delete mode 100644 core/src/main/golang/native/log_premium.go rename core/src/main/golang/native/tun/{metadata_open.go => metadata.go} (95%) delete mode 100644 core/src/main/golang/native/tun/metadata_premium.go rename core/src/main/golang/native/tunnel/{loopback_open.go => loopback.go} (80%) delete mode 100644 core/src/main/golang/native/tunnel/loopback_premium.go rename core/src/main/golang/native/tunnel/{providers_open.go => providers.go} (98%) delete mode 100644 core/src/main/golang/native/tunnel/providers_premium.go diff --git a/core/src/main/golang/native/config/provider.go b/core/src/main/golang/native/config/provider.go index a2b7e153c..28506a5e6 100644 --- a/core/src/main/golang/native/config/provider.go +++ b/core/src/main/golang/native/config/provider.go @@ -25,10 +25,14 @@ func forEachProviders(rawCfg *config.RawConfig, fun func(index int, total int, k func destroyProviders(cfg *config.Config) { for _, p := range cfg.Providers { - _ = p.(io.Closer).Close() + if p, ok := p.(io.Closer); ok { + _ = p.Close() + } } for _, p := range cfg.RuleProviders { - _ = p.(io.Closer).Close() + if p, ok := p.(io.Closer); ok { + _ = p.Close() + } } } diff --git a/core/src/main/golang/native/log_open.go b/core/src/main/golang/native/log.go similarity index 98% rename from core/src/main/golang/native/log_open.go rename to core/src/main/golang/native/log.go index 647d9ceaa..0af650a07 100644 --- a/core/src/main/golang/native/log_open.go +++ b/core/src/main/golang/native/log.go @@ -1,5 +1,3 @@ -//go:build !premium - package main //#include "bridge.h" diff --git a/core/src/main/golang/native/log_premium.go b/core/src/main/golang/native/log_premium.go deleted file mode 100644 index 1ef7b0ed6..000000000 --- a/core/src/main/golang/native/log_premium.go +++ /dev/null @@ -1,75 +0,0 @@ -//go:build premium - -package main - -//#include "bridge.h" -import "C" - -import ( - "strings" - "time" - "unsafe" - - "github.com/metacubex/mihomo/log" -) - -type message struct { - Level string `json:"level"` - Message string `json:"message"` - Time int64 `json:"time"` -} - -func init() { - go func() { - sub := log.Subscribe() - defer log.UnSubscribe(sub) - - for msg := range sub { - - cPayload := C.CString(msg.Payload) - - switch msg.LogLevel { - case log.INFO: - C.log_info(cPayload) - case log.ERROR: - C.log_error(cPayload) - case log.WARNING: - C.log_warn(cPayload) - case log.DEBUG: - C.log_debug(cPayload) - case log.SILENT: - C.log_verbose(cPayload) - } - } - }() -} - -//export subscribeLogcat -func subscribeLogcat(remote unsafe.Pointer) { - go func(remote unsafe.Pointer) { - sub := log.Subscribe() - defer log.UnSubscribe(sub) - - for msg := range sub { - if msg.LogLevel < log.Level() && !strings.HasPrefix(msg.Payload, "[APP]") { - continue - } - - rMsg := &message{ - Level: msg.LogLevel.String(), - Message: msg.Payload, - Time: time.Now().UnixNano() / 1000 / 1000, - } - - if C.logcat_received(remote, marshalJson(rMsg)) != 0 { - C.release_object(remote) - - log.Debugln("Logcat subscriber closed") - - break - } - } - }(remote) - - log.Infoln("[APP] Logcat level: %s", log.Level().String()) -} diff --git a/core/src/main/golang/native/tun/metadata_open.go b/core/src/main/golang/native/tun/metadata.go similarity index 95% rename from core/src/main/golang/native/tun/metadata_open.go rename to core/src/main/golang/native/tun/metadata.go index 2f125454a..a2141abd1 100644 --- a/core/src/main/golang/native/tun/metadata_open.go +++ b/core/src/main/golang/native/tun/metadata.go @@ -1,5 +1,3 @@ -//go:build !premium - package tun import ( diff --git a/core/src/main/golang/native/tun/metadata_premium.go b/core/src/main/golang/native/tun/metadata_premium.go deleted file mode 100644 index 5ac41a774..000000000 --- a/core/src/main/golang/native/tun/metadata_premium.go +++ /dev/null @@ -1,29 +0,0 @@ -//go:build premium - -package tun - -import ( - "net" - "net/netip" - "strconv" - - C "github.com/metacubex/mihomo/constant" -) - -func createMetadata(lAddr, rAddr *net.TCPAddr) *C.Metadata { - srcAddr, _ := netip.AddrFromSlice(lAddr.IP) - dstAddr, _ := netip.AddrFromSlice(rAddr.IP) - - return &C.Metadata{ - NetWork: C.TCP, - Type: C.SOCKS5, - SrcIP: srcAddr, - DstIP: dstAddr, - SrcPort: strconv.Itoa(lAddr.Port), - DstPort: strconv.Itoa(rAddr.Port), - AddrType: C.AtypIPv4, - Host: "", - RawSrcAddr: lAddr, - RawDstAddr: rAddr, - } -} diff --git a/core/src/main/golang/native/tunnel/loopback_open.go b/core/src/main/golang/native/tunnel/loopback.go similarity index 80% rename from core/src/main/golang/native/tunnel/loopback_open.go rename to core/src/main/golang/native/tunnel/loopback.go index f94625ec8..bee13455c 100644 --- a/core/src/main/golang/native/tunnel/loopback_open.go +++ b/core/src/main/golang/native/tunnel/loopback.go @@ -1,5 +1,3 @@ -//go:build !premium - package tunnel import ( diff --git a/core/src/main/golang/native/tunnel/loopback_premium.go b/core/src/main/golang/native/tunnel/loopback_premium.go deleted file mode 100644 index 456a09dc7..000000000 --- a/core/src/main/golang/native/tunnel/loopback_premium.go +++ /dev/null @@ -1,7 +0,0 @@ -//go:build premium - -package tunnel - -import "net/netip" - -var loopback = netip.MustParseAddr("127.0.0.1") diff --git a/core/src/main/golang/native/tunnel/providers_open.go b/core/src/main/golang/native/tunnel/providers.go similarity index 98% rename from core/src/main/golang/native/tunnel/providers_open.go rename to core/src/main/golang/native/tunnel/providers.go index e43443011..26dfe0f68 100644 --- a/core/src/main/golang/native/tunnel/providers_open.go +++ b/core/src/main/golang/native/tunnel/providers.go @@ -1,5 +1,3 @@ -//go:build !premium - package tunnel import ( diff --git a/core/src/main/golang/native/tunnel/providers_premium.go b/core/src/main/golang/native/tunnel/providers_premium.go deleted file mode 100644 index deadd3e97..000000000 --- a/core/src/main/golang/native/tunnel/providers_premium.go +++ /dev/null @@ -1,92 +0,0 @@ -//go:build premium - -package tunnel - -import ( - "errors" - "fmt" - "time" - - P "github.com/metacubex/mihomo/adapter/provider" - "github.com/metacubex/mihomo/constant/provider" - "github.com/metacubex/mihomo/log" - "github.com/metacubex/mihomo/tunnel" -) - -var ErrInvalidType = errors.New("invalid type") - -type Provider struct { - Name string `json:"name"` - VehicleType string `json:"vehicleType"` - Type string `json:"type"` - UpdatedAt int64 `json:"updatedAt"` -} - -func QueryProviders() []*Provider { - r := tunnel.RuleProviders() - p := tunnel.ProxyProviders() - - providers := make([]provider.Provider, 0, len(r)+len(p)) - - for _, rule := range r { - if rule.VehicleType() == provider.Compatible { - continue - } - - providers = append(providers, rule) - } - - for _, proxy := range p { - if proxy.VehicleType() == provider.Compatible { - continue - } - - providers = append(providers, proxy) - } - - result := make([]*Provider, 0, len(providers)) - - for _, p := range providers { - updatedAt := time.Time{} - - if s, ok := p.(P.UpdatableProvider[any]); ok { - updatedAt = s.UpdatedAt() - } - - result = append(result, &Provider{ - Name: p.Name(), - VehicleType: p.VehicleType().String(), - Type: p.Type().String(), - UpdatedAt: updatedAt.UnixNano() / 1000 / 1000, - }) - } - - return result -} - -func UpdateProvider(t string, name string) error { - err := ErrInvalidType - - switch t { - case "Rule": - p := tunnel.RuleProviders()[name] - if p == nil { - return fmt.Errorf("%s not found", name) - } - - err = p.Update() - case "Proxy": - p := tunnel.ProxyProviders()[name] - if p == nil { - return fmt.Errorf("%s not found", name) - } - - err = p.Update() - } - - if err != nil { - log.Warnln("Updating provider %s: %s", name, err.Error()) - } - - return err -}