arblant
firefox-vpn-client / core / internal / proxy / selection.go
selection.go450 lines · 13.5 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 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
package proxy

import (
	"encoding/json"
	"errors"
	"fmt"
	"math/rand/v2"
	"net"
	"net/url"
	"os"
	"path/filepath"
	"slices"
	"strconv"
	"strings"

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

type Candidate struct {
	Addr        string
	CountryName string
	CountryCode string
	CityName    string
	CityCode    string
}

// IsRecommended reports whether the candidate belongs to the recommended
// (anycast) location rather than a specific country.
func (p Candidate) IsRecommended() bool {
	return strings.EqualFold(strings.TrimSpace(p.CountryCode), serverlist.RecommendedCountryCode)
}

type proxySelectionState struct {
	Addr        string `json:"addr"`
	CountryName string `json:"country_name,omitempty"`
	CountryCode string `json:"country_code,omitempty"`
	CityName    string `json:"city_name,omitempty"`
	CityCode    string `json:"city_code,omitempty"`
}

func (p Candidate) CountryNameOrUnknown() string {
	if p.CountryName == "" {
		return "unknown"
	}
	return p.CountryName
}

func (p Candidate) CountryCodeOrUnknown() string {
	if p.CountryCode == "" {
		return "unknown"
	}
	return p.CountryCode
}

func (p Candidate) CityNameOrUnknown() string {
	if p.CityName == "" {
		return "unknown"
	}
	return p.CityName
}

func (p Candidate) CityCodeOrUnknown() string {
	if p.CityCode == "" {
		return "unknown"
	}
	return p.CityCode
}

func resolveProxy(proxyFlag string, countries []serverlist.Country) (Candidate, error) {
	candidate, _, err := ResolveWithStateAndCountry(proxyFlag, "", countries, "", nil)
	return candidate, err
}

func resolveProxyWithState(proxyFlag string, countries []serverlist.Country, stateFile string) (Candidate, bool, error) {
	return ResolveWithStateAndCountry(proxyFlag, "", countries, stateFile, nil)
}

// ResolveWithStateAndCountry selects the upstream: proxyFlag when set,
// else the persisted choice in stateFile when it suits countryFilter and is
// still a list entry, else a server of the location (selectProxyCandidate).
// avoided (nil: none) reports servers that failed recently
// (ServerFailover.Avoided): a persisted one is replaced, and selection
// skips them while the location has another.
func ResolveWithStateAndCountry(proxyFlag, countryFilter string, countries []serverlist.Country, stateFile string, avoided func(addr string) bool) (Candidate, bool, error) {
	if proxyFlag != "" {
		if matched, ok := findProxyCandidate(proxyFlag, countries); ok {
			matched.Addr = proxyFlag
			return matched, false, nil
		}
		return Candidate{Addr: proxyFlag}, false, nil
	}

	if stateFile != "" {
		persisted, err := loadProxySelection(stateFile)
		if err == nil {
			if avoided != nil && avoided(persisted.Addr) && len(countries) > 0 {
				logx.Info("persisted proxy failed recently; selecting another server of the location proxy=%s", persisted.Addr)
			} else if proxyCandidateMatchesCountry(persisted, countryFilter) {
				if matched, ok := findProxyCandidate(persisted.Addr, countries); ok {
					matched.Addr = persisted.Addr
					return matched, true, nil
				}
				if len(countries) == 0 {
					return persisted, true, nil
				}
				logx.Warn("persisted proxy is no longer a server-list entry; selecting a replacement proxy=%s", persisted.Addr)
			} else if countryFilter != "" {
				logx.Info("persisted proxy does not match configured country; selecting a replacement proxy=%s persisted_country_code=%s configured_country=%q",
					persisted.Addr,
					persisted.CountryCodeOrUnknown(),
					countryFilter,
				)
			}
		} else if !errors.Is(err, os.ErrNotExist) {
			logx.Warn("loading persisted proxy selection failed path=%s err=%s", stateFile, logx.Err(err))
		}
	}

	candidate, err := selectProxyCandidate(countries, countryFilter, avoided)
	if err != nil {
		return Candidate{}, false, err
	}
	return candidate, false, nil
}

// selectProxyCandidate picks a server of the location countryFilter
// selects (locationMatcher) in its first city, skipping the servers
// avoided reports (nil: none) unless the location has no other.
func selectProxyCandidate(countries []serverlist.Country, countryFilter string, avoided func(addr string) bool) (Candidate, error) {
	proxies := ConnectCandidates(countries)
	if len(proxies) == 0 {
		return Candidate{}, fmt.Errorf("no CONNECT proxies available from server list or persisted state; pass -country CODE or -proxy HOST:PORT")
	}
	match, err := locationMatcher(proxies, countryFilter)
	if err != nil {
		return Candidate{}, err
	}
	if avoided != nil {
		if candidate, ok := pickInFirstCity(proxies, func(c Candidate) bool { return match(c) && !avoided(c.Addr) }); ok {
			return candidate, nil
		}
	}
	if candidate, ok := pickInFirstCity(proxies, match); ok {
		return candidate, nil
	}
	return Candidate{}, fmt.Errorf("no CONNECT proxy found for country %q; available country codes: %s", strings.TrimSpace(countryFilter), availableCountryCodes(proxies))
}

// locationMatcher returns what selects the servers of the location for
// countryFilter: the country's (or the recommended location's for REC and
// "recommended"); without a filter the recommended location's, as in
// Firefox, or every server when the list has a single country and no
// recommended location. Every location is offered, including those the
// server list marks locked: Mozilla's servers accept an ordinary
// account's pass on them.
func locationMatcher(proxies []Candidate, countryFilter string) (func(Candidate) bool, error) {
	countryFilter = strings.TrimSpace(countryFilter)
	if countryFilter != "" {
		return func(c Candidate) bool { return proxyCandidateMatchesCountry(c, countryFilter) }, nil
	}
	if slices.ContainsFunc(proxies, Candidate.IsRecommended) {
		return Candidate.IsRecommended, nil
	}
	selectedCountryCode := ""
	for _, candidate := range proxies {
		code := strings.ToUpper(strings.TrimSpace(candidate.CountryCode))
		if code == "" {
			continue
		}
		if selectedCountryCode == "" {
			selectedCountryCode = code
			continue
		}
		if code != selectedCountryCode {
			return nil, fmt.Errorf("multiple VPN countries are available; pass -country CODE or -proxy HOST:PORT instead of selecting a random country (available: %s)", availableCountryCodes(proxies))
		}
	}
	return func(Candidate) bool { return true }, nil
}

// LocationCandidates lists, in server-list order, the servers of the
// location countryFilter selects (locationMatcher): the servers that
// ServerFailover moves the upstream among. It is empty when the filter
// selects nothing.
func LocationCandidates(countries []serverlist.Country, countryFilter string) []Candidate {
	proxies := ConnectCandidates(countries)
	match, err := locationMatcher(proxies, countryFilter)
	if err != nil {
		return nil
	}
	var location []Candidate
	for _, c := range proxies {
		if match(c) {
			location = append(location, c)
		}
	}
	return location
}

// pickInFirstCity picks, as Firefox's server list does, the first city
// among the candidates that match and one of its servers at random, so
// the users of a location spread over its servers. ok is false when none
// matches.
func pickInFirstCity(candidates []Candidate, match func(Candidate) bool) (pick Candidate, ok bool) {
	var city []Candidate
	for _, c := range candidates {
		if !match(c) {
			continue
		}
		if len(city) > 0 && (c.CountryCode != city[0].CountryCode || c.CityCode != city[0].CityCode || c.CityName != city[0].CityName) {
			continue
		}
		city = append(city, c)
	}
	if len(city) == 0 {
		return Candidate{}, false
	}
	return city[rand.IntN(len(city))], true
}

func proxyCandidateMatchesCountry(candidate Candidate, countryFilter string) bool {
	countryFilter = strings.TrimSpace(countryFilter)
	if countryFilter == "" {
		return true
	}
	if strings.EqualFold(countryFilter, "recommended") {
		return candidate.IsRecommended()
	}
	return strings.EqualFold(strings.TrimSpace(candidate.CountryCode), countryFilter) ||
		strings.EqualFold(strings.TrimSpace(candidate.CountryName), countryFilter)
}

func availableCountryCodes(candidates []Candidate) string {
	seen := make(map[string]struct{})
	codes := make([]string, 0)
	for _, candidate := range candidates {
		code := strings.ToUpper(strings.TrimSpace(candidate.CountryCode))
		if code == "" {
			continue
		}
		if _, ok := seen[code]; ok {
			continue
		}
		seen[code] = struct{}{}
		codes = append(codes, code)
	}
	if len(codes) == 0 {
		return "unknown"
	}
	return strings.Join(codes, ",")
}

func loadProxySelection(path string) (Candidate, error) {
	data, err := os.ReadFile(path)
	if err != nil {
		return Candidate{}, err
	}
	var state proxySelectionState
	if err := json.Unmarshal(data, &state); err != nil {
		return Candidate{}, err
	}
	if _, err := canonicalProxyAddr(state.Addr); err != nil {
		return Candidate{}, fmt.Errorf("invalid persisted proxy %q: %w", state.Addr, err)
	}
	return Candidate{
		Addr:        state.Addr,
		CountryName: state.CountryName,
		CountryCode: state.CountryCode,
		CityName:    state.CityName,
		CityCode:    state.CityCode,
	}, nil
}

// PersistedSelection returns the saved choice when it suits countryFilter,
// reading only the file: no server list and no log line. The status shows
// it before the proxy first connects to Mozilla, which selects for real.
func PersistedSelection(path, countryFilter string) (Candidate, bool) {
	candidate, err := loadProxySelection(path)
	if err != nil || !proxyCandidateMatchesCountry(candidate, countryFilter) {
		return Candidate{}, false
	}
	return candidate, true
}

func SaveSelection(path string, candidate Candidate) error {
	if path == "" {
		return nil
	}
	if _, err := canonicalProxyAddr(candidate.Addr); err != nil {
		return err
	}
	return writeProxySelectionState(path, proxySelectionState{
		Addr:        candidate.Addr,
		CountryName: candidate.CountryName,
		CountryCode: candidate.CountryCode,
		CityName:    candidate.CityName,
		CityCode:    candidate.CityCode,
	})
}

func writeProxySelectionState(path string, state proxySelectionState) error {
	data, err := json.MarshalIndent(state, "", "  ")
	if err != nil {
		return err
	}
	data = append(data, '\n')
	dir := filepath.Dir(path)
	if err := os.MkdirAll(dir, 0700); err != nil {
		return err
	}
	tmp, err := os.CreateTemp(dir, "."+filepath.Base(path)+".*.tmp")
	if err != nil {
		return err
	}
	tmpName := tmp.Name()
	defer os.Remove(tmpName)
	if err := tmp.Chmod(0600); err != nil {
		_ = tmp.Close()
		return err
	}
	if _, err := tmp.Write(data); err != nil {
		_ = tmp.Close()
		return err
	}
	if err := tmp.Close(); err != nil {
		return err
	}
	if err := os.Rename(tmpName, path); err != nil {
		if removeErr := os.Remove(path); removeErr != nil && !errors.Is(removeErr, os.ErrNotExist) {
			return err
		}
		if retryErr := os.Rename(tmpName, path); retryErr != nil {
			return retryErr
		}
	}
	return os.Chmod(path, 0600)
}

func findProxyCandidate(proxyFlag string, countries []serverlist.Country) (Candidate, bool) {
	target, err := canonicalProxyAddr(proxyFlag)
	if err != nil {
		return Candidate{}, false
	}
	// One host can serve several records (e.g. the anycast host); the
	// first record in server-list order names it.
	for _, candidate := range ConnectCandidates(countries) {
		candidateAddr, err := canonicalProxyAddr(candidate.Addr)
		if err != nil {
			continue
		}
		if strings.EqualFold(candidateAddr, target) {
			return candidate, true
		}
	}
	return Candidate{}, false
}

func canonicalProxyAddr(raw string) (string, error) {
	proxyURL, err := NormalizeURL(raw)
	if err != nil {
		return "", err
	}
	host := proxyURL.Host
	if proxyURL.Port() == "" {
		host = net.JoinHostPort(proxyURL.Hostname(), "443")
	}
	return strings.ToLower(host), nil
}

func connectProxyHosts(countries []serverlist.Country) []string {
	candidates := ConnectCandidates(countries)
	proxies := make([]string, 0, len(candidates))
	for _, candidate := range candidates {
		proxies = append(proxies, candidate.Addr)
	}
	return proxies
}

func ConnectCandidates(countries []serverlist.Country) []Candidate {
	var proxies []Candidate
	for _, country := range countries {
		for _, city := range country.Cities {
			for _, srv := range city.Servers {
				if srv.Quarantined {
					continue
				}
				// Firefox defaults a missing server port to 443 and a
				// missing protocol list to CONNECT on the server itself.
				serverPort := srv.Port
				if serverPort <= 0 {
					serverPort = serverlist.DefaultServerPort
				}
				candidate := func(host string, port int) Candidate {
					return Candidate{
						Addr:        net.JoinHostPort(host, strconv.Itoa(port)),
						CountryName: country.Name,
						CountryCode: country.Code,
						CityName:    city.Name,
						CityCode:    city.Code,
					}
				}
				if len(srv.Protocols) == 0 {
					if srv.Hostname != "" {
						proxies = append(proxies, candidate(srv.Hostname, serverPort))
					}
					continue
				}
				for _, proto := range srv.Protocols {
					if proto.Name != "connect" {
						continue
					}
					if scheme := strings.TrimSpace(proto.Scheme); scheme != "" && !strings.EqualFold(scheme, "https") {
						// Only TLS (https) CONNECT upstreams are supported.
						continue
					}
					host, port := proto.Host, proto.Port
					if host == "" {
						host = srv.Hostname
					}
					if port <= 0 {
						port = serverPort
					}
					if host != "" {
						proxies = append(proxies, candidate(host, port))
					}
				}
			}
		}
	}
	return proxies
}

func NormalizeURL(raw string) (*url.URL, error) {
	raw = strings.TrimSpace(raw)
	if raw == "" {
		return nil, fmt.Errorf("empty proxy value")
	}
	if !strings.Contains(raw, "://") {
		raw = "https://" + raw
	}
	parsed, err := url.Parse(raw)
	if err != nil {
		return nil, err
	}
	if parsed.Host == "" {
		return nil, fmt.Errorf("missing proxy host")
	}
	return parsed, nil
}