forked from projectdiscovery/proxify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
101 lines (87 loc) · 2.04 KB
/
logger.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
package proxify
import (
"fmt"
"net/http"
"net/http/httputil"
"os"
"path"
"github.com/projectdiscovery/gologger"
)
type OptionsLogger struct {
Verbose bool
OutputFolder string
}
type OutputData struct {
userdata UserData
data []byte
}
type Logger struct {
options *OptionsLogger
asyncqueue chan OutputData
}
func NewLogger(options *OptionsLogger) *Logger {
logger := &Logger{
options: options,
asyncqueue: make(chan OutputData, 1000),
}
_ = logger.createOutputFolder()
go logger.AsyncWrite()
return logger
}
func (l *Logger) createOutputFolder() error {
if l.options.OutputFolder == "" {
return nil
}
return os.MkdirAll(l.options.OutputFolder, 0755)
}
func (l *Logger) AsyncWrite() {
for outputdata := range l.asyncqueue {
destFile := path.Join(l.options.OutputFolder, fmt.Sprintf("%s-%s", outputdata.userdata.host, outputdata.userdata.id))
// if it's a response and file doesn't exist skip
f, err := os.OpenFile(destFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
continue
}
fmt.Fprintf(f, "%s", outputdata.data)
f.Close()
if outputdata.userdata.hasResponse {
outputFileName := destFile + ".txt"
if outputdata.userdata.match {
outputFileName = destFile + ".match.txt"
}
_ = os.Rename(destFile, outputFileName)
}
}
}
func (l *Logger) LogRequest(req *http.Request, userdata UserData) error {
reqdump, err := httputil.DumpRequest(req, true)
if err != nil {
return err
}
if l.options.OutputFolder != "" {
l.asyncqueue <- OutputData{data: reqdump, userdata: userdata}
}
if l.options.Verbose {
gologger.Silentf(string(reqdump))
}
return nil
}
func (l *Logger) LogResponse(resp *http.Response, userdata UserData) error {
if resp == nil {
return nil
}
respdump, err := httputil.DumpResponse(resp, true)
if err != nil {
return err
}
if l.options.OutputFolder != "" {
l.asyncqueue <- OutputData{data: respdump, userdata: userdata}
}
if l.options.Verbose {
gologger.Silentf(string(respdump))
}
return nil
}
func (l *Logger) Close() {
close(l.asyncqueue)
}