Make sure non one-shot executions perform reloads.
Allow for instant ^C without waiting for sleep.
This commit is contained in:
parent
cfbf667389
commit
a90ef84368
@ -62,6 +62,9 @@ func main() {
|
|||||||
sigs := make(chan os.Signal, 1)
|
sigs := make(chan os.Signal, 1)
|
||||||
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
||||||
|
|
||||||
|
//Finished notifier:
|
||||||
|
finNotf := make(chan bool, 1)
|
||||||
|
|
||||||
z := time.Now().Sub(y)
|
z := time.Now().Sub(y)
|
||||||
log.Printf("[Main] Took '%s' to fully initialize modules\n", z.String())
|
log.Printf("[Main] Took '%s' to fully initialize modules\n", z.String())
|
||||||
|
|
||||||
@ -69,9 +72,19 @@ func main() {
|
|||||||
syncDur := time.Duration(syncTime) * time.Millisecond
|
syncDur := time.Duration(syncTime) * time.Millisecond
|
||||||
|
|
||||||
go func(exec *bool) {
|
go func(exec *bool) {
|
||||||
|
executePersistence(hostsFile, sourceFile, overwriteMode)
|
||||||
|
sleep(syncDur, finNotf)
|
||||||
for *exec {
|
for *exec {
|
||||||
|
err := hostsFile.ReadHostsFile()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln("Failed to load HOSTS_FILE")
|
||||||
|
}
|
||||||
|
err = sourceFile.ReadHostsFile()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln("Failed to load SOURCE_FILE")
|
||||||
|
}
|
||||||
executePersistence(hostsFile, sourceFile, overwriteMode)
|
executePersistence(hostsFile, sourceFile, overwriteMode)
|
||||||
time.Sleep(syncDur)
|
sleep(syncDur, finNotf)
|
||||||
}
|
}
|
||||||
}(&exec)
|
}(&exec)
|
||||||
|
|
||||||
@ -80,6 +93,7 @@ func main() {
|
|||||||
fmt.Printf("\n")
|
fmt.Printf("\n")
|
||||||
|
|
||||||
*exec = false
|
*exec = false
|
||||||
|
finNotf <- true
|
||||||
|
|
||||||
a := time.Now()
|
a := time.Now()
|
||||||
log.Printf("[Main] Signalling program exit...\n")
|
log.Printf("[Main] Signalling program exit...\n")
|
||||||
@ -107,3 +121,12 @@ func executePersistence(hostsFile *hosts.File, sourceFile *hosts.File, ovrw bool
|
|||||||
log.Println("[Main] Error Writing Hosts File.")
|
log.Println("[Main] Error Writing Hosts File.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func sleep(d time.Duration, n chan bool) {
|
||||||
|
select {
|
||||||
|
case <-n:
|
||||||
|
break
|
||||||
|
case <-time.After(d):
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -9,52 +9,14 @@ import (
|
|||||||
const readBufferSize = 8192
|
const readBufferSize = 8192
|
||||||
|
|
||||||
func NewHostsFile(filePath string) (*File, error) {
|
func NewHostsFile(filePath string) (*File, error) {
|
||||||
theFile, err := os.Open(filePath)
|
theHostFile := &File{
|
||||||
if err != nil {
|
filePath: filePath,
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
defer theFile.Close()
|
err := theHostFile.ReadHostsFile()
|
||||||
var theEntries []Entry
|
if err == nil {
|
||||||
var lenIn int
|
return theHostFile, nil
|
||||||
lineEnding := ""
|
|
||||||
theCBuffer := ""
|
|
||||||
theBuffer := make([]byte, readBufferSize)
|
|
||||||
for err == nil {
|
|
||||||
lenIn, err = theFile.Read(theBuffer)
|
|
||||||
if lenIn > 0 {
|
|
||||||
theCBuffer += string(theBuffer[:lenIn])
|
|
||||||
if lineEnding == "" {
|
|
||||||
if strings.Contains(theCBuffer, "\r\n") {
|
|
||||||
lineEnding = "\r\n"
|
|
||||||
} else if strings.Contains(theCBuffer, "\r") {
|
|
||||||
lineEnding = "\r"
|
|
||||||
} else if strings.Contains(theCBuffer, "\n") {
|
|
||||||
lineEnding = "\n"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if lineEnding == "\r\n" {
|
|
||||||
strings.ReplaceAll(theCBuffer, "\r\n", "\n")
|
|
||||||
} else if lineEnding == "\r" {
|
|
||||||
strings.ReplaceAll(theCBuffer, "\r", "\n")
|
|
||||||
}
|
|
||||||
splt := strings.Split(theCBuffer, "\n")
|
|
||||||
for i := 0; i < len(splt)-1; i++ {
|
|
||||||
theEntries = append(theEntries, NewHostsEntry(splt[i]))
|
|
||||||
}
|
|
||||||
theCBuffer = splt[len(splt)-1]
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if err != io.EOF {
|
return nil, err
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if theCBuffer != "" {
|
|
||||||
theEntries = append(theEntries, NewHostsEntry(theCBuffer))
|
|
||||||
}
|
|
||||||
return &File{
|
|
||||||
filePath: filePath,
|
|
||||||
Entries: theEntries,
|
|
||||||
lineEnding: lineEnding,
|
|
||||||
}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type File struct {
|
type File struct {
|
||||||
@ -63,6 +25,51 @@ type File struct {
|
|||||||
lineEnding string
|
lineEnding string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (f *File) ReadHostsFile() error {
|
||||||
|
f.Entries = nil
|
||||||
|
theFile, err := os.Open(f.filePath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer theFile.Close()
|
||||||
|
var lenIn int
|
||||||
|
f.lineEnding = ""
|
||||||
|
theCBuffer := ""
|
||||||
|
theBuffer := make([]byte, readBufferSize)
|
||||||
|
for err == nil {
|
||||||
|
lenIn, err = theFile.Read(theBuffer)
|
||||||
|
if lenIn > 0 {
|
||||||
|
theCBuffer += string(theBuffer[:lenIn])
|
||||||
|
if f.lineEnding == "" {
|
||||||
|
if strings.Contains(theCBuffer, "\r\n") {
|
||||||
|
f.lineEnding = "\r\n"
|
||||||
|
} else if strings.Contains(theCBuffer, "\r") {
|
||||||
|
f.lineEnding = "\r"
|
||||||
|
} else if strings.Contains(theCBuffer, "\n") {
|
||||||
|
f.lineEnding = "\n"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if f.lineEnding == "\r\n" {
|
||||||
|
strings.ReplaceAll(theCBuffer, "\r\n", "\n")
|
||||||
|
} else if f.lineEnding == "\r" {
|
||||||
|
strings.ReplaceAll(theCBuffer, "\r", "\n")
|
||||||
|
}
|
||||||
|
splt := strings.Split(theCBuffer, "\n")
|
||||||
|
for i := 0; i < len(splt)-1; i++ {
|
||||||
|
f.Entries = append(f.Entries, NewHostsEntry(splt[i]))
|
||||||
|
}
|
||||||
|
theCBuffer = splt[len(splt)-1]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err != io.EOF {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if theCBuffer != "" {
|
||||||
|
f.Entries = append(f.Entries, NewHostsEntry(theCBuffer))
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (f File) WriteHostsFile() error {
|
func (f File) WriteHostsFile() error {
|
||||||
theFile, err := os.OpenFile(f.filePath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
|
theFile, err := os.OpenFile(f.filePath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user