-
Notifications
You must be signed in to change notification settings - Fork 0
/
HttpRequestDetector.go
186 lines (161 loc) · 5.12 KB
/
HttpRequestDetector.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package controllers
import (
"net"
"net/http"
"regexp"
"strings"
)
// HttpRequestDetector helper class that retrieves parameters from HTTP requests.
var HttpRequestDetector = _THttpRequestDetector{}
type _THttpRequestDetector struct {
}
var (
macOsXRegExp = regexp.MustCompile(`/CPU( iPhone)? OS ([0-9\._]+) like Mac OS X/`)
globalUnderscoreRegExp = regexp.MustCompile("/_/g")
androidRegExp = regexp.MustCompile(`/Android ([0-9\.]+)[\);]/`)
webOsRegExp = regexp.MustCompile(`/webOS\/([0-9\.]+)[\);]\//`)
intelMacOsXRegExp = regexp.MustCompile(`/(Intel|PPC) Mac OS X ?([0-9\._]*)[\)\;]/`)
windowsNtRegExp = regexp.MustCompile(`/Windows NT ([0-9\._]+)[\);]/`)
)
// DetectPlatform method are detects the platform (using "user-agent")
// from which the given HTTP request was made.
//
// Parameters:
// - req *http.Request an HTTP request to process.
// Returns: the detected platform and version. Detectable platforms: "mobile", "iphone",
// "ipad", "macosx", "android", "webos", "mac", "windows". Otherwise - "unknown" will
// be returned.
func (c *_THttpRequestDetector) DetectPlatform(req *http.Request) string {
ua := req.Header.Get("user-agent")
var version string
var pattern string
pattern = "/mobile/i"
match, _ := regexp.Match(pattern, ([]byte)(ua))
if match {
return "mobile"
}
pattern = "/like Mac OS X/"
match, _ = regexp.Match(pattern, ([]byte)(ua))
if match {
result := macOsXRegExp.FindAllStringSubmatch(ua, -1)
version = globalUnderscoreRegExp.ReplaceAllString(result[0][2], ".")
pattern = "/iPhone/"
match, _ = regexp.Match(pattern, ([]byte)(ua))
if match {
return "iphone " + version
}
pattern = "/iPad/"
match, _ = regexp.Match(pattern, ([]byte)(ua))
if match {
return "ipad " + version
}
return "macosx " + version
}
pattern = "/Android/"
match, _ = regexp.Match(pattern, ([]byte)(ua))
if match {
version = androidRegExp.FindAllStringSubmatch(ua, -1)[0][1]
return "android " + version
}
pattern = `/webOS\//`
match, _ = regexp.Match(pattern, ([]byte)(ua))
if match {
version = webOsRegExp.FindAllStringSubmatch(ua, -1)[0][1]
return "webos " + version
}
pattern = "/(Intel|PPC) Mac OS X/"
match, _ = regexp.Match(pattern, ([]byte)(ua))
if match {
result := intelMacOsXRegExp.FindAllStringSubmatch(ua, -1)
version = globalUnderscoreRegExp.ReplaceAllString(result[0][2], ".")
return "mac " + version
}
pattern = "/Windows NT/"
match, _ = regexp.Match(pattern, ([]byte)(ua))
if match {
version = windowsNtRegExp.FindAllStringSubmatch(ua, -1)[0][1]
return "windows " + version
}
return "unknown"
}
// DetectBrowser detects the browser (using "user-agent") from which the given HTTP request was made.
//
// Parameters:
// - req *http.Reques an HTTP request to process.
// Returns: the detected browser. Detectable browsers: "chrome", "msie", "firefox",
// "safari". Otherwise - "unknown" will be returned.
func (c *_THttpRequestDetector) DetectBrowser(req *http.Request) string {
ua := req.Header.Get("user-agent")
var pattern string
pattern = "/chrome/i"
match, _ := regexp.Match(pattern, ([]byte)(ua))
if match {
return "chrome"
}
pattern = "/msie/i"
match, _ = regexp.Match(pattern, ([]byte)(ua))
if match {
return "msie"
}
pattern = "/firefox/i"
match, _ = regexp.Match(pattern, ([]byte)(ua))
if match {
return "firefox"
}
pattern = "/safari/i"
match, _ = regexp.Match(pattern, ([]byte)(ua))
if match {
return "safari"
}
if ua == "" {
return "unknown"
}
return ua
}
// DetectAddress method are detects the IP address from which the given HTTP request was received.
//
// Parameters:
// - req *http.Reques an HTTP request to process.
// Returns the detected IP address (without a port). If no IP is detected -
// nil will be returned.
func (c *_THttpRequestDetector) DetectAddress(req *http.Request) string {
var ip string
if len(req.Header.Get("CF-Connecting-IP")) > 1 {
ip = req.Header.Get("CF-Connecting-IP")
ip = string(net.ParseIP(ip))
} else if len(req.Header.Get("X-Forwarded-For")) > 1 {
ip = req.Header.Get("X-Forwarded-For")
ip = string(net.ParseIP(ip))
} else if len(req.Header.Get("X-Real-IP")) > 1 {
ip = req.Header.Get("X-Real-IP")
ip = string(net.ParseIP(ip))
} else {
ip = req.RemoteAddr
if strings.Contains(ip, ":") {
res := strings.Split(ip, ":")
if len(res) > 0 {
ip = net.ParseIP(res[0]).String()
}
} else {
ip = net.ParseIP(ip).String()
}
}
return ip
}
// DetectServerHost method are detects the host name of the request"s destination server.
//
// Parameters:
// - req *http.Request an HTTP request to process.
// Returns: the destination server"s host name.
func (c *_THttpRequestDetector) DetectServerHost(req *http.Request) string {
//TODO: Need fix this
return "" + req.URL.Hostname() // socket.localAddress
}
// DetectServerPort method are detects the request`s destination port number.
//
// Parameters:
// - req *http.Request an HTTP request to process.
// Returns: the detected port number or <code>80</code> (if none are detected).
func (c *_THttpRequestDetector) DetectServerPort(req *http.Request) string {
return req.URL.Port() //socket.localPort
}