/
ip.go
109 lines (99 loc) · 2.8 KB
/
ip.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
package fs
import (
"encoding/hex"
"errors"
"github.com/lavalamp-/ipv666/internal/logging"
"github.com/lavalamp-/ipv666/internal/modeling"
"github.com/lavalamp-/ipv666/internal/persist"
"io/ioutil"
"net"
"strings"
)
func ReadIPsFromFile(filePath string) ([]*net.IP, error) {
bytes, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, err
}
return ParseIPsFromBytes(bytes)
}
func ReadIPsFromAddressTreeBytes(toParse []byte) ([]*net.IP, error) {
var tree modeling.AddressTree
err := persist.Unmarshal(toParse, &tree)
if err != nil {
return nil, err
} else {
return tree.GetAllIPs(), nil
}
}
func ReadIPsFromHexFileBytes(toParse []byte) []*net.IP {
parseString := strings.TrimSpace(string(toParse))
lines := strings.Split(parseString, "\n")
var toReturn []*net.IP
for _, line := range lines {
newIP := net.ParseIP(strings.TrimSpace(line))
if newIP == nil {
logging.Warnf("No IP found from content '%s'.", line)
continue
}
toReturn = append(toReturn, &newIP)
}
return toReturn
}
func fatHexStringToIP(toParse string) (*net.IP, error) {
data, err := hex.DecodeString(toParse)
if err != nil {
return nil, err
}
ip := net.IP(data)
return &ip, nil
}
func ReadIPsFromFatHexFileBytes(toParse []byte) []*net.IP {
parseString := strings.TrimSpace(string(toParse))
lines := strings.Split(parseString, "\n")
var toReturn []*net.IP
for _, line := range lines {
lineStrip := strings.TrimSpace(line)
newIp, err := fatHexStringToIP(lineStrip)
if err != nil {
logging.Warnf("Error thrown when processing bytes %s as fat hex: %s", line, err.Error())
} else {
toReturn = append(toReturn, newIp)
}
}
return toReturn
}
func ReadIPsFromBinaryFileBytes(toParse []byte) []*net.IP {
var toReturn []*net.IP
for i := 0; i < len(toParse); i += 16 {
ipBytes := make([]byte, 16)
copy(ipBytes, toParse[i:i+16])
newIP := net.IP(ipBytes)
toReturn = append(toReturn, &newIP)
}
return toReturn
}
func ParseIPsFromBytes(toParse []byte) ([]*net.IP, error) {
split := strings.Split(string(toParse), "\n")
toCheck := split[0]
if strings.Contains(toCheck, ":") { // Standard ASCII hex with colons
return ReadIPsFromHexFileBytes(toParse), nil
} else if len(toCheck) == 32 { // ASCII hex without colons
return ReadIPsFromFatHexFileBytes(toParse), nil
} else if len(toParse) % 16 == 0 { // Binary representation
return ReadIPsFromBinaryFileBytes(toParse), nil
} else { // IP address tree format
result, err := ReadIPsFromAddressTreeBytes(toParse)
if err != nil {
return nil, errors.New("could not determine the format of IPv6 address bytes")
} else {
return result, nil
}
}
}
func ReadIPsFromHexFile(filePath string) ([]*net.IP, error) {
fileContent, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, err
}
return ReadIPsFromHexFileBytes(fileContent), nil
}