Skip to content

Commit

Permalink
Make user agent more configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Mar 22, 2021
1 parent 1742baf commit 6002723
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 1 deletion.
8 changes: 8 additions & 0 deletions appservice/appservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func Create() *AppService {
intents: make(map[id.UserID]*IntentAPI),
StateStore: NewBasicStateStore(),
Router: mux.NewRouter(),
UserAgent: mautrix.DefaultUserAgent,
}
}

Expand Down Expand Up @@ -93,6 +94,7 @@ type AppService struct {
StateStore StateStore `yaml:"-"`

Router *mux.Router `yaml:"-"`
UserAgent string `yaml:"-"`
server *http.Server
botClient *mautrix.Client
botIntent *IntentAPI
Expand Down Expand Up @@ -198,6 +200,7 @@ func (as *AppService) makeClient(userID id.UserID) *mautrix.Client {
as.Log.Fatalln("Failed to create mautrix client instance:", err)
return nil
}
client.UserAgent = as.UserAgent
client.Syncer = nil
client.Store = nil
client.AppServiceUserID = userID
Expand All @@ -224,6 +227,7 @@ func (as *AppService) BotClient() *mautrix.Client {
as.Log.Fatalln("Failed to create gomatrix instance:", err)
return nil
}
as.botClient.UserAgent = as.UserAgent
as.botClient.Syncer = nil
as.botClient.Store = nil
as.botClient.Logger = as.Log.Sub("Bot")
Expand All @@ -237,6 +241,10 @@ func (as *AppService) Init() (bool, error) {
as.Events = make(chan *event.Event, EventChannelSize)
as.QueryHandler = &QueryHandlerStub{}

if len(as.UserAgent) == 0 {
as.UserAgent = mautrix.DefaultUserAgent
}

as.Log = maulogger.Create()
as.LogConfig.Configure(as.Log)
as.Log.Debugln("Logger initialized successfully.")
Expand Down
1 change: 1 addition & 0 deletions appservice/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func (as *AppService) StartWebsocket(baseURL string, onConnect func()) error {
}
ws, resp, err := websocket.DefaultDialer.Dial(parsed.String(), http.Header{
"Authorization": []string{fmt.Sprintf("Bearer %s", as.Registration.AppToken)},
"User-Agent": []string{as.BotClient().UserAgent},
})
if resp != nil && resp.StatusCode >= 400 {
var errResp ErrorResponse
Expand Down
4 changes: 3 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func DiscoverClientAPI(serverName string) (*ClientWellKnown, error) {
}

req.Header.Set("Accept", "application/json")
req.Header.Set("User-Agent", DefaultUserAgent + " .well-known fetcher")

client := &http.Client{Timeout: 30 * time.Second}
resp, err := client.Do(req)
Expand Down Expand Up @@ -928,6 +929,7 @@ func (cli *Client) UploadMedia(data ReqUploadMedia) (*RespMediaUpload, error) {
req.Header.Set("Content-Type", data.ContentType)
}
req.Header.Set("Authorization", "Bearer "+cli.AccessToken)
req.Header.Set("User-Agent", cli.UserAgent)
req.ContentLength = data.ContentLength

cli.LogRequest(req, fmt.Sprintf("%d bytes", data.ContentLength))
Expand Down Expand Up @@ -1253,7 +1255,7 @@ func NewClient(homeserverURL string, userID id.UserID, accessToken string) (*Cli
}
return &Client{
AccessToken: accessToken,
UserAgent: "mautrix-go " + Version,
UserAgent: DefaultUserAgent,
HomeserverURL: hsURL,
UserID: userID,
Client: &http.Client{Timeout: 180 * time.Second},
Expand Down
2 changes: 2 additions & 0 deletions version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package mautrix

const Version = "v0.9.2"

var DefaultUserAgent = "mautrix-go/" + Version

0 comments on commit 6002723

Please sign in to comment.