Skip to content

Commit

Permalink
Merge pull request #5 from zeropsio/fix_messages_and_ctx_into_ping
Browse files Browse the repository at this point in the history
#xxx - ctx ping + messages
  • Loading branch information
jan-hajek authored Oct 13, 2020
2 parents e1c0621 + 36853b9 commit 6c20a33
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 21 deletions.
3 changes: 2 additions & 1 deletion src/cmd/di.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,9 @@ func createDaemonStorage() (*daemonStorage.Handler, error) {
func createVpn(storage *daemonStorage.Handler, dnsServer *dnsServer.Handler, logger *logger.Handler) *vpn.Handler {
return vpn.New(
vpn.Config{
VpnCheckInterval: time.Second * 1,
VpnCheckInterval: time.Second * 3,
VpnCheckRetryCount: 3,
VpnCheckTimeout: time.Second * 3,
},
logger,
grpcApiClientFactory.New(),
Expand Down
File renamed without changes.
25 changes: 25 additions & 0 deletions src/dnsServer/handler_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package dnsServer

import (
"context"
"net"
)

type Handler struct {
}

func New() *Handler {
return &Handler{}
}

// listen and serve
func (h *Handler) Run(_ context.Context) error {
return nil
}

func (h *Handler) StopForward() {

}

func (h *Handler) SetAddresses(_ net.IP, _ []net.IP, _ net.IP, _ net.IPNet) {
}
20 changes: 11 additions & 9 deletions src/i18n/en.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,20 @@ const (
DeployUploadingStart = "uploading start"
DeployUploadingDone = "uploading done"
DeployDeployingStart = "deploying start"
DeployUploadArchiveFailed = "upload archive failed"
DeployUploadArchiveFailed = "upload archive failed"
DeploySuccess = "project deployed"

// vpn start
VpnStartProjectNameIsEmpty = "project name must be filled"
VpnStartProjectNotFound = "project not found"
VpnStartProjectsWithSameName = "there are multiple projects with same name"
VpnStartDaemonIsUnavailable = "daemon is currently unavailable, did you install it?"
VpnStartInstallDaemonPrompt = "is it ok if we are going to install daemon for you?"
VpnStartTerminatedByUser = "when you will be ready, try `zcli daemon install`"
VpnStartUserIsUnableToWriteYorN = "type 'y' or 'n' please"
VpnStartSuccess = "vpn connection was established"
VpnStartProjectNameIsEmpty = "project name must be filled"
VpnStartProjectNotFound = "project not found"
VpnStartInterfaceAssignFailed = "interface name assign failed"
VpnStartWireguardInterfaceNotfound = "wireguard interface not found"
VpnStartProjectsWithSameName = "there are multiple projects with same name"
VpnStartDaemonIsUnavailable = "daemon is currently unavailable, did you install it?"
VpnStartInstallDaemonPrompt = "is it ok if we are going to install daemon for you?"
VpnStartTerminatedByUser = "when you will be ready, try `zcli daemon install`"
VpnStartUserIsUnableToWriteYorN = "type 'y' or 'n' please"
VpnStartSuccess = "vpn connection was established"

// vpn status
VpnStatusActive = "vpn is active"
Expand Down
1 change: 1 addition & 0 deletions src/vpn/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const (
type Config struct {
VpnCheckInterval time.Duration
VpnCheckRetryCount int
VpnCheckTimeout time.Duration
}

type Handler struct {
Expand Down
18 changes: 12 additions & 6 deletions src/vpn/handler_isVpnAlive.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package vpn

import (
"context"
"os/exec"
)

Expand All @@ -10,13 +11,18 @@ func (h *Handler) isVpnAlive(serverIp string) bool {
return false
}

for i := 0; i < 3; i++ {
_, err := exec.Command("ping6", "-c", "1", serverIp).Output()
if err != nil {
continue
for i := 0; i < h.config.VpnCheckRetryCount; i++ {
if func() bool {
ctx, cancel := context.WithTimeout(context.Background(), h.config.VpnCheckTimeout)
defer cancel()
_, err := exec.CommandContext(ctx, "ping6", "-c", "1", serverIp).Output()
if err != nil {
return false
}
return true
}() {
return true
}

return true
}
return false
}
3 changes: 2 additions & 1 deletion src/vpn/handler_setVpn_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strconv"

"github.com/google/uuid"
"github.com/zerops-io/zcli/src/i18n"
"github.com/zerops-io/zcli/src/utils/cmdRunner"
"github.com/zerops-io/zcli/src/zeropsVpnProtocol"
)
Expand All @@ -29,7 +30,7 @@ func (h *Handler) setVpn(selectedVpnAddress, privateKey string, mtu uint32, resp
re := regexp.MustCompile(`INFO: \((.*)\)`)
submatches := re.FindSubmatch(output)
if len(submatches) != 2 {
return errors.New("vpn interface not found")
return errors.New(i18n.VpnStartWireguardInterfaceNotfound)
}

interfaceName := string(submatches[1])
Expand Down
5 changes: 3 additions & 2 deletions src/vpn/handler_setVpn_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"strconv"

"github.com/google/uuid"
"github.com/zerops-io/zcli/src/i18n"
"github.com/zerops-io/zcli/src/utils/cmdRunner"
"github.com/zerops-io/zcli/src/zeropsVpnProtocol"
)
Expand All @@ -24,11 +25,11 @@ func getNewVpnInterfaceName() (string, error) {
if err == nil {
continue
}
if err.Error() == "no such network interface" {
if err.Error() == "route ip+net: no such network interface" {
return interfaceName, nil
}
}
return "", errors.New("next interface name not available")
return "", errors.New(i18n.VpnStartInterfaceAssignFailed)
}

func (h *Handler) setVpn(selectedVpnAddress, privateKey string, mtu uint32, response *zeropsVpnProtocol.StartVpnResponse) error {
Expand Down
13 changes: 11 additions & 2 deletions src/vpn/handler_startVpn.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/zerops-io/zcli/src/utils/certReader"
"github.com/zerops-io/zcli/src/zeropsApiProtocol"
"github.com/zerops-io/zcli/src/zeropsVpnProtocol"
"google.golang.org/grpc/status"
)

func (h *Handler) startVpn(
Expand Down Expand Up @@ -58,7 +59,11 @@ func (h *Handler) startVpn(
ClientPublicKey: publicKey,
})
if err := utils.HandleGrpcApiError(apiVpnRequestResponse, err); err != nil {
return err
if errStatus, ok := status.FromError(err); ok {
return errors.New(errStatus.Err().Error())
} else {
return err
}
}
h.logger.Debug("vpn request end")

Expand Down Expand Up @@ -96,7 +101,11 @@ func (h *Handler) startVpn(
Expiry: zeropsVpnProtocol.ToProtoTimestamp(expiry),
})
if err := utils.HandleVpnApiError(startVpnResponse, err); err != nil {
return err
if errStatus, ok := status.FromError(err); ok {
return errors.New(errStatus.Err().Error())
} else {
return err
}
}

h.logger.Debug("call start vpn - end")
Expand Down

0 comments on commit 6c20a33

Please sign in to comment.