/
controller.go
215 lines (190 loc) · 7.73 KB
/
controller.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Copyright 2020 Nym Technologies SA
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package mixmining
import (
"net/http"
"sync"
"github.com/didip/tollbooth"
"github.com/didip/tollbooth_gin"
"github.com/gin-gonic/gin"
"github.com/nymtech/node-status-api/models"
)
const MaximumMixnodes = 1500
// explicitly declared so that a similar attack could not be used for gateways this time.
const MaximumGateways = 1000
const SystemVersion = "0.9.2"
// Config for this controller
type Config struct {
BatchSanitizer BatchSanitizer // batch mix reports
GenericSanitizer GenericSanitizer // originally introduced for what was in mix registration
Sanitizer Sanitizer // mix reports
Service IService
}
// controller is the mixmining controller
type controller struct {
service IService
sanitizer Sanitizer
genericSanitizer GenericSanitizer
batchSanitizer BatchSanitizer
mixCount int
gatewayCount int
// ensures that if two nodes register at the same time while there's only a single opening left, they won't
// both be accepted.
// note that the lock is not used when creating mix status or unregistering nodes as those can only DECREMENT
// mix count
registrationLock sync.Mutex
}
// Controller ...
type Controller interface {
CreateMixStatus(c *gin.Context)
RegisterRoutes(router *gin.Engine)
}
// New returns a new mixmining.Controller
func New(cfg Config) Controller {
initialMixCount := cfg.Service.MixCount()
initialGatewayCount := cfg.Service.GatewayCount()
// move all non 0.9.2 nodes to "removed" set
cfg.Service.StartupPurge()
return &controller{cfg.Service, cfg.Sanitizer, cfg.GenericSanitizer, cfg.BatchSanitizer, initialMixCount, initialGatewayCount, sync.Mutex{}}
}
func (controller *controller) RegisterRoutes(router *gin.Engine) {
// use that limiter if no other is specified (1 request per second)
lmt := tollbooth_gin.LimitHandler(tollbooth.NewLimiter(1, nil))
router.POST("/api/mixmining", lmt, controller.CreateMixStatus)
router.POST("/api/mixmining/batch", lmt, controller.BatchCreateMixStatus)
router.GET("/api/mixmining/node/:pubkey/history", lmt, controller.ListMeasurements)
router.GET("/api/mixmining/node/:pubkey/report", lmt, controller.GetMixStatusReport)
router.GET("/api/mixmining/fullreport", lmt, controller.BatchGetMixStatusReport)
}
// ListMeasurements lists mixnode statuses
// @Summary Lists mixnode activity
// @Description Lists all mixnode statuses for a given node pubkey
// @ID listMixStatuses
// @Accept json
// @Produce json
// @Tags mixmining
// @Param pubkey path string true "Mixnode Pubkey"
// @Success 200 {array} models.MixStatus
// @Failure 400 {object} models.Error
// @Failure 404 {object} models.Error
// @Failure 500 {object} models.Error
// @Router /api/mixmining/node/{pubkey}/history [get]
func (controller *controller) ListMeasurements(c *gin.Context) {
pubkey := c.Param("pubkey")
measurements := controller.service.ListMixStatus(pubkey)
c.JSON(http.StatusOK, measurements)
}
// CreateMixStatus ...
// @Summary Lets the network monitor create a new uptime status for a mix
// @Description Nym network monitor sends packets through the system and checks if they make it. The network monitor then hits this method to report whether the node was up at a given time.
// @ID addMixStatus
// @Accept json
// @Produce json
// @Tags mixmining
// @Param object body models.MixStatus true "object"
// @Success 201
// @Failure 400 {object} models.Error
// @Failure 403 {object} models.Error
// @Failure 404 {object} models.Error
// @Failure 500 {object} models.Error
// @Router /api/mixmining [post]
func (controller *controller) CreateMixStatus(c *gin.Context) {
remoteIP := c.ClientIP()
if !(remoteIP == "127.0.0.1" || remoteIP == "::1" || c.Request.RemoteAddr == "127.0.0.1" || c.Request.RemoteAddr == "::1") {
c.JSON(http.StatusForbidden, gin.H{"error": "forbidden"})
return
}
var status models.MixStatus
if err := c.ShouldBindJSON(&status); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
sanitized := controller.sanitizer.Sanitize(status)
persisted := controller.service.CreateMixStatus(sanitized)
controller.service.SaveStatusReport(persisted)
// we don't know how number of active nodes changed - update it
controller.mixCount = controller.service.MixCount()
controller.gatewayCount = controller.service.GatewayCount()
c.JSON(http.StatusCreated, gin.H{"ok": true})
}
// GetMixStatusReport ...
// @Summary Retrieves a summary report of historical mix status
// @Description Provides summary uptime statistics for last 5 minutes, day, week, and month
// @ID getMixStatusReport
// @Accept json
// @Produce json
// @Tags mixmining
// @Param pubkey path string true "Mixnode Pubkey"
// @Success 200
// @Failure 400 {object} models.Error
// @Failure 404 {object} models.Error
// @Failure 500 {object} models.Error
// @Router /api/mixmining/node/{pubkey}/report [get]
func (controller *controller) GetMixStatusReport(c *gin.Context) {
pubkey := c.Param("pubkey")
report := controller.service.GetStatusReport(pubkey)
if (report == models.MixStatusReport{}) {
c.JSON(http.StatusNotFound, gin.H{"error": "not found"})
}
c.JSON(http.StatusOK, report)
}
// BatchCreateMixStatus ...
// @Summary Lets the network monitor create a new uptime status for multiple mixes
// @Description Nym network monitor sends packets through the system and checks if they make it. The network monitor then hits this method to report whether nodes were up at a given time.
// @ID batchCreateMixStatus
// @Accept json
// @Produce json
// @Tags mixmining
// @Param object body models.BatchMixStatus true "object"
// @Success 201
// @Failure 400 {object} models.Error
// @Failure 403 {object} models.Error
// @Failure 404 {object} models.Error
// @Failure 500 {object} models.Error
// @Router /api/mixmining/batch [post]
func (controller *controller) BatchCreateMixStatus(c *gin.Context) {
remoteIP := c.ClientIP()
if !(remoteIP == "127.0.0.1" || remoteIP == "::1" || c.Request.RemoteAddr == "127.0.0.1" || c.Request.RemoteAddr == "::1") {
c.JSON(http.StatusForbidden, gin.H{"error": "forbidden"})
return
}
var status models.BatchMixStatus
if err := c.ShouldBindJSON(&status); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
sanitized := controller.batchSanitizer.Sanitize(status)
persisted := controller.service.BatchCreateMixStatus(sanitized)
controller.service.SaveBatchStatusReport(persisted)
// we don't know how number of active nodes changed - update it
controller.mixCount = controller.service.MixCount()
controller.gatewayCount = controller.service.GatewayCount()
c.JSON(http.StatusCreated, gin.H{"ok": true})
}
// BatchGetMixStatusReport ...
// @Summary Retrieves a summary report of historical mix status
// @Description Provides summary uptime statistics for last 5 minutes, day, week, and month
// @ID batchGetMixStatusReport
// @Accept json
// @Produce json
// @Tags mixmining
// @Success 200
// @Failure 400 {object} models.Error
// @Failure 404 {object} models.Error
// @Failure 500 {object} models.Error
// @Router /api/mixmining/fullreport [get]
func (controller *controller) BatchGetMixStatusReport(c *gin.Context) {
report := controller.service.BatchGetMixStatusReport()
c.JSON(http.StatusOK, report)
}