From 3e48086e67eb1f99ced9d7b2ee839e65b169d16f Mon Sep 17 00:00:00 2001 From: Hiddify Date: Sat, 16 Mar 2024 09:02:55 +0100 Subject: [PATCH] fix: tunnel service issue --- Makefile | 1 + build_windows.bat | 2 +- cmd/cmd_tunnel_service.go | 28 +++++++++++++++++++---- config/admin_service_commander.go | 37 +++++++++++++++++++++++++++---- v2/coreinfo.go | 3 ++- v2/tunnel_platform_service.go | 3 +-- 6 files changed, 62 insertions(+), 12 deletions(-) diff --git a/Makefile b/Makefile index b467408..4debb3b 100644 --- a/Makefile +++ b/Makefile @@ -52,6 +52,7 @@ windows-amd64: curl http://localhost:18020/exit || echo "exited" env GOOS=windows GOARCH=amd64 CC=x86_64-w64-mingw32-gcc $(GOBUILDLIB) -o $(BINDIR)/$(LIBNAME).dll ./custom go install -mod=readonly github.com/akavel/rsrc@latest ||echo "rsrc error in installation" + go run ./cli tunnel exit cp $(BINDIR)/$(LIBNAME).dll ./$(LIBNAME).dll $$(go env GOPATH)/bin/rsrc -ico ./assets/hiddify-cli.ico -o ./cli/bydll/cli.syso ||echo "rsrc error in syso" env GOOS=windows GOARCH=amd64 CC=x86_64-w64-mingw32-gcc CGO_LDFLAGS="$(LIBNAME).dll" $(GOBUILDSRV) -o $(BINDIR)/$(CLINAME).exe ./cli/bydll diff --git a/build_windows.bat b/build_windows.bat index c2fe184..1604054 100644 --- a/build_windows.bat +++ b/build_windows.bat @@ -3,7 +3,7 @@ set GOOS=windows set GOARCH=amd64 set CC=x86_64-w64-mingw32-gcc set CGO_ENABLED=1 -curl http://localhost:18020/exit || echo "Exited" +go run ./cli tunnel exit del bin\libcore.dll bin\HiddifyCli.exe set CGO_LDFLAGS= go build -trimpath -tags with_gvisor,with_quic,with_wireguard,with_ech,with_utls,with_clash_api,with_grpc -ldflags="-w -s" -buildmode=c-shared -o bin/libcore.dll ./custom diff --git a/cmd/cmd_tunnel_service.go b/cmd/cmd_tunnel_service.go index de5b37b..22b189a 100644 --- a/cmd/cmd_tunnel_service.go +++ b/cmd/cmd_tunnel_service.go @@ -2,7 +2,9 @@ package cmd import ( "fmt" + "time" + "github.com/hiddify/libcore/config" v2 "github.com/hiddify/libcore/v2" "github.com/spf13/cobra" @@ -10,12 +12,30 @@ import ( var commandService = &cobra.Command{ Use: "tunnel run/start/stop/install/uninstall", - Short: "Tunnel Service run/start/stop/install/uninstall", - ValidArgs: []string{"run", "start", "stop", "install", "uninstall"}, + Short: "Tunnel Service run/start/stop/install/uninstall/activate/deactivate/exit", + ValidArgs: []string{"run", "start", "stop", "install", "uninstall", "activate", "deactivate", "exit"}, Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs), Run: func(cmd *cobra.Command, args []string) { arg := args[0] - code, out := v2.StartTunnelService(arg) - fmt.Printf("exitCode:%d msg=%s", code, out) + switch arg { + case "activate": + config.ActivateTunnelService(config.ConfigOptions{ + InboundOptions: config.InboundOptions{ + EnableTunService: true, + MixedPort: 2334, + TUNStack: "gvisor", + }, + }) + <-time.After(1 * time.Second) + + case "deactivate": + config.DeactivateTunnelServiceForce() + case "exit": + config.ExitTunnelService() + default: + code, out := v2.StartTunnelService(arg) + fmt.Printf("exitCode:%d msg=%s", code, out) + } + }, } diff --git a/config/admin_service_commander.go b/config/admin_service_commander.go index 3b24984..c5ced53 100644 --- a/config/admin_service_commander.go +++ b/config/admin_service_commander.go @@ -35,15 +35,22 @@ func ActivateTunnelService(opt ConfigOptions) (bool, error) { go startTunnelRequestWithFailover(opt, true) return true, nil } - +func DeactivateTunnelServiceForce() (bool, error) { + return stopTunnelRequest() +} func DeactivateTunnelService() (bool, error) { + // if !isSupportedOS() { // return true, nil // } + if tunnelServiceRunning { - stopTunnelRequest() + res, err := stopTunnelRequest() + if err != nil { + tunnelServiceRunning = false + } + return res, err } - tunnelServiceRunning = false return true, nil } @@ -91,14 +98,36 @@ func stopTunnelRequest() (bool, error) { conn, err := grpc.Dial("127.0.0.1:18020", grpc.WithInsecure()) if err != nil { log.Printf("did not connect: %v", err) + return false, err } defer conn.Close() c := pb.NewTunnelServiceClient(conn) ctx, cancel := context.WithTimeout(context.Background(), time.Second*1) defer cancel() - _, err = c.Stop(ctx, &pb.Empty{}) + res, err := c.Stop(ctx, &pb.Empty{}) if err != nil { + log.Printf("did not Stopped: %v %v", res, err) + return false, err + } + + return true, nil +} + +func ExitTunnelService() (bool, error) { + conn, err := grpc.Dial("127.0.0.1:18020", grpc.WithInsecure()) + if err != nil { + log.Printf("did not connect: %v", err) + return false, err + } + defer conn.Close() + c := pb.NewTunnelServiceClient(conn) + ctx, cancel := context.WithTimeout(context.Background(), time.Second*1) + defer cancel() + + res, err := c.Exit(ctx, &pb.Empty{}) + if res != nil { + log.Printf("did not exit: %v %v", res, err) return false, err } diff --git a/v2/coreinfo.go b/v2/coreinfo.go index 1b1271c..2d9daca 100644 --- a/v2/coreinfo.go +++ b/v2/coreinfo.go @@ -2,6 +2,7 @@ package v2 import ( "encoding/json" + "fmt" "time" "github.com/hiddify/libcore/bridge" @@ -13,7 +14,7 @@ var coreInfoObserver = NewObserver[pb.CoreInfoResponse](10) var CoreState = pb.CoreState_STOPPED func SetCoreStatus(state pb.CoreState, msgType pb.MessageType, message string) pb.CoreInfoResponse { - Log(pb.LogLevel_INFO, pb.LogType_CORE, message) + Log(pb.LogLevel_INFO, pb.LogType_CORE, fmt.Sprintf("%s: %s %s", state.String(), msgType.String(), message)) CoreState = state info := pb.CoreInfoResponse{ CoreState: state, diff --git a/v2/tunnel_platform_service.go b/v2/tunnel_platform_service.go index 57e7d4e..0931f35 100644 --- a/v2/tunnel_platform_service.go +++ b/v2/tunnel_platform_service.go @@ -16,8 +16,7 @@ type hiddifyNext struct{} var port int = 18020 func (m *hiddifyNext) Start(s service.Service) error { - go StartTunnelGrpcServer(fmt.Sprintf("127.0.0.1:%d", port)) - return nil + return StartTunnelGrpcServer(fmt.Sprintf("127.0.0.1:%d", port)) } func (m *hiddifyNext) Stop(s service.Service) error { _, err := Stop()