-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
174 lines (135 loc) · 3.67 KB
/
main.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
package main
import (
"context"
"fmt"
"net"
"net/http"
"net/netip"
"os"
"strconv"
"strings"
"time"
)
var port string
func isEnvVariableTrue(variableName string) bool {
variableValue, variableSpecified := os.LookupEnv(variableName)
if !variableSpecified {
return false
}
if variableValue == "" || variableValue == "1" || variableValue == "true" {
return true
}
return false
}
func padRight(minLength int, str string) string {
if len(str) < minLength {
return str + strings.Repeat(" ", minLength-len(str))
}
return str
}
func getHostname() string {
serverHostname, err := os.Hostname()
if err != nil {
serverHostname = "unknown"
}
return serverHostname
}
func getLocalAddresses(ctx context.Context) []string {
addrs, err := net.InterfaceAddrs()
if err != nil {
return []string{}
}
localAddresses := []string{}
for _, addr := range addrs {
prefix := netip.MustParsePrefix(addr.String())
if prefix.Addr().IsLoopback() || prefix.Addr().IsLinkLocalUnicast() {
continue
}
ctx, cancel := context.WithTimeout(ctx, time.Millisecond*100)
defer cancel()
revNames, err := net.DefaultResolver.LookupAddr(ctx, prefix.Addr().StringExpanded())
if err != nil {
fmt.Println(err)
continue
}
localAddresses = append(localAddresses, fmt.Sprintf("%s %s", padRight(39, prefix.Addr().StringExpanded()), strings.Join(revNames, ", ")))
}
return localAddresses
}
func getClientIp(r *http.Request, shortRevdns bool) string {
clientIp, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
return "unknown"
}
ctx, cancel := context.WithTimeout(r.Context(), time.Millisecond*100)
defer cancel()
revNames, err := net.DefaultResolver.LookupAddr(ctx, clientIp)
if err != nil {
return clientIp
}
if shortRevdns {
return fmt.Sprintf("%s (%s)", clientIp, strings.Join(revNames, ", "))
} else {
return fmt.Sprintf("%s %s", padRight(39, clientIp), strings.Join(revNames, ", "))
}
}
func printKV(w http.ResponseWriter, k, v string) {
fmt.Fprintf(w, "%s: %s\n", padRight(28, k), v)
}
func printSpacer(w http.ResponseWriter) {
fmt.Fprint(w, "\n")
}
func handler(w http.ResponseWriter, r *http.Request) {
printKV(w, "Timestamp", strconv.FormatInt(time.Now().Unix(), 10))
printSpacer(w)
if isEnvVariableTrue("DEBUG_HTTP") {
printKV(w, "HTTP URL", r.URL.String())
printKV(w, "HTTP Host", r.Host)
printKV(w, "HTTP Listen port", port)
printKV(w, "HTTP Referer", r.Referer())
printKV(w, "HTTP User agent", r.UserAgent())
printSpacer(w)
}
if isEnvVariableTrue("DEBUG_SERVER") {
printKV(w, "Server hostname", getHostname())
localAddresses := getLocalAddresses(r.Context())
for _, addr := range localAddresses {
printKV(w, "Server's address", addr)
}
printSpacer(w)
}
if isEnvVariableTrue("DEBUG_CLIENT") {
printKV(w, "Client's IP", getClientIp(r, false))
printSpacer(w)
}
fmt.Printf("Handled request for %s, path http://%s%s\n", getClientIp(r, true), r.Host, r.URL.String())
}
func main() {
somethingEnabled := false
if isEnvVariableTrue("DEBUG_HTTP") {
somethingEnabled = true
fmt.Println("Enabled HTTP debug section")
}
if isEnvVariableTrue("DEBUG_SERVER") {
somethingEnabled = true
fmt.Println("Enabled server debug section")
}
if isEnvVariableTrue("DEBUG_CLIENT") {
somethingEnabled = true
fmt.Println("Enabled client debug section")
}
if !somethingEnabled {
fmt.Println("You need to enable at least one section for this fotware to be useful!")
}
var portSpecified bool
port, portSpecified = os.LookupEnv("HTTP_PORT")
if !portSpecified {
port = "8080"
}
fmt.Printf("Will listen on %s port\n", port)
http.HandleFunc("/", handler)
err := http.ListenAndServe(":"+port, nil)
if err != nil {
panic(err)
}
}