From c7bf57dab3ff21c3376b74d791e19bc7ff94de48 Mon Sep 17 00:00:00 2001 From: Paul Wilde Date: Mon, 16 Aug 2021 13:21:30 +0100 Subject: [PATCH] working on troubes with toiml parsing --- src/default-config/config.default.toml | 13 +++ src/default-config/services.default.toml | 121 +++++++++++++++++++++++ src/global/global.go | 103 +++++++++++++++---- src/go.mod | 3 +- src/go.sum | 12 ++- src/mailautoconf.go | 8 +- src/structs/structs.go | 28 +++++- 7 files changed, 260 insertions(+), 28 deletions(-) create mode 100644 src/default-config/config.default.toml create mode 100644 src/default-config/services.default.toml diff --git a/src/default-config/config.default.toml b/src/default-config/config.default.toml new file mode 100644 index 0000000..47015d4 --- /dev/null +++ b/src/default-config/config.default.toml @@ -0,0 +1,13 @@ +# Sample config.ini file. +# Copy this file to "config/config.ini" and adjust the settings to your requirements + +# The URL of this application +BaseURL = "https://autoconfig.example.com" + +# Set the email domains for use with this service. The first one is primary. +# each will need their own DNS A record for autoconfig.domain.name +Domains = ["example.com","example2,com"] + + +# If you use a different domain to authenticate with, enter it here +LogonDomain = "example.local" diff --git a/src/default-config/services.default.toml b/src/default-config/services.default.toml new file mode 100644 index 0000000..70fe484 --- /dev/null +++ b/src/default-config/services.default.toml @@ -0,0 +1,121 @@ +# The Incoming mail Server Config + +[InMail] + # Enable this service +Enabled = true + + # Mail Type, i.e. IMAP, POP3 + Type = "IMAP" + + # Your IMAP server + Server = "imap.example.com" + + # Your IMAP port + Port = 993 + + # The socket type = SSL, STARTTLS, or NONE + SocketType = "SSL" + + # Use Secure Password Authentication + SPA = false + + # Change to true if you need the domain/logondomain to form part of the username + UsernameIsFQDN = false + + # Do you need to authenticate to your mail server? You should! so this should be false! + NoAuthRequired = false + + # Authentication type, + #"password-cleartext" = Send password in the clear + # (dangerous, if SSL isn't used either). + # AUTH PLAIN, LOGIN or protocol-native login. + #"password-encrypted" = A secure encrypted password mechanism. + # Can be CRAM-MD5 or DIGEST-MD5. Not NTLM. + #"NTLM"= Use NTLM (or NTLMv2 or successors), + # the Windows login mechanism. + Authentication = "password-cleartext" + +# +# +# # The Outgoing mail server config +# [OutMail] +# Name = "OutMail" +# +# # Enable this service +# Enabled = true +# +# # Mail type, likely to only be SMTP +# Type = "SMTP" +# +# # Your SMTP server +# Server = "smtp.example.com" +# +# # Your SMTP port +# Port = 465 +# +# # The socket type = SSL, STARTTLS or NONE +# SocketType = "SSL" +# +# # See InMail > Authentication +# Authentication = "password-cleartext" +# +# # Use Secure Password Authentication +# SPA = false +# +# # Change to true if you need the domain/logondomain to form part of the username +# UsernameIsFQDN = false +# +# # Do you need to authenticate to your mail server? You should! so this should be false! +# NoAuthRequired = false +# +# # Use POP Authentication? You probably shouldn't be. +# POPAuth = false +# +# # This setting is here to limit errors, I'm not sure what it does yet. +# SMTPLast = false +# +# +# # Currently not implemented, see https://wiki.mozilla.org/Thunderbird=Autoconfiguration=ConfigFileFormat +# [Calendar] +# Name = "Calendar" +# # Disable this service +# Enabled = false +# Server = "https://example.com/remote.php/dav/" +# Port = 443 +# Type = "CalDAV" +# Authentication = "http-basic" +# UsernameIsFQDN = false +# +# # Currently not implemented, see https://wiki.mozilla.org/Thunderbird=Autoconfiguration=ConfigFileFormat +# [AddressBook] +# Name = "AddressBook" +# # Disable this service +# Enabled = false +# Server = "https://example.com/remote.php/dav/" +# Port = 443 +# Type = "CardDAV" +# Authentication = "http-basic" +# UsernameIsFQDN = false +# +# # Currently not implemented, see https://wiki.mozilla.org/Thunderbird=Autoconfiguration=ConfigFileFormat +# [WebMail] +# Name = "WebMail" +# # Disable this service +# Enabled = false +# Server = "https://mail.example.com" +# UsernameDivID = "username" +# UsernameDivName = "username" +# PasswordDivName = "password" +# SubmitButtonID = "submit" +# SubmitButtonName = "submit" +# UsernameIsFQDN = false +# +# +# # In theory, additional custom services can be configured and will be displayed with +# # their options on the /get/all URL of this service. The third-party clients would need to +# # check this service as part of their development for this to work +# # Will not be shown in autodiscover.xml/json or config-v1.1.xml/autoconfig.xml +# # i.e Nextcloud - ideally a nextcloud client could check autoconfig for this URL for ease of set up +# #[Nextcloud] +# # Name = "NextCloud" +# # Server = "https://nextcloud.example.com" diff --git a/src/global/global.go b/src/global/global.go index a8b7268..8f85a79 100644 --- a/src/global/global.go +++ b/src/global/global.go @@ -1,8 +1,11 @@ package global import ( . "mailautoconf/structs" - "github.com/vaughan0/go-ini" "fmt" + "github.com/pelletier/go-toml/v2" + "io/ioutil" + "os" + ) // Global variables @@ -12,20 +15,86 @@ const defaultConfigDir string = "default-config/" const configDir string = "config/" func NewConfig() Config { - fmt.Println("Loading Config…") - cfg := "default-config/config.default.ini" - conf, err := ini.LoadFile(cfg) - if err != nil { - fmt.Println(err.Error()) - } - fmt.Println(conf) - fmt.Println("Loading Services…") - srv := "default-config/services.default.ini" - serv, err2 := ini.LoadFile(srv) - if err2 != nil { - fmt.Println(err2.Error()) - } - fmt.Println(serv) - newcfg := Config{} - return newcfg + MainConfig = loadConfig() + return MainConfig +} +func loadConfig() Config { + cfg := Config{} + + fmt.Println("Loading Default Config…") + cfgfile := defaultConfigDir + "config.default.toml" + unmarshalConfig(cfgfile, &cfg) + fmt.Println(cfg) + customcfgfile := configDir + "config.toml" + unmarshalConfig(customcfgfile, &cfg) + fmt.Println(cfg) + svcfile := defaultConfigDir + "services.default.toml" + // cfg.Services = []Service{ + // Service{ + // Name : "first", + // }, + // Service{ + // Name : "second", + // }, + // Service{ + // Name : "third", + // }, + // } + // data, _ := toml.Marshal(cfg) + + // ioutil.WriteFile(svcfile, data, 0) + + unmarshalServices(svcfile, &cfg) + + customsvcfile := configDir + "services.toml" + unmarshalServices(customsvcfile, &cfg) + // fmt.Println(cfg) + fmt.Println("\r\nOur Config :") + fmt.Println(cfg) + return cfg +} + +func unmarshalConfig(file string, cfg *Config) { + if fileExists(file) { + content, err := ioutil.ReadFile(file) + if err != nil { + fmt.Println("Error reading config :", file, " : ", err) + } + err2 := toml.Unmarshal(content, &cfg) + if err2 != nil { + fmt.Println("Error unmarshalling config :", file, " : ", err2) + } + } +} + +func unmarshalServices(file string, cfg *Config) { + if fileExists(file) { + content, err := ioutil.ReadFile(file) + if err != nil { + fmt.Println("Error reading services :", file, " : ", err) + } + customsvcfile := configDir + "services.toml" + content2, err2 := ioutil.ReadFile(file) + if err2 != nil { + fmt.Println("Error reading services :", customsvcfile, " : ", err2) + } + content = []byte(fmt.Sprintf(string(content),string(content2))) + var x map[string]interface{} + err3 := toml.Unmarshal(content, &x) + if err3 != nil { + fmt.Println("Error unmarshalling services :", file, " : ", err3) + } + fmt.Println(x) + } +} + +func fileExists(file string) bool { + exists := false + if _, err := os.Stat(file); err == nil { + exists = true + } else { + fmt.Println(err) + fmt.Printf("File %s does not exist\n", file); + } + return exists } diff --git a/src/go.mod b/src/go.mod index 3bc798b..56c7d88 100644 --- a/src/go.mod +++ b/src/go.mod @@ -3,5 +3,6 @@ module mailautoconf go 1.16 require ( - github.com/vaughan0/go-ini v0.0.0-20130923145212-a98ad7ee00ec // indirect + github.com/pelletier/go-toml v1.9.3 // indirect + github.com/pelletier/go-toml/v2 v2.0.0-beta.3 // indirect ) diff --git a/src/go.sum b/src/go.sum index bac961c..91c5815 100644 --- a/src/go.sum +++ b/src/go.sum @@ -1,2 +1,10 @@ -github.com/vaughan0/go-ini v0.0.0-20130923145212-a98ad7ee00ec h1:DGmKwyZwEB8dI7tbLt/I/gQuP559o/0FrAkHKlQM/Ks= -github.com/vaughan0/go-ini v0.0.0-20130923145212-a98ad7ee00ec/go.mod h1:owBmyHYMLkxyrugmfwE/DLJyW8Ro9mkphwuVErQ0iUw= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pelletier/go-toml v1.9.3 h1:zeC5b1GviRUyKYd6OJPvBU/mcVDVoL1OhT17FCt5dSQ= +github.com/pelletier/go-toml v1.9.3/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c= +github.com/pelletier/go-toml/v2 v2.0.0-beta.3 h1:PNCTU4naEJ8mKal97P3A2qDU74QRQGlv4FXiL1XDqi4= +github.com/pelletier/go-toml/v2 v2.0.0-beta.3/go.mod h1:aNseLYu/uKskg0zpr/kbr2z8yGuWtotWf/0BpGIAL2Y= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.1-0.20210427113832-6241f9ab9942/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/src/mailautoconf.go b/src/mailautoconf.go index 787a5e9..a42da48 100644 --- a/src/mailautoconf.go +++ b/src/mailautoconf.go @@ -5,12 +5,12 @@ import ( "net/http" "log" "mailautoconf/web/handler" - . "mailautoconf/global" + "mailautoconf/global" ) func main() { - MainConfig = NewConfig() + global.NewConfig() http.HandleFunc("/", handler.WebHandler) - fmt.Println("Starting up Web Listener on port 8080") - log.Fatal(http.ListenAndServe(":8080", nil)) + fmt.Println("Starting up Web Listener on port 8010") + log.Fatal(http.ListenAndServe(":8010", nil)) } diff --git a/src/structs/structs.go b/src/structs/structs.go index 46db476..9857770 100644 --- a/src/structs/structs.go +++ b/src/structs/structs.go @@ -1,9 +1,8 @@ package structs -// I don't like the name of this package, consider naming it "core" or -// separating out the structs and core functions import "net/http" + type Session struct { ResponseWriter http.ResponseWriter Request *http.Request @@ -11,8 +10,29 @@ type Session struct { WebContent string } type Config struct { - Services []Service + BaseURL string + Domains []string + LogonDomain string + Services []interface{} } type Service struct { - + Name string + Enabled bool + Type string + Server string + Port int + SocketType string + SPA bool + UsernameIsFQDN bool + NoAuthRequired bool + Authentication string + // For Outgoing Mail + POPAuth bool + SMTPLast bool + // For WebMail (Unused) + UsernameDivID string + UsernameDivName string + PasswordDivName string + SubmitButtonID string + SubmitButtonName string }