Skip to content

Commit

Permalink
User Agent:
Browse files Browse the repository at this point in the history
- Now sends user agent with gateway requests
- git based version is embedded in binary for UA and logs
- configurable string to put in UA for big servers
Fixes #32
  • Loading branch information
karmanyaahm committed Feb 18, 2022
1 parent f5f447f commit 8bab3ba
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ BUILD_DIR=./bin
DOCKER_CMD=docker run --rm -it -v "$$PWD":/usr/src/myapp -w /usr/src/myapp golang:1.17

local:
go build -o up-rewrite
GIT_CMT=$$(git describe --tags) && go build -o up-rewrite -ldflags "-X github.com/karmanyaahm/up_rewrite/config.Version=$$GIT_CMT"
local-docker:
$(DOCKER_CMD) make local
all:
Expand Down
20 changes: 18 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ import (
"github.com/komkom/toml"
)

var Version string = "dev"

var Config Configuration
var ConfigLock sync.RWMutex

type Configuration struct {
ListenAddr string `env:"UP_LISTEN"`
Verbose bool `env:"UP_VERBOSE"`
ListenAddr string `env:"UP_LISTEN"`
Verbose bool `env:"UP_VERBOSE"`
UserAgentID string `env:"UP_UAID"`

Gateway struct {
AllowedHosts []string `env:"UP_GATEWAY_ALLOWEDHOSTS"`
Expand All @@ -34,6 +37,19 @@ type Configuration struct {
}
}

var ua string

func (c Configuration) GetUserAgent() string {
if ua != "" {
return ua
}
ua = "UnifiedPush-Common-Proxies/" + Version
if Config.UserAgentID != "" {
ua += " (" + Config.UserAgentID + ")"
}
return ua
}

func ParseConf(location string) error {
ConfigLock.Lock()
defer ConfigLock.Unlock()
Expand Down
2 changes: 2 additions & 0 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ func gatewayHandler(h Gateway) HttpHandler {
resps := make([]*http.Response, len(reqs))
for i, req := range reqs {
nwritten += req.ContentLength

req.Header.Add("User-Agent", Config.GetUserAgent())
if utils.InStringSlice(config.Config.Gateway.AllowedHosts, req.URL.Host) {
CheckIfRewriteProxy(req.URL.String(), normalClient)
resps[i], err = normalClient.Do(req)
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ func main() {
log.Fatalln(err)
}

log.Println("Starting", Config.GetUserAgent())

handlers = []Handler{
&Config.Rewrite.Gotify,
&Config.Rewrite.FCM,
Expand Down

0 comments on commit 8bab3ba

Please sign in to comment.