arblant
firefox-vpn-client / core / internal / service / sleep.go
sleep.go353 lines · 10.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
package service

import (
	"context"
	"errors"
	"fmt"
	"net"
	"sync"
	"time"

	"github.com/armanlife/firefox-vpn-client/core/internal/accounts"
	"github.com/armanlife/firefox-vpn-client/core/internal/logx"
	"github.com/armanlife/firefox-vpn-client/core/internal/proxy"
)

// Sleep: an engine without a controller listens on proxy.listen but holds
// no upstream session and no pass, renews nothing, and checks no quota.
// Every engine starts asleep, so starting the program sends nothing to
// Mozilla. After proxy.idle_sleep without a connection through Mozilla it
// falls asleep again: the engine closes its controller.
//
// The next connection that needs Mozilla, a connection test, or a
// real-delay test wakes it and waits for the wake: the first wake selects
// the server and connects (server list, pass, "fastest" measurement,
// session; then the automatic connection test and the device
// registration), later wakes build a controller for the same server (a
// new pass and session). Direct connections (routing) never wake it.

// idleCheckInterval is how often the engine checks whether it may sleep.
const idleCheckInterval = 30 * time.Second

// wakeReason says what needs the upstream of a sleeping engine.
type wakeReason int

const (
	// wakeForClient: a client connection; after a failed wake it gets the
	// failure until wakeRetryAt.
	wakeForClient wakeReason = iota
	// wakeForTest: the connection test, which records its own result, so
	// no automatic test follows the first wake.
	wakeForTest
	// wakeForPing: the real-delay test.
	wakeForPing
)

// idleState is an engine's use of its controller. mu also guards
// proxyEngine.controller, so a connection that has taken the controller
// (inUse) is never left with a closed one.
type idleState struct {
	mu       sync.Mutex
	inUse    int       // connections and tests using the controller
	lastUsed time.Time // when inUse last changed
	sleeping time.Time // when the engine fell asleep, zero while awake
	stopped  bool      // the engine stopped; a late wake closes its controller
	// wakeErr is why the last wake failed, at wakeFailedAt, and its
	// failure code after the internet check (failureCode); cleared by a
	// successful wake.
	wakeErr      error
	wakeCode     string
	wakeFailedAt time.Time
	// wakeRetryAt is when client connections may wake the engine again
	// (proxy.WakeRetryDelay after wakeFailures failures in a row), so a
	// browser opening many connections cannot turn failures into a stream
	// of Guardian requests. Tests and real-delay measurements try again
	// at once, unless the failure is lasting (wakeLasting).
	wakeRetryAt  time.Time
	wakeFailures int
	wakeLasting  bool
	// wakeMu serializes wakes; it also guards proxyEngine.upstream and
	// proxyEngine.connected.
	wakeMu sync.Mutex
}

func (s *idleState) touch() {
	s.mu.Lock()
	s.lastUsed = time.Now()
	s.mu.Unlock()
}

// currentController returns the controller, or nil while asleep.
func (e *proxyEngine) currentController() *proxy.Controller {
	e.idle.mu.Lock()
	defer e.idle.mu.Unlock()
	return e.controller
}

// asleepSince reports when the engine fell asleep, or zero.
func (e *proxyEngine) asleepSince() time.Time {
	e.idle.mu.Lock()
	defer e.idle.mu.Unlock()
	return e.idle.sleeping
}

// wakeFailure reports why the last wake failed, when, and its failure
// code; empty after a successful wake.
func (e *proxyEngine) wakeFailure() (time.Time, string, error) {
	e.idle.mu.Lock()
	defer e.idle.mu.Unlock()
	return e.idle.wakeFailedAt, e.idle.wakeCode, e.idle.wakeErr
}

// wakeWait returns the last wake failure while the next wake must wait
// for why, and how long it waits.
func (e *proxyEngine) wakeWait(why wakeReason, now time.Time) (error, time.Duration) {
	e.idle.mu.Lock()
	defer e.idle.mu.Unlock()
	if e.idle.wakeErr == nil || !now.Before(e.idle.wakeRetryAt) {
		return nil, 0
	}
	if why != wakeForClient && !e.idle.wakeLasting {
		return nil, 0
	}
	return e.idle.wakeErr, e.idle.wakeRetryAt.Sub(now)
}

// wakeAgainNow lets the next connection wake the engine at once: the
// network came back or the accounts changed, so the last failure says
// little about the next attempt.
func (e *proxyEngine) wakeAgainNow() {
	e.idle.mu.Lock()
	e.idle.wakeRetryAt, e.idle.wakeFailures, e.idle.wakeLasting = time.Time{}, 0, false
	e.idle.mu.Unlock()
}

// retryNow lets the next connection wake the engine at once, unless the
// last failure is one that retrying cannot fix (wakeLasting).
func (e *proxyEngine) retryNow() {
	e.idle.mu.Lock()
	if !e.idle.wakeLasting {
		e.idle.wakeRetryAt, e.idle.wakeFailures = time.Time{}, 0
	}
	e.idle.mu.Unlock()
}

// useController returns the engine's controller, waking the engine when
// it sleeps, and marks it in use until release is called.
func (d *Daemon) useController(e *proxyEngine, why wakeReason) (c *proxy.Controller, release func(), err error) {
	if c := e.reserve(); c != nil {
		return c, e.releaseFunc(), nil
	}
	e.idle.wakeMu.Lock()
	defer e.idle.wakeMu.Unlock()
	if c := e.reserve(); c != nil { // woken by another connection meanwhile
		return c, e.releaseFunc(), nil
	}
	if err, wait := e.wakeWait(why, time.Now()); err != nil {
		at, _, _ := e.wakeFailure()
		return nil, nil, fmt.Errorf("connecting to Mozilla failed %s ago; trying again in %s: %w", time.Since(at).Round(time.Second), wait.Round(time.Second), err)
	}
	wake := func(e *proxyEngine) error { return d.wake(e, why) }
	if d.wakeHook != nil {
		wake = d.wakeHook
	}
	if err := wake(e); err != nil {
		return nil, nil, err
	}
	if c := e.reserve(); c != nil {
		return c, e.releaseFunc(), nil
	}
	return nil, nil, errProxyNotRunning
}

func (e *proxyEngine) reserve() *proxy.Controller {
	e.idle.mu.Lock()
	defer e.idle.mu.Unlock()
	if e.controller != nil {
		e.idle.inUse++
		e.idle.lastUsed = time.Now()
	}
	return e.controller
}

func (e *proxyEngine) releaseFunc() func() {
	var once sync.Once
	return func() {
		once.Do(func() {
			e.idle.mu.Lock()
			e.idle.inUse--
			e.idle.lastUsed = time.Now()
			e.idle.mu.Unlock()
		})
	}
}

// wake connects a sleeping engine to Mozilla: the first time it selects
// the server (connectEngine), later it reuses it (reconnectEngine).
// idle.wakeMu is held.
func (d *Daemon) wake(e *proxyEngine, why wakeReason) error {
	if d.currentEngine() != e {
		return errProxyNotRunning
	}
	started := time.Now()
	first := !e.connected
	server, _ := e.selection()
	var controller *proxy.Controller
	var err error
	if first {
		controller, err = d.connectEngine(e)
	} else {
		controller, err = d.reconnectEngine(e)
	}
	if err != nil {
		code := failureCode(err)
		now := time.Now()
		e.idle.mu.Lock()
		e.idle.wakeFailures++
		wait := proxy.WakeRetryDelay(err, e.idle.wakeFailures, now)
		if e.selected.Addr != server.Addr {
			// The failover moved to another server of the location (only
			// the server's own failures do): the next wake tries it at once.
			e.idle.wakeFailures, wait = 0, 0
		}
		e.idle.wakeErr, e.idle.wakeCode, e.idle.wakeFailedAt = err, code, now
		e.idle.wakeRetryAt, e.idle.wakeLasting = now.Add(wait), proxy.LastingWakeFailure(err, now)
		e.idle.mu.Unlock()
		logx.Warn("connecting to Mozilla failed; the proxy stays idle and connections wait before trying again retry_in=%s err=%s", wait, logx.Err(err))
		if errors.Is(err, accounts.ErrNoUsableAccount) {
			// The restarted engine reports the wait for an account.
			d.RequestRestart()
		}
		return fmt.Errorf("connecting to Mozilla: %w", err)
	}
	if !d.runController(e, controller) {
		return errProxyNotRunning
	}
	d.markConnected()
	if !first {
		logx.Info("proxy woke up for a connection account=%s took=%s", logx.Email(d.pool.ActiveName()), time.Since(started).Round(time.Millisecond))
		return nil
	}
	logx.Info("proxy connected for its first use account=%s took=%s", logx.Email(d.pool.ActiveName()), time.Since(started).Round(time.Millisecond))
	if why != wakeForTest {
		go d.autoTest(e, "proxy connected")
	}
	d.registerDevices()
	return nil
}

// registerDevices shows every account as "Firefox's built-in VPN" in its
// Mozilla connected services (accounts already registered are skipped),
// once per run, after the proxy first connected: until then the program
// sends Mozilla nothing.
func (d *Daemon) registerDevices() {
	d.devicesOnce.Do(func() {
		go func() {
			ctx, cancel := context.WithCancel(context.Background())
			defer cancel()
			go func() {
				select {
				case <-d.stop:
					cancel()
				case <-ctx.Done():
				}
			}()
			d.pool.EnsureDevices(ctx)
		}()
	})
}

// sleepWhenIdle puts the engine to sleep after proxy.idle_sleep without a
// connection through Mozilla, until the engine stops.
func (d *Daemon) sleepWhenIdle(e *proxyEngine, stopped <-chan struct{}) {
	ticker := time.NewTicker(idleCheckInterval)
	defer ticker.Stop()
	for {
		select {
		case <-stopped:
			return
		case <-ticker.C:
		}
		d.sleepIfIdle(e)
	}
}

// sleepIfIdle puts the engine to sleep when proxy.idle_sleep is on and
// nothing has used its controller for that long, and reports whether it
// did.
func (d *Daemon) sleepIfIdle(e *proxyEngine) bool {
	limit := d.config().Proxy.IdleSleep.Duration()
	if limit <= 0 {
		return false
	}
	e.idle.mu.Lock()
	var c *proxy.Controller
	if e.controller != nil && e.idle.inUse == 0 && time.Since(e.idle.lastUsed) >= limit {
		c, e.controller, e.idle.sleeping = e.controller, nil, time.Now()
	}
	e.idle.mu.Unlock()
	if c == nil {
		return false
	}
	_ = c.Close()
	if err := proxy.WriteIdleStatus(e.statusFile, proxy.StatusSleeping); err != nil {
		logx.Warn("writing status file failed path=%s err=%s", e.statusFile, logx.Err(err))
	}
	logx.Info("no connection through Mozilla for %s; the proxy sleeps and the account is not used until the next connection", limit)
	return true
}

// engineUpstream opens the engine's tunnels through its controller,
// waking it when it sleeps, and keeps it awake while a tunnel is open.
type engineUpstream struct {
	d      *Daemon
	engine *proxyEngine
}

func (u *engineUpstream) OpenTunnel(authority string) (net.Conn, error) {
	return u.OpenTunnelForClient(authority, "")
}

func (u *engineUpstream) OpenTunnelForClient(authority, clientKey string) (net.Conn, error) {
	c, release, err := u.d.useController(u.engine, wakeForClient)
	if err != nil {
		return nil, err
	}
	var conn net.Conn
	if clientKey != "" {
		conn, err = c.OpenTunnelForClient(authority, clientKey)
	} else {
		conn, err = c.OpenTunnel(authority)
	}
	if err != nil {
		release()
		return nil, err
	}
	return releasingConn(conn, release), nil
}

// releasingConn calls release when conn closes, keeping half-close.
func releasingConn(conn net.Conn, release func()) net.Conn {
	r := &releaseConn{Conn: conn, release: release}
	if _, ok := conn.(interface{ CloseWrite() error }); ok {
		return halfClosingReleaseConn{r}
	}
	return r
}

type releaseConn struct {
	net.Conn
	release func()
}

func (c *releaseConn) Close() error {
	err := c.Conn.Close()
	c.release()
	return err
}

type halfClosingReleaseConn struct{ *releaseConn }

func (c halfClosingReleaseConn) CloseWrite() error {
	return c.Conn.(interface{ CloseWrite() error }).CloseWrite()
}