-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
95 lines (79 loc) · 2.16 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
package main
import (
"context"
"fmt"
"log"
"net"
"net/http"
"os"
"strings"
)
type helloWebHandler struct {
port string
hostname string
name string
}
type contextKey struct {
key string
}
var connContextKey = &contextKey{"conn"}
func getLocalAddress(r *http.Request) (string, bool) {
conn, ok := r.Context().Value(connContextKey).(net.Conn)
if ok {
return conn.LocalAddr().String(), true
}
return "", false
}
func (handler helloWebHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Printf("Request received on port %v from %v\n", handler.port, r.RemoteAddr)
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "text/plain")
fmt.Fprintf(w, "Hello, World! You have reached port %v of %v from %v.\n", handler.port, handler.name, r.RemoteAddr)
fmt.Fprintf(w, "\nHost Details:\n")
fmt.Fprintf(w, "Name: %v\n", handler.name)
fmt.Fprintf(w, "Hostname: %v\n", handler.hostname)
addr, ok := getLocalAddress(r)
if ok {
fmt.Fprintf(w, "Server Address: %v\n", addr)
}
addrs, err := net.InterfaceAddrs()
if err == nil {
fmt.Fprintf(w, "All Addresses: %v\n", addrs)
}
fmt.Fprintf(w, "\nRequest Details:\n")
fmt.Fprintf(w, "Method: %v\n", r.Method)
fmt.Fprintf(w, "URL: %v\n", r.URL)
fmt.Fprintf(w, "Protocol: %v\n", r.Proto)
fmt.Fprintf(w, "Host: %v\n", r.Host)
fmt.Fprintf(w, "Header: %v\n", r.Header)
fmt.Fprintf(w, "Body: %v\n", r.Body)
fmt.Fprintf(w, "Content Length: %v\n", r.ContentLength)
fmt.Fprintf(w, "Transfer Encoding: %v\n", r.TransferEncoding)
fmt.Fprintf(w, "Form Data: %v\n", r.Form)
fmt.Fprintf(w, "Remote Address: %v\n", r.RemoteAddr)
}
func main() {
hostname, _ := os.Hostname()
name := os.Getenv("NAME")
if name == "" {
name = hostname
}
ports := os.Getenv("PORTS")
if ports == "" {
ports = "8080"
}
portStrings := strings.Split(ports, ",")
for _, port := range portStrings {
log.Printf("Listening on port %v", port)
server := http.Server{
Addr: ":" + port,
Handler: helloWebHandler{port, hostname, name},
ConnContext: func(ctx context.Context, c net.Conn) context.Context {
return context.WithValue(ctx, connContextKey, c)
},
}
go server.ListenAndServe()
}
for {
}
}