commit 002ec28d308be15223e8e5ac71318727f4c7637c Author: Captain ALM Date: Tue Dec 5 21:30:51 2023 +0000 Initial commit. diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..6313b56 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* text=auto eol=lf diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f2ec00d --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +# Environment variables file +.env + +# Distributable directory +dist/ + +# Test data and logs folders +.data/ +.idea/dataSources.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/HostPersister.iml b/.idea/HostPersister.iml new file mode 100644 index 0000000..5e764c4 --- /dev/null +++ b/.idea/HostPersister.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/discord.xml b/.idea/discord.xml new file mode 100644 index 0000000..d8e9561 --- /dev/null +++ b/.idea/discord.xml @@ -0,0 +1,7 @@ + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..6e6384d --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..049109b --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,11 @@ +Copyright (c) 2023 Captain ALM. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..422b364 --- /dev/null +++ b/Makefile @@ -0,0 +1,56 @@ +SHELL := /bin/bash +PRODUCT_NAME := hostpersister +BIN := dist/${PRODUCT_NAME} +DNAME := ${PRODUCT_NAME}_ +ENTRY_POINT := ./cmd/${PRODUCT_NAME} +HASH := $(shell git rev-parse --short HEAD) +COMMIT_DATE := $(shell git show -s --format=%ci ${HASH}) +BUILD_DATE := $(shell date '+%Y-%m-%d %H:%M:%S') +VERSION := ${HASH} +LD_FLAGS := -s -w -X 'main.buildVersion=${VERSION}' -X 'main.buildDate=${BUILD_DATE}' -X 'main.buildName=${PRODUCT_NAME}' +COMP_BIN := go + +ifeq ($(OS),Windows_NT) + BIN := $(BIN).exe + DNAME := $(DNAME).exe +endif + +.PHONY: build dev test clean deploy d setup s + +build: + mkdir -p dist/ + ${COMP_BIN} build -o "${BIN}" -ldflags="${LD_FLAGS}" ${ENTRY_POINT} + +dev: + mkdir -p dist/ + ${COMP_BIN} build -tags debug -o "${BIN}" -ldflags="${LD_FLAGS}" ${ENTRY_POINT} + ./${BIN} + +test: + ${COMP_BIN} test + +clean: + ${COMP_BIN} clean + rm -r -f dist/ + +setup: + sudo cp "${PRODUCT_NAME}.service" /etc/systemd/system + sudo mkdir -p "/etc/${PRODUCT_NAME}" + sudo touch "/etc/${PRODUCT_NAME}/.env" + sudo systemctl daemon-reload + +s: + sudo cp "${DNAME}.service" /etc/systemd/system + sudo mkdir -p "/etc/${DNAME}" + sudo touch "/etc/${DNAME}/.env" + sudo systemctl daemon-reload + +deploy: build + sudo systemctl stop "${PRODUCT_NAME}" + sudo cp "${BIN}" /usr/local/bin + sudo systemctl start "${PRODUCT_NAME}" + +d: build + sudo systemctl stop "${DNAME}" + sudo cp "${BIN}" "/usr/local/bin/${DNAME}" + sudo systemctl start "${DNAME}" diff --git a/README.md b/README.md new file mode 100644 index 0000000..e0706ef --- /dev/null +++ b/README.md @@ -0,0 +1,13 @@ +# Host Persister + +This allows for hosts specified in a host formatted file to be synced to the actual hosts file so-long as their entries do not already exist. + +The .env file is used to configure the hosts file to use a source (SOURCE_FILE) to be synced to the actual file (HOSTS_FILE). +There is the ability to overwrite existing values as part of sync, so long as the domain is the only one defined on the line (HOSTS_OVERWRITE = 1). +There is the ability to periodic sync with a specified interval (SYNC_TIME). + +Maintainer: +[Captain ALM](https://code.mrmelon54.com/alfred) + +License: +BSD 3-Clause \ No newline at end of file diff --git a/cmd/hostpersister/main.go b/cmd/hostpersister/main.go new file mode 100644 index 0000000..87517c4 --- /dev/null +++ b/cmd/hostpersister/main.go @@ -0,0 +1,109 @@ +package main + +import ( + "fmt" + "github.com/joho/godotenv" + "golang.captainalm.com/HostPersister/hosts" + "log" + "os" + "os/signal" + "strconv" + "sync" + "syscall" + "time" +) + +var ( + buildVersion = "develop" + buildDate = "" +) + +func main() { + log.Printf("[Main] Starting up Host Persister #%s (%s)\n", buildVersion, buildDate) + y := time.Now() + + //Hold main thread till safe shutdown exit: + wg := &sync.WaitGroup{} + wg.Add(1) + + //Load environment file: + err := godotenv.Load() + if err != nil { + log.Fatalln("Error loading .env file") + } + + //Load ENVs + hostsPath := os.Getenv("HOSTS_FILE") + sourcePath := os.Getenv("SOURCE_FILE") + syncTime, err := strconv.ParseInt(os.Getenv("SYNC_TIME"), 10, 64) + if err != nil { + log.Println("[Main] Invalid SYNC_TIME; defaulting to one-shot execution.") + syncTime = 0 + } + overwriteMode := os.Getenv("HOSTS_OVERWRITE") == "1" + + //Load hosts and source files + hostsFile, err := hosts.NewHostsFile(hostsPath) + if err != nil { + log.Fatalln("Failed to load HOSTS_FILE") + } + sourceFile, err := hosts.NewHostsFile(sourcePath) + if err != nil { + log.Fatalln("Failed to load SOURCE_FILE") + } + + if syncTime < 1 { + //One-shot execution: + z := time.Now().Sub(y) + log.Printf("[Main] Took '%s' to fully initialize modules\n", z.String()) + executePersistence(hostsFile, sourceFile, overwriteMode) + } else { + //Sync execution: + sigs := make(chan os.Signal, 1) + signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) + + z := time.Now().Sub(y) + log.Printf("[Main] Took '%s' to fully initialize modules\n", z.String()) + + exec := true + syncDur := time.Duration(syncTime) * time.Millisecond + + go func(exec *bool) { + for *exec { + executePersistence(hostsFile, sourceFile, overwriteMode) + time.Sleep(syncDur) + } + }(&exec) + + go func(exec *bool) { + <-sigs + fmt.Printf("\n") + + *exec = false + + a := time.Now() + log.Printf("[Main] Signalling program exit...\n") + + b := time.Now().Sub(a) + log.Printf("[Main] Took '%s' to fully shutdown modules\n", b.String()) + wg.Done() + }(&exec) + + wg.Wait() + } + log.Println("[Main] Goodbye") +} + +func executePersistence(hostsFile *hosts.File, sourceFile *hosts.File, ovrw bool) { + for _, entry := range sourceFile.Entries { + for _, domain := range entry.Domains { + if (!hostsFile.HasDomain(domain)) || ovrw { + hostsFile.OverwriteDomainSingleton(domain, entry.IPAddress) + } + } + } + err := hostsFile.WriteHostsFile() + if err != nil { + log.Println("[Main] Error Writing Hosts File.") + } +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..94b2a54 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module golang.captainalm.com/HostPersister + +go 1.18 + +require github.com/joho/godotenv v1.5.1 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..d61b19e --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= diff --git a/hostpersister.service b/hostpersister.service new file mode 100644 index 0000000..cafc0f7 --- /dev/null +++ b/hostpersister.service @@ -0,0 +1,15 @@ +# Host Persister Service +[Unit] +Description=Host Persister Service + +[Service] +WorkingDirectory=/etc/hostpersister +ExecStart=/usr/local/bin/hostpersister +User=root +Group=root +Type=simple +Restart=on-failure +RestartSec=15 + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/hostpersister_.service b/hostpersister_.service new file mode 100644 index 0000000..80da56e --- /dev/null +++ b/hostpersister_.service @@ -0,0 +1,15 @@ +# Host Persister Service (Dev) +[Unit] +Description=Host Persister Service (Dev) + +[Service] +WorkingDirectory=/etc/hostpersister +ExecStart=/usr/local/bin/hostpersister +User=root +Group=root +Type=simple +Restart=on-failure +RestartSec=15 + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/hosts/Entry.go b/hosts/Entry.go new file mode 100644 index 0000000..1289650 --- /dev/null +++ b/hosts/Entry.go @@ -0,0 +1,73 @@ +package hosts + +import "strings" + +func NewHostsEntry(lineIn string) Entry { + trLineIn := strings.Trim(lineIn, "\r\n") + lineSplt := strings.Split(trLineIn, " ") + if len(lineSplt) == 1 && strings.HasPrefix(trLineIn, "#") { + return Entry{ + IPAddress: "", + Domains: nil, + comment: trLineIn, + } + } else if len(lineSplt) > 1 { + var theDomains []string + for i := 1; i < len(lineSplt); i++ { + if strings.HasPrefix(lineSplt[i], "#") { + break + } + theDomains = append(theDomains, lineSplt[i]) + } + theComment := "" + theCommentStart := strings.Index(trLineIn, "#") + if theCommentStart > -1 { + theComment = trLineIn[theCommentStart:] + } + return Entry{ + IPAddress: lineSplt[0], + Domains: theDomains, + comment: theComment, + } + } else { + return Entry{ + IPAddress: "", + Domains: nil, + comment: "", + } + } +} + +type Entry struct { + IPAddress string + Domains []string + comment string +} + +func (e Entry) IsFilled() bool { + return e.IPAddress != "" && len(e.Domains) > 0 +} + +func (e Entry) HasDomain(domain string) bool { + if !e.IsFilled() { + return false + } + for _, c := range e.Domains { + if strings.EqualFold(c, domain) { + return true + } + } + return false +} + +func (e Entry) ToLine() string { + if e.IsFilled() { + toReturn := []string{e.IPAddress} + toReturn = append(toReturn, e.Domains...) + if e.comment != "" { + toReturn = append(toReturn, e.comment) + } + return strings.Join(toReturn, " ") + } + return e.comment +} diff --git a/hosts/File.go b/hosts/File.go new file mode 100644 index 0000000..bea0098 --- /dev/null +++ b/hosts/File.go @@ -0,0 +1,115 @@ +package hosts + +import ( + "io" + "os" + "strings" +) + +const readBufferSize = 8192 + +func NewHostsFile(filePath string) (*File, error) { + theFile, err := os.Open(filePath) + if err != nil { + return nil, err + } + defer theFile.Close() + var theEntries []Entry + var lenIn int + 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 + } + if theCBuffer != "" { + theEntries = append(theEntries, NewHostsEntry(theCBuffer)) + } + return &File{ + filePath: filePath, + Entries: theEntries, + lineEnding: lineEnding, + }, nil +} + +type File struct { + filePath string + Entries []Entry + lineEnding string +} + +func (f File) WriteHostsFile() error { + theFile, err := os.OpenFile(f.filePath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644) + if err != nil { + return err + } + defer theFile.Close() + for _, entry := range f.Entries { + _, err = theFile.WriteString(entry.ToLine() + f.lineEnding) + if err != nil { + return err + } + } + return nil +} + +func (f File) HasDomain(domain string) bool { + for _, entry := range f.Entries { + if entry.HasDomain(domain) { + return true + } + } + return false +} + +func (f File) indexDomainSingleton(domain string) int { + for i, entry := range f.Entries { + if len(entry.Domains) == 1 && entry.HasDomain(domain) { + return i + } + } + return -1 +} + +func (f File) HasDomainSingleton(domain string) bool { + return f.indexDomainSingleton(domain) > -1 +} + +func (f *File) OverwriteDomainSingleton(domain string, ipAddress string) { + idx := f.indexDomainSingleton(domain) + if idx == -1 { + f.Entries = append(f.Entries, Entry{ + IPAddress: ipAddress, + Domains: []string{domain}, + }) + } else { + theEntry := f.Entries[idx] + theEntry.IPAddress = ipAddress + f.Entries = append(append(f.Entries[:idx], theEntry), f.Entries[idx+1:]...) + } +}