Performed some cleanup

Mostly go formatting but a little creative spacing and comments
testing
Justin Reichardt 2022-08-19 13:49:24 -05:00
parent 77c020e434
commit e1c576f2a1
4 changed files with 162 additions and 149 deletions

View File

@ -1,9 +1,9 @@
package cfg
import (
"bufio"
"log"
"os"
"bufio"
)
const CFG = `
@ -100,11 +100,11 @@ func cfgparseline(buf string) (uint8, string){
case '#':
state = 2
case 'd':
if (len(buf) < i+10) {
if len(buf) < i+10 {
state = 1
break
}
if (buf[i:(i+9)] == "download=") {
if buf[i:(i+9)] == "download=" {
i += 9
state = 4
body = buf[i:]
@ -112,11 +112,11 @@ func cfgparseline(buf string) (uint8, string){
state = 1
}
case 's':
if (len(buf) < i+6) {
if len(buf) < i+6 {
state = 1
break
}
if (buf[i:(i+5)] == "site=") {
if buf[i:(i+5)] == "site=" {
i += 5
state = 3
body = buf[i:]
@ -125,11 +125,11 @@ func cfgparseline(buf string) (uint8, string){
}
//compare buf[i:(i+3)] to "site"
case 'w':
if (len(buf) < i+10) {
if len(buf) < i+10 {
state = 1
break
}
if (buf[i:(i+10)] == "whitelist=") {
if buf[i:(i+10)] == "whitelist=" {
i += 10
state = 5
body = buf[i:]
@ -137,7 +137,7 @@ func cfgparseline(buf string) (uint8, string){
state = 1
}
}
if (state !=0){
if state != 0 {
return state, body
}
}

View File

@ -1,12 +1,12 @@
package hosts
import (
"os"
"io"
"net/http"
"log"
"bufio"
"io"
"jbreich/rhosts/cfg"
"log"
"net/http"
"os"
)
// siteList holds the location of all the sites along with a list of their location
@ -14,6 +14,7 @@ type siteList struct {
location string
siteEntry []siteEntry
}
// siteEntry holds a single entry and if it is a repeat
type siteEntry struct {
repeat bool
@ -25,33 +26,33 @@ func Update(config cfg.Config, tmpdir, hostsloc string)(err error){
err = error(nil)
err = copystatichosts(tmpdir, hostsloc)
if (err != nil){
if err != nil {
log.Print("Failed to copy static entries")
return nil
return
}
defer os.Remove(tmpdir + "rhosts")
err, siteBuff = downloadcontent(config.Downloads, tmpdir, hostsloc)
if (err != nil){
if err != nil {
log.Print("Failed to download entries")
return nil
return
}
err = writesites(config.Sites, tmpdir, &siteBuff)
if (err != nil){
if err != nil {
log.Print("Failed to failed to copy rhosts static entries")
return nil
return
}
removeduplicates(&siteBuff, &config.Whitelist)
err = write2tmp(tmpdir, &siteBuff)
if (err != nil){
if err != nil {
log.Print("Failed to write sites to tmpfile")
return nil
return
}
err = writetmp2hosts(hostsloc, tmpdir)
if (err != nil){
if err != nil {
log.Print("Failed to copy to hosts file")
return nil
return
}
log.Print("Finished updating host")
return
}
@ -74,11 +75,11 @@ func copystatichosts(tmpdir, hostsloc string) error {
filebuf.Split(bufio.ScanLines)
for res := filebuf.Scan(); res; res = filebuf.Scan() {
buff := filebuf.Text()
if (buff == "# rhosts begin"){
if buff == "# rhosts begin" {
break
}
_, err := file.WriteString(buff + "\n")
if (err != nil) {
if err != nil {
log.Print(err)
return err
}
@ -87,6 +88,7 @@ func copystatichosts(tmpdir, hostsloc string) error {
err = filebuf.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.
func downloadcontent(downloads []string, tmpdir string, hostsloc string) (err error, list []siteList) {
for _, d := range downloads {
@ -94,7 +96,7 @@ func downloadcontent(downloads []string, tmpdir string, hostsloc string) (err er
site.location = d
log.Print("Downloading: ", d)
response, err := http.Get(d)
if (err !=nil) {
if err != nil {
log.Print(err)
log.Print("Looking for old record in hosts file")
downloadoldlookup(hostsloc, d, &site)
@ -120,7 +122,7 @@ func checkDownloadLine (line string) (address siteEntry){
address.site = ""
buff := ""
lineLength := len(line) - 1
for i, c := range(line){
for i, c := range line {
if c != ' ' && i < lineLength {
buff += string(c)
} else if len(buff) > 0 {
@ -137,12 +139,12 @@ func checkDownloadLine (line string) (address siteEntry){
if token[0][0] == '#' {
return
}
for _, t := range(token) {
for _, t := range token {
var period uint
var failed bool
period = 0
failed = false
for _, c := range(t) {
for _, c := range t {
switch c {
case '.':
period++
@ -167,7 +169,7 @@ func downloadoldlookup(hostsloc, d string, site *siteList) error {
var state uint8 = 0
hostsf, err := os.Open(hostsloc)
if (err != nil){
if err != nil {
log.Print(err)
return err
}
@ -179,12 +181,12 @@ func downloadoldlookup(hostsloc, d string, site *siteList) error {
buff := fbuff.Text()
switch state {
case 0:
if (buff == "# rhosts download - " + d){
if buff == "# rhosts download - "+d {
log.Print("Found old record in hosts file:" + buff)
state = 1
}
case 1:
if (len(buff) >=9 && buff[0:8] == "# rhosts"){
if len(buff) >= 9 && buff[0:8] == "# rhosts" {
state = 2
} else {
siteBuff := checkDownloadLine(buff)
@ -220,6 +222,7 @@ func writesites(sites []string, tmpdir string, siteBuff *[]siteList) (err error)
*siteBuff = append(*siteBuff, localList)
return
}
// removeduplicates removes any duplicate or uneeded/unwanted addresses
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"}
@ -248,8 +251,8 @@ func removeduplicates(siteBuff *[]siteList, whitelist *[]string){
}
}
lenEntry := len(entry)
for i,e := range(entry) {
for _,w := range(safewords){
for i, e := range entry {
for _, w := range safewords {
if *e.s == w {
*(entry[i].r) = true
c.s++
@ -259,7 +262,7 @@ func removeduplicates(siteBuff *[]siteList, whitelist *[]string){
if *(entry[i].r) == true {
continue
}
for _,w := range(*whitelist){
for _, w := range *whitelist {
if *e.s == w {
*(entry[i].r) = true
c.w++
@ -272,7 +275,7 @@ func removeduplicates(siteBuff *[]siteList, whitelist *[]string){
if i == lenEntry {
break
}
for j,n := range(entry[i+1:]){
for j, n := range entry[i+1:] {
if *e.s == *n.s {
*(entry[i+j].r) = true
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)
}
// write2tmp write the siteBuff to the tempfile
func write2tmp(tmpdir string, siteBuff *[]siteList) (err error) {
err = nil
@ -292,7 +296,7 @@ func write2tmp(tmpdir string, siteBuff *[]siteList) (err error) {
log.Print(err)
return err
}
for _,location := range(*siteBuff){
for _, location := range *siteBuff {
if len(location.siteEntry) == 0 {
continue
}
@ -300,7 +304,7 @@ func write2tmp(tmpdir string, siteBuff *[]siteList) (err error) {
if err != nil {
return err
}
for _,site := range(location.siteEntry){
for _, site := range location.siteEntry {
if site.repeat == false {
_, err = tmpf.WriteString("0.0.0.0 " + site.site + "\n")
if err != nil {
@ -315,26 +319,26 @@ func write2tmp(tmpdir string, siteBuff *[]siteList) (err error) {
}
return
}
// writetmp2hosts overwrites the hostsfile with the tmp file
func writetmp2hosts(hostsloc, tmpdir string) error {
var err error = nil
tmploc := tmpdir + "rhosts"
hosts, err := os.Create(hostsloc)
if (err != nil){
if err != nil {
log.Print(err)
return err
}
tmp, err := os.Open(tmploc)
if (err != nil){
if err != nil {
log.Print(err)
return err
}
_, err = io.Copy(hosts, tmp)
if (err != nil){
if err != nil {
log.Print(err)
}
return err
}

View File

@ -17,24 +17,22 @@
* along with rhosts. If not, see <https://www.gnu.org/licenses/>.
*/
// rhosts - Program used to maintain a blocklist appended to a host file
package main
import (
"log"
"flag"
"fmt"
"time"
sysos "jbreich/rhosts/sys"
"jbreich/rhosts/serve"
"jbreich/rhosts/cfg"
"jbreich/rhosts/hosts"
"jbreich/rhosts/serve"
sysos "jbreich/rhosts/sys"
"log"
"time"
)
var Exit chan bool
const GPL = `
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/>.
`
func main() {
tmpdir := ""
hostsloc := ""
@ -92,30 +89,42 @@ func main() {
sysos.Detect(&tmpdir, &hostsloc, &cfgloc)
// Read the config file
config := cfg.Create(cfgloc)
err, config := config.Update()
log.Print(config)
if (err != nil){log.Panic("Failed to parse config: " + cfgloc)}
if err != nil {
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 {
err := hosts.Update(config, tmpdir, hostsloc)
if (err != nil){
if err != nil {
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)
log.Printf("Sleeping for %d minutes", interval)
log.Print("Should restart at: " + i)
time.Sleep(time.Duration(interval) * time.Minute)
}else{
break
}
}
serve.Start("blank")
}
<-Exit
}