Skip to content

Commit

Permalink
use 'ping -6' when ping6 is not available
Browse files Browse the repository at this point in the history
  • Loading branch information
l-hellmann committed Jan 10, 2023
1 parent bfb945e commit cd4c7ee
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [v0.12.17] - 2023-01-10

### Changed
- use `ping -6` when `ping6` is not available

## [v0.12.16] - 2022-11-04

### Fixed
Expand Down
3 changes: 3 additions & 0 deletions cmd/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*
!.gitignore
!main.go
60 changes: 56 additions & 4 deletions src/nettools/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,72 @@ package nettools

import (
"context"
"fmt"
"os/exec"
"runtime"
"strings"
)

func HasIPv6PingCommand() bool {
if hasPing6Command() {
return true
}
if _, err := exec.LookPath("ping"); err == nil {
return true
}
return runtime.GOOS == "windows"
}

func hasPing6Command() bool {
_, err := exec.LookPath("ping6")
return err == nil || runtime.GOOS == "windows"
return err == nil
}

func Ping(ctx context.Context, address string) error {
pingCommand := exec.CommandContext(ctx, "ping6", "-c", "1", address)
cmd := []string{"ping6", "-c", "1", address}
if !hasPing6Command() {
cmd = []string{"ping", "-6", "-c", "1", address}
}
if runtime.GOOS == "windows" {
pingCommand = exec.CommandContext(ctx, "ping", "/n", "1", address)
cmd = []string{"ping", "/n", "1", address}
}

return pingCommand.Run()
if out, err := exec.CommandContext(ctx, cmd[0], cmd[1:]...).Output(); err != nil {
return pingError(err, cmd, out)
}
return nil
}

type PingError struct {
err error
cmd []string
output string
}

func (p PingError) Err() error {
return p.err
}

func (p PingError) Output() string {
return p.output
}

func (p PingError) Cmd() string {
return strings.Join(p.cmd, " ")
}

func (p PingError) Error() string {
return fmt.Sprintf("ping => err: %s, exec: %s, output: %s", p.err, p.Cmd(), p.output)
}

func pingError(
err error,
cmd []string,
output []byte,
) error {
return PingError{
err: err,
cmd: cmd,
output: string(output),
}
}
1 change: 1 addition & 0 deletions src/vpn/dnsIsAlive.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func (h *Handler) dnsIsAlive() (bool, error) {
}
err := nettools.Ping(ctx, "node1.master.core.zerops")
if err != nil {
h.logger.Error(err)
return false, nil
}
return true, nil
Expand Down
1 change: 1 addition & 0 deletions src/vpn/isVpnTunnelAlive.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func (h *Handler) isVpnTunnelAlive(ctx context.Context, serverIp net.IP) bool {

err := nettools.Ping(ctx, serverIp.String())
if err != nil {
h.logger.Error(err)
return false
}
return true
Expand Down

0 comments on commit cd4c7ee

Please sign in to comment.