Add detection for WSL address (#1257)

Signed-off-by: Evers, Martijn <mevers@gk-software.com>
This commit is contained in:
Martijn Evers 2023-05-03 10:52:46 +02:00 committed by GitHub
parent 8a51463540
commit 0f060a0277
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 55 additions and 0 deletions

View File

@ -15,7 +15,10 @@ package internal
import ( import (
"flag" "flag"
"fmt"
"net"
"os" "os"
"strings"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
) )
@ -37,10 +40,62 @@ type Config struct {
TraceSamplingRatio float64 `yaml:"TraceSamplingRatio,omitempty"` TraceSamplingRatio float64 `yaml:"TraceSamplingRatio,omitempty"`
} }
func isWSL() bool {
b, err := os.ReadFile("/proc/version")
if err != nil {
return false
}
if strings.Contains(string(b), "WSL") {
return true
}
return false
}
func getInterfaceIpv4Addr(interfaceName string) (addr string, err error) {
var (
ief *net.Interface
addrs []net.Addr
ipv4Addr net.IP
)
if ief, err = net.InterfaceByName(interfaceName); err != nil {
return "", err
}
if addrs, err = ief.Addrs(); err != nil {
return "", err
}
for _, addr := range addrs {
if ipv4Addr = addr.(*net.IPNet).IP.To4(); ipv4Addr != nil {
break
}
}
if ipv4Addr == nil {
return "", fmt.Errorf("interface %s does not have an ipv4 address", interfaceName)
}
return ipv4Addr.String(), nil
}
func whatsupAddr(defAddress string) string { func whatsupAddr(defAddress string) string {
if a := os.Getenv("HOSTADDR"); a != "" { if a := os.Getenv("HOSTADDR"); a != "" {
return a + ":" + WhatsupPort return a + ":" + WhatsupPort
} }
// use eth0 IP address if running WSL, return default if check fails
if isWSL() {
a, err := getInterfaceIpv4Addr("eth0")
if err != nil {
return defAddress
}
return a + ":" + WhatsupPort
}
return defAddress return defAddress
} }