Skip to content

Commit 8f8ab92

Browse files
authored
Merge pull request #105 from zeropsio/ping6
use 'ping -6' when ping6 is not available
2 parents bfb945e + cd4c7ee commit 8f8ab92

File tree

5 files changed

+66
-4
lines changed

5 files changed

+66
-4
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [v0.12.17] - 2023-01-10
8+
9+
### Changed
10+
- use `ping -6` when `ping6` is not available
11+
712
## [v0.12.16] - 2022-11-04
813

914
### Fixed

cmd/.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*
2+
!.gitignore
3+
!main.go

src/nettools/ping.go

+56-4
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,72 @@ package nettools
22

33
import (
44
"context"
5+
"fmt"
56
"os/exec"
67
"runtime"
8+
"strings"
79
)
810

911
func HasIPv6PingCommand() bool {
12+
if hasPing6Command() {
13+
return true
14+
}
15+
if _, err := exec.LookPath("ping"); err == nil {
16+
return true
17+
}
18+
return runtime.GOOS == "windows"
19+
}
20+
21+
func hasPing6Command() bool {
1022
_, err := exec.LookPath("ping6")
11-
return err == nil || runtime.GOOS == "windows"
23+
return err == nil
1224
}
1325

1426
func Ping(ctx context.Context, address string) error {
15-
pingCommand := exec.CommandContext(ctx, "ping6", "-c", "1", address)
27+
cmd := []string{"ping6", "-c", "1", address}
28+
if !hasPing6Command() {
29+
cmd = []string{"ping", "-6", "-c", "1", address}
30+
}
1631
if runtime.GOOS == "windows" {
17-
pingCommand = exec.CommandContext(ctx, "ping", "/n", "1", address)
32+
cmd = []string{"ping", "/n", "1", address}
1833
}
1934

20-
return pingCommand.Run()
35+
if out, err := exec.CommandContext(ctx, cmd[0], cmd[1:]...).Output(); err != nil {
36+
return pingError(err, cmd, out)
37+
}
38+
return nil
39+
}
40+
41+
type PingError struct {
42+
err error
43+
cmd []string
44+
output string
45+
}
46+
47+
func (p PingError) Err() error {
48+
return p.err
49+
}
50+
51+
func (p PingError) Output() string {
52+
return p.output
53+
}
54+
55+
func (p PingError) Cmd() string {
56+
return strings.Join(p.cmd, " ")
57+
}
58+
59+
func (p PingError) Error() string {
60+
return fmt.Sprintf("ping => err: %s, exec: %s, output: %s", p.err, p.Cmd(), p.output)
61+
}
62+
63+
func pingError(
64+
err error,
65+
cmd []string,
66+
output []byte,
67+
) error {
68+
return PingError{
69+
err: err,
70+
cmd: cmd,
71+
output: string(output),
72+
}
2173
}

src/vpn/dnsIsAlive.go

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ func (h *Handler) dnsIsAlive() (bool, error) {
1818
}
1919
err := nettools.Ping(ctx, "node1.master.core.zerops")
2020
if err != nil {
21+
h.logger.Error(err)
2122
return false, nil
2223
}
2324
return true, nil

src/vpn/isVpnTunnelAlive.go

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ func (h *Handler) isVpnTunnelAlive(ctx context.Context, serverIp net.IP) bool {
2020

2121
err := nettools.Ping(ctx, serverIp.String())
2222
if err != nil {
23+
h.logger.Error(err)
2324
return false
2425
}
2526
return true

0 commit comments

Comments
 (0)