Ver código fonte

restapi: Initial commit

tags/v0.5.0
Daniel Oaks 7 anos atrás
pai
commit
ee3853f845
6 arquivos alterados com 110 adições e 0 exclusões
  1. 4
    0
      irc/client_lookup_set.go
  2. 6
    0
      irc/config.go
  3. 14
    0
      irc/dline.go
  4. 70
    0
      irc/rest_api.go
  5. 8
    0
      irc/server.go
  6. 8
    0
      oragono.yaml

+ 4
- 0
irc/client_lookup_set.go Ver arquivo

@@ -43,6 +43,10 @@ func NewClientLookupSet() *ClientLookupSet {
43 43
 	}
44 44
 }
45 45
 
46
+func (clients *ClientLookupSet) Count() int {
47
+	return len(clients.ByNick)
48
+}
49
+
46 50
 func (clients *ClientLookupSet) Has(nick string) bool {
47 51
 	casefoldedName, err := CasefoldName(nick)
48 52
 	if err == nil {

+ 6
- 0
irc/config.go Ver arquivo

@@ -89,6 +89,11 @@ func (conf *OperConfig) PasswordBytes() []byte {
89 89
 	return bytes
90 90
 }
91 91
 
92
+type RestAPIConfig struct {
93
+	Enabled bool
94
+	Listen  string
95
+}
96
+
92 97
 type ConnectionLimitsConfig struct {
93 98
 	CidrLenIPv4 int `yaml:"cidr-len-ipv4"`
94 99
 	CidrLenIPv6 int `yaml:"cidr-len-ipv6"`
@@ -108,6 +113,7 @@ type Config struct {
108 113
 		Listen           []string
109 114
 		Wslisten         string                      `yaml:"ws-listen"`
110 115
 		TLSListeners     map[string]*TLSListenConfig `yaml:"tls-listeners"`
116
+		RestAPI          RestAPIConfig               `yaml:"rest-api"`
111 117
 		CheckIdent       bool                        `yaml:"check-ident"`
112 118
 		Log              string
113 119
 		MOTD             string

+ 14
- 0
irc/dline.go Ver arquivo

@@ -80,6 +80,20 @@ func NewDLineManager() *DLineManager {
80 80
 	return &dm
81 81
 }
82 82
 
83
+// AllBans returns all bans (for use with APIs, etc).
84
+func (dm *DLineManager) AllBans() map[string]IPBanInfo {
85
+	allb := make(map[string]IPBanInfo)
86
+
87
+	for name, info := range dm.addresses {
88
+		allb[name] = info.Info
89
+	}
90
+	for name, info := range dm.networks {
91
+		allb[name] = info.Info
92
+	}
93
+
94
+	return allb
95
+}
96
+
83 97
 // AddNetwork adds a network to the blocked list.
84 98
 func (dm *DLineManager) AddNetwork(network net.IPNet, length *IPRestrictTime, reason string, operReason string) {
85 99
 	netString := network.String()

+ 70
- 0
irc/rest_api.go Ver arquivo

@@ -0,0 +1,70 @@
1
+// Copyright (c) 2016- Daniel Oaks <daniel@danieloaks.net>
2
+// released under the MIT license
3
+
4
+// viewing and modifying accounts, registered channels, dlines, rehashing, etc
5
+
6
+package irc
7
+
8
+import (
9
+	"encoding/json"
10
+	"net/http"
11
+
12
+	"fmt"
13
+
14
+	"github.com/gorilla/mux"
15
+)
16
+
17
+const restErr = "{\"error\":\"An unknown error occurred\"}"
18
+
19
+// restAPIServer is used to keep a link to the current running server since this is the best
20
+// way to do it, given how HTTP handlers dispatch and work.
21
+var restAPIServer *Server
22
+
23
+type restStatusResp struct {
24
+	Clients  int `json:"clients"`
25
+	Opers    int `json:"opers"`
26
+	Channels int `json:"channels"`
27
+}
28
+
29
+type restDLinesResp struct {
30
+	DLines map[string]IPBanInfo `json:"dlines"`
31
+}
32
+
33
+func restStatus(w http.ResponseWriter, r *http.Request) {
34
+	rs := restStatusResp{
35
+		Clients:  restAPIServer.clients.Count(),
36
+		Opers:    len(restAPIServer.operators),
37
+		Channels: len(restAPIServer.channels),
38
+	}
39
+	b, err := json.Marshal(rs)
40
+	if err != nil {
41
+		fmt.Fprintln(w, restErr)
42
+	} else {
43
+		fmt.Fprintln(w, string(b))
44
+	}
45
+}
46
+
47
+func restDLines(w http.ResponseWriter, r *http.Request) {
48
+	rs := restDLinesResp{
49
+		DLines: restAPIServer.dlines.AllBans(),
50
+	}
51
+	b, err := json.Marshal(rs)
52
+	if err != nil {
53
+		fmt.Fprintln(w, restErr)
54
+	} else {
55
+		fmt.Fprintln(w, string(b))
56
+	}
57
+}
58
+
59
+func (s *Server) startRestAPI() {
60
+	// so handlers can ref it later
61
+	restAPIServer = s
62
+
63
+	// start router
64
+	r := mux.NewRouter()
65
+	r.HandleFunc("/status", restStatus)
66
+	r.HandleFunc("/dlines", restDLines)
67
+
68
+	// start api
69
+	go http.ListenAndServe(s.restAPI.Listen, r)
70
+}

+ 8
- 0
irc/server.go Ver arquivo

@@ -100,6 +100,7 @@ type Server struct {
100 100
 	passwords             *PasswordManager
101 101
 	rehashMutex           sync.Mutex
102 102
 	rehashSignal          chan os.Signal
103
+	restAPI               *RestAPIConfig
103 104
 	signals               chan os.Signal
104 105
 	store                 buntdb.DB
105 106
 	whoWas                *WhoWasList
@@ -183,6 +184,7 @@ func NewServer(configFilename string, config *Config) *Server {
183 184
 		operators:      opers,
184 185
 		signals:        make(chan os.Signal, len(ServerExitSignals)),
185 186
 		rehashSignal:   make(chan os.Signal, 1),
187
+		restAPI:        &config.Server.RestAPI,
186 188
 		whoWas:         NewWhoWasList(config.Limits.WhowasEntries),
187 189
 		checkIdent:     config.Server.CheckIdent,
188 190
 	}
@@ -260,6 +262,12 @@ func NewServer(configFilename string, config *Config) *Server {
260 262
 
261 263
 	server.setISupport()
262 264
 
265
+	// start API if enabled
266
+	if server.restAPI.Enabled {
267
+		Log.info.Printf("%s rest API started on %s .", server.name, server.restAPI.Listen)
268
+		server.startRestAPI()
269
+	}
270
+
263 271
 	return server
264 272
 }
265 273
 

+ 8
- 0
oragono.yaml Ver arquivo

@@ -27,6 +27,14 @@ server:
27 27
             key: tls.key
28 28
             cert: tls.crt
29 29
 
30
+    # rest API, for use with web interface
31
+    rest-api:
32
+        # whether the API is enabled or not
33
+        enabled: true
34
+
35
+        # rest API listening port
36
+        listen: "localhost:8090"
37
+
30 38
     # use ident protocol to get usernames
31 39
     check-ident: true
32 40
 

Carregando…
Cancelar
Salvar