Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
goldfix committed May 12, 2019
1 parent fa9410b commit 7a24ac4
Showing 3 changed files with 91 additions and 12 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"go.formatTool": "goimports"

}
68 changes: 56 additions & 12 deletions utils.go
Original file line number Diff line number Diff line change
@@ -4,20 +4,52 @@ import (
"fmt"
"io/ioutil"
"net/http"
"regexp"
"strings"
)

// NON_ROUTABLE 0.0.0.0
var NON_ROUTABLE string = "0.0.0.0"
// NonRoutable 0.0.0.0
var NonRoutable = "0.0.0.0"

// LocalHost 127.0.0.1
var LocalHost = "127.0.0.1"

// SpecificHost
var SpecificHost = []string{
"127.0.0.1 localhost",
"127.0.0.1 localhost.localdomain",
"127.0.0.1 local",
"255.255.255.255 broadcasthost",
"::1 localhost",
"::1 ip6-localhost",
"::1 ip6-loopback",
"fe80::1%lo0 localhost",
"ff00::0 ip6-localnet",
"ff00::0 ip6-mcastprefix",
"ff02::1 ip6-allnodes",
"ff02::2 ip6-allrouters",
"ff02::3 ip6-allhosts",
"0.0.0.0 0.0.0.0",
}

// LOCALHOST 127.0.0.1
var LOCALHOST string = "127.0.0.1"
func isSpecificHost(s string) bool {
for i := range SpecificHost {
if SpecificHost[i] == s {
return true
}
}
return false
}

// removeLocalHost
func removeLocalHost(s string) (string, error) {
s = strings.ReplaceAll(s, "127.0.0.1", "")
s = strings.ReplaceAll(s, "0.0.0.0", "")

if !isSpecificHost(s) {
s = strings.ReplaceAll(s, LocalHost, "")
s = strings.ReplaceAll(s, NonRoutable, "")
}
s = strings.TrimSpace(s)

return s, nil
}

@@ -27,7 +59,9 @@ func removeComments(s string) (string, error) {
if pos > -1 {
s = s[0:pos]
}
s = regexp.MustCompile(`\s+`).ReplaceAllString(s, " ")
s = strings.TrimSpace(s)
s = strings.ToLower(s)
return s, nil
}

@@ -75,10 +109,14 @@ func prepareHostsList(urls []string) (map[string]int, error) {
if chkErr(err) {
return nil, err
}
hst, err = removeLocalHost(hst)
if chkErr(err) {
return nil, err
if isSpecificHost(hst) {
hst, err = removeLocalHost(hst)
if chkErr(err) {
return nil, err
}
}

hosts[hst]++
}
}
return hosts, nil
@@ -88,12 +126,18 @@ func prepareHostsList(urls []string) (map[string]int, error) {
func prepareHostFile(hosts map[string]int) ([]string, error) {
result := make([]string, 0)
for k := range hosts {
if k != "" {
if k == "" {
continue
}
if strings.Index(k, LocalHost) > -1 || strings.Index(k, NonRoutable) > -1 {
continue
}
if isSpecificHost(k) {
result = append(result, k)
} else {
result = append(result, NonRoutable+" "+k)
}

}

return nil, nil
}

34 changes: 34 additions & 0 deletions utils_test.go
Original file line number Diff line number Diff line change
@@ -49,6 +49,9 @@ func Test_removeComments(t *testing.T) {
{"remove_comment_2", args{s: "host.local.it#test"}, "host.local.it", false},
{"remove_comment_3", args{s: "#host.local.it"}, "", false},
{"remove_comment_4", args{s: "# host.local.it #test"}, "", false},
{"remove_comment_space", args{s: "255.255.255.255 broadcasthost"}, "255.255.255.255 broadcasthost", false},
{"remove_comment_tab", args{s: "255.255.255.255 broadcasthost"}, "255.255.255.255 broadcasthost", false},
{"remove_comment_tab_upper", args{s: "255.255.255.255 BROADCASTHOST"}, "255.255.255.255 broadcasthost", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
@@ -121,3 +124,34 @@ func Test_prepareHostsList(t *testing.T) {

}
}

func Test_prepareHostFile(t *testing.T) {
h, _ := prepareHostsList([]string{"https://drive.google.com/uc?authuser=0&id=1BfGJJLtimhoOi9Sm3jYLF6d8XtYBJ5KY&export=download",
"https://drive.google.com/uc?authuser=0&id=1-QRZf_ymrWFZ4XgmXTZJrkhqzhdJMphB&export=download"})
type args struct {
hosts map[string]int
}
tests := []struct {
name string
args args
want []string
wantErr bool
}{
{"Test_prepareHostFile",
args{hosts: h},
nil,
false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := prepareHostFile(tt.args.hosts)
if (err != nil) != tt.wantErr {
t.Errorf("prepareHostFile() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("prepareHostFile() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 7a24ac4

Please sign in to comment.