Performed some cleanup
Mostly go formatting but a little creative spacing and commentsexperimental
parent
f0d459d437
commit
08721b9289
|
|
@ -1,9 +1,9 @@
|
||||||
package cfg
|
package cfg
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"bufio"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const CFG = `
|
const CFG = `
|
||||||
|
|
@ -100,11 +100,11 @@ func cfgparseline(buf string) (uint8, string){
|
||||||
case '#':
|
case '#':
|
||||||
state = 2
|
state = 2
|
||||||
case 'd':
|
case 'd':
|
||||||
if (len(buf) < i+10) {
|
if len(buf) < i+10 {
|
||||||
state = 1
|
state = 1
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
if (buf[i:(i+9)] == "download=") {
|
if buf[i:(i+9)] == "download=" {
|
||||||
i += 9
|
i += 9
|
||||||
state = 4
|
state = 4
|
||||||
body = buf[i:]
|
body = buf[i:]
|
||||||
|
|
@ -112,11 +112,11 @@ func cfgparseline(buf string) (uint8, string){
|
||||||
state = 1
|
state = 1
|
||||||
}
|
}
|
||||||
case 's':
|
case 's':
|
||||||
if (len(buf) < i+6) {
|
if len(buf) < i+6 {
|
||||||
state = 1
|
state = 1
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
if (buf[i:(i+5)] == "site=") {
|
if buf[i:(i+5)] == "site=" {
|
||||||
i += 5
|
i += 5
|
||||||
state = 3
|
state = 3
|
||||||
body = buf[i:]
|
body = buf[i:]
|
||||||
|
|
@ -125,11 +125,11 @@ func cfgparseline(buf string) (uint8, string){
|
||||||
}
|
}
|
||||||
//compare buf[i:(i+3)] to "site"
|
//compare buf[i:(i+3)] to "site"
|
||||||
case 'w':
|
case 'w':
|
||||||
if (len(buf) < i+10) {
|
if len(buf) < i+10 {
|
||||||
state = 1
|
state = 1
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
if (buf[i:(i+10)] == "whitelist=") {
|
if buf[i:(i+10)] == "whitelist=" {
|
||||||
i += 10
|
i += 10
|
||||||
state = 5
|
state = 5
|
||||||
body = buf[i:]
|
body = buf[i:]
|
||||||
|
|
@ -137,7 +137,7 @@ func cfgparseline(buf string) (uint8, string){
|
||||||
state = 1
|
state = 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (state !=0){
|
if state != 0 {
|
||||||
return state, body
|
return state, body
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,12 @@
|
||||||
package hosts
|
package hosts
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
|
||||||
"io"
|
|
||||||
"net/http"
|
|
||||||
"log"
|
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"io"
|
||||||
"jbreich/rhosts/cfg"
|
"jbreich/rhosts/cfg"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
// siteList holds the location of all the sites along with a list of their location
|
// siteList holds the location of all the sites along with a list of their location
|
||||||
|
|
@ -14,6 +14,7 @@ type siteList struct {
|
||||||
location string
|
location string
|
||||||
siteEntry []siteEntry
|
siteEntry []siteEntry
|
||||||
}
|
}
|
||||||
|
|
||||||
// siteEntry holds a single entry and if it is a repeat
|
// siteEntry holds a single entry and if it is a repeat
|
||||||
type siteEntry struct {
|
type siteEntry struct {
|
||||||
repeat bool
|
repeat bool
|
||||||
|
|
@ -25,33 +26,33 @@ func Update(config cfg.Config, tmpdir, hostsloc string)(err error){
|
||||||
|
|
||||||
err = error(nil)
|
err = error(nil)
|
||||||
err = copystatichosts(tmpdir, hostsloc)
|
err = copystatichosts(tmpdir, hostsloc)
|
||||||
if (err != nil){
|
if err != nil {
|
||||||
log.Print("Failed to copy static entries")
|
log.Print("Failed to copy static entries")
|
||||||
return nil
|
return
|
||||||
}
|
}
|
||||||
defer os.Remove(tmpdir + "rhosts")
|
defer os.Remove(tmpdir + "rhosts")
|
||||||
err, siteBuff = downloadcontent(config.Downloads, tmpdir, hostsloc)
|
err, siteBuff = downloadcontent(config.Downloads, tmpdir, hostsloc)
|
||||||
if (err != nil){
|
if err != nil {
|
||||||
log.Print("Failed to download entries")
|
log.Print("Failed to download entries")
|
||||||
return nil
|
return
|
||||||
}
|
}
|
||||||
err = writesites(config.Sites, tmpdir, &siteBuff)
|
err = writesites(config.Sites, tmpdir, &siteBuff)
|
||||||
if (err != nil){
|
if err != nil {
|
||||||
log.Print("Failed to failed to copy rhosts static entries")
|
log.Print("Failed to failed to copy rhosts static entries")
|
||||||
return nil
|
return
|
||||||
}
|
}
|
||||||
removeduplicates(&siteBuff, &config.Whitelist)
|
removeduplicates(&siteBuff, &config.Whitelist)
|
||||||
err = write2tmp(tmpdir, &siteBuff)
|
err = write2tmp(tmpdir, &siteBuff)
|
||||||
if (err != nil){
|
if err != nil {
|
||||||
log.Print("Failed to write sites to tmpfile")
|
log.Print("Failed to write sites to tmpfile")
|
||||||
return nil
|
return
|
||||||
}
|
}
|
||||||
err = writetmp2hosts(hostsloc, tmpdir)
|
err = writetmp2hosts(hostsloc, tmpdir)
|
||||||
if (err != nil){
|
if err != nil {
|
||||||
log.Print("Failed to copy to hosts file")
|
log.Print("Failed to copy to hosts file")
|
||||||
return nil
|
return
|
||||||
}
|
}
|
||||||
|
log.Print("Finished updating host")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -74,11 +75,11 @@ func copystatichosts(tmpdir, hostsloc string) error {
|
||||||
filebuf.Split(bufio.ScanLines)
|
filebuf.Split(bufio.ScanLines)
|
||||||
for res := filebuf.Scan(); res; res = filebuf.Scan() {
|
for res := filebuf.Scan(); res; res = filebuf.Scan() {
|
||||||
buff := filebuf.Text()
|
buff := filebuf.Text()
|
||||||
if (buff == "# rhosts begin"){
|
if buff == "# rhosts begin" {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
_, err := file.WriteString(buff + "\n")
|
_, err := file.WriteString(buff + "\n")
|
||||||
if (err != nil) {
|
if err != nil {
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -87,6 +88,7 @@ func copystatichosts(tmpdir, hostsloc string) error {
|
||||||
err = filebuf.Err()
|
err = filebuf.Err()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// downloadcontent attempts to download the provided url and create a siteList. If the file fails to download it attempts to find an old copy from the hosts file.
|
// downloadcontent attempts to download the provided url and create a siteList. If the file fails to download it attempts to find an old copy from the hosts file.
|
||||||
func downloadcontent(downloads []string, tmpdir string, hostsloc string) (err error, list []siteList) {
|
func downloadcontent(downloads []string, tmpdir string, hostsloc string) (err error, list []siteList) {
|
||||||
for _, d := range downloads {
|
for _, d := range downloads {
|
||||||
|
|
@ -94,7 +96,7 @@ func downloadcontent(downloads []string, tmpdir string, hostsloc string) (err er
|
||||||
site.location = d
|
site.location = d
|
||||||
log.Print("Downloading: ", d)
|
log.Print("Downloading: ", d)
|
||||||
response, err := http.Get(d)
|
response, err := http.Get(d)
|
||||||
if (err !=nil) {
|
if err != nil {
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
log.Print("Looking for old record in hosts file")
|
log.Print("Looking for old record in hosts file")
|
||||||
downloadoldlookup(hostsloc, d, &site)
|
downloadoldlookup(hostsloc, d, &site)
|
||||||
|
|
@ -120,7 +122,7 @@ func checkDownloadLine (line string) (address siteEntry){
|
||||||
address.site = ""
|
address.site = ""
|
||||||
buff := ""
|
buff := ""
|
||||||
lineLength := len(line) - 1
|
lineLength := len(line) - 1
|
||||||
for i, c := range(line){
|
for i, c := range line {
|
||||||
if c != ' ' && i < lineLength {
|
if c != ' ' && i < lineLength {
|
||||||
buff += string(c)
|
buff += string(c)
|
||||||
} else if len(buff) > 0 {
|
} else if len(buff) > 0 {
|
||||||
|
|
@ -137,12 +139,12 @@ func checkDownloadLine (line string) (address siteEntry){
|
||||||
if token[0][0] == '#' {
|
if token[0][0] == '#' {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for _, t := range(token) {
|
for _, t := range token {
|
||||||
var period uint
|
var period uint
|
||||||
var failed bool
|
var failed bool
|
||||||
period = 0
|
period = 0
|
||||||
failed = false
|
failed = false
|
||||||
for _, c := range(t) {
|
for _, c := range t {
|
||||||
switch c {
|
switch c {
|
||||||
case '.':
|
case '.':
|
||||||
period++
|
period++
|
||||||
|
|
@ -167,7 +169,7 @@ func downloadoldlookup(hostsloc, d string, site *siteList) error {
|
||||||
var state uint8 = 0
|
var state uint8 = 0
|
||||||
|
|
||||||
hostsf, err := os.Open(hostsloc)
|
hostsf, err := os.Open(hostsloc)
|
||||||
if (err != nil){
|
if err != nil {
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -179,12 +181,12 @@ func downloadoldlookup(hostsloc, d string, site *siteList) error {
|
||||||
buff := fbuff.Text()
|
buff := fbuff.Text()
|
||||||
switch state {
|
switch state {
|
||||||
case 0:
|
case 0:
|
||||||
if (buff == "# rhosts download - " + d){
|
if buff == "# rhosts download - "+d {
|
||||||
log.Print("Found old record in hosts file:" + buff)
|
log.Print("Found old record in hosts file:" + buff)
|
||||||
state = 1
|
state = 1
|
||||||
}
|
}
|
||||||
case 1:
|
case 1:
|
||||||
if (len(buff) >=9 && buff[0:8] == "# rhosts"){
|
if len(buff) >= 9 && buff[0:8] == "# rhosts" {
|
||||||
state = 2
|
state = 2
|
||||||
} else {
|
} else {
|
||||||
siteBuff := checkDownloadLine(buff)
|
siteBuff := checkDownloadLine(buff)
|
||||||
|
|
@ -220,6 +222,7 @@ func writesites(sites []string, tmpdir string, siteBuff *[]siteList) (err error)
|
||||||
*siteBuff = append(*siteBuff, localList)
|
*siteBuff = append(*siteBuff, localList)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// removeduplicates removes any duplicate or uneeded/unwanted addresses
|
// removeduplicates removes any duplicate or uneeded/unwanted addresses
|
||||||
func removeduplicates(siteBuff *[]siteList, whitelist *[]string) {
|
func removeduplicates(siteBuff *[]siteList, whitelist *[]string) {
|
||||||
var safewords = []string{"localhost", "localhost.localdomain", "broadcasthost", "ip6-loopback", "ip6-localhost", "ip6-localnet", "ip6-mcastprefix", "ip6-allnodes", "ip6-allrouters", "ip6-allhosts", "local"}
|
var safewords = []string{"localhost", "localhost.localdomain", "broadcasthost", "ip6-loopback", "ip6-localhost", "ip6-localnet", "ip6-mcastprefix", "ip6-allnodes", "ip6-allrouters", "ip6-allhosts", "local"}
|
||||||
|
|
@ -248,8 +251,8 @@ func removeduplicates(siteBuff *[]siteList, whitelist *[]string){
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
lenEntry := len(entry)
|
lenEntry := len(entry)
|
||||||
for i,e := range(entry) {
|
for i, e := range entry {
|
||||||
for _,w := range(safewords){
|
for _, w := range safewords {
|
||||||
if *e.s == w {
|
if *e.s == w {
|
||||||
*(entry[i].r) = true
|
*(entry[i].r) = true
|
||||||
c.s++
|
c.s++
|
||||||
|
|
@ -259,7 +262,7 @@ func removeduplicates(siteBuff *[]siteList, whitelist *[]string){
|
||||||
if *(entry[i].r) == true {
|
if *(entry[i].r) == true {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
for _,w := range(*whitelist){
|
for _, w := range *whitelist {
|
||||||
if *e.s == w {
|
if *e.s == w {
|
||||||
*(entry[i].r) = true
|
*(entry[i].r) = true
|
||||||
c.w++
|
c.w++
|
||||||
|
|
@ -272,7 +275,7 @@ func removeduplicates(siteBuff *[]siteList, whitelist *[]string){
|
||||||
if i == lenEntry {
|
if i == lenEntry {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
for j,n := range(entry[i+1:]){
|
for j, n := range entry[i+1:] {
|
||||||
if *e.s == *n.s {
|
if *e.s == *n.s {
|
||||||
*(entry[i+j].r) = true
|
*(entry[i+j].r) = true
|
||||||
c.d++
|
c.d++
|
||||||
|
|
@ -282,6 +285,7 @@ func removeduplicates(siteBuff *[]siteList, whitelist *[]string){
|
||||||
}
|
}
|
||||||
log.Printf("Total: %d\tDuplicates: %d\tSafeWords: %d\tWhitelisted: %d\n", lenEntry, c.d, c.s, c.w)
|
log.Printf("Total: %d\tDuplicates: %d\tSafeWords: %d\tWhitelisted: %d\n", lenEntry, c.d, c.s, c.w)
|
||||||
}
|
}
|
||||||
|
|
||||||
// write2tmp write the siteBuff to the tempfile
|
// write2tmp write the siteBuff to the tempfile
|
||||||
func write2tmp(tmpdir string, siteBuff *[]siteList) (err error) {
|
func write2tmp(tmpdir string, siteBuff *[]siteList) (err error) {
|
||||||
err = nil
|
err = nil
|
||||||
|
|
@ -292,7 +296,7 @@ func write2tmp(tmpdir string, siteBuff *[]siteList) (err error) {
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
for _,location := range(*siteBuff){
|
for _, location := range *siteBuff {
|
||||||
if len(location.siteEntry) == 0 {
|
if len(location.siteEntry) == 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
@ -300,7 +304,7 @@ func write2tmp(tmpdir string, siteBuff *[]siteList) (err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
for _,site := range(location.siteEntry){
|
for _, site := range location.siteEntry {
|
||||||
if site.repeat == false {
|
if site.repeat == false {
|
||||||
_, err = tmpf.WriteString("0.0.0.0 " + site.site + "\n")
|
_, err = tmpf.WriteString("0.0.0.0 " + site.site + "\n")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -315,26 +319,26 @@ func write2tmp(tmpdir string, siteBuff *[]siteList) (err error) {
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// writetmp2hosts overwrites the hostsfile with the tmp file
|
// writetmp2hosts overwrites the hostsfile with the tmp file
|
||||||
func writetmp2hosts(hostsloc, tmpdir string) error {
|
func writetmp2hosts(hostsloc, tmpdir string) error {
|
||||||
var err error = nil
|
var err error = nil
|
||||||
tmploc := tmpdir + "rhosts"
|
tmploc := tmpdir + "rhosts"
|
||||||
|
|
||||||
hosts, err := os.Create(hostsloc)
|
hosts, err := os.Create(hostsloc)
|
||||||
if (err != nil){
|
if err != nil {
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
tmp, err := os.Open(tmploc)
|
tmp, err := os.Open(tmploc)
|
||||||
if (err != nil){
|
if err != nil {
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
_, err = io.Copy(hosts, tmp)
|
_, err = io.Copy(hosts, tmp)
|
||||||
if (err != nil){
|
if err != nil {
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,24 +17,22 @@
|
||||||
* along with rhosts. If not, see <https://www.gnu.org/licenses/>.
|
* along with rhosts. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
// rhosts - Program used to maintain a blocklist appended to a host file
|
// rhosts - Program used to maintain a blocklist appended to a host file
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
|
||||||
sysos "jbreich/rhosts/sys"
|
|
||||||
"jbreich/rhosts/serve"
|
|
||||||
"jbreich/rhosts/cfg"
|
"jbreich/rhosts/cfg"
|
||||||
"jbreich/rhosts/hosts"
|
"jbreich/rhosts/hosts"
|
||||||
|
"jbreich/rhosts/serve"
|
||||||
|
sysos "jbreich/rhosts/sys"
|
||||||
|
"log"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var Exit chan bool
|
var Exit chan bool
|
||||||
|
|
||||||
|
|
||||||
const GPL = `
|
const GPL = `
|
||||||
rhosts maintains a blocklist and appends it to the system hosts file
|
rhosts maintains a blocklist and appends it to the system hosts file
|
||||||
|
|
||||||
|
|
@ -54,7 +52,6 @@ const GPL =`
|
||||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
`
|
`
|
||||||
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
tmpdir := ""
|
tmpdir := ""
|
||||||
hostsloc := ""
|
hostsloc := ""
|
||||||
|
|
@ -92,30 +89,42 @@ func main() {
|
||||||
|
|
||||||
sysos.Detect(&tmpdir, &hostsloc, &cfgloc)
|
sysos.Detect(&tmpdir, &hostsloc, &cfgloc)
|
||||||
|
|
||||||
|
// Read the config file
|
||||||
config := cfg.Create(cfgloc)
|
config := cfg.Create(cfgloc)
|
||||||
err, config := config.Update()
|
err, config := config.Update()
|
||||||
log.Print(config)
|
if err != nil {
|
||||||
if (err != nil){log.Panic("Failed to parse config: " + cfgloc)}
|
log.Panic("Failed to parse config: " + cfgloc)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Starting web server
|
||||||
|
serve.Start("blank")
|
||||||
|
|
||||||
|
// Update the hosts file
|
||||||
|
if daemon == false {
|
||||||
|
err := hosts.Update(config, tmpdir, hostsloc)
|
||||||
|
if err != nil {
|
||||||
|
log.Print(err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
|
||||||
for true {
|
for true {
|
||||||
err := hosts.Update(config, tmpdir, hostsloc)
|
err := hosts.Update(config, tmpdir, hostsloc)
|
||||||
if (err != nil){
|
if err != nil {
|
||||||
log.Print(err)
|
log.Print(err)
|
||||||
}
|
}
|
||||||
log.Print("Finished updating host")
|
|
||||||
if (daemon == true){
|
// Check if daemon
|
||||||
|
if daemon == false {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
i := time.Now().Add(time.Duration(interval) * time.Minute).Format(time.Layout)
|
i := time.Now().Add(time.Duration(interval) * time.Minute).Format(time.Layout)
|
||||||
log.Printf("Sleeping for %d minutes", interval)
|
log.Printf("Sleeping for %d minutes", interval)
|
||||||
log.Print("Should restart at: " + i)
|
log.Print("Should restart at: " + i)
|
||||||
time.Sleep(time.Duration(interval) * time.Minute)
|
time.Sleep(time.Duration(interval) * time.Minute)
|
||||||
}else{
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
serve.Start("blank")
|
}
|
||||||
<-Exit
|
<-Exit
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue