This repository has been archived by the owner on Mar 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
dataServer.go
206 lines (177 loc) · 4.86 KB
/
dataServer.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
package dataServer
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"os"
"github.com/shipt/specter/cmd"
"github.com/namsral/flag"
"github.com/pkg/errors"
externalip "github.com/GlenDC/go-external-ip"
"github.com/hpcloud/tail"
"github.com/satyrius/gonx"
log "github.com/sirupsen/logrus"
)
var previousOffset int64
type msg struct {
SrcIP string `json:"src_ip"`
DstIP string `json:"dst_ip"`
HTTPStatus string `json:"http_status"`
XFwdFor string `json:"http_x_forwarded_for"`
HostProxy string `json:"host_proxy,omitempty"`
}
type tailReader struct {
*tail.Tail
cur bytes.Buffer
}
type ngninxLogReader interface {
Read() (*gonx.Entry, error)
}
var conf string
var format string
var logFile string
var server string
func init() {
flag.StringVar(&conf, "conf", "", "Nginx config file (e.g. /etc/nginx/nginx.conf)")
flag.StringVar(&format, "format", "main", "Nginx log_format name")
flag.StringVar(&logFile, "log", "", "The location of the access.log file. Reads from STDIN if no value is set")
flag.StringVar(&server, "server", "http://localhost:1323", "The Specter webserver's server IP:Port")
}
func (t *tailReader) Read(b []byte) (int, error) {
if t.cur.Len() == 0 {
t.cur.WriteString((<-t.Lines).Text)
t.cur.WriteByte('\n')
}
n, err := t.cur.Read(b)
if err == io.EOF {
return n, nil
}
return n, err
}
func getExternalIP() (net.IP, error) {
exip := externalip.DefaultConsensus(nil, nil)
ip, err := exip.ExternalIP()
if err != nil {
return ip, errors.Wrap(err, "error getting external IP")
}
log.Info("Server IP:", ip.String())
return ip, nil
}
func tailFile(logFile string) (*tail.Tail, error) {
tail, err := tail.TailFile(logFile, tail.Config{Logger: tail.DiscardingLogger, Follow: true, ReOpen: true, Location: &tail.SeekInfo{Offset: 0, Whence: os.SEEK_END}})
return tail, errors.Wrap(err, "Error tailing file")
}
func logReader(tail *tail.Tail) *tailReader {
return &tailReader{Tail: tail}
}
func sendMessage(url string, mBytes []byte) error {
_, err := http.Post(url, "application/json", bytes.NewReader(mBytes))
log.Debug("sending data to webserver")
return errors.Wrap(err, "error sending message")
}
func processLog(reader ngninxLogReader, ip net.IP) (msg, error) {
rec, err := reader.Read()
if err == io.EOF {
return msg{}, nil
}
if err != nil {
return msg{}, errors.Wrap(err, "error reading the log file")
}
// Process the record...
ra, err := rec.Field("remote_addr")
if err != nil {
log.WithFields(log.Fields{
"Error": err,
}).Warn("error getting the remote address from the access.log")
return msg{}, nil
}
s, err := rec.Field("status")
if err != nil {
log.WithFields(log.Fields{
"Error": err,
}).Warn("error getting the status from the access.log")
return msg{}, nil
}
x, err := rec.Field("http_x_forwarded_for")
if err != nil {
log.WithFields(log.Fields{
"Error": err,
}).Warn("error getting http_x_forwarded_for from the access.log")
return msg{}, nil
}
p, err := rec.Field("proxy_host")
return msg{SrcIP: ra, DstIP: ip.String(), HTTPStatus: s, XFwdFor: x, HostProxy: p}, nil
}
// Start starts and runs the data server
func Start() {
flag.Parse()
if !cmd.IsFlagPassed("conf") {
log.Fatal(`you did not set a Nginx Config File.`)
}
if !cmd.IsFlagPassed("format") {
log.Warn(`you did not set a Nginx log_format name, using "main"`)
}
if !cmd.IsFlagPassed("log") {
log.Warn(`you did not set a log location, using STDIN"`)
}
if !cmd.IsFlagPassed("server") {
log.Warn(`you did not set a server to send data to, using localhost:1323`)
}
log.Info("Starting Dataserver")
log.Debugf("conf flag is set to: %s", conf)
log.Debugf("format flag is set to: %s", format)
log.Debugf("log flag is set to: %s", logFile)
log.Debugf("server flag is set to: %s", server)
url := fmt.Sprintf("%s/logs", server)
ip, err := getExternalIP()
if err != nil {
log.WithFields(log.Fields{
"Error": err,
}).Fatal("error calling getExteranlIp")
}
tail, err := tailFile(logFile)
if err != nil {
log.WithFields(log.Fields{
"Error": err,
}).Fatal("error calling tailFile")
}
cf, err := os.Open(conf)
if err != nil {
log.WithFields(log.Fields{
"Error": err,
}).Fatal("error opening nginx config file")
}
defer cf.Close()
reader, err := gonx.NewNginxReader(logReader(tail), cf, format)
if err != nil {
log.WithFields(log.Fields{
"Error": err,
}).Fatal("error creating reader the nginx config file")
}
for {
m, err := processLog(reader, ip)
if err != nil {
log.WithFields(log.Fields{
"Error": err,
}).Fatal("error processing log file")
}
if (msg{} == m) {
continue
}
mBytes, err := json.Marshal(m)
if err != nil {
log.WithFields(log.Fields{
"Error": err,
}).Warn("error serializing message")
}
err = sendMessage(url, mBytes)
if err != nil {
log.WithFields(log.Fields{
"Error": err,
}).Warn("error posting to specter webserver")
}
}
}