fix: tunnel service issue

This commit is contained in:
Hiddify
2024-03-16 09:02:55 +01:00
parent 7bc75b890f
commit 3e48086e67
6 changed files with 62 additions and 12 deletions

View File

@@ -52,6 +52,7 @@ windows-amd64:
curl http://localhost:18020/exit || echo "exited" curl http://localhost:18020/exit || echo "exited"
env GOOS=windows GOARCH=amd64 CC=x86_64-w64-mingw32-gcc $(GOBUILDLIB) -o $(BINDIR)/$(LIBNAME).dll ./custom 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 install -mod=readonly github.com/akavel/rsrc@latest ||echo "rsrc error in installation"
go run ./cli tunnel exit
cp $(BINDIR)/$(LIBNAME).dll ./$(LIBNAME).dll 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" $$(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 env GOOS=windows GOARCH=amd64 CC=x86_64-w64-mingw32-gcc CGO_LDFLAGS="$(LIBNAME).dll" $(GOBUILDSRV) -o $(BINDIR)/$(CLINAME).exe ./cli/bydll

View File

@@ -3,7 +3,7 @@ set GOOS=windows
set GOARCH=amd64 set GOARCH=amd64
set CC=x86_64-w64-mingw32-gcc set CC=x86_64-w64-mingw32-gcc
set CGO_ENABLED=1 set CGO_ENABLED=1
curl http://localhost:18020/exit || echo "Exited" go run ./cli tunnel exit
del bin\libcore.dll bin\HiddifyCli.exe del bin\libcore.dll bin\HiddifyCli.exe
set CGO_LDFLAGS= 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 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

View File

@@ -2,7 +2,9 @@ package cmd
import ( import (
"fmt" "fmt"
"time"
"github.com/hiddify/libcore/config"
v2 "github.com/hiddify/libcore/v2" v2 "github.com/hiddify/libcore/v2"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@@ -10,12 +12,30 @@ import (
var commandService = &cobra.Command{ var commandService = &cobra.Command{
Use: "tunnel run/start/stop/install/uninstall", Use: "tunnel run/start/stop/install/uninstall",
Short: "Tunnel Service run/start/stop/install/uninstall", Short: "Tunnel Service run/start/stop/install/uninstall/activate/deactivate/exit",
ValidArgs: []string{"run", "start", "stop", "install", "uninstall"}, ValidArgs: []string{"run", "start", "stop", "install", "uninstall", "activate", "deactivate", "exit"},
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs), Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
arg := args[0] arg := args[0]
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) code, out := v2.StartTunnelService(arg)
fmt.Printf("exitCode:%d msg=%s", code, out) fmt.Printf("exitCode:%d msg=%s", code, out)
}
}, },
} }

View File

@@ -35,15 +35,22 @@ func ActivateTunnelService(opt ConfigOptions) (bool, error) {
go startTunnelRequestWithFailover(opt, true) go startTunnelRequestWithFailover(opt, true)
return true, nil return true, nil
} }
func DeactivateTunnelServiceForce() (bool, error) {
return stopTunnelRequest()
}
func DeactivateTunnelService() (bool, error) { func DeactivateTunnelService() (bool, error) {
// if !isSupportedOS() { // if !isSupportedOS() {
// return true, nil // return true, nil
// } // }
if tunnelServiceRunning { if tunnelServiceRunning {
stopTunnelRequest() res, err := stopTunnelRequest()
} if err != nil {
tunnelServiceRunning = false tunnelServiceRunning = false
}
return res, err
}
return true, nil return true, nil
} }
@@ -91,14 +98,36 @@ func stopTunnelRequest() (bool, error) {
conn, err := grpc.Dial("127.0.0.1:18020", grpc.WithInsecure()) conn, err := grpc.Dial("127.0.0.1:18020", grpc.WithInsecure())
if err != nil { if err != nil {
log.Printf("did not connect: %v", err) log.Printf("did not connect: %v", err)
return false, err
} }
defer conn.Close() defer conn.Close()
c := pb.NewTunnelServiceClient(conn) c := pb.NewTunnelServiceClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*1) ctx, cancel := context.WithTimeout(context.Background(), time.Second*1)
defer cancel() defer cancel()
_, err = c.Stop(ctx, &pb.Empty{}) res, err := c.Stop(ctx, &pb.Empty{})
if err != nil { 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 return false, err
} }

View File

@@ -2,6 +2,7 @@ package v2
import ( import (
"encoding/json" "encoding/json"
"fmt"
"time" "time"
"github.com/hiddify/libcore/bridge" "github.com/hiddify/libcore/bridge"
@@ -13,7 +14,7 @@ var coreInfoObserver = NewObserver[pb.CoreInfoResponse](10)
var CoreState = pb.CoreState_STOPPED var CoreState = pb.CoreState_STOPPED
func SetCoreStatus(state pb.CoreState, msgType pb.MessageType, message string) pb.CoreInfoResponse { 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 CoreState = state
info := pb.CoreInfoResponse{ info := pb.CoreInfoResponse{
CoreState: state, CoreState: state,

View File

@@ -16,8 +16,7 @@ type hiddifyNext struct{}
var port int = 18020 var port int = 18020
func (m *hiddifyNext) Start(s service.Service) error { func (m *hiddifyNext) Start(s service.Service) error {
go StartTunnelGrpcServer(fmt.Sprintf("127.0.0.1:%d", port)) return StartTunnelGrpcServer(fmt.Sprintf("127.0.0.1:%d", port))
return nil
} }
func (m *hiddifyNext) Stop(s service.Service) error { func (m *hiddifyNext) Stop(s service.Service) error {
_, err := Stop() _, err := Stop()