Skip to content

Commit

Permalink
Cleaned up code to use models
Browse files Browse the repository at this point in the history
  • Loading branch information
reneManqueros committed Oct 10, 2022
1 parent 6d5e4d6 commit f25078b
Show file tree
Hide file tree
Showing 14 changed files with 225 additions and 209 deletions.
13 changes: 7 additions & 6 deletions src/arpscan.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"
"sync"
"time"
"watchyourlan/models"
)

func scan_iface(iface string) string {
Expand All @@ -17,15 +18,15 @@ func scan_iface(iface string) string {
}
}

func parse_output(text string) []Host {
var foundHosts = []Host{}
func parse_output(text string) []models.Host {
var foundHosts []models.Host

perString := strings.Split(text, "\n")
currentTime := time.Now()

for _, host := range perString {
if host != "" {
var oneHost Host
var oneHost models.Host
p := strings.Split(host, " ")
oneHost.Ip = p[0]
oneHost.Mac = p[1]
Expand All @@ -39,9 +40,9 @@ func parse_output(text string) []Host {
return foundHosts
}

func arp_scan() []Host {
var foundHosts = []Host{}
perString := strings.Split(AppConfig.Iface, " ")
func arp_scan() []models.Host {
var foundHosts []models.Host
perString := strings.Split(models.AppConfig.Iface, " ")
wg := sync.WaitGroup{}
wg.Add(len(perString))
arpScanMutex := &sync.Mutex{}
Expand Down
15 changes: 8 additions & 7 deletions src/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,29 @@ package main
import (
"fmt"
"log"
"watchyourlan/models"
)

func host_in_db(host Host, dbHosts []Host) bool { // Check if host is already in DB
func host_in_db(host models.Host, dbHosts []models.Host) bool { // Check if host is already in DB
for _, oneHost := range dbHosts {
if host.Ip == oneHost.Ip && host.Mac == oneHost.Mac && host.Hw == oneHost.Hw {
oneHost.Date = host.Date
oneHost.Now = 1
db_update(oneHost)
oneHost.Update()
return true
}
}
return false
}

func hosts_compare(foundHosts []Host, dbHosts []Host) {
func hosts_compare(foundHosts []models.Host, dbHosts []models.Host) {
for _, oneHost := range foundHosts {
if !(host_in_db(oneHost, dbHosts)) {
oneHost.Now = 1 // Mark host online
oneHost.Now = 1 // Mark host online
msg := fmt.Sprintf("UNKNOWN HOST IP: '%s', MAC: '%s', Hw: '%s'", oneHost.Ip, oneHost.Mac, oneHost.Hw)
log.Println("WARN:", msg)
shoutr_notify(msg) // Notify through Shoutrrr
db_insert(oneHost)
shoutr_notify(msg) // Notify through Shoutrrr
oneHost.Add()
}
}
}
}
45 changes: 0 additions & 45 deletions src/db-connect.go

This file was deleted.

53 changes: 0 additions & 53 deletions src/db-edit.go

This file was deleted.

11 changes: 11 additions & 0 deletions src/helpers/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package helpers

import "strings"

func Quote(str string) string {
return strings.ReplaceAll(str, "'", "''")
}

func Unquote(str string) string {
return strings.ReplaceAll(str, "''", "'")
}
47 changes: 12 additions & 35 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,31 @@ package main

import (
"time"
"watchyourlan/models"
)

type Host struct {
Id uint16
Name string
Ip string
Mac string
Hw string
Date string
Known uint16
Now uint16
}

type Conf struct {
Iface string
DbPath string
GuiIP string
GuiPort string
GuiAuth string
ShoutUrl string
Theme string
Timeout int
}

var AppConfig Conf
var AllHosts []Host
var AllHosts models.Hosts

func scan_and_compare() {
var foundHosts []Host
var dbHosts []Host
var foundHosts models.Hosts
var dbHosts models.Hosts
for { // Endless
foundHosts = arp_scan() // Scan interfaces
dbHosts = db_select() // Select everything from DB
db_setnow() // Mark hosts in DB as offline
dbHosts = models.HostsGetAll() // Select everything from DB
AllHosts.SetLastSeen() // Mark hosts in DB as offline
hosts_compare(foundHosts, dbHosts) // Compare hosts online and in DB
// and add them to DB
AllHosts = db_select()
time.Sleep(time.Duration(AppConfig.Timeout) * time.Second) // Timeout
AllHosts = models.HostsGetAll()
time.Sleep(time.Duration(models.AppConfig.Timeout) * time.Second) // Timeout
}
}

func main() {
AllHosts = []Host{}
AppConfig = get_config() // Get config from Defaults, Config file, Env

db_create() // Check if DB exists. Create if not
AllHosts = models.Hosts{}
models.AppConfig = models.Conf{}
models.AppConfig.Get()

AllHosts.CreateDBIfNew() // Check if DB exists. Create if not
go scan_and_compare()

webgui() // Start web GUI
}
41 changes: 25 additions & 16 deletions src/getconfig.go → src/models/config.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
package main
package models

import "github.com/spf13/viper"

type Conf struct {
Iface string
DbPath string
GuiIP string
GuiPort string
GuiAuth string
ShoutUrl string
Theme string
Timeout int
}

import (
"github.com/spf13/viper"
)
var AppConfig Conf

const configPath = "/data/config"

func get_config() (config Conf) {
func (c *Conf) Get() {
viper.SetDefault("IFACE", "enp1s0")
viper.SetDefault("DBPATH", "/data/db.sqlite")
viper.SetDefault("GUIIP", "localhost")
Expand All @@ -22,19 +33,17 @@ func get_config() (config Conf) {

viper.AutomaticEnv() // Get ENVIRONMENT variables

config.Iface = viper.Get("IFACE").(string)
config.DbPath = viper.Get("DBPATH").(string)
config.GuiIP = viper.Get("GUIIP").(string)
config.GuiPort = viper.Get("GUIPORT").(string)
config.GuiAuth = viper.Get("GUIAUTH").(string)
config.Timeout = viper.GetInt("TIMEOUT")
config.ShoutUrl = viper.Get("SHOUTRRR_URL").(string)
config.Theme = viper.Get("THEME").(string)

return config
c.Iface = viper.Get("IFACE").(string)
c.DbPath = viper.Get("DBPATH").(string)
c.GuiIP = viper.Get("GUIIP").(string)
c.GuiPort = viper.Get("GUIPORT").(string)
c.GuiAuth = viper.Get("GUIAUTH").(string)
c.Timeout = viper.GetInt("TIMEOUT")
c.ShoutUrl = viper.Get("SHOUTRRR_URL").(string)
c.Theme = viper.Get("THEME").(string)
}

func write_config() {
func (c *Conf) Set() {
viper.SetConfigFile(configPath)
viper.SetConfigType("env")
viper.Set("THEME", AppConfig.Theme)
Expand Down
Loading

0 comments on commit f25078b

Please sign in to comment.