You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

oragono.go 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright (c) 2012-2014 Jeremy Latt
  2. // Copyright (c) 2014-2015 Edmund Huber
  3. // Copyright (c) 2016-2017 Daniel Oaks <daniel@danieloaks.net>
  4. // released under the MIT license
  5. package main
  6. import (
  7. "fmt"
  8. "log"
  9. "math/rand"
  10. "strings"
  11. "syscall"
  12. "time"
  13. "github.com/docopt/docopt-go"
  14. "github.com/oragono/oragono/irc"
  15. "github.com/oragono/oragono/irc/logger"
  16. "github.com/oragono/oragono/irc/mkcerts"
  17. "github.com/oragono/oragono/irc/passwd"
  18. stackimpact "github.com/stackimpact/stackimpact-go"
  19. "golang.org/x/crypto/ssh/terminal"
  20. )
  21. func main() {
  22. version := irc.SemVer
  23. usage := `oragono.
  24. Usage:
  25. oragono initdb [--conf <filename>] [--quiet]
  26. oragono upgradedb [--conf <filename>] [--quiet]
  27. oragono genpasswd [--conf <filename>] [--quiet]
  28. oragono mkcerts [--conf <filename>] [--quiet]
  29. oragono run [--conf <filename>] [--quiet]
  30. oragono -h | --help
  31. oragono --version
  32. Options:
  33. --conf <filename> Configuration file to use [default: ircd.yaml].
  34. --quiet Don't show startup/shutdown lines.
  35. -h --help Show this screen.
  36. --version Show version.`
  37. arguments, _ := docopt.Parse(usage, nil, true, version, false)
  38. configfile := arguments["--conf"].(string)
  39. config, err := irc.LoadConfig(configfile)
  40. if err != nil {
  41. log.Fatal("Config file did not load successfully: ", err.Error())
  42. }
  43. logman, err := logger.NewManager(config.Logging)
  44. if err != nil {
  45. log.Fatal("Logger did not load successfully:", err.Error())
  46. }
  47. if arguments["genpasswd"].(bool) {
  48. fmt.Print("Enter Password: ")
  49. bytePassword, err := terminal.ReadPassword(int(syscall.Stdin))
  50. if err != nil {
  51. log.Fatal("Error reading password:", err.Error())
  52. }
  53. password := string(bytePassword)
  54. encoded, err := passwd.GenerateEncodedPassword(password)
  55. if err != nil {
  56. log.Fatal("encoding error:", err.Error())
  57. }
  58. fmt.Print("\n")
  59. fmt.Println(encoded)
  60. } else if arguments["initdb"].(bool) {
  61. irc.InitDB(config.Datastore.Path)
  62. if !arguments["--quiet"].(bool) {
  63. log.Println("database initialized: ", config.Datastore.Path)
  64. }
  65. } else if arguments["upgradedb"].(bool) {
  66. irc.UpgradeDB(config.Datastore.Path)
  67. if !arguments["--quiet"].(bool) {
  68. log.Println("database upgraded: ", config.Datastore.Path)
  69. }
  70. } else if arguments["mkcerts"].(bool) {
  71. if !arguments["--quiet"].(bool) {
  72. log.Println("making self-signed certificates")
  73. }
  74. for name, conf := range config.Server.TLSListeners {
  75. if !arguments["--quiet"].(bool) {
  76. log.Printf(" making cert for %s listener\n", name)
  77. }
  78. host := config.Server.Name
  79. err := mkcerts.CreateCert("Oragono", host, conf.Cert, conf.Key)
  80. if err == nil {
  81. if !arguments["--quiet"].(bool) {
  82. log.Printf(" Certificate created at %s : %s\n", conf.Cert, conf.Key)
  83. }
  84. } else {
  85. log.Fatal(" Could not create certificate:", err.Error())
  86. }
  87. }
  88. } else if arguments["run"].(bool) {
  89. rand.Seed(time.Now().UTC().UnixNano())
  90. if !arguments["--quiet"].(bool) {
  91. logman.Info("startup", fmt.Sprintf("Oragono v%s starting", irc.SemVer))
  92. }
  93. // profiling
  94. if config.Debug.StackImpact.Enabled {
  95. if config.Debug.StackImpact.AgentKey == "" || config.Debug.StackImpact.AppName == "" {
  96. logman.Error("startup", "Could not start StackImpact - agent-key or app-name are undefined")
  97. return
  98. }
  99. agent := stackimpact.NewAgent()
  100. agent.Start(stackimpact.Options{AgentKey: config.Debug.StackImpact.AgentKey, AppName: config.Debug.StackImpact.AppName})
  101. defer agent.RecordPanic()
  102. logman.Info("startup", fmt.Sprintf("StackImpact profiling started as %s", config.Debug.StackImpact.AppName))
  103. }
  104. // warning if running a non-final version
  105. if strings.Contains(irc.SemVer, "unreleased") {
  106. logman.Warning("startup", "You are currently running an unreleased beta version of Oragono that may be unstable and could corrupt your database.\nIf you are running a production network, please download the latest build from https://oragono.io/downloads.html and run that instead.")
  107. }
  108. server, err := irc.NewServer(config, logman)
  109. if err != nil {
  110. logman.Error("startup", fmt.Sprintf("Could not load server: %s", err.Error()))
  111. return
  112. }
  113. if !arguments["--quiet"].(bool) {
  114. logman.Info("startup", "Server running")
  115. defer logman.Info("shutdown", fmt.Sprintf("Oragono v%s exiting", irc.SemVer))
  116. }
  117. server.Run()
  118. }
  119. }