Skip to content

Commit

Permalink
Added V2Ray into the disconnect command
Browse files Browse the repository at this point in the history
  • Loading branch information
bsrinivas8687 committed Feb 20, 2023
1 parent 6d883f6 commit c98caaf
Showing 1 changed file with 31 additions and 22 deletions.
53 changes: 31 additions & 22 deletions cmd/disconnect.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package cmd

import (
"encoding/json"
"os"
"path/filepath"

"github.com/cosmos/cosmos-sdk/client"
"github.com/spf13/cobra"

"github.com/sentinel-official/cli-client/services/v2ray"
v2raytypes "github.com/sentinel-official/cli-client/services/v2ray/types"
"github.com/sentinel-official/cli-client/services/wireguard"
wireguardtypes "github.com/sentinel-official/cli-client/services/wireguard/types"
clienttypes "github.com/sentinel-official/cli-client/types"
Expand All @@ -27,36 +30,42 @@ func DisconnectCmd() *cobra.Command {
statusFilePath = filepath.Join(ctx.HomeDir, "status.json")
)

if err := status.LoadFromPath(statusFilePath); err != nil {
if err = status.LoadFromPath(statusFilePath); err != nil {
return err
}

if status.IFace != "" {
var (
service = wireguard.NewWireGuard().
WithConfig(
&wireguardtypes.Config{
Name: status.IFace,
},
)
)
var service clienttypes.Service
if status.Type == 1 {
var cfg wireguardtypes.Config
if err = json.Unmarshal(status.Info, &cfg); err != nil {
return err
}

if service.IsUp() {
if err := service.PreDown(); err != nil {
return err
}
if err := service.Down(); err != nil {
return err
}
if err := service.PostDown(); err != nil {
return err
}
service = wireguard.NewWireGuard(&cfg)
} else if status.Type == 2 {
var cfg v2raytypes.Config
if err = json.Unmarshal(status.Info, &cfg); err != nil {
return err
}

return os.Remove(statusFilePath)
service = v2ray.NewV2Ray(&cfg)
} else {
return nil
}

if service.IsUp() {
if err = service.PreDown(); err != nil {
return err
}
if err = service.Down(); err != nil {
return err
}
if err = service.PostDown(); err != nil {
return err
}
}

return nil
return os.Remove(statusFilePath)
},
}

Expand Down

0 comments on commit c98caaf

Please sign in to comment.