~poptart/x

x/ua/ua.go -rw-r--r-- 2.4 KiB
53aeea26 — poptart Updated the UA to use the omahaproxy release info 6 months ago
                                                                                
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
package useragent

import (
	"encoding/json"
	"io/ioutil"
	"net/http"
	"strconv"
	"errors"
	"time"
)

var DEFAULT_INTERVAL = int(time.Hour * 24 * 30)
var UA_URL = "https://omahaproxy.appspot.com/json"

type UserAgentCache struct {
	LastUpdate string
	Interval   int
	UserAgent  string
}

type ChromeRelease []struct {
	Os       string `json:"os"`
	Versions []struct {
		BranchCommit       string `json:"branch_commit"`
		BranchBasePosition string `json:"branch_base_position"`
		SkiaCommit         string `json:"skia_commit"`
		V8Version          string `json:"v8_version"`
		PreviousVersion    string `json:"previous_version"`
		V8Commit           string `json:"v8_commit"`
		TrueBranch         string `json:"true_branch"`
		PreviousReldate    string `json:"previous_reldate"`
		BranchBaseCommit   string `json:"branch_base_commit"`
		Version            string `json:"version"`
		CurrentReldate     string `json:"current_reldate"`
		CurrentVersion     string `json:"current_version"`
		Os                 string `json:"os"`
		Channel            string `json:"channel"`
		ChromiumCommit     string `json:"chromium_commit"`
	} `json:"versions"`
}

func GetCurrentUA() (string, error) {
	cr := ChromeRelease{}
	resp, err := http.Get(UA_URL)
	if err != nil {
		return "", err
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return "", err
	}
	err = json.Unmarshal(body, &cr)
	if err != nil {
		return "", err
	}
	for _, r := range cr {
		switch r.Os {
			case "win64":
				for _, w := range r.Versions {
					if w.Channel == "stable" {
						return "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/"+w.Version+" Safari/537.36", nil
					}
				}
		}
	}
	return "", errors.New("Could not unmarshal request")
}

func SaveUACache(file string) error {
	t := time.Now().Unix()
	ua, err := GetCurrentUA()
	if err != nil {
		return err
	}
	uac := UserAgentCache{
		Interval:   DEFAULT_INTERVAL,
		LastUpdate: strconv.FormatInt(t, 10),
		UserAgent:  ua,
	}
	uaf, err := json.Marshal(uac)
	if err != nil {
		return err
	}
	err = ioutil.WriteFile(file, []byte(uaf), 0644)
	if err != nil {
		return err
	}
	return nil
}

//func GetUA() (string, error) {
//
//}

func GetUACache(file string) (UserAgentCache, error) {
	var uac UserAgentCache
	uaf, err := ioutil.ReadFile(file)
	if err != nil {
		return UserAgentCache{}, err
	}
	err = json.Unmarshal(uaf, &uac)
	if err != nil {
		return UserAgentCache{}, err
	}
	return uac, nil
}