aboutsummaryrefslogtreecommitdiff
path: root/web/user_agent.go
blob: 249f6b7238be5988854330dac9f6a8ed595dc31d (plain)
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
package web

import (
	"fmt"
	"net/http"
	"os"
	"runtime"
	"time"

	"github.com/celrenheit/sandflake"
)

var (
	startID  = sandflake.Next()
	hostname = "<unknown>"
	started  = time.Now()
)

func init() {
	http.DefaultTransport = &userAgentTransport{http.DefaultTransport}

	name, _ := os.Hostname()
	if name != "" {
		hostname = name
	}
}

// GenUserAgent creates a unique User-Agent string for outgoing HTTP requests.
func GenUserAgent() string {
	return fmt.Sprintf("github.com-Xe-x (%s/%s/%s; %s; +https://within.website/.x.botinfo) Alive (%s; sandflake) Hostname/%s Started (%s)", runtime.Version(), runtime.GOOS, runtime.GOARCH, os.Args[0], startID.String(), hostname, started.Format(time.RFC3339))
}

type userAgentTransport struct {
	rt http.RoundTripper
}

func (uat userAgentTransport) RoundTrip(r *http.Request) (*http.Response, error) {
	r.Header.Set("User-Agent", GenUserAgent())
	return uat.rt.RoundTrip(r)
}