Skip to content

Commit

Permalink
improvement: determine vpn activity by checking existence of 'zerops'…
Browse files Browse the repository at this point in the history
… network interface, no more pinging
  • Loading branch information
l-hellmann authored and jansaidl committed Oct 3, 2024
1 parent b657844 commit b456f8f
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/cmd/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func logoutCmd() *cmdBuilder.Cmd {
}

uxBlocks.PrintInfo(styles.InfoLine(i18n.T(i18n.LogoutVpnDisconnecting)))
if isVpnUp(ctx, uxBlocks, 1) {
if isVpnUp(uxBlocks) {
_ = disconnectVpn(ctx, uxBlocks)
}
uxBlocks.PrintInfo(styles.SuccessLine(i18n.T(i18n.LogoutSuccess)))
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func rootCmd() *cmdBuilder.Cmd {
}

var vpnStatusText string
if isVpnUp(ctx, cmdData.UxBlocks, 1) {
if isVpnUp(cmdData.UxBlocks) {
vpnStatusText = i18n.T(i18n.VpnCheckingConnectionIsActive)
} else {
vpnStatusText = i18n.T(i18n.VpnCheckingConnectionIsNotActive)
Expand Down
1 change: 1 addition & 0 deletions src/cmd/vpnDown.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
func vpnDownCmd() *cmdBuilder.Cmd {
return cmdBuilder.NewCmd().
Use("down").
Alias("d", "stop").
Short(i18n.T(i18n.CmdDescVpnDown)).
HelpFlag(i18n.T(i18n.CmdHelpVpnDown)).
LoggedUserRunFunc(func(ctx context.Context, cmdData *cmdBuilder.LoggedUserCmdData) error {
Expand Down
40 changes: 13 additions & 27 deletions src/cmd/vpnUp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ package cmd

import (
"context"
"net"
"os"
"time"

"github.com/pkg/errors"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"

"github.com/zeropsio/zcli/src/cliStorage"
"github.com/zeropsio/zcli/src/cmd/scope"
"github.com/zeropsio/zcli/src/cmdBuilder"
Expand All @@ -16,7 +15,6 @@ import (
"github.com/zeropsio/zcli/src/entity"
"github.com/zeropsio/zcli/src/file"
"github.com/zeropsio/zcli/src/i18n"
"github.com/zeropsio/zcli/src/nettools"
"github.com/zeropsio/zcli/src/uxBlock"
"github.com/zeropsio/zcli/src/uxBlock/styles"
"github.com/zeropsio/zcli/src/uxHelpers"
Expand All @@ -25,13 +23,13 @@ import (
"github.com/zeropsio/zerops-go/dto/input/path"
"github.com/zeropsio/zerops-go/types"
"github.com/zeropsio/zerops-go/types/uuid"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
)

const vpnCheckAddress = "logger.core.zerops"

func vpnUpCmd() *cmdBuilder.Cmd {
return cmdBuilder.NewCmd().
Use("up").
Alias("u", "start").
Short(i18n.T(i18n.CmdDescVpnUp)).
ScopeLevel(scope.Project).
Arg(scope.ProjectArgName, cmdBuilder.OptionalArg()).
Expand All @@ -40,7 +38,7 @@ func vpnUpCmd() *cmdBuilder.Cmd {
LoggedUserRunFunc(func(ctx context.Context, cmdData *cmdBuilder.LoggedUserCmdData) error {
uxBlocks := cmdData.UxBlocks

if isVpnUp(ctx, uxBlocks, 1) {
if isVpnUp(uxBlocks) {
if cmdData.Params.GetBool("auto-disconnect") {
err := disconnectVpn(ctx, uxBlocks)
if err != nil {
Expand Down Expand Up @@ -133,7 +131,7 @@ func vpnUpCmd() *cmdBuilder.Cmd {
}

// wait for the vpn to be up
if isVpnUp(ctx, uxBlocks, 6) {
if isVpnUp(uxBlocks) {
uxBlocks.PrintInfo(styles.SuccessLine(i18n.T(i18n.VpnUp)))
} else {
uxBlocks.PrintWarning(styles.WarningLine(i18n.T(i18n.VpnPingFailed)))
Expand All @@ -143,28 +141,16 @@ func vpnUpCmd() *cmdBuilder.Cmd {
})
}

func isVpnUp(ctx context.Context, uxBlocks uxBlock.UxBlocks, attempts int) bool {
p := []uxHelpers.Process{
{
F: func(ctx context.Context) error {
for i := 0; i < attempts; i++ {
err := nettools.Ping(ctx, vpnCheckAddress)
if err == nil {
return nil
}
// errNoSuchInterface copied from 'net' package, because it's private :)
var errNoSuchInterface = errors.New("no such network interface")

time.Sleep(time.Millisecond * 500)
}
return errors.New(i18n.T(i18n.VpnPingFailed))
},
RunningMessage: i18n.T(i18n.VpnCheckingConnection),
ErrorMessageMessage: "",
SuccessMessage: "",
},
func isVpnUp(uxBlocks uxBlock.UxBlocks) bool {
_, err := net.InterfaceByName(constants.WgInterfaceName)
opError := &net.OpError{}
// cannot use errors.Is(), because std error package does not implement Is() interface, so we have this abomination
if errors.As(err, &opError) && opError.Err.Error() != errNoSuchInterface.Error() {
uxBlocks.PrintWarning(styles.WarningLine(i18n.T(i18n.WarnVpnInterface, opError.Err.Error())))
}

err := uxHelpers.ProcessCheckWithSpinner(ctx, uxBlocks, p)

return err == nil
}

Expand Down
1 change: 1 addition & 0 deletions src/cmdBuilder/buildCobraCmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func buildCobraCmd(
argNames[i] = argName
}
cobraCmd.Use = strings.Join(append([]string{cmd.use}, argNames...), " ")
cobraCmd.Aliases = cmd.aliases

if cmd.scopeLevel != nil {
cmd.scopeLevel.AddCommandFlags(cmd)
Expand Down
6 changes: 6 additions & 0 deletions src/cmdBuilder/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type ScopeLevel interface {

type Cmd struct {
use string
aliases []string
short string
long string
helpTemplate string
Expand Down Expand Up @@ -60,6 +61,11 @@ func (cmd *Cmd) Use(use string) *Cmd {
return cmd
}

func (cmd *Cmd) Alias(aliases ...string) *Cmd {
cmd.aliases = append(cmd.aliases, aliases...)
return cmd
}

func (cmd *Cmd) SetHelpTemplate(template string) *Cmd {
cmd.helpTemplate = template
return cmd
Expand Down
5 changes: 3 additions & 2 deletions src/i18n/en.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,9 @@ at https://docs.zerops.io/references/cli for further details.`,
CmdDescEnv: "Displays global environment variables, their paths and additional options",

// vpn
CmdHelpVpn: "the vpn command.",
CmdDescVpn: "VPN commands group",
CmdHelpVpn: "the vpn command.",
CmdDescVpn: "VPN commands group",
WarnVpnInterface: "Error getting vpn network interface: %s",

// vpn up
CmdHelpVpnUp: "the vpn up command.",
Expand Down
10 changes: 6 additions & 4 deletions src/i18n/i18n.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package i18n

import "fmt"

func T(textConst string, args ...interface{}) string {
func T(textConst string, args ...any) string {
translation, exists := en[textConst]
if !exists {
return "[missing translation] " + textConst
format := textConst
return "[missing translation] " + fmt.Sprintf(format, args...)
}
if len(args) > 0 {
return fmt.Sprintf(translation, args...)
Expand Down Expand Up @@ -181,8 +182,9 @@ const (
Documentation = "Documentation"

// vpn
CmdHelpVpn = "CmdHelpVpn"
CmdDescVpn = "CmdDescVpn"
CmdHelpVpn = "CmdHelpVpn"
CmdDescVpn = "CmdDescVpn"
WarnVpnInterface = "WarnVpnInterface"

// vpn up
CmdHelpVpnUp = "CmdHelpVpnUp"
Expand Down
6 changes: 3 additions & 3 deletions src/uxBlock/spinner.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ type spinnerModel struct {

type MergeMessage []tea.Cmd

func XXX(cmdList ...tea.Cmd) func() tea.Msg {
func sequence(cmdList ...tea.Cmd) func() tea.Msg {
return func() tea.Msg {
return MergeMessage(cmdList)
}
Expand All @@ -69,7 +69,7 @@ func (m *spinnerModel) Init() tea.Cmd {
ticks[i] = m.spinners[i].init()
}

return XXX(ticks...)
return sequence(ticks...)
}

func (m *spinnerModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
Expand Down Expand Up @@ -109,7 +109,7 @@ func (m *spinnerModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}(i)
}
wg.Wait()
return m, XXX(cmdList...)
return m, sequence(cmdList...)
}
return m, nil
}
Expand Down

0 comments on commit b456f8f

Please sign in to comment.