@@ -23,6 +23,10 @@ const (
23
23
datagramReadBufferSize = 64 * 1024
24
24
)
25
25
26
+ // A function type which gets the TLS peer name from the connection. Can return
27
+ // ok=false to terminate the connection
28
+ type TlsPeerNameFunc func (tlsConn * tls.Conn ) (tlsPeer string , ok bool )
29
+
26
30
type Server struct {
27
31
listeners []net.Listener
28
32
connections []net.Conn
@@ -33,11 +37,12 @@ type Server struct {
33
37
handler Handler
34
38
lastError error
35
39
readTimeoutMilliseconds int64
40
+ tlsPeerNameFunc TlsPeerNameFunc
36
41
}
37
42
38
43
//NewServer returns a new Server
39
44
func NewServer () * Server {
40
- return & Server {}
45
+ return & Server {tlsPeerNameFunc : defaultTlsPeerName }
41
46
}
42
47
43
48
//Sets the syslog format (RFC3164 or RFC5424 or RFC6587)
@@ -55,6 +60,21 @@ func (s *Server) SetTimeout(millseconds int64) {
55
60
s .readTimeoutMilliseconds = millseconds
56
61
}
57
62
63
+ // Set the function that extracts a TLS peer name from the TLS connection
64
+ func (s * Server ) SetTlsPeerNameFunc (tlsPeerNameFunc TlsPeerNameFunc ) {
65
+ s .tlsPeerNameFunc = tlsPeerNameFunc
66
+ }
67
+
68
+ // Default TLS peer name function - returns the CN of the certificate
69
+ func defaultTlsPeerName (tlsConn * tls.Conn ) (tlsPeer string , ok bool ) {
70
+ state := tlsConn .ConnectionState ()
71
+ if len (state .PeerCertificates ) <= 0 {
72
+ return "" , false
73
+ }
74
+ cn := state .PeerCertificates [0 ].Subject .CommonName
75
+ return cn , true
76
+ }
77
+
58
78
//Configure the server for listen on an UDP addr
59
79
func (s * Server ) ListenUDP (addr string ) error {
60
80
udpAddr , err := net .ResolveUDPAddr ("udp" , addr )
@@ -171,20 +191,37 @@ func (s *Server) goScanConnection(connection net.Conn) {
171
191
scanner .Split (sf )
172
192
}
173
193
174
- var scanCloser * ScanCloser
175
- scanCloser = & ScanCloser {scanner , connection }
176
-
177
194
remoteAddr := connection .RemoteAddr ()
178
195
var client string
179
196
if remoteAddr != nil {
180
197
client = remoteAddr .String ()
181
198
}
182
199
200
+ tlsPeer := ""
201
+ if tlsConn , ok := connection .(* tls.Conn ); ok {
202
+ // Handshake now so we get the TLS peer information
203
+ if err := tlsConn .Handshake (); err != nil {
204
+ connection .Close ()
205
+ return
206
+ }
207
+ if s .tlsPeerNameFunc != nil {
208
+ var ok bool
209
+ tlsPeer , ok = s .tlsPeerNameFunc (tlsConn )
210
+ if ! ok {
211
+ connection .Close ()
212
+ return
213
+ }
214
+ }
215
+ }
216
+
217
+ var scanCloser * ScanCloser
218
+ scanCloser = & ScanCloser {scanner , connection }
219
+
183
220
s .wait .Add (1 )
184
- go s .scan (scanCloser , client )
221
+ go s .scan (scanCloser , client , tlsPeer )
185
222
}
186
223
187
- func (s * Server ) scan (scanCloser * ScanCloser , client string ) {
224
+ func (s * Server ) scan (scanCloser * ScanCloser , client string , tlsPeer string ) {
188
225
loop:
189
226
for {
190
227
select {
@@ -196,7 +233,7 @@ loop:
196
233
scanCloser .closer .SetReadDeadline (time .Now ().Add (time .Duration (s .readTimeoutMilliseconds ) * time .Millisecond ))
197
234
}
198
235
if scanCloser .Scan () {
199
- s .parser ([]byte (scanCloser .Text ()), client )
236
+ s .parser ([]byte (scanCloser .Text ()), client , tlsPeer )
200
237
} else {
201
238
break loop
202
239
}
@@ -206,7 +243,7 @@ loop:
206
243
s .wait .Done ()
207
244
}
208
245
209
- func (s * Server ) parser (line []byte , client string ) {
246
+ func (s * Server ) parser (line []byte , client string , tlsPeer string ) {
210
247
parser := s .format .GetParser (line )
211
248
err := parser .Parse ()
212
249
if err != nil {
@@ -215,6 +252,7 @@ func (s *Server) parser(line []byte, client string) {
215
252
216
253
logParts := parser .Dump ()
217
254
logParts ["client" ] = client
255
+ logParts ["tls_peer" ] = tlsPeer
218
256
219
257
s .handler .Handle (logParts , int64 (len (line )), err )
220
258
}
@@ -315,10 +353,10 @@ func (s *Server) goParseDatagrams() {
315
353
}
316
354
if sf := s .format .GetSplitFunc (); sf != nil {
317
355
if _ , token , err := sf (msg .message , true ); err == nil {
318
- s .parser (token , msg .client )
356
+ s .parser (token , msg .client , "" )
319
357
}
320
358
} else {
321
- s .parser (msg .message , msg .client )
359
+ s .parser (msg .message , msg .client , "" )
322
360
}
323
361
}
324
362
}
0 commit comments