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

Register and report User-Agents used in Lantern session #2959

Merged
merged 3 commits into from
Aug 18, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/github.com/getlantern/flashlight/client/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"sync"

"github.com/getlantern/detour"
"github.com/getlantern/flashlight/logging"
)

const (
Expand All @@ -21,6 +22,8 @@ const (
// handler available from getHandler() and latest ReverseProxy available from
// getReverseProxy().
func (client *Client) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
logging.RegisterUserAgent(req.Header.Get("User-Agent"))

if req.Method == httpConnectMethod {
// CONNECT requests are often used for HTTPS requests.
log.Tracef("Intercepting CONNECT %s", req.URL)
Expand Down
17 changes: 9 additions & 8 deletions src/github.com/getlantern/flashlight/logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,14 +215,15 @@ func (w logglyErrorWriter) Write(b []byte) (int, error) {
}

extra := map[string]string{
"logLevel": "ERROR",
"osName": runtime.GOOS,
"osArch": runtime.GOARCH,
"osVersion": "",
"language": w.lang,
"country": geolookup.GetCountry(),
"timeZone": w.tz,
"version": w.versionToLoggly,
"logLevel": "ERROR",
"osName": runtime.GOOS,
"osArch": runtime.GOARCH,
"osVersion": "",
"language": w.lang,
"country": geolookup.GetCountry(),
"timeZone": w.tz,
"version": w.versionToLoggly,
"sessionUserAgents": getSessionUserAgents(),
}

// extract last 2 (at most) chunks of fullMessage to message, without prefix,
Expand Down
48 changes: 48 additions & 0 deletions src/github.com/getlantern/flashlight/logging/useragents.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package logging

import (
"bytes"
"fmt"
"regexp"
"sync"
)

var (
userAgents = make(map[string]int)
agentsMutex = &sync.Mutex{}
reg = regexp.MustCompile("^Go.*package http$")
)

// registerUserAgent tries to find the User-Agent in the HTTP request
// and keep track of the applications using Lantern during this session
func RegisterUserAgent(agent string) {
// Do this asynchronously because it is not a critical operation,
// so there is no wait for the mutex in the caller goroutine
go func() {
if agent != "" {
agentsMutex.Lock()
defer agentsMutex.Unlock()
if n, ok := userAgents[agent]; ok {
userAgents[agent] = n + 1
} else {
userAgents[agent] = 1
}
}
}()
}

// getSessionUserAgents returns the
func getSessionUserAgents() string {
agentsMutex.Lock()
defer agentsMutex.Unlock()

var buffer bytes.Buffer

for key, val := range userAgents {
if !reg.MatchString(key) {
buffer.WriteString(key)
buffer.WriteString(fmt.Sprintf(": %d requests; ", val))
}
}
return string(buffer.String())
}