-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcontroller.go
380 lines (338 loc) · 11.6 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
// SPDX-FileCopyrightText: 2022-present Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
package controller
import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"strconv"
"strings"
"time"
"github.com/omec-project/metricfunc/config"
"github.com/omec-project/metricfunc/internal/metricdata"
"github.com/omec-project/metricfunc/logger"
"golang.org/x/net/http2"
)
var (
ControllerConfig config.Config
client *http.Client
)
// creating for testing
var RogueChannel chan RogueIPs
type Targets struct {
EnterpriseId string `yaml:"name,omitempty" json:"name,omitempty"`
}
type RogueIPs struct {
IpAddresses []string `yaml:"ipaddresses,omitempty" json:"ipaddresses,omitempty"`
}
type UserAppService struct {
UserAppServiceUrl string `yaml:"userAppServiceUrl,omitempty" json:"userAppServiceUrl,omitempty"`
PollInterval int `yaml:"pollInterval,omitempty" json:"pollInterval,omitempty"`
RogueIPs RogueIPs `yaml:"rogueips,omitempty" json:"rogueips,omitempty"`
}
type RocService struct {
RocServiceUrl string `yaml:"rocServiceUrl,omitempty" json:"rocServiceUrl,omitempty"`
SiteInfo []SiteInfo `yaml:"site-info,omitempty" json:"site-info,omitempty"`
}
type SimCard struct {
SimId string `yaml:"sim-id,omitempty" json:"sim-id,omitempty"`
Imsi string `yaml:"imsi,omitempty" json:"imsi,omitempty"`
DisplayName string `yaml:"display-name,omitempty" json:"display-name,omitempty"`
Enable *bool `yaml:"enable,omitempty" json:"enable,omitempty"`
}
type SiteInfo struct {
SiteId string `yaml:"site-id,omitempty" json:"site-id,omitempty"`
SimCardDetails []SimCard `yaml:"sim-card,omitempty" json:"sim-card,omitempty"`
}
func InitControllerConfig(CConfig *config.Config) error {
ControllerConfig = *CConfig
// Read provided config
logger.ControllerLog.Infoln("controller configuration")
// set http client
if ControllerConfig.Info.HttpVersion == 2 {
client = &http.Client{
Transport: &http2.Transport{
AllowHTTP: true,
DialTLS: func(network, addr string, _ *tls.Config) (net.Conn, error) {
return net.Dial(network, addr)
},
},
Timeout: 5 * time.Second,
}
} else {
client = &http.Client{
Timeout: 5 * time.Second,
}
}
if ControllerConfig.Configuration.UserAppApiServer.PollInterval == 0 {
ControllerConfig.Configuration.UserAppApiServer.PollInterval = 30
}
addr := ControllerConfig.Configuration.UserAppApiServer.Addr
ControllerConfig.Configuration.UserAppApiServer.Addr = strings.TrimSpace(addr)
logger.ControllerLog.Infof(
"api server endpoint: %v:%v",
ControllerConfig.Configuration.UserAppApiServer.Addr,
ControllerConfig.Configuration.UserAppApiServer.Port,
)
logger.ControllerLog.Infoln("pollInterval", ControllerConfig.Configuration.UserAppApiServer.PollInterval)
ControllerConfig.Configuration.RocEndPoint.Addr = strings.TrimSpace(ControllerConfig.Configuration.RocEndPoint.Addr)
logger.ControllerLog.Infof(
"roc endpoint: %v:%v",
ControllerConfig.Configuration.RocEndPoint.Addr,
ControllerConfig.Configuration.RocEndPoint.Port,
)
return nil
}
func getNextBackoffInterval(retry, interval uint) uint {
mFactor := 1.5
nextInterval := float64(retry*interval) * mFactor
if nextInterval > 10 {
return 10
}
return uint(nextInterval)
}
func sendHttpReqMsgWithoutRetry(req *http.Request) (*http.Response, error) {
rsp, err := client.Do(req)
if err != nil {
logger.ControllerLog.Errorf("http req send error [%+v]", err)
return nil, err
}
if rsp.StatusCode == http.StatusAccepted ||
rsp.StatusCode == http.StatusOK || rsp.StatusCode == http.StatusNoContent ||
rsp.StatusCode == http.StatusCreated {
logger.ControllerLog.Infoln("successful response from peer:", http.StatusText(rsp.StatusCode))
return rsp, nil
} else {
logger.ControllerLog.Errorf("http rsp error [%v]", http.StatusText(rsp.StatusCode))
err := rsp.Body.Close()
if err != nil {
logger.ControllerLog.Warnf("body close error: %v", err)
}
return nil, fmt.Errorf("error response: %v", http.StatusText(rsp.StatusCode))
}
}
func sendHttpReqMsg(req *http.Request) (*http.Response, error) {
// Keep sending request to http server until response is success
var retries uint = 0
var body []byte
var err error
if req.Body != nil {
body, err = io.ReadAll(req.Body)
if err != nil {
logger.ControllerLog.Warnf("error reading body: %v", err)
}
}
for {
cloneReq := req.Clone(context.Background())
req.Body = io.NopCloser(bytes.NewReader(body))
cloneReq.Body = io.NopCloser(bytes.NewReader(body))
rsp, err := client.Do(cloneReq)
retries += 1
if err != nil {
nextInterval := getNextBackoffInterval(retries, 2)
logger.ControllerLog.Warnf("http req send error [%+v], retrying after %v sec...", err, nextInterval)
time.Sleep(time.Second * time.Duration(nextInterval))
continue
}
if rsp.StatusCode == http.StatusAccepted ||
rsp.StatusCode == http.StatusOK || rsp.StatusCode == http.StatusNoContent ||
rsp.StatusCode == http.StatusCreated {
logger.ControllerLog.Infoln("get config from peer success")
err := req.Body.Close()
if err != nil {
logger.ControllerLog.Warnf("body close error: %v", err)
}
return rsp, nil
} else {
nextInterval := getNextBackoffInterval(retries, 2)
logger.ControllerLog.Warnf(
"http rsp error [%v], retrying after [%v] sec...",
http.StatusText(rsp.StatusCode),
nextInterval,
)
err := rsp.Body.Close()
if err != nil {
logger.ControllerLog.Warnf("body close error: %v", err)
}
time.Sleep(time.Second * time.Duration(nextInterval))
}
}
}
func validateIPs(ips RogueIPs) (validIps RogueIPs) {
for _, ip := range ips.IpAddresses {
if net.ParseIP(ip) == nil {
logger.ControllerLog.Errorf("userAppApp response received with IP Address: %s - Invalid", ip)
continue
}
validIps.IpAddresses = append(validIps.IpAddresses, ip)
}
logger.ControllerLog.Debugf("rogueIPs [%v] received from userAppApp", validIps.IpAddresses)
return validIps
}
func (userAppClient *UserAppService) GetRogueIPs(rogueIPChannel chan RogueIPs) {
userAppServerApi := userAppClient.UserAppServiceUrl
logger.ControllerLog.Infoln("userAppApp Url:", userAppServerApi)
req, err := http.NewRequest(http.MethodGet, userAppServerApi, nil)
if err != nil {
logger.ControllerLog.Errorln("an error occurred", err)
return
}
for {
rsp, httpErr := sendHttpReqMsg(req)
if httpErr != nil {
logger.ControllerLog.Errorf("get message [%v] returned error [%+v]", userAppServerApi, err)
time.Sleep(10 * time.Second)
continue
}
var rogueIPs RogueIPs
if rsp != nil {
if rsp.Body != nil {
err := json.NewDecoder(rsp.Body).Decode(&rogueIPs)
if err != nil {
logger.ControllerLog.Errorln("userAppApp response body decode failed:", err)
} else {
logger.ControllerLog.Infoln("received rogueIPs from userAppApp:", rogueIPs)
ips := validateIPs(rogueIPs)
if len(ips.IpAddresses) > 0 {
// writing rogueIPs into channel
rogueIPChannel <- ips
}
}
} else {
logger.ControllerLog.Infoln("http response body from userAppApp is empty")
}
}
time.Sleep(time.Duration(userAppClient.PollInterval) * time.Second)
}
}
func (rocClient *RocService) GetTargets() (names []Targets) {
rocTargetsApi := rocClient.RocServiceUrl + "/aether-roc-api/targets"
req, err := http.NewRequest(http.MethodGet, rocTargetsApi, nil)
if err != nil {
logger.ControllerLog.Errorf("get targets request error occurred %v", err)
return
}
rsp, httpErr := sendHttpReqMsgWithoutRetry(req)
if httpErr != nil {
logger.ControllerLog.Errorf("get message [%v] returned error [%v]", rocTargetsApi, httpErr.Error())
return
}
if rsp != nil {
if rsp.Body != nil {
err := json.NewDecoder(rsp.Body).Decode(&names)
if err != nil {
logger.ControllerLog.Errorln("unable to decode Targets:", err)
} else {
logger.ControllerLog.Infoln("get targets received from RoC:", names)
}
} else {
logger.ControllerLog.Errorln("get targets http response body is empty")
}
} else {
logger.ControllerLog.Errorln("get targets http response is empty")
}
return
}
func (rocClient *RocService) DisableSimcard(targets []Targets, imsi string) {
for _, target := range targets {
rocSiteApi := rocClient.RocServiceUrl + "/aether-roc-api/aether/v2.1.x/" + target.EnterpriseId + "/site"
req, err := http.NewRequest(http.MethodGet, rocSiteApi, nil)
if err != nil {
logger.ControllerLog.Errorf("GetSiteInfo request error occurred %v", err)
return
}
rsp, httpErr := sendHttpReqMsgWithoutRetry(req)
if httpErr != nil {
logger.ControllerLog.Errorf("GetSiteInfo message [%v] returned error [%v]", rocSiteApi, httpErr.Error())
continue
}
var siteInfo []SiteInfo
if rsp != nil {
if rsp.Body != nil {
err := json.NewDecoder(rsp.Body).Decode(&siteInfo)
if err != nil {
logger.ControllerLog.Errorln("unable to decode SiteInfo:", err)
} else {
logger.ControllerLog.Infoln("GetSiteInfo received from RoC:", siteInfo)
}
b, err := io.ReadAll(rsp.Body)
if err != nil {
logger.ControllerLog.Warnf("error reading body: %v", err)
}
logger.ControllerLog.Infof("SimDetails received from RoC: %s", string(b))
} else {
logger.ControllerLog.Errorln("GetSiteInfo http response body is empty")
continue
}
} else {
logger.ControllerLog.Errorln("GetSiteInfo http response is empty")
continue
}
var rocDisableSimCard *SimCard
for _, siteInfo := range siteInfo {
for _, simCard := range siteInfo.SimCardDetails {
imsi = strings.TrimPrefix(imsi, "imsi-")
if simCard.Imsi == imsi {
logger.ControllerLog.Infof("SimCard %v details found in site [%v]", imsi, siteInfo.SiteId)
rocDisableSimCard = &simCard
break
}
}
if rocDisableSimCard != nil {
rocDisableImsiApi := rocSiteApi + "/" + siteInfo.SiteId + "/sim-card/" + rocDisableSimCard.SimId
var val bool
rocDisableSimCard.Enable = &val
b, err := json.Marshal(&rocDisableSimCard)
if err != nil {
logger.ControllerLog.Warnf("error marshalling imsi %v: %v", rocDisableSimCard.Imsi, err)
}
reqMsgBody := bytes.NewBuffer(b)
logger.ControllerLog.Debugln("rest API to disable imsi:", rocDisableImsiApi)
logger.ControllerLog.Debugln("post msg body:", reqMsgBody)
req, err := http.NewRequest(http.MethodPost, rocDisableImsiApi, reqMsgBody)
if err != nil {
logger.ControllerLog.Warnf("error with new request: %v", err)
}
req.Header.Set("Content-Type", "application/json; charset=utf-8")
_, httpErr := sendHttpReqMsgWithoutRetry(req)
if httpErr != nil {
logger.ControllerLog.Errorf("post message [%v] returned error [%v]", rocDisableImsiApi, httpErr.Error())
}
return
}
}
}
logger.ControllerLog.Warnf("imsi details not found in Targets and SiteInfo: [%v]", imsi)
}
func RogueIPHandler(rogueIPChannel chan RogueIPs) {
addr := ControllerConfig.Configuration.RocEndPoint.Addr
port := ControllerConfig.Configuration.RocEndPoint.Port
rocClient := RocService{
RocServiceUrl: "http://" + addr + ":" + strconv.Itoa(port),
}
for rogueIPs := range rogueIPChannel {
for _, ipaddr := range rogueIPs.IpAddresses {
// get IP to imsi mapping from metricfunc
subscriberInfo, err := metricdata.GetSubscriberImsiFromIpAddr(ipaddr)
if err != nil {
logger.ControllerLog.Errorln("subscriber details doesn't exist with imsi", err)
continue
}
logger.ControllerLog.Infof("subscriber Imsi [%v] of the IP: [%v]", subscriberInfo.Imsi, ipaddr)
// get enterprises or targets from ROC
targets := rocClient.GetTargets()
if len(targets) == 0 {
logger.ControllerLog.Errorln("get targets returns nil")
} else {
// get siteinfo from ROC
rocClient.DisableSimcard(targets, subscriberInfo.Imsi)
}
}
}
}