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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. var commit = ""
  22. // get a password from stdin from the user
  23. func getPassword() string {
  24. bytePassword, err := terminal.ReadPassword(int(syscall.Stdin))
  25. if err != nil {
  26. log.Fatal("Error reading password:", err.Error())
  27. }
  28. return string(bytePassword)
  29. }
  30. func main() {
  31. version := irc.SemVer
  32. usage := `oragono.
  33. Usage:
  34. oragono initdb [--conf <filename>] [--quiet]
  35. oragono upgradedb [--conf <filename>] [--quiet]
  36. oragono genpasswd [--conf <filename>] [--quiet]
  37. oragono mkcerts [--conf <filename>] [--quiet]
  38. oragono run [--conf <filename>] [--quiet]
  39. oragono -h | --help
  40. oragono --version
  41. Options:
  42. --conf <filename> Configuration file to use [default: ircd.yaml].
  43. --quiet Don't show startup/shutdown lines.
  44. -h --help Show this screen.
  45. --version Show version.`
  46. arguments, _ := docopt.Parse(usage, nil, true, version, false)
  47. configfile := arguments["--conf"].(string)
  48. config, err := irc.LoadConfig(configfile)
  49. if err != nil {
  50. log.Fatal("Config file did not load successfully: ", err.Error())
  51. }
  52. logman, err := logger.NewManager(config.Logging)
  53. if err != nil {
  54. log.Fatal("Logger did not load successfully:", err.Error())
  55. }
  56. if arguments["genpasswd"].(bool) {
  57. fmt.Print("Enter Password: ")
  58. password := getPassword()
  59. fmt.Print("\n")
  60. fmt.Print("Reenter Password: ")
  61. confirm := getPassword()
  62. fmt.Print("\n")
  63. if confirm != password {
  64. log.Fatal("passwords do not match")
  65. }
  66. encoded, err := passwd.GenerateEncodedPassword(password)
  67. if err != nil {
  68. log.Fatal("encoding error:", err.Error())
  69. }
  70. fmt.Println(encoded)
  71. } else if arguments["initdb"].(bool) {
  72. irc.InitDB(config.Datastore.Path)
  73. if !arguments["--quiet"].(bool) {
  74. log.Println("database initialized: ", config.Datastore.Path)
  75. }
  76. } else if arguments["upgradedb"].(bool) {
  77. irc.UpgradeDB(config.Datastore.Path)
  78. if !arguments["--quiet"].(bool) {
  79. log.Println("database upgraded: ", config.Datastore.Path)
  80. }
  81. } else if arguments["mkcerts"].(bool) {
  82. if !arguments["--quiet"].(bool) {
  83. log.Println("making self-signed certificates")
  84. }
  85. for name, conf := range config.Server.TLSListeners {
  86. if !arguments["--quiet"].(bool) {
  87. log.Printf(" making cert for %s listener\n", name)
  88. }
  89. host := config.Server.Name
  90. err := mkcerts.CreateCert("Oragono", host, conf.Cert, conf.Key)
  91. if err == nil {
  92. if !arguments["--quiet"].(bool) {
  93. log.Printf(" Certificate created at %s : %s\n", conf.Cert, conf.Key)
  94. }
  95. } else {
  96. log.Fatal(" Could not create certificate:", err.Error())
  97. }
  98. }
  99. } else if arguments["run"].(bool) {
  100. rand.Seed(time.Now().UTC().UnixNano())
  101. if !arguments["--quiet"].(bool) {
  102. logman.Info("startup", fmt.Sprintf("Oragono v%s starting", irc.SemVer))
  103. if commit == "" {
  104. logman.Debug("startup", fmt.Sprintf("Could not get current commit"))
  105. } else {
  106. logman.Info("startup", fmt.Sprintf("Running commit %s", commit))
  107. }
  108. }
  109. // set current git commit
  110. irc.Commit = commit
  111. if commit != "" {
  112. irc.Ver = fmt.Sprintf("%s-%s", irc.Ver, commit)
  113. }
  114. // profiling
  115. if config.Debug.StackImpact.Enabled {
  116. if config.Debug.StackImpact.AgentKey == "" || config.Debug.StackImpact.AppName == "" {
  117. logman.Error("startup", "Could not start StackImpact - agent-key or app-name are undefined")
  118. return
  119. }
  120. agent := stackimpact.NewAgent()
  121. agent.Start(stackimpact.Options{AgentKey: config.Debug.StackImpact.AgentKey, AppName: config.Debug.StackImpact.AppName})
  122. defer agent.RecordPanic()
  123. logman.Info("startup", fmt.Sprintf("StackImpact profiling started as %s", config.Debug.StackImpact.AppName))
  124. }
  125. // warning if running a non-final version
  126. if strings.Contains(irc.SemVer, "unreleased") {
  127. 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.")
  128. }
  129. server, err := irc.NewServer(config, logman)
  130. if err != nil {
  131. logman.Error("startup", fmt.Sprintf("Could not load server: %s", err.Error()))
  132. return
  133. }
  134. if !arguments["--quiet"].(bool) {
  135. logman.Info("startup", "Server running")
  136. defer logman.Info("shutdown", fmt.Sprintf("Oragono v%s exiting", irc.SemVer))
  137. }
  138. server.Run()
  139. }
  140. }