-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
214 lines (187 loc) · 4.96 KB
/
Copy pathmain.go
File metadata and controls
214 lines (187 loc) · 4.96 KB
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
package main
import (
"encoding/json"
"log"
"net/http"
"os"
"os/exec"
"time"
)
type TConfiguration struct {
NetflowAdress string
NetflowBufferSize int
WebPort string
CmdDown string
CmdUp string
}
type TIpTraffic uint64
type TipRecord struct {
Traffic TIpTraffic
Limit TIpTraffic
InFullSpeed bool
OffCountStart int
OffCountStop int
SpeedUp string
SpeedDown string
}
type TAddTraffic struct {
Id uint32
Traffic TIpTraffic
}
type setLimit struct {
Id uint32
Value TipRecord
}
type getRequest struct {
Id uint32
Response chan getResponse
}
type getResponse struct {
Found bool
IpRec TipRecord
}
var (
puts chan TAddTraffic
gets chan getRequest
sets chan setLimit
maxCheckIP uint32
minCheckIP uint32
)
func AddLimitToIp(ip uint32, value TIpTraffic) {
response := GetIpInfo(ip)
response.IpRec.Limit += value
sets <- setLimit{
Id: ip,
Value: response.IpRec,
}
}
func SetLimitToIp(ip uint32, value TipRecord) {
sets <- setLimit{
Id: ip,
Value: value,
}
}
func AddTraffic(id uint32, value TIpTraffic) {
puts <- TAddTraffic{
Id: id,
Traffic: value,
}
}
func GetIpInfo(id uint32) getResponse {
getResponses := make(chan getResponse)
gets <- getRequest{
Id: id,
Response: getResponses,
}
response := <-getResponses
return response
}
func RunCMD(Id uint32, ipRec TipRecord, cmd string, speed string) {
// Тут блокируем IP
ips := intToStrIP(Id)
exec.Command(cmd, ips, speed).Run()
log.Printf("ip:%s\n%v\ncmd: %s %s %s\n", ips, ipRec, cmd, ips, speed)
}
func ChanProcess(sctrldCfg TConfiguration) {
var cur_hour int
var old_limit TIpTraffic
storage := map[uint32]TipRecord{}
for {
select {
case put := <-puts:
value, found := storage[put.Id]
if found {
cur_hour = time.Now().Hour()
if !((cur_hour >= value.OffCountStart) && (cur_hour < value.OffCountStop)) {
old_limit = value.Traffic
value.Traffic += put.Traffic
if (value.Traffic > value.Limit) && ((old_limit < value.Limit) || (value.InFullSpeed)) {
value.InFullSpeed = false
go RunCMD(put.Id, value, sctrldCfg.CmdDown, value.SpeedDown)
}
storage[put.Id] = value
} else {
if !(value.InFullSpeed) {
value.InFullSpeed = true
storage[put.Id] = value
go RunCMD(put.Id, value, sctrldCfg.CmdUp, value.SpeedUp)
}
}
}
case get := <-gets:
value, found := storage[get.Id]
get.Response <- getResponse{
IpRec: value,
Found: found,
}
case set := <-sets:
if set.Id > maxCheckIP {
maxCheckIP = set.Id
}
if set.Id < minCheckIP {
minCheckIP = set.Id
}
value, _ := storage[set.Id]
value.Limit = set.Value.Limit
value.OffCountStart = set.Value.OffCountStart
value.OffCountStop = set.Value.OffCountStop
value.SpeedUp = set.Value.SpeedUp
value.SpeedDown = set.Value.SpeedDown
storage[set.Id] = value
if value.Limit > value.Traffic {
go RunCMD(set.Id, value, sctrldCfg.CmdUp, value.SpeedUp)
}
}
}
}
func main() {
var sctrldCfg TConfiguration
// Do any necessary teardown.
// чтоб не проверять все ip установим границы
maxCheckIP = 0
minCheckIP = 4294967295
file, _ := os.Open("sctrld.config")
decoder := json.NewDecoder(file)
err := decoder.Decode(&sctrldCfg)
if err != nil {
sctrldCfg.NetflowAdress = "0.0.0.0:2055"
sctrldCfg.WebPort = "8080"
sctrldCfg.NetflowBufferSize = 212992
}
file.Close()
log.Printf("Start netflow listening on %v\n", sctrldCfg.NetflowAdress)
go ListenNetflow(sctrldCfg.NetflowAdress, sctrldCfg.NetflowBufferSize)
puts = make(chan TAddTraffic)
gets = make(chan getRequest)
sets = make(chan setLimit)
http.HandleFunc("/runtime/", httpGetRuntime) // /v1/get/?ip=127.0.0.1
http.HandleFunc("/v1/get/", httpGetStat) // /v1/get/?ip=127.0.0.1
http.HandleFunc("/v1/add/", httpAddLimit) // /v1/add/?ip=127.0.0.1&limit=100
http.HandleFunc("/v1/set/", httpSetLimit) // /v1/set/?ip=127.0.0.1&limit=200
log.Printf("Start web listening on %v\n", sctrldCfg.WebPort)
go http.ListenAndServe(":"+sctrldCfg.WebPort, nil)
//go AddLimitToIp(2130706433, 100) // 127.0.0.1
//storage[2130706433] = TipRecord{0, 100}
/*
создаем 10 канадов для работы со своей зоной IP
и каждую зону передавать по своим каналам
а также для каждой зоны будет свой map
что ускорит обработку и запаралелит еще больше
проверку IP нужно сделать через AND 53EF FFFF и SHR 16
53EF FFFF = not AC100000
172.16.0.0 AC100000 0
172.17.0.0 AC110000 1
172.18.0.0 AC120000 2
172.19.0.0 AC130000 3
172.20.0.0 AC140000 4
172.21.0.0 AC150000 5
172.22.0.0 AC160000 6
172.23.0.0 AC170000 7
172.24.0.0 AC180000 8
172.25.0.0 AC190000 9
172.26.0.0 AC1A0000 10
172.27.0.0 AC1B0000 11
172.28.0.0 AC1C0000 12
*/
ChanProcess(sctrldCfg)
}