2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2021-03-31 23:26:11 +00:00
|
|
|
|
2021-12-06 02:26:38 +00:00
|
|
|
//go:build !windows && !darwin
|
2021-03-31 23:26:11 +00:00
|
|
|
|
2023-04-18 21:26:58 +00:00
|
|
|
package netmon
|
2021-03-31 23:26:11 +00:00
|
|
|
|
|
|
|
import (
|
2021-04-02 07:24:29 +00:00
|
|
|
"bytes"
|
2021-03-31 23:26:11 +00:00
|
|
|
"errors"
|
2021-04-02 07:24:29 +00:00
|
|
|
"os"
|
2021-03-31 23:26:11 +00:00
|
|
|
"runtime"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"tailscale.com/types/logger"
|
|
|
|
)
|
|
|
|
|
2023-04-18 21:26:58 +00:00
|
|
|
func newPollingMon(logf logger.Logf, m *Monitor) (osMon, error) {
|
2021-03-31 23:26:11 +00:00
|
|
|
return &pollingMon{
|
|
|
|
logf: logf,
|
|
|
|
m: m,
|
|
|
|
stop: make(chan struct{}),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// pollingMon is a bad but portable implementation of the link monitor
|
|
|
|
// that works by polling the interface state every 10 seconds, in lieu
|
|
|
|
// of anything to subscribe to.
|
|
|
|
type pollingMon struct {
|
|
|
|
logf logger.Logf
|
2023-04-18 21:26:58 +00:00
|
|
|
m *Monitor
|
2021-03-31 23:26:11 +00:00
|
|
|
|
|
|
|
closeOnce sync.Once
|
|
|
|
stop chan struct{}
|
|
|
|
}
|
|
|
|
|
2022-04-20 19:03:50 +00:00
|
|
|
func (pm *pollingMon) IsInterestingInterface(iface string) bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-03-31 23:26:11 +00:00
|
|
|
func (pm *pollingMon) Close() error {
|
|
|
|
pm.closeOnce.Do(func() {
|
|
|
|
close(pm.stop)
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-02 07:24:29 +00:00
|
|
|
func (pm *pollingMon) isCloudRun() bool {
|
|
|
|
// https: //cloud.google.com/run/docs/reference/container-contract#env-vars
|
|
|
|
if os.Getenv("K_REVISION") == "" || os.Getenv("K_CONFIGURATION") == "" ||
|
|
|
|
os.Getenv("K_SERVICE") == "" || os.Getenv("PORT") == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
vers, err := os.ReadFile("/proc/version")
|
|
|
|
if err != nil {
|
|
|
|
pm.logf("Failed to read /proc/version: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return string(bytes.TrimSpace(vers)) == "Linux version 4.4.0 #1 SMP Sun Jan 10 15:06:54 PST 2016"
|
|
|
|
}
|
|
|
|
|
2021-03-31 23:26:11 +00:00
|
|
|
func (pm *pollingMon) Receive() (message, error) {
|
|
|
|
d := 10 * time.Second
|
|
|
|
if runtime.GOOS == "android" {
|
|
|
|
// We'll have Android notify the link monitor to wake up earlier,
|
|
|
|
// so this can go very slowly there, to save battery.
|
|
|
|
// https://github.com/tailscale/tailscale/issues/1427
|
|
|
|
d = 10 * time.Minute
|
2023-08-23 22:04:58 +00:00
|
|
|
} else if pm.isCloudRun() {
|
2021-04-02 07:24:29 +00:00
|
|
|
// Cloud Run routes never change at runtime. the containers are killed within
|
|
|
|
// 15 minutes by default, set the interval long enough to be effectively infinite.
|
|
|
|
pm.logf("monitor polling: Cloud Run detected, reduce polling interval to 24h")
|
|
|
|
d = 24 * time.Hour
|
|
|
|
}
|
2023-08-23 22:04:58 +00:00
|
|
|
timer := time.NewTimer(d)
|
|
|
|
defer timer.Stop()
|
2021-03-31 23:26:11 +00:00
|
|
|
for {
|
|
|
|
select {
|
2023-08-23 22:04:58 +00:00
|
|
|
case <-timer.C:
|
|
|
|
return unspecifiedMessage{}, nil
|
2021-03-31 23:26:11 +00:00
|
|
|
case <-pm.stop:
|
|
|
|
return nil, errors.New("stopped")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|