forked from alfred/HostPersister
Initial commit.
This commit is contained in:
commit
002ec28d30
1
.gitattributes
vendored
Normal file
1
.gitattributes
vendored
Normal file
@ -0,0 +1 @@
|
||||
* text=auto eol=lf
|
9
.gitignore
vendored
Normal file
9
.gitignore
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
# Environment variables file
|
||||
.env
|
||||
|
||||
# Distributable directory
|
||||
dist/
|
||||
|
||||
# Test data and logs folders
|
||||
.data/
|
||||
.idea/dataSources.xml
|
8
.idea/.gitignore
vendored
Normal file
8
.idea/.gitignore
vendored
Normal file
@ -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
|
9
.idea/HostPersister.iml
Normal file
9
.idea/HostPersister.iml
Normal file
@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="WEB_MODULE" version="4">
|
||||
<component name="Go" enabled="true" />
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
7
.idea/discord.xml
Normal file
7
.idea/discord.xml
Normal file
@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="DiscordProjectSettings">
|
||||
<option name="show" value="PROJECT_FILES" />
|
||||
<option name="description" value="" />
|
||||
</component>
|
||||
</project>
|
8
.idea/modules.xml
Normal file
8
.idea/modules.xml
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/HostPersister.iml" filepath="$PROJECT_DIR$/.idea/HostPersister.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
11
LICENSE.md
Normal file
11
LICENSE.md
Normal file
@ -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.
|
56
Makefile
Normal file
56
Makefile
Normal file
@ -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}"
|
13
README.md
Normal file
13
README.md
Normal file
@ -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
|
109
cmd/hostpersister/main.go
Normal file
109
cmd/hostpersister/main.go
Normal file
@ -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.")
|
||||
}
|
||||
}
|
5
go.mod
Normal file
5
go.mod
Normal file
@ -0,0 +1,5 @@
|
||||
module golang.captainalm.com/HostPersister
|
||||
|
||||
go 1.18
|
||||
|
||||
require github.com/joho/godotenv v1.5.1
|
2
go.sum
Normal file
2
go.sum
Normal file
@ -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=
|
15
hostpersister.service
Normal file
15
hostpersister.service
Normal file
@ -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
|
15
hostpersister_.service
Normal file
15
hostpersister_.service
Normal file
@ -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
|
73
hosts/Entry.go
Normal file
73
hosts/Entry.go
Normal file
@ -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
|
||||
}
|
115
hosts/File.go
Normal file
115
hosts/File.go
Normal file
@ -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:]...)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user