Filters out duplicates and comments

Creates a list of only the sites that can then be used to remove
duplicates and then set the sending address, currently 0.0.0.0
stable
Justin Reichardt 2022-02-23 21:22:12 -06:00
parent 01584ea27a
commit 6a5b2b7363
1 changed files with 174 additions and 56 deletions

View File

@ -31,6 +31,16 @@ import (
"flag"
"time"
)
// siteList holds the location of all the sites along with a list of their location
type siteList struct {
location string
siteEntry []siteEntry
}
// siteEntry holds a single entry and if it is a repeat
type siteEntry struct {
repeat bool
site string
}
func main() {
tmpdir := ""
@ -38,6 +48,7 @@ func main() {
cfgloc := ""
var daemon bool=false
var interval int=1440
var siteBuff []siteList
// Parsing Flags
flag.BoolVar(&daemon, "d", false, "Should this be run in daemon mode")
@ -61,21 +72,28 @@ func main() {
continue
}
defer os.Remove(tmpdir + "rhosts")
err = downloadcontent(downloads, tmpdir, hostsloc)
err, siteBuff = downloadcontent(downloads, tmpdir, hostsloc)
if (err != nil){
log.Print("Failed to download entries")
continue
}
err = writesites(sites, tmpdir)
err = writesites(sites, tmpdir, &siteBuff)
if (err != nil){
log.Print("Failed to failed to copy rhosts static entries")
continue
}
removeduplicates(&siteBuff)
err = write2tmp(tmpdir, &siteBuff)
if (err != nil){
log.Print("Failed to write sites to tmpfile")
continue
}
err = writetmp2hosts(hostsloc, tmpdir)
if (err != nil){
log.Print("Failed to copy to hosts file")
continue
}
log.Print("Finished updating host")
if (daemon == true){
time.Sleep(time.Duration(interval) * time.Minute)
@ -216,49 +234,82 @@ func copystatichosts(tmpdir, hostsloc string) error {
err = filebuf.Err()
return err
}
// downloadcontent attempts to download the provided url to the temp file. 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) error{
var err error=nil
fileloc := tmpdir + "rhosts"
log.Print("Opening: ", fileloc)
file,err := os.OpenFile(fileloc, os.O_APPEND|os.O_WRONLY, 0644)
if (err != nil) {
log.Print(err)
return err
}
defer file.Close()
// 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 {
var site siteList
site.location = d
log.Print("Downloading: ",d)
file.WriteString("# rhosts download - " + d + "\n")
if (err != nil) {
}
response, err := http.Get(d)
if (err !=nil) {
log.Print(err)
log.Print("Looking for old record in hosts file")
downloadoldlookup(file, hostsloc, d)
continue
downloadoldlookup(hostsloc, d, &site)
}else{
_,err := io.Copy(file,response.Body)
if (err != nil){
log.Print(err)
return err
defer response.Body.Close()
scanner := bufio.NewScanner(response.Body)
for scanner.Scan() {
resp := checkDownloadLine(scanner.Text())
if resp.site != "" {
site.siteEntry = append(site.siteEntry, resp)
}
}
}
defer response.Body.Close()
file.WriteString("\n")
if (err != nil) {
log.Print(err)
return err
}
list = append(list, site)
}
return err
return err, list
}
// downloadoldlookup attemps to find an entry in the hosts file and write it to the temp file.
func downloadoldlookup(file *os.File, hostsloc, d string) error {
// checkDownloadLine parses the download line into just the address that needs to be blocked
func checkDownloadLine (line string) (address siteEntry){
var token []string
address.repeat = false
address.site = ""
buff := ""
lineLength := len(line) -1
for i, c := range(line){
if c != ' ' && i < lineLength {
buff += string(c)
}else if len(buff) > 0 {
if i == lineLength{
buff += string(c)
}
token = append(token,buff)
buff = ""
}
}
if len(token) == 0 {
return
}
if token[0][0] == '#' {
return
}
for _, t := range(token) {
var period uint
var failed bool
period = 0
failed = false
for _, c := range(t) {
switch c{
case '.':
period ++
case '#':
return
case ':':
failed = true
break
}
}
if period <=2 && failed == false {
address.site = t
return
}
}
return
}
// downloadoldlookup attemps to find an entry in the hosts file and add it to the siteList
func downloadoldlookup(hostsloc, d string, site *siteList) error {
var err error = nil
var state uint8 = 0
@ -283,10 +334,9 @@ func downloadoldlookup(file *os.File, hostsloc, d string) error {
if (len(buff) >=9 && buff[0:8] == "# rhosts"){
state = 2
}else{
_,err := file.WriteString(buff + "\n")
if (err != nil) {
log.Print(err)
return err
siteBuff := checkDownloadLine(buff)
if siteBuff.site != "" {
site.siteEntry = append(site.siteEntry, siteBuff)
}
}
case 3:
@ -298,32 +348,100 @@ func downloadoldlookup(file *os.File, hostsloc, d string) error {
return err
}
// writesites writes the list of sites from the config file to the temp file
func writesites(sites []string, tmpdir string) error {
var err error = nil
// writesites writes the list of sites from the config file to the local siteList
func writesites(sites []string, tmpdir string, siteBuff *[]siteList) (err error) {
var localList siteList
localList.location = "local"
err = nil
fileloc := tmpdir + "rhosts"
log.Print("Opening: " + fileloc)
file,err := os.OpenFile(fileloc, os.O_APPEND|os.O_WRONLY, 0644)
defer file.Close()
if (err != nil) {
log.Print(err)
return err
}
_,err = file.WriteString("# rhosts sites\n")
if (err != nil){
log.Print(err)
return err
if len(sites) == 0 {
return
}
for _,s := range sites {
_,err = file.WriteString(s)
if (err != nil){
log.Print(err)
break
var site siteEntry
site.repeat = false
site.site = s
localList.siteEntry = append(localList.siteEntry,site)
}
*siteBuff = append(*siteBuff,localList)
return
}
// removeduplicates removes any duplicate addresses
func removeduplicates(siteBuff *[]siteList){
var safewords = []string{"localhost", "localhost.localdomain", "broadcasthost", "ip6-loopback", "ip6-localhost", "ip6-localnet", "ip6-mcastprefix", "ip6-allnodes", "ip6-allrouters", "ip6-allhosts", "local"}
var counter = uint(0)
log.Print("Checking for duplicates")
var entry []struct{
r *bool
s *string
}
var entryBuff struct{
r *bool
s *string
}
_ = entry
for i := len((*siteBuff))-1; i > -1; i --{
for j := len((*siteBuff)[i].siteEntry)-1; j > -1; j -- {
entryBuff.r = &((*siteBuff)[i].siteEntry[j].repeat)
entryBuff.s = &((*siteBuff)[i].siteEntry[j].site)
entry = append(entry,entryBuff)
}
}
return err
}
lenEntry := len(entry)
for i,e := range(entry) {
for _,w := range(safewords){
if *e.s == w {
*(entry[i].r) = true
counter ++
break
}
}
if *(entry[i].r) == true {
continue
}
if i == lenEntry {
break
}
for j,n := range(entry[i+1:]){
if *e.s == *n.s || *e.s == "localhost" {
*(entry[i+j].r) = true
counter ++
}
}
}
log.Printf("Removed %d uneeded entries\n", counter)
}
// write2tmp write the siteBuff to the tempfile
func write2tmp(tmpdir string, siteBuff *[]siteList) (err error) {
err = nil
tmploc := tmpdir+ "rhosts"
tmpf, err := os.OpenFile(tmploc, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
defer tmpf.Close()
if err != nil {
log.Print(err)
return err
}
for _,location := range(*siteBuff){
if len(location.siteEntry) == 0 {
continue
}
_,err := tmpf.WriteString("# rhosts download - " + location.location + "\n")
if err != nil {
return err
}
for _,site := range(location.siteEntry){
if site.repeat == false {
_, err = tmpf.WriteString("0.0.0.0 " + site.site + "\n")
if err != nil {
return err
}
}
}
}
return
}
// writetmp2hosts overwrites the hostsfile with the tmp file
func writetmp2hosts(hostsloc, tmpdir string) error {
var err error = nil