Commit aac923b3 authored by Lysander Trischler's avatar Lysander Trischler

Support old multi user User-Agent headers

parent c1b6d041
......@@ -27,11 +27,13 @@ func main() {
if ua.IsTwtxt() {
twtxtUAs++
if ua.TwtxtURL != "" {
singleUsers[ua.TwtxtURL] = ua.TwtxtNick
urlCounter[ua.TwtxtURL]++
if len(ua.TwtxtURLs) > 0 {
for i, twtxtURL := range ua.TwtxtURLs {
singleUsers[twtxtURL] = ua.TwtxtNicks[i]
urlCounter[twtxtURL]++
}
} else if ua.WhoFollowsURL != "" {
u, err := neturl.Parse(ua.WhoFollowsURL)
u, err := url.Parse(ua.WhoFollowsURL)
if err != nil {
fmt.Printf("ERROR: Invalid Who Follows Resource: %v\n", err)
urlCounter["ERROR"]++
......
......@@ -9,13 +9,13 @@ import (
type UserAgent struct {
ClientName string
ClientVersion string
TwtxtURL string
TwtxtNick string
TwtxtURLs []string
TwtxtNicks []string
WhoFollowsURL string
}
func (ua UserAgent) IsTwtxt() bool {
return ua.TwtxtURL != "" || ua.TwtxtNick != "" || ua.WhoFollowsURL != ""
return len(ua.TwtxtURLs) > 0 || len(ua.TwtxtNicks) > 0 || ua.WhoFollowsURL != ""
}
func ExtractUserAgentFromNginxAccessLog(line string) string {
......@@ -53,8 +53,8 @@ func ParseUserAgent(userAgent string) UserAgent {
singleUser := singleUserRegex.FindStringSubmatch(userAgent)
if len(singleUser) == 3 {
ua.TwtxtURL = singleUser[1]
ua.TwtxtNick = singleUser[2]
ua.TwtxtURLs = []string{singleUser[1]}
ua.TwtxtNicks = []string{singleUser[2]}
return ua
}
......@@ -66,22 +66,27 @@ func ParseUserAgent(userAgent string) UserAgent {
oldMultiUser6 := oldMultiUser6Regex.FindStringSubmatch(userAgent)
if len(oldMultiUser6) == 3 {
// TODO generate known nicks and URLs
ua.WhoFollowsURL = oldMultiUser6[2]
setNicksAndURLs(&ua, oldMultiUser6[1], ua.WhoFollowsURL)
return ua
}
oldMultiUser25 := oldMultiUser25Regex.FindStringSubmatch(userAgent)
if len(oldMultiUser25) == 3 {
u, err := url.Parse(oldMultiUser25[2])
if err != nil {
return ua
}
a := u
u = a
// TODO generate known nicks and URLs
setNicksAndURLs(&ua, oldMultiUser25[1], oldMultiUser25[2])
return ua
}
return ua
}
func setNicksAndURLs(ua *UserAgent, followerNicks, referenceURL string) {
ua.TwtxtNicks = strings.Split(followerNicks, " ")
u, err := url.Parse(referenceURL)
if err == nil {
baseURL := u.Scheme + "://" + u.Host + "/user/"
for _, nick := range ua.TwtxtNicks {
ua.TwtxtURLs = append(ua.TwtxtURLs, baseURL+nick+"/twtxt.txt")
}
}
}
......@@ -58,8 +58,8 @@ func TestParseUserAgent(t *testing.T) {
expected: UserAgent{
ClientName: "Mozilla",
ClientVersion: "5.0",
TwtxtURL: "",
TwtxtNick: "",
TwtxtURLs: nil,
TwtxtNicks: nil,
WhoFollowsURL: "",
},
},
......@@ -69,8 +69,8 @@ func TestParseUserAgent(t *testing.T) {
expected: UserAgent{
ClientName: "twtxt",
ClientVersion: "0.1.0@ec2f019",
TwtxtURL: "https://txt.sour.is/user/xuu/twtxt.txt",
TwtxtNick: "xuu",
TwtxtURLs: []string{"https://txt.sour.is/user/xuu/twtxt.txt"},
TwtxtNicks: []string{"xuu"},
WhoFollowsURL: "",
},
},
......@@ -80,8 +80,8 @@ func TestParseUserAgent(t *testing.T) {
expected: UserAgent{
ClientName: "twtxt",
ClientVersion: "0.1.0@35cbec4",
TwtxtURL: "",
TwtxtNick: "",
TwtxtURLs: nil,
TwtxtNicks: nil,
WhoFollowsURL: "https://twtxt.net/whoFollows?followers=9&token=3VFp9uq2B",
},
},
......@@ -91,8 +91,8 @@ func TestParseUserAgent(t *testing.T) {
expected: UserAgent{
ClientName: "twtxt",
ClientVersion: "0.1.0@35cbec4",
TwtxtURL: "",
TwtxtNick: "",
TwtxtURLs: nil,
TwtxtNicks: nil,
WhoFollowsURL: "https://twtxt.net/whoFollows?followers=9&token=3VFp9uq2B",
},
},
......@@ -102,8 +102,8 @@ func TestParseUserAgent(t *testing.T) {
expected: UserAgent{
ClientName: "twtxt",
ClientVersion: "0.1.0@69ac73b",
TwtxtURL: "",
TwtxtNick: "",
TwtxtURLs: []string{"https://twt.nfld.uk/user/alice/twtxt.txt", "https://twt.nfld.uk/user/jlj/twtxt.txt"},
TwtxtNicks: []string{"alice", "jlj"},
WhoFollowsURL: "",
},
},
......@@ -113,8 +113,13 @@ func TestParseUserAgent(t *testing.T) {
expected: UserAgent{
ClientName: "twtxt",
ClientVersion: "0.1.0@37fd365",
TwtxtURL: "",
TwtxtNick: "",
TwtxtURLs: []string{
"https://twtxt.net/user/adi/twtxt.txt",
"https://twtxt.net/user/antonio/twtxt.txt",
"https://twtxt.net/user/darch/twtxt.txt",
"https://twtxt.net/user/gareppa/twtxt.txt",
"https://twtxt.net/user/ionores/twtxt.txt"},
TwtxtNicks: []string{"adi", "antonio", "darch", "gareppa", "ionores"},
WhoFollowsURL: "https://twtxt.net/whoFollows?uri=https://lyse.isobeef.org/twtxt.txt&nick=lyse&token=OzcdPbe6Z",
},
},
......@@ -124,10 +129,9 @@ func TestParseUserAgent(t *testing.T) {
expected: UserAgent{
ClientName: "",
ClientVersion: "",
TwtxtURL: "",
TwtxtNick: "",
TwtxtURLs: nil,
TwtxtNicks: nil,
WhoFollowsURL: "",
// TODO
},
},
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment