-
Notifications
You must be signed in to change notification settings - Fork 5
/
net_headers.go
executable file
·76 lines (66 loc) · 1.86 KB
/
net_headers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package utils
import (
"net/http"
"github.com/mileusna/useragent"
)
// Retrieves a country code via request header, prefer cloudflare, then vercel
func GetCountryCode(r *http.Request) string {
countryCode := r.Header.Get("CF-IPCountry")
if countryCode == "" {
countryCode = r.Header.Get("X-Vercel-IP-Country")
}
return countryCode
}
// Parses user agent to return device type, os, and browser
type ClientDeviceType string
const (
Desktop ClientDeviceType = "desktop"
Mobile ClientDeviceType = "mobile"
Tablet ClientDeviceType = "tablet"
Bot ClientDeviceType = "bot"
Unknown ClientDeviceType = "unknown"
)
type ClientDeviceInfo struct {
DeviceType ClientDeviceType `json:"device_type"`
DeviceOs string `json:"device_os"`
DeviceBrowser string `json:"device_browser"`
DeviceBrowserVersion string `json:"device_browser_version"`
}
func GetClientDeviceInfo(r *http.Request) ClientDeviceInfo {
userAgent := r.Header.Get("User-Agent")
client := useragent.Parse(userAgent)
deviceType := Unknown
if client.Mobile {
deviceType = Mobile
} else if client.Tablet {
deviceType = Tablet
} else if client.Bot {
deviceType = Bot
} else if client.Desktop {
deviceType = Desktop
}
return ClientDeviceInfo{
DeviceType: deviceType,
DeviceOs: client.OS,
DeviceBrowser: client.Name,
DeviceBrowserVersion: client.Version,
}
}
// Get a clients real user IP Address
func GetIPAddress(r *http.Request) string {
IPAddress := r.Header.Get("CF-Connecting-IP")
if IPAddress == "" {
IPAddress = r.Header.Get("X-Real-Ip")
}
if IPAddress == "" {
IPAddress = r.Header.Get("X-Forwarded-For")
}
if IPAddress == "" {
IPAddress = r.RemoteAddr
}
return IPAddress
}
func GetThumbmarkID(r *http.Request) string {
thumbmarkID := r.Header.Get("X-Thumbmark-ID")
return thumbmarkID
}