HTTPAuthentificationOverXMPP/main.go

148 lines
3.7 KiB
Go

package main
import (
"git.kingpenguin.tk/chteufleur/HTTPAuthentificationOverXMPP.git/http"
"git.kingpenguin.tk/chteufleur/HTTPAuthentificationOverXMPP.git/xmpp"
"github.com/jimlawless/cfg"
"log"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"time"
)
const (
Version = "v0.5-dev"
configurationDirectory = "http-auth"
configurationFilePath = "httpAuth.conf"
langFilePath = "messages.lang"
oauthClientStoreFilePath = "clientStore.json"
PathConfEnvVariable = "XDG_CONFIG_DIRS"
DefaultXdgConfigDirs = "/etc/xdg"
)
var (
mapConfig = make(map[string]string)
)
func init() {
log.Printf("Running HTTP-Auth %v", Version)
if !loadConfigFile() {
log.Fatal("Failed to load configuration file.")
}
loadLangFile()
loadOauthClientStore()
// HTTP config
httpTimeout, err := strconv.Atoi(mapConfig["http_timeout_sec"])
if err == nil && httpTimeout > 0 && httpTimeout < xmpp.MaxTimeout {
log.Println("Define HTTP timeout to " + strconv.Itoa(httpTimeout) + " second")
xmpp.TimeoutSec = httpTimeout
}
httpPort, err := strconv.Atoi(mapConfig["http_port"])
if err == nil {
log.Println("Define HTTP port to " + strconv.Itoa(httpPort))
http.HttpPortBind = httpPort
}
httpsPort, err := strconv.Atoi(mapConfig["https_port"])
if err == nil {
log.Println("Define HTTPS port to " + strconv.Itoa(httpsPort))
http.HttpsPortBind = httpsPort
http.CertPath = mapConfig["https_cert_path"]
http.KeyPath = mapConfig["https_key_path"]
}
bindAddressIPv4 := mapConfig["http_bind_address_ipv4"]
if bindAddressIPv4 != "" {
http.BindAddressIPv4 = bindAddressIPv4
}
bindAddressIPv6 := mapConfig["http_bind_address_ipv6"]
if bindAddressIPv6 != "" {
http.BindAddressIPv6 = bindAddressIPv6
}
// XMPP config
xmpp_server_address := mapConfig["xmpp_server_address"]
if xmpp_server_address != "" {
xmpp.Addr = xmpp_server_address
}
xmpp_server_port := mapConfig["xmpp_server_port"]
if xmpp_server_port != "" {
xmpp.Port = xmpp_server_port
}
xmpp.JidStr = mapConfig["xmpp_jid"]
xmpp.Secret = mapConfig["xmpp_secret"]
xmpp.Debug = mapConfig["xmpp_debug"] == "true"
xmpp.VerifyCertValidity = mapConfig["xmpp_verify_cert_validity"] != "false" // Default TRUE
}
func loadConfigurationFile(file string) string {
ret := ""
envVariable := os.Getenv(PathConfEnvVariable)
if envVariable == "" {
envVariable = DefaultXdgConfigDirs
}
for _, path := range strings.Split(envVariable, ":") {
log.Println("Try to find file (" + file + ") into " + path)
configFile := path + "/" + configurationDirectory + "/" + file
if _, err := os.Stat(configFile); err == nil {
// The file exist
ret = configFile
break
}
}
return ret
}
func loadConfigFile() bool {
ret := false
configFile := loadConfigurationFile(configurationFilePath)
if configFile != "" && cfg.Load(configFile, mapConfig) == nil {
// And has been loaded succesfully
log.Println("Find configuration file at " + configFile)
ret = true
}
return ret
}
func loadLangFile() bool {
ret := false
langFile := loadConfigurationFile(langFilePath)
if cfg.Load(langFile, xmpp.MapLangs) == nil {
// And has been loaded succesfully
log.Println("Find messages lang file at " + langFile)
ret = true
}
return ret
}
func loadOauthClientStore() {
clientStore := loadConfigurationFile(oauthClientStoreFilePath)
err := http.LoadConfigClient(clientStore)
if err == nil {
log.Printf("Find OAuth client store file at " + clientStore)
} else {
log.Printf("Failed to load OAuth client store: ", err)
}
}
func main() {
go http.Run()
go xmpp.Run()
sigchan := make(chan os.Signal, 1)
signal.Notify(sigchan, os.Interrupt)
signal.Notify(sigchan, syscall.SIGTERM)
signal.Notify(sigchan, os.Kill)
<-sigchan
log.Println("Exit main()")
time.Sleep(1 * time.Second)
}