~poptart/x

53aeea2679f06c27229807cd53f43e3fa518358a — poptart 7 months ago 63542d5 master ua
Updated the UA to use the omahaproxy release info
2 files changed, 45 insertions(+), 13 deletions(-)

M cmd/uacmd.go
M ua/ua.go
M cmd/uacmd.go => cmd/uacmd.go +6 -3
@@ 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)
	}
}

M ua/ua.go => ua/ua.go +39 -10
@@ 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 {