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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. stackimpact "github.com/stackimpact/stackimpact-go"
  18. "golang.org/x/crypto/bcrypt"
  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. hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.MinCost)
  67. if err != nil {
  68. log.Fatal("encoding error:", err.Error())
  69. }
  70. fmt.Println(string(hash))
  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. err = irc.UpgradeDB(config)
  78. if err != nil {
  79. log.Fatal("Error while upgrading db:", err.Error())
  80. }
  81. if !arguments["--quiet"].(bool) {
  82. log.Println("database upgraded: ", config.Datastore.Path)
  83. }
  84. } else if arguments["mkcerts"].(bool) {
  85. if !arguments["--quiet"].(bool) {
  86. log.Println("making self-signed certificates")
  87. }
  88. for name, conf := range config.Server.TLSListeners {
  89. if !arguments["--quiet"].(bool) {
  90. log.Printf(" making cert for %s listener\n", name)
  91. }
  92. host := config.Server.Name
  93. err := mkcerts.CreateCert("Oragono", host, conf.Cert, conf.Key)
  94. if err == nil {
  95. if !arguments["--quiet"].(bool) {
  96. log.Printf(" Certificate created at %s : %s\n", conf.Cert, conf.Key)
  97. }
  98. } else {
  99. log.Fatal(" Could not create certificate:", err.Error())
  100. }
  101. }
  102. } else if arguments["run"].(bool) {
  103. rand.Seed(time.Now().UTC().UnixNano())
  104. if !arguments["--quiet"].(bool) {
  105. logman.Info("startup", fmt.Sprintf("Oragono v%s starting", irc.SemVer))
  106. if commit == "" {
  107. logman.Debug("startup", fmt.Sprintf("Could not get current commit"))
  108. } else {
  109. logman.Info("startup", fmt.Sprintf("Running commit %s", commit))
  110. }
  111. }
  112. // set current git commit
  113. irc.Commit = commit
  114. if commit != "" {
  115. irc.Ver = fmt.Sprintf("%s-%s", irc.Ver, commit)
  116. }
  117. // profiling
  118. if config.Debug.StackImpact.Enabled {
  119. if config.Debug.StackImpact.AgentKey == "" || config.Debug.StackImpact.AppName == "" {
  120. logman.Error("startup", "Could not start StackImpact - agent-key or app-name are undefined")
  121. return
  122. }
  123. agent := stackimpact.NewAgent()
  124. agent.Start(stackimpact.Options{AgentKey: config.Debug.StackImpact.AgentKey, AppName: config.Debug.StackImpact.AppName})
  125. defer agent.RecordPanic()
  126. logman.Info("startup", fmt.Sprintf("StackImpact profiling started as %s", config.Debug.StackImpact.AppName))
  127. }
  128. // warning if running a non-final version
  129. if strings.Contains(irc.SemVer, "unreleased") {
  130. 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.")
  131. }
  132. server, err := irc.NewServer(config, logman)
  133. if err != nil {
  134. logman.Error("startup", fmt.Sprintf("Could not load server: %s", err.Error()))
  135. return
  136. }
  137. if !arguments["--quiet"].(bool) {
  138. logman.Info("startup", "Server running")
  139. defer logman.Info("shutdown", fmt.Sprintf("Oragono v%s exiting", irc.SemVer))
  140. }
  141. server.Run()
  142. }
  143. }