Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GOAL2-781] Add user-agent to all outgoing http requests #160

Merged
merged 10 commits into from
Jul 18, 2019
12 changes: 12 additions & 0 deletions network/wsNetwork.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"net/url"
"path"
"regexp"
"runtime"
"sort"
"strings"
"sync"
Expand Down Expand Up @@ -1463,6 +1464,9 @@ const InstanceNameHeader = "X-Algorand-InstanceName"
// PriorityChallengeHeader HTTP header informs a client about the challenge it should sign to increase network priority.
const PriorityChallengeHeader = "X-Algorand-PriorityChallenge"

// UserAgentHeader is the HTTP header identify the user agent.
const UserAgentHeader = "User-Agent"

var websocketsScheme = map[string]string{"http": "ws", "https": "wss"}

var errBadAddr = errors.New("bad address")
Expand Down Expand Up @@ -1560,6 +1564,7 @@ func (wn *WebsocketNetwork) tryConnect(addr, gossipAddr string) {
defer wn.wg.Done()
requestHeader := make(http.Header)
wn.setHeaders(requestHeader)
SetUserAgentHeader(requestHeader)
myInstanceName := wn.log.GetInstanceName()
requestHeader.Set(InstanceNameHeader, myInstanceName)
conn, response, err := websocketDialer.DialContext(wn.ctx, gossipAddr, requestHeader)
Expand Down Expand Up @@ -1760,3 +1765,10 @@ func justHost(hostPort string) string {
}
return host
}

// SetUserAgentHeader adds the User-Agent header to the provided heades map.
func SetUserAgentHeader(header http.Header) {
version := config.GetCurrentVersion()
ua := fmt.Sprintf("algod/%d.%d (%s; commit=%s; %d) %s(%s)", version.Major, version.Minor, version.Channel, version.CommitHash, version.BuildNumber, runtime.GOOS, runtime.GOARCH)
header.Set(UserAgentHeader, ua)
}
7 changes: 7 additions & 0 deletions network/wsNetwork_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1417,3 +1417,10 @@ func TestForceMessageRelaying(t *testing.T) {
}

}

func TestSetUserAgentHeader(t *testing.T) {
headers := http.Header{}
SetUserAgentHeader(headers)
require.Equal(t, 1, len(headers))
t.Log(headers)
}
1 change: 1 addition & 0 deletions rpcs/httpFetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func (hf *HTTPFetcher) GetBlockBytes(ctx context.Context, r basics.Round) (data
return nil, err
}
request = request.WithContext(ctx)
network.SetUserAgentHeader(request.Header)
response, err := hf.client.Do(request)
if err != nil {
hf.log.Debugf("GET %#v : %s", blockURL, err)
Expand Down
1 change: 1 addition & 0 deletions rpcs/httpTxSync.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ func (hts *HTTPTxSync) Sync(ctx context.Context, bloom *bloom.Filter) (txns []tr
return nil, err
}
request.Header.Set("Content-Type", requestContentType)
network.SetUserAgentHeader(request.Header)
request = request.WithContext(ctx)
response, err := client.Do(request)
if err != nil {
Expand Down