multiwan.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // multiwan.go — the uplink policy a router's own panel manages.
  2. //
  3. // A router with two internet connections runs a policy: one carries traffic
  4. // and the other waits, or both carry a share of it. NetPulse does not decide
  5. // any of that — the router's panel does — but it has to report it, because
  6. // without it the connection panel shows the address of whichever uplink holds
  7. // the cheapest route, which under a mark-based policy is not the one traffic
  8. // leaves by. The address on screen is then the address nobody is using.
  9. package probe
  10. // MultiWanInfo mirrors the policy as the panel reports it. nil means this
  11. // router reports no multi-WAN at all — no policy manager installed, a single
  12. // uplink, or simply an older agent.
  13. type MultiWanInfo struct {
  14. // Mode: off | failover | balance | custom. "custom" is a configuration
  15. // the panel recognises but did not write.
  16. Mode string `json:"mode"`
  17. // Managed: the panel wrote this configuration itself.
  18. Managed bool `json:"managed,omitempty"`
  19. // Active: the uplink carrying traffic right now.
  20. Active string `json:"active,omitempty"`
  21. // Primary: the preferred uplink, in failover mode.
  22. Primary string `json:"primary,omitempty"`
  23. // Sticky: while balancing, each device stays on one connection. Worth
  24. // saying out loud, or the percentages read as a per-device mix.
  25. Sticky bool `json:"sticky,omitempty"`
  26. Uplinks []WanUplink `json:"uplinks"`
  27. }
  28. // WanUplink is one internet connection. Deliberately fewer fields than the
  29. // panel holds: metrics, weights, tracking targets and section names are how
  30. // the policy is expressed, not what somebody reading a dashboard needs.
  31. type WanUplink struct {
  32. Name string `json:"name"`
  33. Proto string `json:"proto,omitempty"`
  34. Port string `json:"port,omitempty"` // physical socket; empty on a modem
  35. IP string `json:"ip,omitempty"`
  36. Gateway string `json:"gateway,omitempty"`
  37. Up bool `json:"up"`
  38. Active bool `json:"active"`
  39. Primary bool `json:"primary,omitempty"`
  40. // SharePct: portion of traffic this one carries while balancing.
  41. SharePct int `json:"sharePct,omitempty"`
  42. // Metered: mobile broadband, where traffic costs money.
  43. Metered bool `json:"metered,omitempty"`
  44. // Online: the policy manager's own verdict on the link —
  45. // online | offline | unknown. Empty when it is not running.
  46. Online string `json:"online,omitempty"`
  47. }
  48. // ActiveUplinkName is the uplink carrying traffic, or "" when there is no
  49. // policy saying so. Fed to the prober, which then reports that connection's
  50. // address instead of the one holding the cheapest route.
  51. func ActiveUplinkName(m *MultiWanInfo) string {
  52. if m == nil {
  53. return ""
  54. }
  55. return m.Active
  56. }