-
Notifications
You must be signed in to change notification settings - Fork 31
/
http.go
159 lines (148 loc) · 3.66 KB
/
http.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
// goforever - processes management
// Copyright (c) 2013 Garrett Woodworth (https://github.com/gwoo).
package main
import (
"encoding/base64"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"strings"
)
func HttpServer() {
http.HandleFunc("/favicon.ico", http.NotFound)
http.HandleFunc("/", AuthHandler(Handler))
fmt.Printf("goforever serving port %s\n", config.Port)
fmt.Printf("goforever serving IP %s\n", config.IP)
bindAddress := fmt.Sprintf("%s:%s", config.IP, config.Port)
if isHttps() == false {
if err := http.ListenAndServe(bindAddress, nil); err != nil {
log.Fatal("ListenAndServe: ", err)
}
return
}
log.Printf("SSL enabled.\n")
if err := http.ListenAndServeTLS(bindAddress, "cert.pem", "key.pem", nil); err != nil {
log.Fatal("ListenAndServeTLS: ", err)
}
}
func isHttps() bool {
_, cerr := os.Open("cert.pem")
_, kerr := os.Open("key.pem")
if os.IsNotExist(cerr) || os.IsNotExist(kerr) {
return false
}
return true
}
func Handler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "DELETE":
DeleteHandler(w, r)
return
case "POST":
PostHandler(w, r)
return
case "PUT":
PutHandler(w, r)
return
case "GET":
GetHandler(w, r)
return
}
}
func GetHandler(w http.ResponseWriter, r *http.Request) {
var output []byte
var err error
switch r.URL.Path[1:] {
case "":
output, err = json.Marshal(daemon.children.keys())
default:
output, err = json.Marshal(daemon.children.get(r.URL.Path[1:]))
}
if err != nil {
log.Printf("Get Error: %#v", err)
return
}
fmt.Fprintf(w, "%s", output)
}
func PostHandler(w http.ResponseWriter, r *http.Request) {
name := r.URL.Path[1:]
p := daemon.children.get(name)
if p == nil {
fmt.Fprintf(w, "%s does not exist.", name)
return
}
cp, _, _ := p.find()
if cp != nil {
fmt.Fprintf(w, "%s already running.", name)
return
}
ch := RunProcess(name, p)
fmt.Fprintf(w, "%s", <-ch)
}
func PutHandler(w http.ResponseWriter, r *http.Request) {
name := r.URL.Path[1:]
p := daemon.children.get(name)
if p == nil {
fmt.Fprintf(w, "%s does not exist.", name)
return
}
p.find()
ch, _ := p.restart()
fmt.Fprintf(w, "%s", <-ch)
}
func DeleteHandler(w http.ResponseWriter, r *http.Request) {
name := r.URL.Path[1:]
p := daemon.children.get(name)
if p == nil {
fmt.Fprintf(w, "%s does not exist.", name)
return
}
p.find()
p.stop()
fmt.Fprintf(w, "%s stopped.", name)
}
func AuthHandler(fn func(http.ResponseWriter, *http.Request)) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
url := r.URL
for k, v := range r.Header {
fmt.Printf(" %s = %s\n", k, v[0])
}
auth, ok := r.Header["Authorization"]
if !ok {
log.Printf("Unauthorized access to %s", url)
w.Header().Add("WWW-Authenticate", "basic realm=\"host\"")
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprintf(w, "Not Authorized.")
return
}
encoded := strings.Split(auth[0], " ")
if len(encoded) != 2 || encoded[0] != "Basic" {
log.Printf("Strange Authorization %q", auth)
w.WriteHeader(http.StatusBadRequest)
return
}
decoded, err := base64.StdEncoding.DecodeString(encoded[1])
if err != nil {
log.Printf("Cannot decode %q: %s", auth, err)
w.WriteHeader(http.StatusBadRequest)
return
}
parts := strings.Split(string(decoded), ":")
if len(parts) != 2 {
log.Printf("Unknown format for credentials %q", decoded)
w.WriteHeader(http.StatusBadRequest)
return
}
if parts[0] == config.Username && parts[1] == config.Password {
fn(w, r)
return
}
log.Printf("Unauthorized access to %s", url)
w.Header().Add("WWW-Authenticate", "basic realm=\"host\"")
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprintf(w, "Not Authorized.")
return
}
}