-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest_api.go
189 lines (156 loc) · 4.15 KB
/
rest_api.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
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright (c) 2016-2017 Daniel Oaks <daniel@danieloaks.net>
// released under the MIT license
// viewing and modifying accounts, registered channels, dlines, rehashing, etc
package irc
import (
"encoding/json"
"net/http"
"strconv"
"time"
"fmt"
"github.com/gorilla/mux"
"github.com/tidwall/buntdb"
)
const restErr = "{\"error\":\"An unknown error occurred\"}"
// restAPIServer is used to keep a link to the current running server since this is the best
// way to do it, given how HTTP handlers dispatch and work.
var restAPIServer *Server
type restInfoResp struct {
ServerName string `json:"server-name"`
NetworkName string `json:"network-name"`
Version string `json:"version"`
}
type restStatusResp struct {
Clients int `json:"clients"`
Opers int `json:"opers"`
Channels int `json:"channels"`
}
type restXLinesResp struct {
DLines map[string]IPBanInfo `json:"dlines"`
KLines map[string]IPBanInfo `json:"klines"`
}
type restAcct struct {
Name string `json:"name"`
RegisteredAt time.Time `json:"registered-at"`
Clients int `json:"clients"`
}
type restAccountsResp struct {
Verified map[string]restAcct `json:"verified"`
}
type restRehashResp struct {
Successful bool `json:"successful"`
Error string `json:"error"`
Time time.Time `json:"time"`
}
func restInfo(w http.ResponseWriter, r *http.Request) {
rs := restInfoResp{
Version: SemVer,
ServerName: restAPIServer.name,
NetworkName: restAPIServer.networkName,
}
b, err := json.Marshal(rs)
if err != nil {
fmt.Fprintln(w, restErr)
} else {
fmt.Fprintln(w, string(b))
}
}
func restStatus(w http.ResponseWriter, r *http.Request) {
rs := restStatusResp{
Clients: restAPIServer.clients.Count(),
Opers: len(restAPIServer.operators),
Channels: restAPIServer.channels.Len(),
}
b, err := json.Marshal(rs)
if err != nil {
fmt.Fprintln(w, restErr)
} else {
fmt.Fprintln(w, string(b))
}
}
func restGetXLines(w http.ResponseWriter, r *http.Request) {
rs := restXLinesResp{
DLines: restAPIServer.dlines.AllBans(),
KLines: restAPIServer.klines.AllBans(),
}
b, err := json.Marshal(rs)
if err != nil {
fmt.Fprintln(w, restErr)
} else {
fmt.Fprintln(w, string(b))
}
}
func restGetAccounts(w http.ResponseWriter, r *http.Request) {
rs := restAccountsResp{
Verified: make(map[string]restAcct),
}
// get accounts
err := restAPIServer.store.View(func(tx *buntdb.Tx) error {
tx.AscendKeys("account.exists *", func(key, value string) bool {
key = key[len("account.exists "):]
_, err := tx.Get(fmt.Sprintf(keyAccountVerified, key))
verified := err == nil
fmt.Println(fmt.Sprintf(keyAccountVerified, key))
// get other details
name, _ := tx.Get(fmt.Sprintf(keyAccountName, key))
regTimeStr, _ := tx.Get(fmt.Sprintf(keyAccountRegTime, key))
regTimeInt, _ := strconv.ParseInt(regTimeStr, 10, 64)
regTime := time.Unix(regTimeInt, 0)
var clients int
acct := restAPIServer.accounts[key]
if acct != nil {
clients = len(acct.Clients)
}
if verified {
rs.Verified[key] = restAcct{
Name: name,
RegisteredAt: regTime,
Clients: clients,
}
} else {
//TODO(dan): Add to unverified list
}
return true // true to continue I guess?
})
return nil
})
b, err := json.Marshal(rs)
if err != nil {
fmt.Fprintln(w, restErr)
} else {
fmt.Fprintln(w, string(b))
}
}
func restRehash(w http.ResponseWriter, r *http.Request) {
err := restAPIServer.rehash()
rs := restRehashResp{
Successful: err == nil,
Time: time.Now(),
}
if err != nil {
rs.Error = err.Error()
}
b, err := json.Marshal(rs)
if err != nil {
fmt.Fprintln(w, restErr)
} else {
fmt.Fprintln(w, string(b))
}
}
func (s *Server) startRestAPI() {
// so handlers can ref it later
restAPIServer = s
// start router
r := mux.NewRouter()
// GET methods
rg := r.Methods("GET").Subrouter()
rg.HandleFunc("/info", restInfo)
rg.HandleFunc("/status", restStatus)
rg.HandleFunc("/xlines", restGetXLines)
rg.HandleFunc("/accounts", restGetAccounts)
// PUT methods
rp := r.Methods("POST").Subrouter()
rp.HandleFunc("/rehash", restRehash)
// start api
go http.ListenAndServe(s.restAPI.Listen, r)
}