/
core.go
60 lines (53 loc) · 1.32 KB
/
core.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
package adscore
import (
"net"
"strings"
"encoding/base64"
"github.com/jackc/pgx"
)
// Parsing Base64 encode client IP and Mac
func ParseParams(params string) (clientIp pgx.NullString, ipV4 bool, clientMac pgx.NullString) {
ipV4 = false
clientIp = pgx.NullString{Valid: false}
clientMac = pgx.NullString{Valid: false}
b, err := base64.StdEncoding.DecodeString(params)
if err != nil {
// fmt.Println(err.Error())
umac, err := net.ParseMAC(params)
if err == nil {
clientMac = pgx.NullString{String: umac.String(), Valid: true}
}
return clientIp, ipV4, clientMac
}
s := strings.SplitN(string(b), "|", 2)
if len(s) != 2 {
return clientIp, ipV4, clientMac
}
clientIp, ipV4 = GetIP(s[0])
umac, err := net.ParseMAC(s[1])
if err == nil {
clientMac = pgx.NullString{String: umac.String(), Valid: true}
}
return clientIp, ipV4, clientMac
}
// Return valid IP
func GetIP(s string) (clientIp pgx.NullString, ipV4 bool) {
ipV4 = false
clientIp = pgx.NullString{Valid: false}
ip, _, err := net.SplitHostPort(s)
if err == nil {
clientIp = pgx.NullString{String: ip, Valid: true}
if net.ParseIP(ip).To4() != nil {
ipV4 = true
}
} else {
ip := net.ParseIP(s)
if ip != nil {
clientIp = pgx.NullString{String: ip.String(), Valid: true}
}
if ip.To4() != nil {
ipV4 = true
}
}
return clientIp, ipV4
}