Skip to content

Commit

Permalink
Merge pull request #9 from goldfix/ver_0.6.2
Browse files Browse the repository at this point in the history
Ver 0.6.2
  • Loading branch information
goldfix authored Sep 3, 2019
2 parents 34f5705 + 5f815ae commit 52545d9
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 27 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
- Add and remove blacklist hosts, from your hosts file.
- Possibility to customize your download blacklist links.
- Possibility to esclude specific hosts.
- Only for Windows.
- *Linux version is wip...*
- Windows & Linux version.

### Use:

Expand All @@ -26,9 +25,9 @@ Options:
-v, --version view version
Command:
load load custom hosts from external urls declared in the file: '<USER_FOLDER>/.pigHosts/pigHosts.urls'
unload disable and remove custom hosts
load load custom hosts from external urls declared in the file: '.pigHosts/pigHosts.urls'
force_init delete and create a new set of configuration files: '.pigHosts/pigHosts.excluded' and '.pigHosts/pigHosts.urls' in your user/home folder
force_init delete and create a new set of configuration files: '<USER_FOLDER>/.pigHosts/pigHosts.excluded' and '<USER_FOLDER>/.pigHosts/pigHosts.urls'
```

Expand Down
4 changes: 2 additions & 2 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ tasks:
desc: Download modules and force a build of project using vendor folder.
deps: [mod-vendor]
cmds:
- go build -mod vendor -i -a -o $GOPATH/bin/pigHosts.exe ./cmd/...
- go build -mod vendor -i -a -o $GOPATH/bin/pigHosts{{exeExt}} ./cmd/...
build:
desc: Build project using vendor folder.
cmds:
- go build -mod vendor -i -o $GOPATH/bin/pigHosts.exe ./cmd/...
- go build -mod vendor -i -o $GOPATH/bin/pigHosts{{exeExt}} ./cmd/...
mod-vendor:
desc: Download module into vendor folder.
cmds:
Expand Down
21 changes: 12 additions & 9 deletions cmd/pighosts/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"fmt"
"os"
pighosts "pigHosts"
"runtime/debug"
Expand All @@ -26,7 +25,11 @@ func init() {
}

func main() {
usage := `pigHost
homeFolder, err := os.UserHomeDir()
ChkErr(err)

usage := `
pigHost
Usage: pigHost [load | unload | force_init] [-h | -v | -o]
pigHost (load)
Expand All @@ -41,9 +44,10 @@ Options:
-v, --version view version
Command:
load load custom hosts from external urls declared in the file: '` + homeFolder + `/.pigHosts/pigHosts.urls'
unload disable and remove custom hosts
load load custom hosts from external urls declared in the file: '.pigHosts/pigHosts.urls'
force_init delete and create a new set of configuration files: '.pigHosts/pigHosts.excluded' and '.pigHosts/pigHosts.urls' in your user/home folder`
force_init delete and create a new set of configuration files: '` + homeFolder + `/.pigHosts/pigHosts.excluded' and '` + homeFolder + `/.pigHosts/pigHosts.urls'
`

arguments, err := docopt.ParseDoc(usage)
ChkErr(err)
Expand Down Expand Up @@ -83,25 +87,24 @@ Command:
r, err = arguments.Bool("load")
ChkErr(err)
if r {

logrus.Info("Start process...")
err = pighosts.LoadHostsFile()
ChkErr(err)
logrus.Info("End process.")

os.Exit(0)
}
docopt.PrintHelpAndExit(err, usage)

err = fmt.Errorf("My Err: %v", "super error!")
ChkErr(err)

logrus.Info("Try to use: option -h or --help for help online.\n")
os.Exit(0)
}

// ChkErr check returned error
func ChkErr(err error) {
if err != nil {
logrus.Error(err, "\n\n")
logrus.Errorf("Version : %v, commit %v, built at %v", version, commit, date)
logrus.Errorf("Version : %v - Commit: %v - Built: %v", version, commit, date)
logrus.Errorf("Stack : %s", string(debug.Stack()))
os.Exit(1)
}
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ go 1.12
require github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815

require github.com/sirupsen/logrus v1.4.2

require github.com/briandowns/spinner v1.6.1
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
github.com/briandowns/spinner v1.6.1 h1:LBxHu5WLyVuVEtTD72xegiC7QJGx598LBpo3ywKTapA=
github.com/briandowns/spinner v1.6.1/go.mod h1://Zf9tMcxfRUA36V23M6YGEAv+kECGfvpnLTnb8n4XQ=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815 h1:bWDMxwH3px2JBh6AyO7hdCn/PkvCZXii8TGj7sbtEbQ=
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU=
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
27 changes: 21 additions & 6 deletions resource.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
package pighosts

import "time"
import "os"
import (
"os"
"runtime"
"time"

"github.com/briandowns/spinner"
)

var spinnerInd = spinner.New(spinner.CharSets[9], 100*time.Millisecond)
var pigHostsUrls = ""
var pigHostsExcluded = ""

const numHostPerLine = 9

const nonRoutable = "0.0.0.0"
const localHostIP4 = "127.0.0.1"
const localHostIP6 = "::1"
const hostFileWin = "/Windows/System32/drivers/etc/hosts"
const hostFileLinux = "/etc/hosts"
const numHostPerLineWin = 9
const numHostPerLineLinux = 1

var filterSpecificHostDefault = []string{
"127.0.0.1 localhost",
Expand Down Expand Up @@ -58,8 +66,8 @@ var filterSpecificHostTmp = []string{}
const headerHostFile = "###--pigHost_START------------------------------------"
const footerHostFile = "###--pigHosts_END-------------------------------------"

const hostFile = "/Windows/System32/drivers/etc/hosts"

var hostFile = hostFileWin
var numHostPerLine = numHostPerLineWin
var hostFileNew = os.TempDir() + "/pigHostBak/host.new"
var hostFileEmpty = os.TempDir() + "/pigHostBak/host.empty"
var hostFileBak = os.TempDir() + "/pigHostBak/host_" + time.Now().Format("20060201T1504") + ".bak"
Expand All @@ -80,3 +88,10 @@ const manifest = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
</security>
</trustInfo>
</assembly>`

func init() {
if runtime.GOOS != "windows" {
hostFile = hostFileLinux
numHostPerLine = numHostPerLineLinux
}
}
2 changes: 1 addition & 1 deletion utilsConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func InitPigHosts(force bool) error {

if _, err := os.Stat(homeFolder); os.IsNotExist(err) {
err = nil
err = os.Mkdir(homeFolder, os.ModeDir)
err = os.Mkdir(homeFolder, os.ModePerm)
if err != nil {
return err
}
Expand Down
19 changes: 15 additions & 4 deletions utilsHostFile.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

func UnloadHostsFile() error {

spinnerInd.Restart()
//prepare a new empty version of host file
err := prepareHostFile(nil)
if err != nil {
Expand All @@ -29,6 +30,7 @@ func UnloadHostsFile() error {
if err != nil {
return err
}
spinnerInd.Stop()

return nil
}
Expand All @@ -38,12 +40,14 @@ func LoadHostsFile() error {
logrus.Info("Download hosts list:")
hosts := make([]string, 0)
for _, k := range defaultHostsUrlsTmp {
logrus.Info("\t", k)
spinnerInd.Restart()
z, err := downlaodRemoteList(k)
if err != nil {
return err
}
hosts = append(hosts, z...)
logrus.Info("\t", k, " -- Num. hosts download: ", len(z))
spinnerInd.Stop()
}

a := prepareHostsList(hosts)
Expand All @@ -61,12 +65,13 @@ func LoadHostsFile() error {
if err != nil {
return err
}
logrus.Info("Host file updated.")
logrus.Info("Hosts file updated.")

return nil
}

func downlaodRemoteList(url string) ([]string, error) {

if url == "" || (!strings.HasPrefix(url, "http://") && !strings.HasPrefix(url, "https://")) {
return []string{}, nil
}
Expand All @@ -92,17 +97,19 @@ func downlaodRemoteList(url string) ([]string, error) {
}

r := strings.FieldsFunc(strings.ReplaceAll(string(b), "\r\n", "\n"), f)

return r, nil
}

func prepareHostFile(hosts []string) error {

header := "\n\n" + headerHostFile
footer := "\n\n" + footerHostFile + "\n\n"

dir := path.Dir(hostFileNew)
if _, err := os.Stat(dir); os.IsNotExist(err) {
err = nil
err = os.Mkdir(dir, os.ModeDir)
err = os.Mkdir(dir, os.ModePerm)
if err != nil {
return err
}
Expand Down Expand Up @@ -151,6 +158,7 @@ func prepareHostFile(hosts []string) error {
}

func readHostFile() (string, error) {

result := ""
f, err := os.OpenFile(hostFile, os.O_RDONLY, os.ModeType)
if err != nil {
Expand Down Expand Up @@ -180,14 +188,16 @@ func readHostFile() (string, error) {
b := make([]byte, startLine)
f.ReadAt(b, 0)
result = string(b)

return result, nil
}

func backupHostFile(s string) (int64, error) {

dir := path.Dir(hostFileBak)
if _, err := os.Stat(dir); os.IsNotExist(err) {
err = nil
err = os.Mkdir(dir, os.ModeDir)
err = os.Mkdir(dir, os.ModePerm)
if err != nil {
return 0, err
}
Expand All @@ -204,5 +214,6 @@ func backupHostFile(s string) (int64, error) {
if err != nil {
return 0, err
}

return stat.Size(), nil
}
7 changes: 6 additions & 1 deletion utilsHostString_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pighosts

import (
"reflect"
"runtime"
"strings"
"testing"
)
Expand Down Expand Up @@ -107,6 +108,10 @@ func Test_splitHostPerLine(t *testing.T) {
type args struct {
hosts map[string]int
}
result := []int{10, 7}
if runtime.GOOS != "windows" {
result = []int{2, 2}
}
tests := []struct {
name string
args args
Expand All @@ -130,7 +135,7 @@ func Test_splitHostPerLine(t *testing.T) {
"1phads7.com": 2,
"test.test.io": 1},
},
[]int{10, 7},
result,
},
}
for _, tt := range tests {
Expand Down

0 comments on commit 52545d9

Please sign in to comment.