-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
48 lines (42 loc) · 1.15 KB
/
server.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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"strconv"
"time"
)
func main() {
http.HandleFunc("/ncsi.txt", func(w http.ResponseWriter, r *http.Request) {
trace(w, r)
fmt.Fprintf(w, "Microsoft NCSI")
})
http.HandleFunc("/connecttest.txt", func(w http.ResponseWriter, r *http.Request) {
trace(w, r)
fmt.Fprintf(w, "Microsoft Connect Test")
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
trace(w, r)
fmt.Fprintf(w, "<HTML><HEAD><TITLE>Success</TITLE></HEAD><BODY>Success</BODY></HTML>")
})
listen := os.Getenv("CAPTIVE_LISTEN")
if listen == "" {
listen = ":8080"
}
log.Println("Listening on", listen)
log.Fatal(http.ListenAndServe(listen, nil))
}
func trace(w http.ResponseWriter, r *http.Request) {
requestID := strconv.FormatInt(time.Now().UnixNano(), 36)
log.Printf("[%s] %s %s", requestID, r.Method, r.RequestURI)
log.Printf("[%s] From %s", requestID, r.RemoteAddr)
log.Printf("[%s] Content-Length %d", requestID, r.ContentLength)
headers, err := json.Marshal(r.Header)
if err != nil {
log.Printf("[%s] %v", requestID, r.Header)
} else {
log.Printf("[%s] %s", requestID, headers)
}
}