From 736f9df3028176efb5ee9b125bc4e664e8948d36 Mon Sep 17 00:00:00 2001 From: Lars Date: Sun, 4 Feb 2024 14:43:30 +0000 Subject: [PATCH] Separate credentials to their own config --- .gitignore | 3 ++- credentials.example.json | 10 +++++++ go.mod | 2 ++ go.sum | 2 ++ main.go | 58 +++++++++++++++++++++++++++++++++++++++- 5 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 credentials.example.json diff --git a/.gitignore b/.gitignore index 80e322e..477fc07 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ env __debug* tls*.crt tls*.key -*.py \ No newline at end of file +*.py +credentials.json \ No newline at end of file diff --git a/credentials.example.json b/credentials.example.json new file mode 100644 index 0000000..09f8150 --- /dev/null +++ b/credentials.example.json @@ -0,0 +1,10 @@ +{ + "all": { + "password": "abc123", + "allowedDomains": [] + }, + "specific": { + "password": "123abc", + "allowedDomains": ["example.com"] + } +} \ No newline at end of file diff --git a/go.mod b/go.mod index c9d5cbc..1cc01ce 100644 --- a/go.mod +++ b/go.mod @@ -6,3 +6,5 @@ require ( github.com/emersion/go-sasl v0.0.0-20220912192320-0145f2c60ead // direct github.com/emersion/go-smtp v0.16.0 // direct ) + +require golang.org/x/exp v0.0.0-20240119083558-1b970713d09a // indirect diff --git a/go.sum b/go.sum index 3c26b88..baf0ba0 100644 --- a/go.sum +++ b/go.sum @@ -3,3 +3,5 @@ github.com/emersion/go-sasl v0.0.0-20220912192320-0145f2c60ead h1:fI1Jck0vUrXT8b github.com/emersion/go-sasl v0.0.0-20220912192320-0145f2c60ead/go.mod h1:iL2twTeMvZnrg54ZoPDNfJaJaqy0xIQFuBdrLsmspwQ= github.com/emersion/go-smtp v0.16.0 h1:eB9CY9527WdEZSs5sWisTmilDX7gG+Q/2IdRcmubpa8= github.com/emersion/go-smtp v0.16.0/go.mod h1:qm27SGYgoIPRot6ubfQ/GpiPy/g3PaZAVRxiO/sDUgQ= +golang.org/x/exp v0.0.0-20240119083558-1b970713d09a h1:Q8/wZp0KX97QFTc2ywcOE0YRjZPVIx+MXInMzdvQqcA= +golang.org/x/exp v0.0.0-20240119083558-1b970713d09a/go.mod h1:idGWGoKP1toJGkd5/ig9ZLuPcZBC3ewk7SzmH0uou08= diff --git a/main.go b/main.go index 9971bcd..a05b9b1 100644 --- a/main.go +++ b/main.go @@ -16,8 +16,12 @@ import ( "github.com/emersion/go-sasl" "github.com/emersion/go-smtp" + "golang.org/x/exp/maps" ) +var TERMINATED string +var LASTMOD int64 + type Credential struct { Password string `json:"password"` AllowedDomains []string `json:"allowedDomains"` @@ -131,6 +135,7 @@ func (s *Session) SendMail() error { } func (s *Session) AuthPlain(username, password string) error { + fmt.Println(config.Credentials) val, ok := config.Credentials[username] if ok && val.Password == password { @@ -223,17 +228,39 @@ func ListenHealthcheck() { } func main() { + TERMINATED = "" + // Create config config = &Config{} + credentials := &map[string]Credential{} + + // Get last changed timestamp + credentialsInfo, err := os.Stat("credentials.json") + if err != nil { + log.Fatal(err) + TERMINATED = err.Error() + } + + LASTMOD = credentialsInfo.ModTime().Unix() // Read config.json into temp string configFile, err := os.ReadFile("config.json") if err != nil { log.Fatal(err) + TERMINATED = err.Error() + } + + credentialFile, err := os.ReadFile("credentials.json") + if err != nil { + log.Fatal(err) + TERMINATED = err.Error() } // Unmarshal config json.Unmarshal(configFile, &config) + json.Unmarshal(credentialFile, &credentials) + + config.Credentials = *credentials be := &RelayBackend{} @@ -332,5 +359,34 @@ func main() { go Listen(smtps) - ListenHealthcheck() + go ListenHealthcheck() + + for { + if TERMINATED != "" { + log.Fatal(TERMINATED) + panic(fmt.Errorf(TERMINATED)) + } + time.Sleep(time.Duration(10) * time.Second) + + fileInfo, err := os.Stat("credentials.json") + if err != nil { + TERMINATED = err.Error() + } + + if LASTMOD < fileInfo.ModTime().Unix() { + updatedCredentials := &map[string]Credential{} + credentialFile, err := os.ReadFile("credentials.json") + if err != nil { + TERMINATED = err.Error() + } + + json.Unmarshal(credentialFile, &updatedCredentials) + + config.Credentials = *updatedCredentials + LASTMOD = fileInfo.ModTime().Unix() + + fmt.Println("Updated credentials") + fmt.Println(maps.Keys(*credentials)) + } + } }