main.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package main
  2. import (
  3. "henrymail/config"
  4. "henrymail/database"
  5. "henrymail/dkim"
  6. "henrymail/imap"
  7. "henrymail/processors"
  8. "henrymail/smtp"
  9. "henrymail/web"
  10. "log"
  11. "math/rand"
  12. )
  13. func main() {
  14. config.SetupConfig()
  15. config.SetupResolver()
  16. tlsConfig := config.GetTLSConfig()
  17. db := database.NewDatabase()
  18. login := database.NewLogin(db)
  19. // submission agent processing chain
  20. var msaChain processors.MsgProcessor = processors.NewSender(db)
  21. if config.GetBool(config.DkimSign) {
  22. msaChain = processors.NewDkimSigner(dkim.GetOrCreateDkim(), msaChain)
  23. }
  24. // transfer agent processing chain
  25. mtaChain := processors.NewSaver(db)
  26. if config.GetBool(config.DkimVerify) {
  27. mtaChain = processors.NewDkimVerifier(mtaChain)
  28. }
  29. // SPF checker
  30. // Virus scanner
  31. // Spam filter
  32. SeedData(login)
  33. smtp.StartMsa(msaChain, login, tlsConfig)
  34. smtp.StartMta(mtaChain, tlsConfig)
  35. imap.StartImap(login, db, tlsConfig)
  36. web.StartWebAdmin(login, db, tlsConfig)
  37. if config.GetBool(config.FakeDns) {
  38. StartFakeDns(config.GetString(config.FakeDnsAddress), "udp")
  39. }
  40. // Wait for exit
  41. select {}
  42. }
  43. func SeedData(login database.Login) {
  44. var pw string
  45. if config.GetString(config.AdminPassword) == "" {
  46. pw = randSeq(8)
  47. } else {
  48. pw = config.GetString(config.AdminPassword)
  49. }
  50. usr, err := login.NewUser(config.GetString(config.AdminUsername),
  51. pw, true)
  52. if err == nil {
  53. log.Printf("Generated admin user: %v password %v", usr.Username, pw)
  54. }
  55. }
  56. const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  57. func randSeq(n int) string {
  58. b := make([]byte, n)
  59. for i := range b {
  60. b[i] = letters[rand.Intn(len(letters))]
  61. }
  62. return string(b)
  63. }