Provide an HTTP anthentification over XMPP (XEP-0070).

main.go 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package main
  2. import (
  3. "git.kingpenguin.tk/chteufleur/HTTPAuthentificationOverXMPP.git/http"
  4. "git.kingpenguin.tk/chteufleur/HTTPAuthentificationOverXMPP.git/xmpp"
  5. "github.com/jimlawless/cfg"
  6. "log"
  7. "os"
  8. "os/signal"
  9. "strconv"
  10. "strings"
  11. "syscall"
  12. "time"
  13. )
  14. const (
  15. Version = "v0.5-dev"
  16. configurationFilePath = "http-auth/httpAuth.conf"
  17. langFilePath = "http-auth/messages.lang"
  18. PathConfEnvVariable = "XDG_CONFIG_DIRS"
  19. DefaultXdgConfigDirs = "/etc/xdg"
  20. )
  21. var (
  22. mapConfig = make(map[string]string)
  23. )
  24. func init() {
  25. log.Printf("Running HTTP-Auth %v", Version)
  26. if !loadConfigFile() {
  27. log.Fatal("Failed to load configuration file.")
  28. }
  29. loadLangFile()
  30. // HTTP config
  31. httpTimeout, err := strconv.Atoi(mapConfig["http_timeout_sec"])
  32. if err == nil && httpTimeout > 0 && httpTimeout < http.MaxTimeout {
  33. log.Println("Define HTTP timeout to " + strconv.Itoa(httpTimeout) + " second")
  34. http.TimeoutSec = httpTimeout
  35. }
  36. httpPort, err := strconv.Atoi(mapConfig["http_port"])
  37. if err == nil {
  38. log.Println("Define HTTP port to " + strconv.Itoa(httpPort))
  39. http.HttpPortBind = httpPort
  40. }
  41. httpsPort, err := strconv.Atoi(mapConfig["https_port"])
  42. if err == nil {
  43. log.Println("Define HTTPS port to " + strconv.Itoa(httpsPort))
  44. http.HttpsPortBind = httpsPort
  45. http.CertPath = mapConfig["https_cert_path"]
  46. http.KeyPath = mapConfig["https_key_path"]
  47. }
  48. bindAddressIPv4 := mapConfig["http_bind_address_ipv4"]
  49. if bindAddressIPv4 != "" {
  50. http.BindAddressIPv4 = bindAddressIPv4
  51. }
  52. bindAddressIPv6 := mapConfig["http_bind_address_ipv6"]
  53. if bindAddressIPv6 != "" {
  54. http.BindAddressIPv6 = bindAddressIPv6
  55. }
  56. // XMPP config
  57. xmpp_server_address := mapConfig["xmpp_server_address"]
  58. if xmpp_server_address != "" {
  59. xmpp.Addr = xmpp_server_address
  60. }
  61. xmpp_server_port := mapConfig["xmpp_server_port"]
  62. if xmpp_server_port != "" {
  63. xmpp.Port = xmpp_server_port
  64. }
  65. xmpp.JidStr = mapConfig["xmpp_jid"]
  66. xmpp.Secret = mapConfig["xmpp_secret"]
  67. xmpp.Debug = mapConfig["xmpp_debug"] == "true"
  68. xmpp.VerifyCertValidity = mapConfig["xmpp_verify_cert_validity"] != "false" // Default TRUE
  69. }
  70. func loadConfigFile() bool {
  71. ret := false
  72. envVariable := os.Getenv(PathConfEnvVariable)
  73. if envVariable == "" {
  74. envVariable = DefaultXdgConfigDirs
  75. }
  76. for _, path := range strings.Split(envVariable, ":") {
  77. log.Println("Try to find configuration file into " + path)
  78. configFile := path + "/" + configurationFilePath
  79. if _, err := os.Stat(configFile); err == nil {
  80. // The config file exist
  81. if cfg.Load(configFile, mapConfig) == nil {
  82. // And has been loaded succesfully
  83. log.Println("Find configuration file at " + configFile)
  84. ret = true
  85. break
  86. }
  87. }
  88. }
  89. return ret
  90. }
  91. func loadLangFile() bool {
  92. ret := false
  93. envVariable := os.Getenv(PathConfEnvVariable)
  94. if envVariable == "" {
  95. envVariable = DefaultXdgConfigDirs
  96. }
  97. for _, path := range strings.Split(envVariable, ":") {
  98. log.Println("Try to find messages lang file into " + path)
  99. langFile := path + "/" + langFilePath
  100. if _, err := os.Stat(langFile); err == nil {
  101. // The config file exist
  102. if cfg.Load(langFile, xmpp.MapLangs) == nil {
  103. // And has been loaded succesfully
  104. log.Println("Find messages lang file at " + langFile)
  105. ret = true
  106. break
  107. }
  108. }
  109. }
  110. return ret
  111. }
  112. func main() {
  113. go http.Run()
  114. go xmpp.Run()
  115. sigchan := make(chan os.Signal, 1)
  116. signal.Notify(sigchan, os.Interrupt)
  117. signal.Notify(sigchan, syscall.SIGTERM)
  118. signal.Notify(sigchan, os.Kill)
  119. <-sigchan
  120. log.Println("Exit main()")
  121. time.Sleep(1 * time.Second)
  122. }