mirror of
https://github.com/juanfont/headscale.git
synced 2024-12-24 21:25:52 -05:00
Reuse Ping function for DERP ping
This commit is contained in:
parent
b40cf732d6
commit
510b5af4ed
@ -25,7 +25,6 @@ type TailscaleClient interface {
|
||||
WaitForLogout() error
|
||||
WaitForPeers(expected int) error
|
||||
Ping(hostnameOrIP string, opts ...tsic.PingOption) error
|
||||
PingViaDERP(hostnameOrIP string, opts ...tsic.PingOption) error
|
||||
Curl(url string, opts ...tsic.CurlOption) (string, error)
|
||||
ID() string
|
||||
}
|
||||
|
@ -545,49 +545,6 @@ func (t *TailscaleInContainer) Ping(hostnameOrIP string, opts ...PingOption) err
|
||||
|
||||
command = append(command, hostnameOrIP)
|
||||
|
||||
return t.pool.Retry(func() error {
|
||||
result, _, err := t.Execute(command)
|
||||
if err != nil {
|
||||
log.Printf(
|
||||
"failed to run ping command from %s to %s, err: %s",
|
||||
t.Hostname(),
|
||||
hostnameOrIP,
|
||||
err,
|
||||
)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
if !strings.Contains(result, "pong") && !strings.Contains(result, "is local") {
|
||||
return backoff.Permanent(errTailscalePingFailed)
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// PingViaDERP executes the Tailscale ping command and pings a hostname
|
||||
// or IP via the DERP network (i.e., not a direct connection). It accepts a series of DERPPingOption.
|
||||
// TODO(kradalby): Make multiping, go routine magic.
|
||||
func (t *TailscaleInContainer) PingViaDERP(hostnameOrIP string, opts ...PingOption) error {
|
||||
args := pingArgs{
|
||||
timeout: time.Second,
|
||||
count: defaultPingCount,
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
opt(&args)
|
||||
}
|
||||
|
||||
command := []string{
|
||||
"tailscale", "ping",
|
||||
fmt.Sprintf("--timeout=%s", args.timeout),
|
||||
fmt.Sprintf("--c=%d", args.count),
|
||||
"--until-direct=false",
|
||||
}
|
||||
|
||||
command = append(command, hostnameOrIP)
|
||||
|
||||
return t.pool.Retry(func() error {
|
||||
result, _, err := t.Execute(
|
||||
command,
|
||||
@ -614,8 +571,12 @@ func (t *TailscaleInContainer) PingViaDERP(hostnameOrIP string, opts ...PingOpti
|
||||
return backoff.Permanent(errTailscalePingFailed)
|
||||
}
|
||||
|
||||
if !strings.Contains(result, "via DERP") {
|
||||
return backoff.Permanent(errTailscalePingNotDERP)
|
||||
if !args.direct {
|
||||
if strings.Contains(result, "via DERP") {
|
||||
return nil
|
||||
} else {
|
||||
return backoff.Permanent(errTailscalePingNotDERP)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
|
@ -40,10 +40,11 @@ func pingDerpAllHelper(t *testing.T, clients []TailscaleClient, addrs []string)
|
||||
continue
|
||||
}
|
||||
|
||||
err := client.PingViaDERP(
|
||||
err := client.Ping(
|
||||
addr,
|
||||
tsic.WithPingTimeout(derpPingTimeout),
|
||||
tsic.WithPingCount(derpPingCount),
|
||||
tsic.WithPingUntilDirect(false),
|
||||
)
|
||||
if err != nil {
|
||||
t.Errorf("failed to ping %s from %s: %s", addr, client.Hostname(), err)
|
||||
|
Loading…
Reference in New Issue
Block a user