-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetect.go
More file actions
77 lines (66 loc) · 1.91 KB
/
Copy pathdetect.go
File metadata and controls
77 lines (66 loc) · 1.91 KB
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
package main
import (
"fmt"
"io"
"net/http"
"net/url"
"regexp"
"strings"
"time"
)
var (
nginxVersionRegex = regexp.MustCompile(`(?i)nginx/([0-9]+\.[0-9]+\.[0-9]+)`)
nginxBodySigRegex = regexp.MustCompile(`(?i)<center>\s*nginx(?:/([0-9]+\.[0-9]+\.[0-9]+))?\s*</center>`)
)
const unknownVersion = "Unknown (version hidden)"
func normalizeURL(target string) (*url.URL, error) {
if !strings.HasPrefix(target, "http://") && !strings.HasPrefix(target, "https://") {
target = "http://" + target
}
return url.Parse(target)
}
func detectFromServerHeader(result *NginxResult, header, method string) {
if !strings.Contains(strings.ToLower(header), "nginx") {
return
}
result.IsNginx = true
if result.DetectionMethod == "" {
result.DetectionMethod = method
}
if matches := nginxVersionRegex.FindStringSubmatch(header); len(matches) > 1 {
if result.NginxVersion == "" || result.NginxVersion == unknownVersion {
result.NginxVersion = matches[1]
}
} else if result.NginxVersion == "" {
result.NginxVersion = unknownVersion
}
}
func probe404(client *http.Client, target *url.URL, result *NginxResult) {
probeURL := *target
probeURL.Path = fmt.Sprintf("/_goscouter_non_existent_404_probe_%d", time.Now().UnixNano())
resp, err := doGet(client, &probeURL)
if err != nil {
return
}
defer resp.Body.Close()
probeServer := resp.Header.Get("Server")
if probeServer != "" && result.ServerHeader == "" {
result.ServerHeader = probeServer
}
detectFromServerHeader(result, probeServer, "404 Probe Server Header")
body, err := io.ReadAll(io.LimitReader(resp.Body, 4096))
if err != nil || len(body) == 0 {
return
}
matches := nginxBodySigRegex.FindStringSubmatch(string(body))
if len(matches) == 0 {
return
}
result.IsNginx = true
if result.DetectionMethod == "" {
result.DetectionMethod = "404 Error Page HTML Signature"
}
if len(matches) > 1 && matches[1] != "" {
result.NginxVersion = matches[1]
}
}