mirror of
https://github.com/TwiN/gatus.git
synced 2026-02-16 07:26:12 +00:00
fix(client): update icmp/ping logic to determine pinger privileged mode (#1346)
* fix(pinger): update logic to determine pinger privileged mode * add some unit tests for pinger Signed-off-by: Zee Aslam <zeet6613@gmail.com> * undo accidental removal Signed-off-by: Zee Aslam <zeet6613@gmail.com> * check for cap_net_raw by trying to open a raw socket and checking for permission error Signed-off-by: Zee Aslam <zeet6613@gmail.com> * revert syscall after testing. It is unable to build a binary on windows Signed-off-by: Zee Aslam <zeet6613@gmail.com> * remove extra import * review icmp section of readme. No changes required Signed-off-by: Zee Aslam <zeet6613@gmail.com> * Update client/client.go Co-authored-by: TwiN <twin@linux.com> * Update client/client.go Match function name Co-authored-by: TwiN <twin@linux.com> * Update client/client.go Remove extra line Co-authored-by: TwiN <twin@linux.com> --------- Signed-off-by: Zee Aslam <zeet6613@gmail.com> Co-authored-by: TwiN <twin@linux.com>
This commit is contained in:
@@ -2970,7 +2970,7 @@ endpoints:
|
|||||||
Only the placeholders `[CONNECTED]`, `[IP]` and `[RESPONSE_TIME]` are supported for endpoints of type ICMP.
|
Only the placeholders `[CONNECTED]`, `[IP]` and `[RESPONSE_TIME]` are supported for endpoints of type ICMP.
|
||||||
You can specify a domain prefixed by `icmp://`, or an IP address prefixed by `icmp://`.
|
You can specify a domain prefixed by `icmp://`, or an IP address prefixed by `icmp://`.
|
||||||
|
|
||||||
If you run Gatus on Linux, please read the Linux section on https://github.com/prometheus-community/pro-bing#linux
|
If you run Gatus on Linux, please read the Linux section on [https://github.com/prometheus-community/pro-bing#linux]
|
||||||
if you encounter any problems.
|
if you encounter any problems.
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/smtp"
|
"net/smtp"
|
||||||
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@@ -343,12 +344,7 @@ func Ping(address string, config *Config) (bool, time.Duration) {
|
|||||||
pinger := ping.New(address)
|
pinger := ping.New(address)
|
||||||
pinger.Count = 1
|
pinger.Count = 1
|
||||||
pinger.Timeout = config.Timeout
|
pinger.Timeout = config.Timeout
|
||||||
// Set the pinger's privileged mode to true for every GOOS except darwin
|
pinger.SetPrivileged(ShouldRunPingerAsPrivileged())
|
||||||
// See https://github.com/TwiN/gatus/issues/132
|
|
||||||
//
|
|
||||||
// Note that for this to work on Linux, Gatus must run with sudo privileges.
|
|
||||||
// See https://github.com/prometheus-community/pro-bing#linux
|
|
||||||
pinger.SetPrivileged(runtime.GOOS != "darwin")
|
|
||||||
pinger.SetNetwork(config.Network)
|
pinger.SetNetwork(config.Network)
|
||||||
err := pinger.Run()
|
err := pinger.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -364,6 +360,25 @@ func Ping(address string, config *Config) (bool, time.Duration) {
|
|||||||
return true, 0
|
return true, 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ShouldRunPingerAsPrivileged will determine whether or not to run pinger in privileged mode.
|
||||||
|
// It should be set to privileged when running as root, and always on windows. See https://pkg.go.dev/github.com/macrat/go-parallel-pinger#Pinger.SetPrivileged
|
||||||
|
func ShouldRunPingerAsPrivileged() bool {
|
||||||
|
// Set the pinger's privileged mode to false for darwin
|
||||||
|
// See https://github.com/TwiN/gatus/issues/132
|
||||||
|
// linux should also be set to false, but there are potential complications
|
||||||
|
// See https://github.com/TwiN/gatus/pull/748 and https://github.com/TwiN/gatus/issues/697#issuecomment-2081700989
|
||||||
|
//
|
||||||
|
// Note that for this to work on Linux, Gatus must run with sudo privileges. (in certain cases)
|
||||||
|
// See https://github.com/prometheus-community/pro-bing#linux
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
// To actually check for cap_net_raw capabilities, we would need to add "kernel.org/pub/linux/libs/security/libcap/cap" to gatus.
|
||||||
|
// Or use a syscall and check for permission errors, but this requires platform specific compilation
|
||||||
|
// As a backstop we can simply check the effective user id and run as privileged when running as root
|
||||||
|
return os.Geteuid() == 0
|
||||||
|
}
|
||||||
|
|
||||||
// QueryWebSocket opens a websocket connection, write `body` and return a message from the server
|
// QueryWebSocket opens a websocket connection, write `body` and return a message from the server
|
||||||
func QueryWebSocket(address, body string, headers map[string]string, config *Config) (bool, []byte, error) {
|
func QueryWebSocket(address, body string, headers map[string]string, config *Config) (bool, []byte, error) {
|
||||||
const (
|
const (
|
||||||
|
|||||||
@@ -6,6 +6,8 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
|
"os"
|
||||||
|
"runtime"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -129,6 +131,33 @@ func TestPing(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestShouldRunPingerAsPrivileged(t *testing.T) {
|
||||||
|
// Don't run in parallel since we're testing system-dependent behavior
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
result := ShouldRunPingerAsPrivileged()
|
||||||
|
if !result {
|
||||||
|
t.Error("On Windows, ShouldRunPingerAsPrivileged() should return true")
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Non-Windows tests
|
||||||
|
result := ShouldRunPingerAsPrivileged()
|
||||||
|
isRoot := os.Geteuid() == 0
|
||||||
|
|
||||||
|
// Test cases based on current environment
|
||||||
|
if isRoot {
|
||||||
|
if !result {
|
||||||
|
t.Error("When running as root, ShouldRunPingerAsPrivileged() should return true")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// When not root, the result depends on raw socket creation
|
||||||
|
// We can at least verify the function runs without panic
|
||||||
|
t.Logf("Non-root privileged result: %v", result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
func TestCanPerformStartTLS(t *testing.T) {
|
func TestCanPerformStartTLS(t *testing.T) {
|
||||||
type args struct {
|
type args struct {
|
||||||
address string
|
address string
|
||||||
|
|||||||
Reference in New Issue
Block a user