2017-06-22 18:51:11 +00:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2017-06-22 20:50:50 +00:00
|
|
|
"strconv"
|
2017-06-22 18:51:11 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/aerogo/aero"
|
2017-10-21 15:07:25 +00:00
|
|
|
"github.com/aerogo/http/client"
|
2019-04-23 05:52:55 +00:00
|
|
|
"github.com/akyoto/color"
|
2019-06-03 09:32:43 +00:00
|
|
|
"github.com/animenotifier/notify.moe/arn"
|
2017-06-22 18:51:11 +00:00
|
|
|
"github.com/mssola/user_agent"
|
|
|
|
)
|
|
|
|
|
|
|
|
// UserInfo updates user related information after each request.
|
2019-06-01 04:55:49 +00:00
|
|
|
func UserInfo(next aero.Handler) aero.Handler {
|
|
|
|
return func(ctx aero.Context) error {
|
|
|
|
err := next(ctx)
|
2017-06-22 18:51:11 +00:00
|
|
|
|
2017-06-23 14:58:12 +00:00
|
|
|
// Ignore non-HTML requests
|
2019-06-01 04:55:49 +00:00
|
|
|
contentType := ctx.Response().Header("Content-Type")
|
2017-06-23 17:22:39 +00:00
|
|
|
|
2018-03-25 15:32:42 +00:00
|
|
|
if !strings.HasPrefix(contentType, "text/html") {
|
2019-06-01 04:55:49 +00:00
|
|
|
return nil
|
2017-06-23 14:58:12 +00:00
|
|
|
}
|
2017-06-22 18:51:11 +00:00
|
|
|
|
2019-11-17 07:59:34 +00:00
|
|
|
user := arn.GetUserFromContext(ctx)
|
2017-06-22 18:51:11 +00:00
|
|
|
|
2017-06-23 14:58:12 +00:00
|
|
|
// When there's no user logged in, nothing to update
|
|
|
|
if user == nil {
|
2019-06-01 04:55:49 +00:00
|
|
|
return nil
|
2017-06-23 14:58:12 +00:00
|
|
|
}
|
2017-06-22 18:51:11 +00:00
|
|
|
|
2019-06-16 23:31:57 +00:00
|
|
|
// Bind local variables and start a coroutine
|
|
|
|
ip := ctx.IP()
|
|
|
|
userAgent := ctx.Request().Header("User-Agent")
|
|
|
|
go updateUserInfo(ip, userAgent, user)
|
|
|
|
|
2019-06-01 04:55:49 +00:00
|
|
|
return err
|
2017-06-22 18:51:11 +00:00
|
|
|
}
|
2017-06-23 14:58:12 +00:00
|
|
|
}
|
2017-06-22 18:51:11 +00:00
|
|
|
|
2017-06-23 15:00:41 +00:00
|
|
|
// Update browser and OS data
|
2019-06-16 23:31:57 +00:00
|
|
|
func updateUserInfo(ip string, userAgent string, user *arn.User) {
|
|
|
|
if user.UserAgent != userAgent {
|
|
|
|
user.UserAgent = userAgent
|
2017-06-22 18:51:11 +00:00
|
|
|
|
|
|
|
// Parse user agent
|
|
|
|
parsed := user_agent.New(user.UserAgent)
|
|
|
|
|
|
|
|
// Browser
|
|
|
|
user.Browser.Name, user.Browser.Version = parsed.Browser()
|
|
|
|
|
|
|
|
// OS
|
|
|
|
os := parsed.OSInfo()
|
|
|
|
user.OS.Name = os.Name
|
|
|
|
user.OS.Version = os.Version
|
|
|
|
}
|
|
|
|
|
2017-06-23 14:58:12 +00:00
|
|
|
user.LastSeen = arn.DateTimeUTC()
|
|
|
|
user.Save()
|
2019-06-01 15:28:22 +00:00
|
|
|
|
2019-06-16 23:31:57 +00:00
|
|
|
if user.IP == ip {
|
|
|
|
return
|
2019-06-01 15:28:22 +00:00
|
|
|
}
|
2019-06-16 23:31:57 +00:00
|
|
|
|
|
|
|
updateUserLocation(user, ip)
|
2017-06-23 14:58:12 +00:00
|
|
|
}
|
2017-06-22 18:51:11 +00:00
|
|
|
|
2017-06-23 15:00:41 +00:00
|
|
|
// Updates the location of the user.
|
2017-06-23 14:58:12 +00:00
|
|
|
func updateUserLocation(user *arn.User, newIP string) {
|
|
|
|
user.IP = newIP
|
2018-04-07 12:17:33 +00:00
|
|
|
|
|
|
|
if arn.APIKeys.IPInfoDB.ID == "" {
|
|
|
|
if arn.IsProduction() {
|
|
|
|
color.Red("IPInfoDB key not defined")
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-06-27 14:23:57 +00:00
|
|
|
locationAPI := "https://api.ipinfodb.com/v3/ip-city/?key=" + arn.APIKeys.IPInfoDB.ID + "&ip=" + user.IP + "&format=json"
|
2017-10-21 15:07:25 +00:00
|
|
|
response, err := client.Get(locationAPI).End()
|
2017-06-22 18:51:11 +00:00
|
|
|
|
2017-10-21 15:07:25 +00:00
|
|
|
if err != nil {
|
|
|
|
color.Red("Couldn't fetch location data | Error: %s | IP: %s", err.Error(), user.IP)
|
2017-06-23 14:58:12 +00:00
|
|
|
return
|
2017-06-22 18:51:11 +00:00
|
|
|
}
|
|
|
|
|
2017-10-21 15:07:25 +00:00
|
|
|
if response.StatusCode() != http.StatusOK {
|
2017-06-23 14:58:12 +00:00
|
|
|
color.Red("Couldn't fetch location data | Status: %d | IP: %s", response.StatusCode, user.IP)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
newLocation := arn.IPInfoDBLocation{}
|
2019-06-01 04:55:49 +00:00
|
|
|
err = response.Unmarshal(&newLocation)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
color.Red("Couldn't deserialize location data | Status: %d | IP: %s", response.StatusCode, user.IP)
|
|
|
|
return
|
|
|
|
}
|
2017-06-23 14:58:12 +00:00
|
|
|
|
2019-06-01 15:28:22 +00:00
|
|
|
if newLocation.CountryName == "" || newLocation.CountryName == "-" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user.Location.CountryName = newLocation.CountryName
|
|
|
|
user.Location.CountryCode = newLocation.CountryCode
|
|
|
|
user.Location.Latitude, _ = strconv.ParseFloat(newLocation.Latitude, 64)
|
|
|
|
user.Location.Longitude, _ = strconv.ParseFloat(newLocation.Longitude, 64)
|
|
|
|
user.Location.CityName = newLocation.CityName
|
|
|
|
user.Location.RegionName = newLocation.RegionName
|
|
|
|
user.Location.TimeZone = newLocation.TimeZone
|
|
|
|
user.Location.ZipCode = newLocation.ZipCode
|
|
|
|
|
|
|
|
// Make South Korea easier to read
|
|
|
|
if user.Location.CountryName == "Korea, Republic of" {
|
|
|
|
user.Location.CountryName = "South Korea"
|
2017-06-23 14:58:12 +00:00
|
|
|
}
|
2019-06-01 15:28:22 +00:00
|
|
|
|
|
|
|
user.Save()
|
2017-06-22 18:51:11 +00:00
|
|
|
}
|