config.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package main
  2. import (
  3. "encoding/json"
  4. "flag"
  5. "fmt"
  6. "io/ioutil"
  7. "log"
  8. "os"
  9. "strings"
  10. )
  11. type GlobalConfig struct {
  12. Allowed_hosts string `json:"GM_ALLOWED_HOSTS"`
  13. Primary_host string `json:"GM_PRIMARY_MAIL_HOST"`
  14. Verbose bool `json:"verbose"`
  15. Mysql_table string `json:"GM_MAIL_TABLE"`
  16. Mysql_db string `json:"MYSQL_DB"`
  17. Mysql_host string `json:"MYSQL_HOST"`
  18. Mysql_pass string `json:"MYSQL_PASS"`
  19. Mysql_user string `json:"MYSQL_USER"`
  20. Servers []ServerConfig `json:"servers"`
  21. Pid_file string `json:"pid_file,omitempty"`
  22. Save_workers_size int `json:"save_workers_size"`
  23. redis_expire_seconds int `json:"redis_expire_seconds"`
  24. }
  25. type ServerConfig struct {
  26. Is_enabled bool `json:"is_enabled"`
  27. Host_name string `json:"host_name"`
  28. Max_size int `json:"max_size"`
  29. Private_key_file string `json:"private_key_file"`
  30. Public_key_file string `json:"public_key_file"`
  31. Timeout int `json:"timeout"`
  32. Listen_interface string `json:"listen_interface"`
  33. Start_tls_on bool `json:"start_tls_on,omitempty"`
  34. Is_tls_on bool `json:"is_tls_on,omitempty"`
  35. Max_clients int `json:"max_clients"`
  36. Log_file string `json:"log_file"`
  37. }
  38. // defaults. Overwrite any of these in the configure() function which loads them from a json file
  39. /*
  40. var gConfig = map[string]interface{}{
  41. "GSMTP_MAX_SIZE": "131072",
  42. "GSMTP_HOST_NAME": "server.example.com", // This should also be set to reflect your RDNS
  43. "GSMTP_VERBOSE": "Y",
  44. "GSMTP_LOG_FILE": "", // Eg. /var/log/goguerrilla.log or leave blank if no logging
  45. "GSMTP_TIMEOUT": "100", // how many seconds before timeout.
  46. "MYSQL_HOST": "127.0.0.1:3306",
  47. "MYSQL_USER": "gmail_mail",
  48. "MYSQL_PASS": "ok",
  49. "MYSQL_DB": "gmail_mail",
  50. "GM_MAIL_TABLE": "new_mail",
  51. "GSTMP_LISTEN_INTERFACE": "0.0.0.0:25",
  52. "GSMTP_PUB_KEY": "/etc/ssl/certs/ssl-cert-snakeoil.pem",
  53. "GSMTP_PRV_KEY": "/etc/ssl/private/ssl-cert-snakeoil.key",
  54. "GM_ALLOWED_HOSTS": "guerrillamail.de,guerrillamailblock.com",
  55. "GM_PRIMARY_MAIL_HOST": "guerrillamail.com",
  56. "GM_MAX_CLIENTS": "500",
  57. "NGINX_AUTH_ENABLED": "N", // Y or N
  58. "NGINX_AUTH": "127.0.0.1:8025", // If using Nginx proxy, ip and port to serve Auth requsts
  59. "PID_FILE": "/var/run/go-guerrilla.pid",
  60. "GSMTP_MAIL_EXPIRE_SECONDS": "72000",
  61. }
  62. */
  63. var theConfig GlobalConfig
  64. var flagVerbose, flagIface, flagConfigFile string
  65. // config is read at startup, or when a SIG_HUP is caught
  66. func readConfig() {
  67. log.SetOutput(os.Stdout)
  68. // parse command line arguments
  69. if !flag.Parsed() {
  70. flag.StringVar(&flagConfigFile, "config", "goguerrilla.conf", "Path to the configuration file")
  71. flag.StringVar(&flagVerbose, "v", "n", "Verbose, [y | n] ")
  72. flag.StringVar(&flagIface, "if", "", "Interface and port to listen on, eg. 127.0.0.1:2525 ")
  73. flag.Parse()
  74. }
  75. // load in the config.
  76. b, err := ioutil.ReadFile(flagConfigFile)
  77. if err != nil {
  78. log.Fatalln("Could not read config file", err)
  79. }
  80. theConfig = GlobalConfig{}
  81. err = json.Unmarshal(b, &theConfig)
  82. //fmt.Println(theConfig)
  83. //fmt.Println(fmt.Sprintf("allowed hosts: %s", theConfig.Allowed_hosts))
  84. //log.Fatalln("Could not parse config file:", theConfig)
  85. if err != nil {
  86. fmt.Println("Could not parse config file:", err)
  87. log.Fatalln("Could not parse config file:", err)
  88. }
  89. // copy command line flag over so it takes precedence
  90. if len(flagVerbose) > 0 && strings.ToUpper(flagVerbose) == "Y" {
  91. theConfig.Verbose = true
  92. }
  93. if len(flagIface) > 0 {
  94. theConfig.Servers[0].Listen_interface = flagIface
  95. }
  96. // map the allow hosts for easy lookup
  97. if len(theConfig.Allowed_hosts) > 0 {
  98. if arr := strings.Split(theConfig.Allowed_hosts, ","); len(arr) > 0 {
  99. for i := 0; i < len(arr); i++ {
  100. allowedHosts[arr[i]] = true
  101. }
  102. }
  103. } else {
  104. log.Fatalln("Config error, GM_ALLOWED_HOSTS must be s string.")
  105. }
  106. if theConfig.Pid_file == "" {
  107. theConfig.Pid_file = "/var/run/go-guerrilla.pid"
  108. }
  109. return
  110. }