-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
141 lines (127 loc) · 4.63 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
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
package web
import (
"context"
"fmt"
"net/http"
"os/exec"
"time"
"github.com/cbroglie/mustache"
"github.com/chtjonas/go-lg/internal/assets"
"github.com/chtjonas/go-lg/internal/storage"
"github.com/gorilla/mux"
)
type Server struct {
r *mux.Router
s *storage.Store
srv *http.Server
ver string
}
func NewServer(store *storage.Store, ver string) *Server {
s := &Server{
s: store,
ver: ver,
}
r := mux.NewRouter().StrictSlash(true)
r.HandleFunc("/", s.getHomePage)
r.HandleFunc("/ping", s.getPingForm)
r.HandleFunc("/ping/action", s.submitPingForm)
r.HandleFunc("/ping/{uid}", s.getPingResults)
r.HandleFunc("/traceroute", s.getTracerouteForm)
r.HandleFunc("/traceroute/action", s.submitTracerouteForm)
r.HandleFunc("/traceroute/{uid}", s.getTracerouteResults)
s.r = r
return s
}
func (serv *Server) Start(addr string) error {
serv.srv = &http.Server{
Addr: addr,
Handler: serv.r,
WriteTimeout: time.Second * 60,
ReadTimeout: time.Second * 60,
IdleTimeout: time.Second * 90,
}
return serv.srv.ListenAndServe()
}
func (serv *Server) Stop(ctx context.Context) error {
return serv.srv.Shutdown(ctx)
}
func (serv *Server) getHomePage(w http.ResponseWriter, r *http.Request) {
partial, _ := assets.Asset("assets/home.html.mustache")
layout, _ := assets.Asset("assets/layout.html.mustache")
context := map[string]string{"title": "Home Page", "version": serv.ver}
str, _ := mustache.RenderInLayout(string(partial), string(layout), context)
fmt.Fprint(w, str)
}
func (serv *Server) getPingForm(w http.ResponseWriter, r *http.Request) {
partial, _ := assets.Asset("assets/form.html.mustache")
layout, _ := assets.Asset("assets/layout.html.mustache")
context := map[string]string{"title": "Ping Report", "submissionURL": "/ping/action"}
str, _ := mustache.RenderInLayout(string(partial), string(layout), context)
fmt.Fprint(w, str)
}
func (serv *Server) submitPingForm(w http.ResponseWriter, r *http.Request) {
target := r.URL.Query().Get("target")
protocolVersion := r.URL.Query().Get("protocolVersion")
var cmd *exec.Cmd
if protocolVersion == "4" {
cmd = exec.Command("ping", "-4", "-c", "4", target)
} else if protocolVersion == "6" {
cmd = exec.Command("ping", "-6", "-c", "4", target)
} else {
cmd = exec.Command("ping", "-c", "4", target)
}
stdout, _ := cmd.Output()
uid, _ := serv.s.Write("ping", stdout)
redirect("ping", uid, w, r)
}
func (serv *Server) getPingResults(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
stdout := serv.s.Read("ping", vars["uid"])
if len(stdout) == 0 {
stdout = []byte("HTTP 404 Report Not Found")
w.WriteHeader(http.StatusNotFound)
}
partial, _ := assets.Asset("assets/form.html.mustache")
layout, _ := assets.Asset("assets/layout.html.mustache")
context := map[string]string{"title": "Ping Report", "code": string(stdout), "submissionURL": "/ping/action"}
str, _ := mustache.RenderInLayout(string(partial), string(layout), context)
fmt.Fprint(w, str)
}
func (serv *Server) getTracerouteForm(w http.ResponseWriter, r *http.Request) {
partial, _ := assets.Asset("assets/form.html.mustache")
layout, _ := assets.Asset("assets/layout.html.mustache")
context := map[string]string{"title": "Traceroute Report", "submissionURL": "/traceroute/action"}
str, _ := mustache.RenderInLayout(string(partial), string(layout), context)
fmt.Fprint(w, str)
}
func (serv *Server) submitTracerouteForm(w http.ResponseWriter, r *http.Request) {
target := r.URL.Query().Get("target")
protocolVersion := r.URL.Query().Get("protocolVersion")
var cmd *exec.Cmd
if protocolVersion == "4" {
cmd = exec.Command("mtr", "-4", "-c", "4", "--report-wide", target)
} else if protocolVersion == "6" {
cmd = exec.Command("mtr", "-6", "-c", "4", "--report-wide", target)
} else {
cmd = exec.Command("mtr", "-c", "4", "--report-wide", target)
}
stdout, _ := cmd.Output()
uid, _ := serv.s.Write("traceroute", stdout)
redirect("traceroute", uid, w, r)
}
func (serv *Server) getTracerouteResults(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
stdout := serv.s.Read("traceroute", vars["uid"])
if len(stdout) == 0 {
stdout = []byte("HTTP 404 Report Not Found")
w.WriteHeader(http.StatusNotFound)
}
partial, _ := assets.Asset("assets/form.html.mustache")
layout, _ := assets.Asset("assets/layout.html.mustache")
context := map[string]string{"title": "Traceroute Report", "code": string(stdout), "submissionURL": "/traceroute/action"}
str, _ := mustache.RenderInLayout(string(partial), string(layout), context)
fmt.Fprint(w, str)
}
func redirect(base string, uid string, w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/"+base+"/"+uid, http.StatusTemporaryRedirect)
}