-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathclient.go
52 lines (43 loc) · 1.24 KB
/
client.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
package event
import (
"context"
"net"
"github.com/oschwald/maxminddb-golang"
"github.com/code-payments/code-server/pkg/grpc/client"
"github.com/code-payments/code-server/pkg/netutil"
"github.com/code-payments/code-server/pkg/pointer"
"github.com/code-payments/code-server/pkg/code/data/event"
)
// InjectClientDetails injects client details into the provided event record. Metadata
// is provided on a best-effort basis.
//
// todo: We probably need a bit of a refactor for MaxMind (ie. put behind an interface, add to DataProvider, etc)
func InjectClientDetails(ctx context.Context, db *maxminddb.Reader, eventRecord *event.Record, isSource bool) {
ip, err := client.GetIPAddr(ctx)
if err != nil {
return
}
if isSource {
eventRecord.SourceClientIp = pointer.String(ip)
} else {
eventRecord.DestinationClientIp = pointer.String(ip)
}
if db == nil {
return
}
parsed := net.ParseIP(ip)
if parsed == nil {
return
}
metadata, err := netutil.GetIpMetadata(ctx, db, ip)
if err != nil {
return
}
if isSource {
eventRecord.SourceClientCity = metadata.City
eventRecord.SourceClientCountry = metadata.Country
} else {
eventRecord.DestinationClientCity = metadata.City
eventRecord.DestinationClientCountry = metadata.Country
}
}