forked from drtoful/certinel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
176 lines (149 loc) · 3.86 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package certinel
import (
_ "embed"
"encoding/json"
"errors"
"fmt"
"net/http"
"github.com/codegangsta/negroni"
"github.com/gorilla/mux"
)
var (
ErrInvalidArgument = errors.New("invalid argument")
IndexHtml string
)
func parseDomain(r *http.Request) (*Domain, error) {
values := r.URL.Query()
d, ok := values["domain"]
if !ok || len(d) == 0 || len(d[0]) == 0 {
return nil, ErrInvalidArgument
}
p, ok := values["port"]
if !ok || len(p) == 0 || len(p[0]) == 0 {
return nil, ErrInvalidArgument
}
domain := &Domain{Domain: d[0], Port: p[0]}
return domain, nil
}
func addDomain(w http.ResponseWriter, r *http.Request) {
var req Domain
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
if len(req.Domain) == 0 || len(req.Port) == 0 {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("missing data"))
return
}
// start new domain checker
go CheckDomain(req.Domain, req.Port)
w.WriteHeader(http.StatusOK)
}
func deleteDomain(w http.ResponseWriter, r *http.Request) {
domain, err := parseDomain(r)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
if err := domain.Delete(); err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.WriteHeader(http.StatusOK)
}
func getDomains(w http.ResponseWriter, r *http.Request) {
domains := GetDomains()
type _domain struct {
Domain string `json:"domain"`
Port string `json:"port"`
Status *Status `json:"status"`
}
type _result struct {
Valid int `json:"valid"`
Invalid int `json:"invalid"`
Domains []*_domain `json:"domains"`
}
result := make([]*_domain, 0)
valid := 0
invalid := 0
for _, d := range domains {
status, err := d.Status()
if err != nil {
continue
}
result = append(result, &_domain{
Domain: d.Domain,
Port: d.Port,
Status: status,
})
if status.Valid {
valid += 1
} else {
invalid += 1
}
}
data, err := json.Marshal(_result{
Valid: valid,
Invalid: invalid,
Domains: result,
})
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
w.Write(data)
}
func getDomainCerts(w http.ResponseWriter, r *http.Request) {
domain, err := parseDomain(r)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
current, history, err := domain.CertList()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
type _json struct {
Current *Certificate `json:"current"`
History []*Certificate `json:"history,omitempty"`
}
value := &_json{Current: current, History: history[1:]}
data, err := json.Marshal(value)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
w.Write(data)
}
func getIndex(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, IndexHtml)
}
func StartAPIServer(port, indexHtml string) {
IndexHtml = indexHtml // Copy the indexHtml from main to the package global here
router := mux.NewRouter()
api := router.PathPrefix("/api").Subrouter()
api = api.StrictSlash(true)
api.Path("/domains").Methods("PUT").HandlerFunc(addDomain)
api.Path("/domains").Methods("GET").HandlerFunc(getDomains)
api.Path("/domains").Methods("DELETE").HandlerFunc(deleteDomain)
api.Path("/certs").Methods("GET").HandlerFunc(getDomainCerts)
router.Path("/").Methods("GET").HandlerFunc(getIndex)
n := negroni.New(negroni.NewRecovery())
n.UseHandler(router)
n.Run(":" + port)
}