Performed some cleanup
Mostly go formatting but a little creative spacing and commentstesting
parent
77c020e434
commit
e1c576f2a1
|
|
@ -1,9 +1,9 @@
|
||||||
package cfg
|
package cfg
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"bufio"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const CFG = `
|
const CFG = `
|
||||||
|
|
@ -24,33 +24,33 @@ const CFG = `
|
||||||
#download=https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts`
|
#download=https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts`
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
CfgLoc string
|
CfgLoc string
|
||||||
Sites []string
|
Sites []string
|
||||||
Downloads []string
|
Downloads []string
|
||||||
Whitelist []string
|
Whitelist []string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create initialized a config to be used the entire session
|
// Create initialized a config to be used the entire session
|
||||||
func Create(cfgLoc string)(cfg Config){
|
func Create(cfgLoc string) (cfg Config) {
|
||||||
cfg.CfgLoc = cfgLoc
|
cfg.CfgLoc = cfgLoc
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// cfgparse recieves the location of the config file and returns a list of sites to add and content to download
|
// cfgparse recieves the location of the config file and returns a list of sites to add and content to download
|
||||||
func (cfg Config) Update() (error, Config){
|
func (cfg Config) Update() (error, Config) {
|
||||||
l := (cfg.CfgLoc + "rhosts.cfg")
|
l := (cfg.CfgLoc + "rhosts.cfg")
|
||||||
var err error=nil
|
var err error = nil
|
||||||
log.Print("Opening: ", l)
|
log.Print("Opening: ", l)
|
||||||
if _,err = os.Stat(cfg.CfgLoc); os.IsNotExist(err) {
|
if _, err = os.Stat(cfg.CfgLoc); os.IsNotExist(err) {
|
||||||
log.Print(cfg.CfgLoc + " Does not exist, attempting to create it")
|
log.Print(cfg.CfgLoc + " Does not exist, attempting to create it")
|
||||||
err = os.MkdirAll(cfg.CfgLoc,0755)
|
err = os.MkdirAll(cfg.CfgLoc, 0755)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Could not create " + cfg.CfgLoc)
|
log.Fatal("Could not create " + cfg.CfgLoc)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if _,err = os.Stat(l); os.IsNotExist(err) {
|
if _, err = os.Stat(l); os.IsNotExist(err) {
|
||||||
log.Print(l + " does not exist, attempting to create a placeholder")
|
log.Print(l + " does not exist, attempting to create a placeholder")
|
||||||
err = os.WriteFile(l,[]byte(CFG),0644)
|
err = os.WriteFile(l, []byte(CFG), 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Unable to create file: " + l)
|
log.Fatal("Unable to create file: " + l)
|
||||||
}
|
}
|
||||||
|
|
@ -63,16 +63,16 @@ func (cfg Config) Update() (error, Config){
|
||||||
}
|
}
|
||||||
filebuf := bufio.NewScanner(file)
|
filebuf := bufio.NewScanner(file)
|
||||||
filebuf.Split(bufio.ScanLines)
|
filebuf.Split(bufio.ScanLines)
|
||||||
for res := filebuf.Scan();res;res = filebuf.Scan() {
|
for res := filebuf.Scan(); res; res = filebuf.Scan() {
|
||||||
state, body := cfgparseline(filebuf.Text())
|
state, body := cfgparseline(filebuf.Text())
|
||||||
switch state {
|
switch state {
|
||||||
case 3:
|
case 3:
|
||||||
cfg.Sites =append(cfg.Sites,body)
|
cfg.Sites = append(cfg.Sites, body)
|
||||||
case 4:
|
case 4:
|
||||||
cfg.Downloads = append(cfg.Downloads,body)
|
cfg.Downloads = append(cfg.Downloads, body)
|
||||||
case 5:
|
case 5:
|
||||||
cfg.Whitelist = append(cfg.Whitelist,body)
|
cfg.Whitelist = append(cfg.Whitelist, body)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
err = filebuf.Err()
|
err = filebuf.Err()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -83,7 +83,7 @@ func (cfg Config) Update() (error, Config){
|
||||||
}
|
}
|
||||||
|
|
||||||
// cfgparseline reads a single line of the config and returns the type and content of the line
|
// cfgparseline reads a single line of the config and returns the type and content of the line
|
||||||
func cfgparseline(buf string) (uint8, string){
|
func cfgparseline(buf string) (uint8, string) {
|
||||||
// State options
|
// State options
|
||||||
// 0 - Init
|
// 0 - Init
|
||||||
// 1 - Error
|
// 1 - Error
|
||||||
|
|
@ -91,54 +91,54 @@ func cfgparseline(buf string) (uint8, string){
|
||||||
// 3 - Site
|
// 3 - Site
|
||||||
// 4 - Download
|
// 4 - Download
|
||||||
// 5 - Whitelist
|
// 5 - Whitelist
|
||||||
var state uint8= 0
|
var state uint8 = 0
|
||||||
body :=buf[:]
|
body := buf[:]
|
||||||
for i:=0; i<len(buf);i++ {
|
for i := 0; i < len(buf); i++ {
|
||||||
//fmt.Printf("%c",buf[i])
|
//fmt.Printf("%c",buf[i])
|
||||||
switch buf[i] {
|
switch buf[i] {
|
||||||
case ' ':
|
case ' ':
|
||||||
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:]
|
||||||
} else{
|
} else {
|
||||||
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:]
|
||||||
} else{
|
} else {
|
||||||
state = 0
|
state = 0
|
||||||
}
|
}
|
||||||
//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:]
|
||||||
} else{
|
} else {
|
||||||
state = 1
|
state = 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (state !=0){
|
if state != 0 {
|
||||||
return state,body
|
return state, body
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return state, body
|
return state, body
|
||||||
|
|
|
||||||
|
|
@ -1,57 +1,58 @@
|
||||||
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
|
||||||
type siteList struct {
|
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
|
||||||
site string
|
site string
|
||||||
}
|
}
|
||||||
|
|
||||||
func Update(config cfg.Config, tmpdir, hostsloc string)(err error){
|
func Update(config cfg.Config, tmpdir, hostsloc string) (err error) {
|
||||||
var siteBuff []siteList
|
var siteBuff []siteList
|
||||||
|
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -72,33 +73,34 @@ func copystatichosts(tmpdir, hostsloc string) error {
|
||||||
}
|
}
|
||||||
filebuf := bufio.NewScanner(filer)
|
filebuf := bufio.NewScanner(filer)
|
||||||
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_,err = file.WriteString("# rhosts begin\n")
|
_, err = file.WriteString("# rhosts begin\n")
|
||||||
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 {
|
||||||
var site siteList
|
var site siteList
|
||||||
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)
|
||||||
}else{
|
} else {
|
||||||
defer response.Body.Close()
|
defer response.Body.Close()
|
||||||
scanner := bufio.NewScanner(response.Body)
|
scanner := bufio.NewScanner(response.Body)
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
|
|
@ -114,22 +116,22 @@ func downloadcontent(downloads []string, tmpdir string, hostsloc string) (err er
|
||||||
}
|
}
|
||||||
|
|
||||||
// checkDownloadLine parses the download line into just the address that needs to be blocked
|
// checkDownloadLine parses the download line into just the address that needs to be blocked
|
||||||
func checkDownloadLine (line string) (address siteEntry){
|
func checkDownloadLine(line string) (address siteEntry) {
|
||||||
var token []string
|
var token []string
|
||||||
address.repeat = false
|
address.repeat = false
|
||||||
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 {
|
||||||
if i == lineLength{
|
if i == lineLength {
|
||||||
buff += string(c)
|
buff += string(c)
|
||||||
}
|
}
|
||||||
token = append(token,buff)
|
token = append(token, buff)
|
||||||
buff = ""
|
buff = ""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(token) == 0 {
|
if len(token) == 0 {
|
||||||
return
|
return
|
||||||
|
|
@ -137,15 +139,15 @@ 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++
|
||||||
case '#':
|
case '#':
|
||||||
return
|
return
|
||||||
case ':':
|
case ':':
|
||||||
|
|
@ -153,7 +155,7 @@ func checkDownloadLine (line string) (address siteEntry){
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if period <=2 && failed == false {
|
if period <= 2 && failed == false {
|
||||||
address.site = t
|
address.site = t
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -165,9 +167,9 @@ func checkDownloadLine (line string) (address siteEntry){
|
||||||
func downloadoldlookup(hostsloc, d string, site *siteList) error {
|
func downloadoldlookup(hostsloc, d string, site *siteList) error {
|
||||||
var err error = nil
|
var err error = nil
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
@ -175,18 +177,18 @@ func downloadoldlookup(hostsloc, d string, site *siteList) error {
|
||||||
|
|
||||||
fbuff := bufio.NewScanner(hostsf)
|
fbuff := bufio.NewScanner(hostsf)
|
||||||
fbuff.Split(bufio.ScanLines)
|
fbuff.Split(bufio.ScanLines)
|
||||||
for res := fbuff.Scan();res;res = fbuff.Scan() {
|
for res := fbuff.Scan(); res; res = fbuff.Scan() {
|
||||||
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)
|
||||||
if siteBuff.site != "" {
|
if siteBuff.site != "" {
|
||||||
site.siteEntry = append(site.siteEntry, siteBuff)
|
site.siteEntry = append(site.siteEntry, siteBuff)
|
||||||
|
|
@ -195,7 +197,7 @@ func downloadoldlookup(hostsloc, d string, site *siteList) error {
|
||||||
case 3:
|
case 3:
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return err
|
return err
|
||||||
|
|
@ -211,17 +213,18 @@ func writesites(sites []string, tmpdir string, siteBuff *[]siteList) (err error)
|
||||||
if len(sites) == 0 {
|
if len(sites) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for _,s := range sites {
|
for _, s := range sites {
|
||||||
var site siteEntry
|
var site siteEntry
|
||||||
site.repeat = false
|
site.repeat = false
|
||||||
site.site = s
|
site.site = s
|
||||||
localList.siteEntry = append(localList.siteEntry,site)
|
localList.siteEntry = append(localList.siteEntry, site)
|
||||||
}
|
}
|
||||||
*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"}
|
||||||
var c struct {
|
var c struct {
|
||||||
d uint
|
d uint
|
||||||
|
|
@ -232,37 +235,37 @@ func removeduplicates(siteBuff *[]siteList, whitelist *[]string){
|
||||||
c.s = 0
|
c.s = 0
|
||||||
c.w = 0
|
c.w = 0
|
||||||
log.Print("Checking for duplicates")
|
log.Print("Checking for duplicates")
|
||||||
var entry []struct{
|
var entry []struct {
|
||||||
r *bool
|
r *bool
|
||||||
s *string
|
s *string
|
||||||
}
|
}
|
||||||
var entryBuff struct{
|
var entryBuff struct {
|
||||||
r *bool
|
r *bool
|
||||||
s *string
|
s *string
|
||||||
}
|
}
|
||||||
for i := len((*siteBuff))-1; i > -1; i --{
|
for i := len((*siteBuff)) - 1; i > -1; i-- {
|
||||||
for j := len((*siteBuff)[i].siteEntry)-1; j > -1; j -- {
|
for j := len((*siteBuff)[i].siteEntry) - 1; j > -1; j-- {
|
||||||
entryBuff.r = &((*siteBuff)[i].siteEntry[j].repeat)
|
entryBuff.r = &((*siteBuff)[i].siteEntry[j].repeat)
|
||||||
entryBuff.s = &((*siteBuff)[i].siteEntry[j].site)
|
entryBuff.s = &((*siteBuff)[i].siteEntry[j].site)
|
||||||
entry = append(entry,entryBuff)
|
entry = append(entry, entryBuff)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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++
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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++
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -272,35 +275,36 @@ 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++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
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
|
||||||
tmploc := tmpdir+ "rhosts"
|
tmploc := tmpdir + "rhosts"
|
||||||
tmpf, err := os.OpenFile(tmploc, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
|
tmpf, err := os.OpenFile(tmploc, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
|
||||||
defer tmpf.Close()
|
defer tmpf.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
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
|
||||||
}
|
}
|
||||||
_,err := tmpf.WriteString("# rhosts download - " + location.location + "\n")
|
_, err := tmpf.WriteString("# rhosts download - " + location.location + "\n")
|
||||||
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,25 +17,23 @@
|
||||||
* 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
|
||||||
|
|
||||||
Copyright (C) 2021 Justin Reichardt
|
Copyright (C) 2021 Justin Reichardt
|
||||||
|
|
@ -54,15 +52,14 @@ 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 := ""
|
||||||
cfgloc := ""
|
cfgloc := ""
|
||||||
var daemon bool=false
|
var daemon bool = false
|
||||||
var interval int=1440
|
var interval int = 1440
|
||||||
var versionflag bool=false
|
var versionflag bool = false
|
||||||
var removetimestamp bool=false
|
var removetimestamp bool = false
|
||||||
|
|
||||||
// Parsing Flags
|
// Parsing Flags
|
||||||
flag.BoolVar(&daemon, "d", false, "Should this be run in daemon mode")
|
flag.BoolVar(&daemon, "d", false, "Should this be run in daemon mode")
|
||||||
|
|
@ -80,42 +77,54 @@ func main() {
|
||||||
// Check if timestamp should be removed
|
// Check if timestamp should be removed
|
||||||
if removetimestamp {
|
if removetimestamp {
|
||||||
log.SetFlags(0)
|
log.SetFlags(0)
|
||||||
}else{
|
} else {
|
||||||
// GPL information
|
// GPL information
|
||||||
fmt.Println(GPL)
|
fmt.Println(GPL)
|
||||||
}
|
}
|
||||||
|
|
||||||
if daemon {
|
if daemon {
|
||||||
log.Print("daemon:" , daemon)
|
log.Print("daemon:", daemon)
|
||||||
log.Print("interval:",interval)
|
log.Print("interval:", interval)
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
for true {
|
if daemon == false {
|
||||||
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")
|
} else {
|
||||||
if (daemon == true){
|
|
||||||
i := time.Now().Add(time.Duration(interval) * time.Minute).Format(time.Layout)
|
for true {
|
||||||
log.Printf("Sleeping for %d minutes", interval)
|
err := hosts.Update(config, tmpdir, hostsloc)
|
||||||
log.Print("Should restart at: " + i)
|
if err != nil {
|
||||||
time.Sleep(time.Duration(interval) * time.Minute)
|
log.Print(err)
|
||||||
}else{
|
}
|
||||||
break
|
|
||||||
|
// 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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
serve.Start("blank")
|
<-Exit
|
||||||
<- Exit
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
// Provides the web server for rhosts to relay altered content
|
// Provides the web server for rhosts to relay altered content
|
||||||
package serve
|
package serve
|
||||||
|
|
||||||
import(
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -11,15 +11,15 @@ func Start(certLoc string) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func httpServer() (err error){
|
func httpServer() (err error) {
|
||||||
err = http.ListenAndServe("127.0.0.1:80",http.HandlerFunc(httpHandler))
|
err = http.ListenAndServe("127.0.0.1:80", http.HandlerFunc(httpHandler))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
func httpsServer(certLoc *string) (err error){
|
func httpsServer(certLoc *string) (err error) {
|
||||||
err = http.ListenAndServeTLS("127.0.0.1:80",*certLoc + "ca.crt", *certLoc + "ca.key",http.HandlerFunc(httpHandler))
|
err = http.ListenAndServeTLS("127.0.0.1:80", *certLoc+"ca.crt", *certLoc+"ca.key", http.HandlerFunc(httpHandler))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func httpHandler(w http.ResponseWriter, r *http.Request) {
|
func httpHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
http.Error(w,"Test",200)
|
http.Error(w, "Test", 200)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue