From 59987747e52918315d57e2f4b40b61e9cb4c21fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Fri, 4 Aug 2023 17:13:46 +0800 Subject: [PATCH] platform: Improve status --- constant/proxy.go | 45 +++++++++++++++++++++++++++- experimental/clashapi/proxies.go | 30 +------------------ experimental/libbox/command_group.go | 6 ++++ experimental/libbox/setup.go | 4 +++ inbound/builder.go | 2 +- inbound/socks.go | 2 +- option/inbound.go | 4 +-- option/outbound.go | 4 +-- outbound/builder.go | 2 +- outbound/socks.go | 2 +- test/vless_test.go | 2 +- 11 files changed, 64 insertions(+), 39 deletions(-) diff --git a/constant/proxy.go b/constant/proxy.go index f875d7e0..7fc12d36 100644 --- a/constant/proxy.go +++ b/constant/proxy.go @@ -7,7 +7,7 @@ const ( TypeDirect = "direct" TypeBlock = "block" TypeDNS = "dns" - TypeSocks = "socks" + TypeSOCKS = "socks" TypeHTTP = "http" TypeMixed = "mixed" TypeShadowsocks = "shadowsocks" @@ -27,3 +27,46 @@ const ( TypeSelector = "selector" TypeURLTest = "urltest" ) + +func ProxyDisplayName(proxyType string) string { + switch proxyType { + case TypeDirect: + return "Direct" + case TypeBlock: + return "Block" + case TypeDNS: + return "DNS" + case TypeSOCKS: + return "SOCKS" + case TypeHTTP: + return "HTTP" + case TypeShadowsocks: + return "Shadowsocks" + case TypeVMess: + return "VMess" + case TypeTrojan: + return "Trojan" + case TypeNaive: + return "Naive" + case TypeWireGuard: + return "WireGuard" + case TypeHysteria: + return "Hysteria" + case TypeTor: + return "Tor" + case TypeSSH: + return "SSH" + case TypeShadowTLS: + return "ShadowTLS" + case TypeShadowsocksR: + return "ShadowsocksR" + case TypeVLESS: + return "VLESS" + case TypeSelector: + return "Selector" + case TypeURLTest: + return "URLTest" + default: + return "Unknown" + } +} diff --git a/experimental/clashapi/proxies.go b/experimental/clashapi/proxies.go index 8e3e19f4..f312481c 100644 --- a/experimental/clashapi/proxies.go +++ b/experimental/clashapi/proxies.go @@ -63,38 +63,10 @@ func proxyInfo(server *Server, detour adapter.Outbound) *badjson.JSONObject { var info badjson.JSONObject var clashType string switch detour.Type() { - case C.TypeDirect: - clashType = "Direct" case C.TypeBlock: clashType = "Reject" - case C.TypeSocks: - clashType = "Socks" - case C.TypeHTTP: - clashType = "HTTP" - case C.TypeShadowsocks: - clashType = "Shadowsocks" - case C.TypeVMess: - clashType = "VMess" - case C.TypeTrojan: - clashType = "Trojan" - case C.TypeHysteria: - clashType = "Hysteria" - case C.TypeWireGuard: - clashType = "WireGuard" - case C.TypeShadowsocksR: - clashType = "ShadowsocksR" - case C.TypeVLESS: - clashType = "VLESS" - case C.TypeTor: - clashType = "Tor" - case C.TypeSSH: - clashType = "SSH" - case C.TypeSelector: - clashType = "Selector" - case C.TypeURLTest: - clashType = "URLTest" default: - clashType = "Direct" + clashType = C.ProxyDisplayName(detour.Type()) } info.Put("type", clashType) info.Put("name", detour.Tag()) diff --git a/experimental/libbox/command_group.go b/experimental/libbox/command_group.go index 6ff8770e..fef043cb 100644 --- a/experimental/libbox/command_group.go +++ b/experimental/libbox/command_group.go @@ -4,6 +4,7 @@ import ( "encoding/binary" "io" "net" + "time" "github.com/sagernet/sing-box/adapter" "github.com/sagernet/sing-box/common/urltest" @@ -71,6 +72,11 @@ func (s *CommandServer) handleGroupConn(conn net.Conn) error { } } select { + case <-ctx.Done(): + return ctx.Err() + case <-time.After(2 * time.Second): + } + select { case <-ctx.Done(): return ctx.Err() case <-s.urlTestUpdate: diff --git a/experimental/libbox/setup.go b/experimental/libbox/setup.go index 2348a03a..e028151f 100644 --- a/experimental/libbox/setup.go +++ b/experimental/libbox/setup.go @@ -48,3 +48,7 @@ func Version() string { func FormatBytes(length int64) string { return humanize.IBytes(uint64(length)) } + +func ProxyDisplayType(proxyType string) string { + return C.ProxyDisplayName(proxyType) +} diff --git a/inbound/builder.go b/inbound/builder.go index d62225a1..5243e6b8 100644 --- a/inbound/builder.go +++ b/inbound/builder.go @@ -24,7 +24,7 @@ func New(ctx context.Context, router adapter.Router, logger log.ContextLogger, o return NewTProxy(ctx, router, logger, options.Tag, options.TProxyOptions), nil case C.TypeDirect: return NewDirect(ctx, router, logger, options.Tag, options.DirectOptions), nil - case C.TypeSocks: + case C.TypeSOCKS: return NewSocks(ctx, router, logger, options.Tag, options.SocksOptions), nil case C.TypeHTTP: return NewHTTP(ctx, router, logger, options.Tag, options.HTTPOptions) diff --git a/inbound/socks.go b/inbound/socks.go index 7471341f..8cc256ef 100644 --- a/inbound/socks.go +++ b/inbound/socks.go @@ -27,7 +27,7 @@ type Socks struct { func NewSocks(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.SocksInboundOptions) *Socks { inbound := &Socks{ myInboundAdapter{ - protocol: C.TypeSocks, + protocol: C.TypeSOCKS, network: []string{N.NetworkTCP}, ctx: ctx, router: router, diff --git a/option/inbound.go b/option/inbound.go index 9533532b..ef56be90 100644 --- a/option/inbound.go +++ b/option/inbound.go @@ -38,7 +38,7 @@ func (h Inbound) MarshalJSON() ([]byte, error) { v = h.TProxyOptions case C.TypeDirect: v = h.DirectOptions - case C.TypeSocks: + case C.TypeSOCKS: v = h.SocksOptions case C.TypeHTTP: v = h.HTTPOptions @@ -79,7 +79,7 @@ func (h *Inbound) UnmarshalJSON(bytes []byte) error { v = &h.TProxyOptions case C.TypeDirect: v = &h.DirectOptions - case C.TypeSocks: + case C.TypeSOCKS: v = &h.SocksOptions case C.TypeHTTP: v = &h.HTTPOptions diff --git a/option/outbound.go b/option/outbound.go index 6a394b74..5b8eb936 100644 --- a/option/outbound.go +++ b/option/outbound.go @@ -36,7 +36,7 @@ func (h Outbound) MarshalJSON() ([]byte, error) { v = h.DirectOptions case C.TypeBlock, C.TypeDNS: v = nil - case C.TypeSocks: + case C.TypeSOCKS: v = h.SocksOptions case C.TypeHTTP: v = h.HTTPOptions @@ -81,7 +81,7 @@ func (h *Outbound) UnmarshalJSON(bytes []byte) error { v = &h.DirectOptions case C.TypeBlock, C.TypeDNS: v = nil - case C.TypeSocks: + case C.TypeSOCKS: v = &h.SocksOptions case C.TypeHTTP: v = &h.HTTPOptions diff --git a/outbound/builder.go b/outbound/builder.go index 45d48425..f32c5de6 100644 --- a/outbound/builder.go +++ b/outbound/builder.go @@ -27,7 +27,7 @@ func New(ctx context.Context, router adapter.Router, logger log.ContextLogger, t return NewBlock(logger, tag), nil case C.TypeDNS: return NewDNS(router, tag), nil - case C.TypeSocks: + case C.TypeSOCKS: return NewSocks(router, logger, tag, options.SocksOptions) case C.TypeHTTP: return NewHTTP(router, logger, tag, options.HTTPOptions) diff --git a/outbound/socks.go b/outbound/socks.go index f314c9be..97579fd3 100644 --- a/outbound/socks.go +++ b/outbound/socks.go @@ -39,7 +39,7 @@ func NewSocks(router adapter.Router, logger log.ContextLogger, tag string, optio } outbound := &Socks{ myOutboundAdapter: myOutboundAdapter{ - protocol: C.TypeSocks, + protocol: C.TypeSOCKS, network: options.Network.Build(), router: router, logger: logger, diff --git a/test/vless_test.go b/test/vless_test.go index a5f38052..7757bc11 100644 --- a/test/vless_test.go +++ b/test/vless_test.go @@ -491,7 +491,7 @@ func testVLESSXrayInbound(t *testing.T, flow string) { }, }, { - Type: C.TypeSocks, + Type: C.TypeSOCKS, Tag: "vless-out", SocksOptions: option.SocksOutboundOptions{ ServerOptions: option.ServerOptions{