-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
374 lines (316 loc) · 9.11 KB
/
main.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
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"os/exec"
"os/signal"
"path/filepath"
"runtime"
"strconv"
"syscall"
"time"
"github.com/Jeffail/tunny"
"github.com/fastscanner/common"
"github.com/fastscanner/scanner"
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
log "github.com/sirupsen/logrus"
"github.com/valyala/fasthttp"
)
var (
isdev bool
version bool
logFile string
confFile string
addr string
unix string
pidFile string = "fastscanner.pid"
)
func initSetLimit(cpu_max uint64, core_max uint64) error {
/*
cpuNum := runtime.NumCPU()
runtime.GOMAXPROCS(runtime.NumCPU() -1 )
*/
var rlimit syscall.Rlimit
// 限制cpu个数, 程序使用超过限制, 可能会导致程序被kill
/*
rlimit.Cur = 1
rlimit.Max = cpu_max
syscall.Setrlimit(syscall.RLIMIT_CPU, &rlimit)
err := syscall.Getrlimit(syscall.RLIMIT_CPU, &rlimit)
if err != nil {
return err
}
*/
//set core limit
rlimit.Cur = 100 //以字节为单位
rlimit.Max = rlimit.Cur + core_max
if err := syscall.Setrlimit(syscall.RLIMIT_CORE, &rlimit); err != nil {
return err
}
if err := syscall.Getrlimit(syscall.RLIMIT_CORE, &rlimit); err != nil {
return err
}
//set nofile rlimit
var rLimit syscall.Rlimit
rLimit.Cur = 65535
rLimit.Max = 65535
err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit)
if err != nil {
log.Fatal("err:", err.Error())
}
//set procs && cpu nums use; If n < 1, it does not change the current setting
runtime.GOMAXPROCS(int(fastScanner.conf.CPUNum))
return nil
}
func init() {
//parse flag
flag.BoolVar(&isdev, "d", false, "run in dev mode")
flag.BoolVar(&version, "version", false, "output version info")
flag.StringVar(&logFile, "log", "./logs/fastscanner.log", "error.log")
flag.StringVar(&confFile, "conf", "./conf/config.json", "config file")
flag.StringVar(&addr, "addr", ":9999", "TCP address to listen to")
flag.StringVar(&unix, "unix", "/tmp/fasthttp_hyperscan.sock", "Unix domain to listen to")
flag.Parse()
//log init
log.SetFormatter(&log.TextFormatter{
DisableColors: true,
TimestampFormat: "1970-00-00 00:00:00",
})
// Start flag: -d
if isdev {
log.SetOutput(os.Stdout)
log.SetLevel(log.DebugLevel)
} else {
logPath := logFile
rtt_writer, _ := rotatelogs.New(
logPath+".%Y%m%d%H%M",
rotatelogs.WithLinkName(logFile),
rotatelogs.WithMaxAge(72*time.Hour),
rotatelogs.WithRotationTime(24*time.Hour),
)
log.SetFormatter(&log.TextFormatter{
DisableColors: true,
FullTimestamp: true,
})
log.SetOutput(rtt_writer)
log.SetLevel(log.InfoLevel)
}
}
//run in fasthttp goroutine
func request_handler(ctx *fasthttp.RequestCtx) {
/*
fmt.Fprintf(ctx, "Hello, world!\n\n")
fmt.Fprintf(ctx, "Request method is %q\n", ctx.Method())
fmt.Fprintf(ctx, "RequestURI is %q\n", ctx.RequestURI())
fmt.Fprintf(ctx, "Requested path is %q\n", ctx.Path())
fmt.Fprintf(ctx, "Host is %q\n", ctx.Host())
fmt.Fprintf(ctx, "Query string is %q\n", ctx.QueryArgs())
fmt.Fprintf(ctx, "User-Agent is %q\n", ctx.UserAgent())
fmt.Fprintf(ctx, "Connection has been established at %s\n", ctx.ConnTime())
fmt.Fprintf(ctx, "Request has been started at %s\n", ctx.Time())
fmt.Fprintf(ctx, "Serial request number for the current connection is %d\n", ctx.ConnRequestNum())
fmt.Fprintf(ctx, "Your ip is %q\n\n", ctx.RemoteIP())
fmt.Fprintf(ctx, "Raw request is:\n---CUT---\n%s\n---CUT---", &ctx.Request)
*/
request_id := strconv.FormatUint(ctx.ID(), 10)
distCtx := scanner.DistWorkerContext{DistWorker: distWorker}
distCtx.Data = make(map[string][]byte)
if len(ctx.Method()) > 0 {
distCtx.Data["request_method"] = ctx.Method()
}
//For ngx mirror
//X-Real-IP
x_real_ip := ctx.Request.Header.Peek("X-Real-IP")
if len(x_real_ip) > 0 {
distCtx.Data["x_real_ip"] = x_real_ip
}
//Host
host := ctx.Request.Header.Peek("Host")
if len(host) > 0 {
distCtx.Data["host"] = x_real_ip
}
//X-Original-URI
request_uri := ctx.Request.Header.Peek("X-Original-URI")
if len(request_uri) > 0 {
distCtx.Data["request_uri"] = request_uri
}
/*
if len(ctx.RequestURI()) > 0 {
distCtx.Data["request_uri"] = ctx.RequestURI()
}
*/
if len(ctx.Referer()) > 0 {
distCtx.Data["http_referer"] = ctx.Referer()
}
if len(ctx.UserAgent()) > 0 {
distCtx.Data["http_user_agent"] = ctx.UserAgent()
}
if len(ctx.PostBody()) > 0 {
distCtx.Data["request_body"] = ctx.PostBody()
}
//性能优化:
// + 不走匹配流程: Requests/sec: 73028.06
// + 走匹配流量: Requests/sec: 23948.57
// + 走匹配不打日志: Requests/sec: 32835.78
res, err := distWorker.Pool.ProcessTimed(&distCtx, time.Second*5)
if err == tunny.ErrJobTimedOut {
log.WithFields(log.Fields{"hsCtx": distCtx, "rerr": err.Error()}).Error("Error: Request timed out!")
return
}
scanner.HSContextsShow(res.([]scanner.HSContext))
var hitRes []scanner.HSContext
for _, r := range res.([]scanner.HSContext) {
if len(r.Results) == 0 {
continue
}
hitRes = append(hitRes, r)
}
var hitResJson []byte
if len(hitRes) > 0 {
hitResJson, err = json.Marshal(hitRes)
if err != nil {
log.WithFields(log.Fields{"waf-request-id": request_id, "Error": err.Error()}).Error("Error:")
}
}
status_code := 200
ctx.Response.Header.Set("waf-request-id", request_id)
if len(hitRes) > 0 {
status_code = 403
ctx.Response.Header.Set("anti-type", "secrule")
ctx.Response.SetStatusCode(status_code)
if isdev {
if len(hitResJson) > 0 {
ctx.Response.Header.Set("waf-hit-rules", string(hitResJson))
}
}
}
tmpStr := fmt.Sprintf("waf-request-id:%s status:%d", request_id, status_code)
if len(hitResJson) > 0 {
tmpStr = tmpStr + " waf-hit-rules:" + string(hitResJson)
}
log.Info(tmpStr)
//Set Waf-Header
ctx.SetContentType("text/plain; charset=utf8")
/*
// Set cookies
var c fasthttp.Cookie
c.SetKey("cookie-name")
c.SetValue("cookie-value")
ctx.Response.Header.SetCookie(&c)
*/
}
type Conf struct {
//Debug bool `json:"debug"`
Version string `json:"version"`
LogLevel int `json:"loglevel"`
CPUNum int `json:"cpunum"`
ScannerNum int `json:"scannernum"`
//ProcNum int `json:"procnum"`
}
type FastScanner struct {
confFile string
conf Conf
}
func ServeStart(mctx *context.Context) {
//start server
go func() {
if len(unix) > 0 {
if err := fasthttp.ListenAndServeUNIX(unix, 0666, request_handler); err != nil {
log.WithField("err", err.Error()).Fatal("Error: fasthttp.ListenAndServeUNIX")
}
log.Info("Start server done! Listen on:", unix)
} else {
if err := fasthttp.ListenAndServe(addr, request_handler); err != nil {
log.WithField("err", err.Error()).Fatal("Error: fasthttp.ListenAndServe")
}
}
}()
//exit before main exit
for {
select {
case <-(*mctx).Done():
log.Debug("Recv mctx Done...")
default:
time.Sleep(time.Second)
}
}
}
var fastScanner FastScanner
var distWorker *scanner.DistWorker
func main() {
//init cpu && core limit
if err := initSetLimit(2, 2*1024*1024); err != nil {
log.Error("Error:", err.Error())
}
//Start Daemon
if !isdev {
//Daemon
//判断当其是否是子进程,当父进程return之后,子进程会被 系统1 号进程接管
if os.Getppid() != 1 {
// 将命令行参数中执行文件路径转换成可用路径
filePath, _ := filepath.Abs(os.Args[0])
cmd := exec.Command(filePath, os.Args[1:]...)
// 将其他命令传入生成出的进程
cmd.Stdin = os.Stdin // 给新进程设置文件描述符,可以重定向到文件中
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Start() // 开始执行新进程,不等待新进程退出
return
}
}
//Check PID File
exDir, err := os.Executable()
if err != nil {
log.Fatal("Error:", err.Error())
}
pathDir := filepath.Dir(exDir)
once_pid, started := common.PidfileExit(pathDir + "/" + pidFile)
if started {
if once_pid > 0 {
log.Error("exec already exist:", once_pid)
return
}
} else {
if once_pid > 0 {
log.Info("start new proc:%d", once_pid)
//HandleSingle() //信号处理
defer os.Remove(pidFile) //程序退出后删除pid文件
}
}
//Start process
log.Info("Starting ... pid:", once_pid)
//init ctx && signal
mctx, cancel := context.WithCancel(context.Background())
sigCh := make(chan os.Signal)
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
//read Main Conf
confData, err := ioutil.ReadFile(confFile)
if err != nil {
log.Fatal("Read conf error!")
}
if err := json.Unmarshal(confData, &fastScanner.conf); err != nil {
log.Fatal("Parse main conf error!")
}
if !isdev && fastScanner.conf.LogLevel > 0 {
log.SetLevel(log.Level(fastScanner.conf.LogLevel))
log.Info("Reset loglevel to:", fastScanner.conf.LogLevel)
}
log.WithFields(log.Fields{"version": fastScanner.conf.Version, "LogLevel": fastScanner.conf.LogLevel}).Info()
// distWorker is called in request_handler
distWorker, err = scanner.NewDistWorker(fastScanner.conf.ScannerNum, confData, &mctx, nil)
if err != nil {
log.Fatalln("Error: scanner.NewDistWorker! err:", err.Error())
}
go ServeStart(&mctx)
//============= MODULE ===============
//wait for exit signal
<-sigCh
//module clean
//main clean
cancel()
log.Warn("Stop done!")
}