-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.go
More file actions
105 lines (85 loc) · 1.91 KB
/
helpers.go
File metadata and controls
105 lines (85 loc) · 1.91 KB
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
package main
import (
"bufio"
"log"
"net"
"os"
"strings"
"github.com/spf13/viper"
)
func readZone(path string) ([]string, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, scanner.Err()
}
func getId(url string) string {
return strings.Split(url, ".")[0]
}
func checkIP(ip string, ipRange []string) bool {
ip_net := net.ParseIP(ip)
for _, r := range ipRange {
_, ipnetA, _ := net.ParseCIDR(r)
if ipnetA.Contains(ip_net) {
return true
}
}
return false
}
func checkProvider(ipAddr string) string {
for k, v := range rangeMap {
rv := checkIP(ipAddr, v)
if rv {
return k
}
}
return "Unknown"
}
func configSetup() {
// read in the ranges
f := map[string]string{"Cloudflare": "iplists/cloudflare_ipv4.txt",
"NextDNS": "iplists/nextdns_ipv4.txt"}
for k, v := range f {
lines, err := readZone(v)
if err != nil {
log.Fatalf("readZone: %s", err)
}
rangeMap[k] = lines
}
// set config defaults
viper.SetDefault("dns_addr", "127.0.0.1")
viper.SetDefault("dns_port", "8053")
viper.SetDefault("http_addr", "127.0.0.1")
viper.SetDefault("http_port", "8080")
viper.SetDefault("responseIP", "127.0.0.1")
// load a config file
viper.SetConfigType("yaml")
viper.SetConfigName("config.yaml")
viper.AddConfigPath(".")
err := viper.ReadInConfig()
if err != nil {
log.Printf("Error: %s. Using defaults.\n", err)
}
if viper.Get("dns_addr") != nil {
dns_addr = viper.GetString("dns_addr")
}
if viper.Get("dns_port") != nil {
dns_port = viper.GetInt("dns_port")
}
if viper.Get("http_addr") != nil {
http_addr = viper.GetString("http_addr")
}
if viper.Get("http_port") != nil {
http_port = viper.GetInt("http_port")
}
if viper.Get("responseIP") != nil {
responseIP = viper.GetString("responseIP")
}
}