Skip to content

Commit

Permalink
Merge pull request #420 from Plaenkler/add-logs-to-frontend
Browse files Browse the repository at this point in the history
[ADD] Logs to frontend
  • Loading branch information
Plaenkler authored Sep 30, 2024
2 parents a5ab211 + 5010a39 commit e5ba184
Show file tree
Hide file tree
Showing 19 changed files with 206 additions and 114 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/linters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ jobs:
VALIDATE_ALL_CODEBASE: true
VALIDATE_HTML: false
VALIDATE_JAVASCRIPT_STANDARD: false
VALIDATE_JAVASCRIPT_PRETTIER: false
VALIDATE_JSCPD: false
VALIDATE_GO: false
VALIDATE_GO_MODULES: false
golangci-lint:
Expand Down
16 changes: 8 additions & 8 deletions cmd/def/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@ import (

func main() {
database.Start()
log.Infof("[main-main-1] started database connection")
log.Infof("started database connection")
go ddns.Start()
log.Infof("[main-main-2] started ddns service")
log.Infof("started ddns service")
go session.Start()
log.Infof("[main-main-3] started session service")
log.Infof("started session service")
go server.Start()
log.Infof("[main-main-4] started webserver")
log.Infof("started webserver")
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
server.Stop()
log.Infof("[main-main-5] stopped webserver")
log.Infof("stopped webserver")
session.Stop()
log.Infof("[main-main-6] stopped session service")
log.Infof("stopped session service")
ddns.Stop()
log.Infof("[main-main-7] stopped ddns service")
log.Infof("stopped ddns service")
database.Stop()
log.Infof("[main-main-8] stopped database connection")
log.Infof("stopped database connection")
}
24 changes: 12 additions & 12 deletions cmd/svc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,25 @@ type program struct{}

func (p *program) Start(_ service.Service) error {
database.Start()
log.Infof("[main-Start-1] started database connection")
log.Infof("started database connection")
go ddns.Start()
log.Infof("[main-Start-2] started ddns service")
log.Infof("started ddns service")
go session.Start()
log.Infof("[main-Start-3] started session service")
log.Infof("started session service")
go server.Start()
log.Infof("[main-Start-4] started webserver")
log.Infof("started webserver")
return nil
}

func (p *program) Stop(_ service.Service) error {
server.Stop()
log.Infof("[main-Stop-1] stopped webserver")
log.Infof("stopped webserver")
session.Stop()
log.Infof("[main-Stop-2] stopped session service")
log.Infof("stopped session service")
ddns.Stop()
log.Infof("[main-Stop-3] stopped ddns service")
log.Infof("stopped ddns service")
database.Stop()
log.Infof("[main-Stop-4] stopped database connection")
log.Infof("stopped database connection")
return nil
}

Expand All @@ -43,14 +43,14 @@ func main() {
p := &program{}
err := p.Start(nil)
if err != nil {
log.Fatalf("[main-main-0] failed to start service: %v", err)
log.Fatalf("failed to start service: %v", err)
}
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
err = p.Stop(nil)
if err != nil {
log.Fatalf("[main-main-0] failed to stop service: %v", err)
log.Fatalf("failed to stop service: %v", err)
}
return
}
Expand All @@ -62,10 +62,10 @@ func main() {
prg := &program{}
s, err := service.New(prg, svcConfig)
if err != nil {
log.Fatalf("[main-main-1] failed to create service: %v", err)
log.Fatalf("failed to create service: %v", err)
}
err = s.Run()
if err != nil {
log.Errorf("[main-main-2] failed to run service: %v", err)
log.Errorf("failed to run service: %v", err)
}
}
18 changes: 9 additions & 9 deletions pkg/cipher/cipher.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func init() {
var err error
key, err = read()
if err != nil {
log.Fatalf("[cipher-init-1] could not load key: %v", err)
log.Fatalf("could not load key: %v", err)
}
}

Expand Down Expand Up @@ -80,41 +80,41 @@ func generate() ([]byte, error) {
func Encrypt(plaintext string) (string, error) {
encrypter, err := aes.NewCipher(key)
if err != nil {
return "", fmt.Errorf("[cipher-Encrypt-1] encryption failed: %s", err)
return "", fmt.Errorf("encryption failed: %s", err)
}
gcm, err := cipher.NewGCM(encrypter)
if err != nil {
return "", fmt.Errorf("[cipher-Encrypt-2] encryption failed: %s", err)
return "", fmt.Errorf("encryption failed: %s", err)
}
nonce := make([]byte, gcm.NonceSize())
_, err = io.ReadFull(rand.Reader, nonce)
if err != nil {
return "", fmt.Errorf("[cipher-Encrypt-3] encryption failed: %s", err)
return "", fmt.Errorf("encryption failed: %s", err)
}
return base64.StdEncoding.EncodeToString(gcm.Seal(nonce, nonce, []byte(plaintext), nil)), nil
}

func Decrypt(ciphertext string) ([]byte, error) {
c, err := aes.NewCipher(key)
if err != nil {
return nil, fmt.Errorf("[cipher-Decrypt-1] decryption failed: %s", err)
return nil, fmt.Errorf("decryption failed: %s", err)
}
gcm, err := cipher.NewGCM(c)
if err != nil {
return nil, fmt.Errorf("[cipher-Decrypt-2] decryption failed: %s", err)
return nil, fmt.Errorf("decryption failed: %s", err)
}
cipherBytes, err := base64.StdEncoding.DecodeString(ciphertext)
if err != nil {
return nil, fmt.Errorf("[cipher-Decrypt-3] decryption failed: %s", err)
return nil, fmt.Errorf("decryption failed: %s", err)
}
nonceSize := gcm.NonceSize()
if len(cipherBytes) < nonceSize {
return nil, fmt.Errorf("[cipher-Decrypt-4] decryption failed: %s", err)
return nil, fmt.Errorf("decryption failed: %s", err)
}
nonce, cipherBytes := cipherBytes[:nonceSize], cipherBytes[nonceSize:]
plaintext, err := gcm.Open(nil, nonce, cipherBytes, nil)
if err != nil {
return nil, fmt.Errorf("[cipher-Decrypt-5] decryption failed: %s", err)
return nil, fmt.Errorf("decryption failed: %s", err)
}
return plaintext, nil
}
4 changes: 2 additions & 2 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var (
func init() {
err := load()
if err != nil {
log.Fatalf("[config-init-1] initialization failed: %s", err.Error())
log.Fatalf("initialization failed: %s", err.Error())
}
}

Expand Down Expand Up @@ -83,7 +83,7 @@ func create() error {
if err != nil {
return err
}
log.Infof("[config-create-1] created default configuration")
log.Infof("created default configuration")
return nil
}

Expand Down
10 changes: 5 additions & 5 deletions pkg/database/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ func Start() {
oc.Do(func() {
err := createDBDir()
if err != nil {
log.Fatalf("[database-Start-1] failed to create database directory: %s", err.Error())
log.Fatalf("failed to create database directory: %s", err.Error())
}
db, err = openDBConnection()
if err != nil {
log.Fatalf("[database-Start-2] failed to open database connection: %s", err.Error())
log.Fatalf("failed to open database connection: %s", err.Error())
}
err = migrateDBSchema(db)
if err != nil {
log.Fatalf("[database-Start-3] failed to migrate database schema: %s", err.Error())
log.Fatalf("failed to migrate database schema: %s", err.Error())
}
})
}
Expand Down Expand Up @@ -70,12 +70,12 @@ func Stop() {
}
sqlDB, err := db.DB()
if err != nil {
log.Errorf("[database-Stop-1] failed to get underlying DB connection: %s", err.Error())
log.Errorf("failed to get underlying DB connection: %s", err.Error())
return
}
err = sqlDB.Close()
if err != nil {
log.Errorf("[database-Stop-2] failed to close DB connection: %s", err.Error())
log.Errorf("failed to close DB connection: %s", err.Error())
}
}

Expand Down
10 changes: 5 additions & 5 deletions pkg/ddns/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,21 @@ func GetPublicIP() (string, error) {
if cRes != "" {
addr, err := resolveIPAddress(cRes)
if err != nil {
return "", fmt.Errorf("[ddns-GetPublicIP-1] resolver %s failed: %s", cRes, err)
return "", fmt.Errorf("resolver %s failed: %s", cRes, err)
}
log.Infof("[ddns-GetPublicIP-2] %s succeeded: %s", cRes, addr)
log.Infof("%s succeeded: %s", cRes, addr)
return addr, nil
}
for r := range resolvers {
addr, err := resolveIPAddress(resolvers[r])
if err != nil {
log.Errorf("[ddns-GetPublicIP-3] resolver %s failed: %s", r, err)
log.Errorf("resolver %s failed: %s", r, err)
continue
}
log.Infof("[ddns-GetPublicIP-4] %s succeeded: %s", r, addr)
log.Infof("%s succeeded: %s", r, addr)
return addr, nil
}
return "", fmt.Errorf("[ddns-GetPublicIP-5] all resolvers failed")
return "", fmt.Errorf("all resolvers failed")
}

func resolveIPAddress(url string) (string, error) {
Expand Down
24 changes: 12 additions & 12 deletions pkg/ddns/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,25 @@ func Start() {
updateInterval(interval, ticker)
address, err := GetPublicIP()
if err != nil {
log.Errorf("[ddns-Start-1] failed to get public IP address: %v", err)
log.Errorf("failed to get public IP address: %v", err)
continue
}
newAddress := model.IPAddress{
Address: address,
}
db := database.GetDatabase()
if db == nil {
log.Errorf("[ddns-Start-2] failed to get database connection")
log.Errorf("failed to get database connection")
continue
}
err = db.FirstOrCreate(&newAddress, newAddress).Error
if err != nil {
log.Errorf("[ddns-Start-3] failed to save new IP address: %v", err)
log.Errorf("failed to save new IP address: %v", err)
continue
}
jobs := getSyncJobs(db, newAddress.ID)
if len(jobs) == 0 {
log.Infof("[ddns-Start-4] no dynamic DNS record needs to be updated")
log.Infof("no dynamic DNS record needs to be updated")
continue
}
updateDDNSEntries(db, jobs, newAddress)
Expand All @@ -63,15 +63,15 @@ func updateInterval(interval time.Duration, ticker *time.Ticker) {
newInterval := time.Second * time.Duration(config.Get().Interval)
if interval != newInterval && newInterval > 0 {
ticker.Reset(newInterval)
log.Infof("[ddns-updateInterval-1] changed interval from %v to %v", interval, newInterval)
log.Infof("changed interval from %v to %v", interval, newInterval)
}
}

func getSyncJobs(db *gorm.DB, addressID uint) []model.SyncJob {
var jobs []model.SyncJob
err := db.Where("NOT ip_address_id = ? OR ip_address_id IS NULL", addressID).Find(&jobs).Error
if err != nil {
log.Errorf("[ddns-getSyncJobs-1] failed to get DDNS update jobs: %v", err)
log.Errorf("failed to get DDNS update jobs: %v", err)
return nil
}
return jobs
Expand All @@ -81,30 +81,30 @@ func updateDDNSEntries(db *gorm.DB, jobs []model.SyncJob, a model.IPAddress) {
for _, job := range jobs {
updater, ok := updaters[job.Provider]
if !ok {
log.Errorf("[ddns-updateDDNSEntries-1] no updater found for job %v", job.ID)
log.Errorf("no updater found for job %v", job.ID)
continue
}
params, err := cipher.Decrypt(job.Params)
if err != nil {
log.Errorf("[ddns-updateDDNSEntries-2] failed to decrypt job params for job %v: %s", job.ID, err)
log.Errorf("failed to decrypt job params for job %v: %s", job.ID, err)
continue
}
request := reflect.New(reflect.TypeOf(updater.Request)).Interface()
err = json.Unmarshal(params, &request)
if err != nil {
log.Errorf("[ddns-updateDDNSEntries-3] failed to unmarshal job params for job %v: %s", job.ID, err)
log.Errorf("failed to unmarshal job params for job %v: %s", job.ID, err)
continue
}
err = updater.Updater(request, a.Address)
if err != nil {
log.Errorf("[ddns-updateDDNSEntries-4] failed to update DDNS entry for job %v: %s", job.ID, err)
log.Errorf("failed to update DDNS entry for job %v: %s", job.ID, err)
continue
}
err = db.Model(&job).Update("ip_address_id", a.ID).Error
if err != nil {
log.Errorf("[ddns-updateDDNSEntries-5] failed to update IP address for job %v: %s", job.ID, err)
log.Errorf("failed to update IP address for job %v: %s", job.ID, err)
}
log.Infof("[ddns-updateDDNSEntries-6] updated DDNS entry for ID: %v", job.ID)
log.Infof("updated DDNS entry for ID: %v", job.ID)
}
}

Expand Down
Loading

0 comments on commit e5ba184

Please sign in to comment.