-
-
Notifications
You must be signed in to change notification settings - Fork 357
/
app.go
171 lines (152 loc) · 3.99 KB
/
app.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package app
import (
"bytes"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"regexp"
"strings"
"time"
"github.com/crazy-max/WindowsSpyBlocker/app/utils/config"
"github.com/crazy-max/WindowsSpyBlocker/app/utils/file"
"github.com/crazy-max/WindowsSpyBlocker/app/utils/netu"
"github.com/crazy-max/WindowsSpyBlocker/app/utils/pathu"
"github.com/crazy-max/WindowsSpyBlocker/app/utils/print"
"github.com/crazy-max/WindowsSpyBlocker/app/whois"
)
// DownloadLib download an external library
func DownloadLib(lib config.Lib) error {
if lib.OutputPath != "" {
if _, err := os.Stat(lib.OutputPath); os.IsNotExist(err) {
fmt.Printf("Creating folder %s... ", strings.TrimLeft(lib.OutputPath, pathu.Current))
if err := file.CreateSubfolder(lib.OutputPath); err != nil {
print.Error(err)
return err
}
print.Ok()
}
if _, err := os.Stat(lib.Checkfile); err != nil {
fmt.Printf("Downloading %s...", lib.Url)
if err := netu.DownloadFile(lib.Dest, lib.Url); err != nil {
fmt.Print(" ")
print.Error(err)
return err
}
fmt.Print(" ")
print.Ok()
fmt.Printf("Unzipping %s... ", strings.TrimLeft(lib.Dest, pathu.Current))
if err := file.Unzip(lib.Dest, lib.OutputPath); err != nil {
print.Error(err)
return err
}
print.Ok()
fmt.Printf("Seeking checkfile %s... ", strings.TrimLeft(lib.Checkfile, pathu.Current))
if _, err := os.Stat(lib.Checkfile); err != nil {
print.Error(err)
return err
}
print.Ok()
}
} else {
fmt.Printf("Downloading %s...", lib.Url)
if err := netu.DownloadFile(lib.Dest, lib.Url); err != nil {
fmt.Print(" ")
print.Error(err)
return err
}
fmt.Print(" ")
print.Ok()
}
return nil
}
// GetFilteredIpOrDomain get an ip address or domain filtered by excluded values in app.conf
func GetFilteredIpOrDomain(ipOrDomain string) string {
ipOrDomain = strings.ToLower(ipOrDomain)
if netu.IsPrivateIp(ipOrDomain) {
return ""
}
if netu.IsValidIPv4(ipOrDomain) {
for _, exp := range config.App.Exclude.Ips {
if isIpExcluded(ipOrDomain, exp) {
return ""
}
}
} else {
for _, exp := range config.App.Exclude.Hosts {
if isDomainExcluded(ipOrDomain, exp) {
return ""
}
}
}
whoisRes := whois.GetWhois(ipOrDomain)
if whoisRes != (whois.Whois{}) {
for _, exp := range config.App.Exclude.Orgs {
if isOrgExcluded(whoisRes.Org, exp) {
return ""
}
}
}
return ipOrDomain
}
func isIpExcluded(ipStr string, exp string) bool {
ip := net.ParseIP(ipStr)
if ip.To4() == nil {
return true
}
if strings.Contains(exp, "-") {
ipRange := strings.SplitN(exp, "-", 2)
if len(ipRange) != 2 {
return false
}
ipRange0 := net.ParseIP(ipRange[0])
ipRange1 := net.ParseIP(ipRange[1])
if ipRange0.To4() == nil || ipRange1.To4() == nil {
return false
}
if bytes.Compare(ip, ipRange0) >= 0 && bytes.Compare(ip, ipRange1) <= 0 {
//fmt.Println(host + " in range of " + ipEx)
return true
}
} else if !netu.IsValidIPv4(exp) {
return false
} else if exp == ipStr {
//fmt.Println(host + " = " + ipEx)
return true
}
return false
}
func isDomainExcluded(host string, exp string) bool {
re := regexp.MustCompile(`(?i)^` + strings.Replace(exp, "*", `([^"]+)`, -1) + "$")
matches := re.FindAllString(host, -1)
if len(matches) == 1 {
return true
}
return false
}
func isOrgExcluded(org string, exp string) bool {
re := regexp.MustCompile(`(?i)^` + strings.Replace(exp, "*", `([^"]+)`, -1) + "$")
matches := re.FindAllString(org, -1)
if len(matches) == 1 {
return true
}
return false
}
// GetLatestVersion returns the latest version from github
func GetLatestVersion() (string, error) {
client := &http.Client{Timeout: 2 * time.Second}
response, err := client.Get(config.Settings.Uris.LatestVersion)
if err != nil {
return "", err
}
defer response.Body.Close()
if response.StatusCode == 200 {
bodyBytes, err := ioutil.ReadAll(response.Body)
if err != nil {
return "", err
}
return string(bodyBytes), nil
}
return "", fmt.Errorf("Status code %d", response.StatusCode)
}