better logging management
This commit is contained in:
parent
ab63a14df7
commit
c5ce0c1902
8 changed files with 111 additions and 60 deletions
|
@ -1,7 +1,7 @@
|
||||||
version: '3.3'
|
version: '3.3'
|
||||||
services:
|
services:
|
||||||
mailautoconf:
|
mailautoconf:
|
||||||
container_name: mailautoconf
|
container_name: mailautoconf-pod
|
||||||
ports:
|
ports:
|
||||||
- '8010:8010'
|
- '8010:8010'
|
||||||
volumes:
|
volumes:
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
---
|
---
|
||||||
Version : "0.1.3"
|
Version : "0.1.4"
|
||||||
# Sample config.yaml file.
|
# Sample config.yaml file.
|
||||||
# Copy this file to "config/config.yaml" and adjust the
|
# Copy this file to "config/config.yaml" and adjust the
|
||||||
# settings to your requirements
|
# settings to your requirements
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
package global
|
package global
|
||||||
import (
|
import (
|
||||||
. "mailautoconf/structs"
|
. "mailautoconf/global/structs"
|
||||||
|
"mailautoconf/global/logger"
|
||||||
"fmt"
|
"fmt"
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
// "os"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"text/template"
|
"text/template"
|
||||||
"path"
|
"path"
|
||||||
|
@ -39,16 +40,16 @@ func NewConfig() Config {
|
||||||
}
|
}
|
||||||
func loadConfig() Config {
|
func loadConfig() Config {
|
||||||
cfg := Config{}
|
cfg := Config{}
|
||||||
fmt.Println("Loading Default Config…")
|
logger.Log("Loading Default Config…")
|
||||||
cfgfile := defaultConfigDir + "config.default.yaml"
|
cfgfile := defaultConfigDir + "config.default.yaml"
|
||||||
unmarshalConfig(cfgfile, &cfg)
|
unmarshalConfig(cfgfile, &cfg)
|
||||||
fmt.Println("Loading Custom Config…")
|
logger.Log("Loading Custom Config…")
|
||||||
customcfgfile := configDir + "config.yaml"
|
customcfgfile := configDir + "config.yaml"
|
||||||
unmarshalConfig(customcfgfile, &cfg)
|
unmarshalConfig(customcfgfile, &cfg)
|
||||||
fmt.Println("Loading Default Services…")
|
logger.Log("Loading Default Services…")
|
||||||
svcfile := defaultConfigDir + "services.default.yaml"
|
svcfile := defaultConfigDir + "services.default.yaml"
|
||||||
unmarshalConfig(svcfile, &cfg)
|
unmarshalConfig(svcfile, &cfg)
|
||||||
fmt.Println("Loading Custom Services…")
|
logger.Log("Loading Custom Services…")
|
||||||
customsvcfile := configDir + "services.yaml"
|
customsvcfile := configDir + "services.yaml"
|
||||||
unmarshalConfig(customsvcfile, &cfg)
|
unmarshalConfig(customsvcfile, &cfg)
|
||||||
removeDisabledItems(&cfg)
|
removeDisabledItems(&cfg)
|
||||||
|
@ -64,21 +65,19 @@ func loadXMLTemplates(){
|
||||||
"onoff": chooseOnOff,
|
"onoff": chooseOnOff,
|
||||||
}
|
}
|
||||||
t, err := template.New(name).Funcs(fmap).ParseFiles(tmpl)
|
t, err := template.New(name).Funcs(fmap).ParseFiles(tmpl)
|
||||||
if err != nil {
|
logger.CheckError(err)
|
||||||
panic (err)
|
|
||||||
}
|
|
||||||
Templates[name] = t
|
Templates[name] = t
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
func unmarshalConfig(file string, cfg *Config) {
|
func unmarshalConfig(file string, cfg *Config) {
|
||||||
if FileExists(file) {
|
if logger.FileExists(file) {
|
||||||
content, err := ioutil.ReadFile(file)
|
content, err := ioutil.ReadFile(file)
|
||||||
if err != nil {
|
if !logger.ErrorOK(err){
|
||||||
fmt.Println("Error reading config :", file, " : ", err)
|
logger.Log("Error reading config :", file, " : ", fmt.Sprintf("%v",err))
|
||||||
}
|
}
|
||||||
err2 := yaml.Unmarshal(content, &cfg)
|
err2 := yaml.Unmarshal(content, &cfg)
|
||||||
if err2 != nil {
|
if !logger.ErrorOK(err2){
|
||||||
fmt.Println("Error unmarshaling config :", file, " : ", err2)
|
logger.Log("Error unmarshaling config :", file, " : ", fmt.Sprintf("%v",err2))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -108,22 +107,11 @@ func removeDisabledItems(cfg *Config) {
|
||||||
}
|
}
|
||||||
cfg.OtherServices = new_svcs
|
cfg.OtherServices = new_svcs
|
||||||
}
|
}
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
func JSONify(content interface{}) string {
|
func JSONify(content interface{}) string {
|
||||||
data, err := json.Marshal(content)
|
data, err := json.Marshal(content)
|
||||||
if err != nil {
|
logger.CheckError(err)
|
||||||
fmt.Println(err)
|
|
||||||
}
|
|
||||||
return string(data)
|
return string(data)
|
||||||
}
|
}
|
||||||
func parseUsername(svc Service, email string) string {
|
func parseUsername(svc Service, email string) string {
|
||||||
|
@ -160,6 +148,6 @@ func GetSessionIP() string {
|
||||||
if forwarded != "" {
|
if forwarded != "" {
|
||||||
ip = forwarded
|
ip = forwarded
|
||||||
}
|
}
|
||||||
fmt.Printf("Session %s Connect From : %s\r\f",ThisSession.ID, ip)
|
logger.Log("Session ", ThisSession.ID, " Connect From : ", ip)
|
||||||
return ip
|
return ip
|
||||||
}
|
}
|
||||||
|
|
73
src/global/logger/logger.go
Normal file
73
src/global/logger/logger.go
Normal file
|
@ -0,0 +1,73 @@
|
||||||
|
package logger
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
const logDir = "config/logs"
|
||||||
|
func Log(str ...string) {
|
||||||
|
makeLogDir()
|
||||||
|
|
||||||
|
line := ""
|
||||||
|
for _, s := range str {
|
||||||
|
line = line + s
|
||||||
|
}
|
||||||
|
line = line + "\r\n"
|
||||||
|
log.Print(line)
|
||||||
|
t := time.Now()
|
||||||
|
logname := fmt.Sprintf("%s_log.log",t.Format("200601"))
|
||||||
|
logfile := fmt.Sprintf("%s/%s",logDir, logname)
|
||||||
|
line = fmt.Sprintf("%s %s",t.Format("2006/01/02 15:04:05"), line)
|
||||||
|
if !FileExists(logfile) {
|
||||||
|
err := ioutil.WriteFile(logfile, []byte(line), 0755)
|
||||||
|
CheckError(err)
|
||||||
|
} else {
|
||||||
|
file, err := os.OpenFile(logfile, os.O_APPEND|os.O_WRONLY, 0755)
|
||||||
|
CheckError(err)
|
||||||
|
defer file.Close()
|
||||||
|
if _, err := file.WriteString(line); err != nil {
|
||||||
|
CheckError(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
func CheckError(err error) (ok bool) {
|
||||||
|
// here for obsolescence
|
||||||
|
return ErrorOK(err)
|
||||||
|
}
|
||||||
|
func ErrorOK(err error) (ok bool) {
|
||||||
|
ok = true // All is OK, err == nil
|
||||||
|
if err != nil {
|
||||||
|
ok = false // There's an error, print it
|
||||||
|
e := fmt.Sprintf("%v",err)
|
||||||
|
Log(e)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
func Fatal(err error) {
|
||||||
|
e := fmt.Sprintf("%v",err)
|
||||||
|
Log(e)
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
func makeLogDir(){
|
||||||
|
_, err := os.Stat(logDir)
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
os.Mkdir(logDir, 0755)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func FileExists(file string) bool {
|
||||||
|
exists := false
|
||||||
|
_, err := os.Stat(file);
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
log.Print("File does not exist : ", file);
|
||||||
|
} else if err == nil {
|
||||||
|
exists = true
|
||||||
|
} else {
|
||||||
|
log.Fatal(err)
|
||||||
|
log.Print("File %s does not exist\n", file);
|
||||||
|
}
|
||||||
|
return exists
|
||||||
|
}
|
|
@ -1,16 +1,17 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
// "fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"log"
|
// "log"
|
||||||
"mailautoconf/web/handler"
|
"mailautoconf/web/handler"
|
||||||
"mailautoconf/global"
|
"mailautoconf/global"
|
||||||
|
"mailautoconf/global/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
global.NewConfig()
|
global.NewConfig()
|
||||||
http.HandleFunc("/", handler.WebHandler)
|
http.HandleFunc("/", handler.WebHandler)
|
||||||
fmt.Println("Starting up Web Listener on port 8010")
|
logger.Log("Starting up Web Listener on port 8010")
|
||||||
log.Fatal(http.ListenAndServe(":8010", nil))
|
logger.Fatal(http.ListenAndServe(":8010", nil))
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,21 +1,22 @@
|
||||||
package handler
|
package handler
|
||||||
import (
|
import (
|
||||||
. "mailautoconf/structs"
|
|
||||||
. "mailautoconf/global"
|
. "mailautoconf/global"
|
||||||
|
. "mailautoconf/global/structs"
|
||||||
|
"mailautoconf/global/logger"
|
||||||
"mailautoconf/web/responses"
|
"mailautoconf/web/responses"
|
||||||
"strings"
|
"strings"
|
||||||
"net/http"
|
"net/http"
|
||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
func WebHandler(w http.ResponseWriter, r *http.Request) {
|
func WebHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
|
|
||||||
ThisSession = Session{}
|
ThisSession = Session{}
|
||||||
ThisSession.ResponseWriter = w
|
ThisSession.ResponseWriter = w
|
||||||
ThisSession.Request = r
|
ThisSession.Request = r
|
||||||
|
|
||||||
ThisSession.ID = NewSessionID()
|
ThisSession.ID = NewSessionID()
|
||||||
fmt.Printf("Session %s Request For : %s\r\f",ThisSession.ID, r.URL)
|
url := fmt.Sprintf("%s", r.URL)
|
||||||
|
logger.Log("Session ", ThisSession.ID, " Request For : ", url )
|
||||||
ThisSession.IP = GetSessionIP()
|
ThisSession.IP = GetSessionIP()
|
||||||
|
|
||||||
ThisSession.Path = strings.ToLower(r.URL.Path[1:])
|
ThisSession.Path = strings.ToLower(r.URL.Path[1:])
|
||||||
|
@ -35,11 +36,9 @@ func WebHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
default:
|
default:
|
||||||
ThisSession.WebContent = responses.DefaultResponse()
|
ThisSession.WebContent = responses.DefaultResponse()
|
||||||
}
|
}
|
||||||
|
webOutput()
|
||||||
writeWebOutput()
|
|
||||||
}
|
}
|
||||||
|
func webOutput(){
|
||||||
func writeWebOutput () {
|
|
||||||
ThisSession.ResponseWriter.Header().Set("Content-Type", ThisSession.ContentType)
|
ThisSession.ResponseWriter.Header().Set("Content-Type", ThisSession.ContentType)
|
||||||
fmt.Fprintf(ThisSession.ResponseWriter, ThisSession.WebContent)
|
fmt.Fprintf(ThisSession.ResponseWriter, ThisSession.WebContent)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
package responses
|
package responses
|
||||||
import (
|
import (
|
||||||
"mailautoconf/global"
|
"mailautoconf/global"
|
||||||
. "mailautoconf/structs"
|
"mailautoconf/global/logger"
|
||||||
|
. "mailautoconf/global/structs"
|
||||||
// "text/template"
|
// "text/template"
|
||||||
"fmt"
|
"fmt"
|
||||||
// "path"
|
// "path"
|
||||||
|
@ -16,9 +17,7 @@ func MozAutoconfig() string {
|
||||||
// https://wiki.mozilla.org/Thunderbird:Autoconfiguration:ConfigFileFormat
|
// https://wiki.mozilla.org/Thunderbird:Autoconfiguration:ConfigFileFormat
|
||||||
|
|
||||||
// parse the querystring
|
// parse the querystring
|
||||||
if err := global.ThisSession.Request.ParseForm(); err != nil {
|
logger.CheckError(global.ThisSession.Request.ParseForm())
|
||||||
fmt.Println(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// build the response
|
// build the response
|
||||||
response := Response{}
|
response := Response{}
|
||||||
|
@ -33,9 +32,7 @@ func MozAutoconfig() string {
|
||||||
var result bytes.Buffer
|
var result bytes.Buffer
|
||||||
template := global.Templates["autoconfig.xml"]
|
template := global.Templates["autoconfig.xml"]
|
||||||
err := template.Execute(&result, response)
|
err := template.Execute(&result, response)
|
||||||
if err != nil {
|
logger.CheckError(err)
|
||||||
fmt.Println(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// return our string of xml
|
// return our string of xml
|
||||||
return result.String()
|
return result.String()
|
||||||
|
@ -53,9 +50,7 @@ func MsAutoDiscoverXML() string {
|
||||||
// </Autodiscover>
|
// </Autodiscover>
|
||||||
|
|
||||||
// Parse the form to get the values
|
// Parse the form to get the values
|
||||||
if err := global.ThisSession.Request.ParseForm(); err != nil {
|
logger.CheckError(global.ThisSession.Request.ParseForm())
|
||||||
fmt.Println(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// convert the input to a string so we can extract the email address
|
// convert the input to a string so we can extract the email address
|
||||||
form := fmt.Sprintf("%s",global.ThisSession.Request.Form)
|
form := fmt.Sprintf("%s",global.ThisSession.Request.Form)
|
||||||
|
@ -68,7 +63,7 @@ func MsAutoDiscoverXML() string {
|
||||||
replace := regexp.MustCompile(`\<[\/]?EMailAddress\>`)
|
replace := regexp.MustCompile(`\<[\/]?EMailAddress\>`)
|
||||||
email = replace.ReplaceAllString(email,``)
|
email = replace.ReplaceAllString(email,``)
|
||||||
|
|
||||||
fmt.Printf("Session %s Request for email : %s\r\f",global.ThisSession.ID,email)
|
logger.Log("Session ",global.ThisSession.ID ," Request for email : ",email)
|
||||||
// build the reponse
|
// build the reponse
|
||||||
response := Response{}
|
response := Response{}
|
||||||
response.Email = email
|
response.Email = email
|
||||||
|
@ -79,9 +74,7 @@ func MsAutoDiscoverXML() string {
|
||||||
global.ThisSession.ContentType = "application/xml"
|
global.ThisSession.ContentType = "application/xml"
|
||||||
var result bytes.Buffer
|
var result bytes.Buffer
|
||||||
err := template.Execute(&result, response)
|
err := template.Execute(&result, response)
|
||||||
if err != nil {
|
logger.CheckError(err)
|
||||||
fmt.Println(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// return our string of xml
|
// return our string of xml
|
||||||
return result.String()
|
return result.String()
|
||||||
|
@ -93,7 +86,6 @@ func MsAutoDiscoverJSON() string {
|
||||||
// /autodiscover/autodiscover.json?Email=you@your.domain&Protocol=Autodiscoverv1&RedirectCount=1
|
// /autodiscover/autodiscover.json?Email=you@your.domain&Protocol=Autodiscoverv1&RedirectCount=1
|
||||||
email = global.ThisSession.Request.FormValue("Email")
|
email = global.ThisSession.Request.FormValue("Email")
|
||||||
protocol := global.ThisSession.Request.FormValue("Protocol")
|
protocol := global.ThisSession.Request.FormValue("Protocol")
|
||||||
fmt.Println(protocol)
|
|
||||||
global.ThisSession.ContentType = "application/json"
|
global.ThisSession.ContentType = "application/json"
|
||||||
switch strings.ToLower(protocol) {
|
switch strings.ToLower(protocol) {
|
||||||
case "autodiscoverv1":
|
case "autodiscoverv1":
|
||||||
|
@ -102,13 +94,11 @@ func MsAutoDiscoverJSON() string {
|
||||||
response.Url = fmt.Sprintf("%s/Autodiscover/Autodiscover.xml", global.MainConfig.BaseURL)
|
response.Url = fmt.Sprintf("%s/Autodiscover/Autodiscover.xml", global.MainConfig.BaseURL)
|
||||||
return global.JSONify(response)
|
return global.JSONify(response)
|
||||||
default:
|
default:
|
||||||
|
|
||||||
response := MSAutodiscoverJSONError{}
|
response := MSAutodiscoverJSONError{}
|
||||||
response.ErrorCode = "InvalidProtocol";
|
response.ErrorCode = "InvalidProtocol";
|
||||||
response.ErrorMessage = fmt.Sprintf("The given protocol value '%s' is invalid. Supported values are 'AutodiscoverV1'", protocol)
|
response.ErrorMessage = fmt.Sprintf("The given protocol value '%s' is invalid. Supported values are 'AutodiscoverV1'", protocol)
|
||||||
return global.JSONify(response)
|
return global.JSONify(response)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
func DefaultResponse() string {
|
func DefaultResponse() string {
|
||||||
response := Response{}
|
response := Response{}
|
||||||
|
|
Loading…
Reference in a new issue