mirror of
https://github.com/nikdoof/aaisp-chaos.git
synced 2025-12-14 15:22:17 +00:00
Set User-Agent header
It's nice to third parties if you set User-Agent so the caller can be
identified. By default Go will set a User-Agent header to a string like
"Go-http-client/1.1".
The library now sets it to a string that identifies the source of the
program (github.com/jamesog/aaisp-chaos) and the compiled Go OS, arch
and version, to aid any potential debugging:
chaos-go (darwin; amd64; go1.15.5) github.com/jamesog/aaisp-chaos
This commit is contained in:
10
chaos.go
10
chaos.go
@@ -8,6 +8,7 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -55,6 +56,14 @@ func (a Auth) form() url.Values {
|
|||||||
return f
|
return f
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func userAgent() string {
|
||||||
|
return fmt.Sprintf("chaos-go (%s; %s; %s) github.com/jamesog/aaisp-chaos",
|
||||||
|
runtime.GOOS,
|
||||||
|
runtime.GOARCH,
|
||||||
|
runtime.Version(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
func (api API) makeRequest(url string) ([]byte, error) {
|
func (api API) makeRequest(url string) ([]byte, error) {
|
||||||
client := &http.Client{
|
client := &http.Client{
|
||||||
Timeout: 10 * time.Second,
|
Timeout: 10 * time.Second,
|
||||||
@@ -64,6 +73,7 @@ func (api API) makeRequest(url string) ([]byte, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
req.Header.Set("User-Agent", userAgent())
|
||||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user