/*
* Copyright 2021 Justin Reichardt
*
* This file is part of rhosts.
*
* rhosts is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* rhosts is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with rhosts. If not, see .
*/
// rhosts - Program used to maintain a blocklist within a hostfile
package main
import (
"runtime"
"os"
"io"
"bufio"
"log"
"net/http"
"flag"
"time"
)
func main() {
tmpdir := ""
hostsloc := ""
cfgloc := ""
i := true
var daemon bool=false
var interval int=1440
// Parsing Flags
flag.BoolVar(&daemon, "d", false, "Should this be run in daemon mode")
flag.IntVar(&interval, "t", 1440, "Minutes until next run of daemon")
flag.Parse()
log.Print("daemon:" , daemon)
log.Print("interval:",interval)
sysdetect (&tmpdir, &hostsloc, &cfgloc)
for i == true {
err := error(nil)
i = daemon
sites, downloads, err := cfgparse(cfgloc)
if (err != nil){
log.Print("Failed to parse config file")
continue
}
err = copystatichosts(tmpdir, hostsloc)
if (err != nil){
log.Print("Failed to copy static entries")
continue
}
err = downloadcontent(downloads, tmpdir)
if (err != nil){
log.Print("Failed to download entries")
continue
}
err = writesites(sites, tmpdir)
if (err != nil){
log.Print("Failed to failed to copy rhosts static entries")
continue
}
err = writetmp2hosts(hostsloc, tmpdir)
if (err != nil){
log.Print("Failed to copy to hosts file")
continue
}
log.Print("Finished updating host")
if (i == true){
time.Sleep(time.Duration(interval) * time.Minute)
}
}
}
func sysdetect (tmpdir, hostsloc, cfgloc *string) {
// Detect OS and set params
switch runtime.GOOS {
case "windows":
log.Fatal("Windows is not supported")
*tmpdir = "C:\\tmp"
*hostsloc = "C:\\Windows\\System32\\drivers\\etc\\hosts"
case "linux":
*tmpdir = "/tmp/"
*hostsloc = "/etc/hosts"
*cfgloc ="/etc/rhosts/rhosts.cfg"
case "ios":
log.Fatal("IOS is not supported")
default:
log.Fatal(runtime.GOOS," is not supported")
}
}
func cfgparse (cfgloc string) ([]string, []string, error){
var err error=nil
var downloads []string
var sites []string
log.Print("Opening: ", cfgloc)
file, err := os.Open(cfgloc)
defer file.Close()
if err != nil {
log.Print(err)
return nil, nil,err
}
filebuf := bufio.NewScanner(file)
filebuf.Split(bufio.ScanLines)
for res := filebuf.Scan();res;res = filebuf.Scan() {
state, body := cfgparseline(filebuf.Text())
switch state {
case 3:
sites =append(sites,body)
case 4:
downloads = append(downloads,body)
}
}
err = filebuf.Err()
if err != nil {
log.Print(err)
return nil, nil, err
}
return sites, downloads, err
}
func cfgparseline(buf string) (uint8, string){
// State options
// 0 - Init
// 1 - Error
// 2 - Comment
// 3 - Site
// 4 - Download
var state uint8= 0
body :=buf[:]
for i:=0; i