From 53aeea2679f06c27229807cd53f43e3fa518358a Mon Sep 17 00:00:00 2001 From: poptart Date: Tue, 14 Apr 2020 15:56:16 -0500 Subject: [PATCH] Updated the UA to use the omahaproxy release info --- cmd/uacmd.go | 9 ++++++--- ua/ua.go | 49 +++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 45 insertions(+), 13 deletions(-) diff --git a/cmd/uacmd.go b/cmd/uacmd.go index dbaab7b..c456516 100644 --- a/cmd/uacmd.go +++ b/cmd/uacmd.go @@ -7,13 +7,16 @@ import ( ) func main() { - if _, err := os.Stat("/home/poptart/.config/hosaka_uacache"); err == nil { - b, _ := useragent.GetUACache("/home/poptart/.config/hosaka_uacache") + if _, err := os.Stat(os.Getenv("HOME")+"/.config/hosaka_uacache"); err == nil { + b, _ := useragent.GetUACache(os.Getenv("HOME")+"/.config/hosaka_uacache") fmt.Println(b.UserAgent) } else if os.IsNotExist(err) { - e := useragent.SaveUACache("/home/poptart/.config/hosaka_uacache") + e := useragent.SaveUACache(os.Getenv("HOME")+"/.config/hosaka_uacache") if e != nil { fmt.Println(e) } + + b, _ := useragent.GetUACache(os.Getenv("HOME")+"/.config/hosaka_uacache") + fmt.Println(b.UserAgent) } } diff --git a/ua/ua.go b/ua/ua.go index d602a08..c1b0063 100644 --- a/ua/ua.go +++ b/ua/ua.go @@ -2,18 +2,15 @@ package useragent import ( "encoding/json" - "fmt" "io/ioutil" "net/http" - "regexp" "strconv" + "errors" "time" ) var DEFAULT_INTERVAL = int(time.Hour * 24 * 30) -var UA_URL = "https://raw.githubusercontent.com/chromium/chromium/master/content/public/common/user_agent.h" -var REG1 = `Mozilla\/5.0 \(Windows[^"]*` -var REG2 = `like Gecko\) Chrome[^"]*` +var UA_URL = "https://omahaproxy.appspot.com/json" type UserAgentCache struct { LastUpdate string @@ -21,9 +18,29 @@ type UserAgentCache struct { 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) { - re1 := regexp.MustCompile(REG1) - re2 := regexp.MustCompile(REG2) + cr := ChromeRelease{} resp, err := http.Get(UA_URL) if err != nil { return "", err @@ -33,9 +50,21 @@ func GetCurrentUA() (string, error) { if err != nil { return "", err } - m1 := re1.Find(body) - m2 := re2.Find(body) - return fmt.Sprintf("%s%s", string(m1), string(m2)), nil + 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 { -- 2.26.2