arblant
firefox-vpn-client / core / internal / serverlist / version.go
version.go53 lines · 1.6 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
package serverlist

import (
	"context"
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"strings"

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

// productDetailsURL lists Mozilla's current Firefox versions.
const productDetailsURL = "https://product-details.mozilla.org/1.0/firefox_versions.json"

// LatestFirefoxVersion returns the newest Firefox version Mozilla publishes
// (Nightly, else the release), which every version-gated server-list
// record written for current Firefox accepts.
func LatestFirefoxVersion(ctx context.Context) (string, error) {
	req, err := http.NewRequestWithContext(ctx, http.MethodGet, productDetailsURL, nil)
	if err != nil {
		return "", err
	}
	controlplane.ApplyFirefoxHeaders(req)
	resp, err := controlplane.Do(req)
	if err != nil {
		return "", fmt.Errorf("fetching Firefox versions: %w", err)
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		return "", fmt.Errorf("Firefox versions returned HTTP %d", resp.StatusCode)
	}
	data, err := io.ReadAll(io.LimitReader(resp.Body, 64<<10))
	if err != nil {
		return "", err
	}
	var versions struct {
		Nightly string `json:"FIREFOX_NIGHTLY"`
		Release string `json:"LATEST_FIREFOX_VERSION"`
	}
	if err := json.Unmarshal(data, &versions); err != nil {
		return "", fmt.Errorf("parsing Firefox versions: %w", err)
	}
	// The release names this program in its User-Agent.
	controlplane.SetFirefoxVersion(versions.Release)
	for _, v := range []string{versions.Nightly, versions.Release} {
		if v = strings.TrimSpace(v); v != "" && v[0] >= '0' && v[0] <= '9' {
			return v, nil
		}
	}
	return "", fmt.Errorf("no Firefox version in the product details")
}