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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. "bufio"
  8. "fmt"
  9. "log"
  10. "os"
  11. "strings"
  12. "syscall"
  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/utils"
  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. fd := int(os.Stdin.Fd())
  25. if terminal.IsTerminal(fd) {
  26. bytePassword, err := terminal.ReadPassword(int(syscall.Stdin))
  27. if err != nil {
  28. log.Fatal("Error reading password:", err.Error())
  29. }
  30. return string(bytePassword)
  31. }
  32. reader := bufio.NewReader(os.Stdin)
  33. text, _ := reader.ReadString('\n')
  34. return strings.TrimSpace(text)
  35. }
  36. func main() {
  37. version := irc.SemVer
  38. usage := `oragono.
  39. Usage:
  40. oragono initdb [--conf <filename>] [--quiet]
  41. oragono upgradedb [--conf <filename>] [--quiet]
  42. oragono genpasswd [--conf <filename>] [--quiet]
  43. oragono mkcerts [--conf <filename>] [--quiet]
  44. oragono mksecret [--conf <filename>] [--quiet]
  45. oragono run [--conf <filename>] [--quiet]
  46. oragono -h | --help
  47. oragono --version
  48. Options:
  49. --conf <filename> Configuration file to use [default: ircd.yaml].
  50. --quiet Don't show startup/shutdown lines.
  51. -h --help Show this screen.
  52. --version Show version.`
  53. arguments, _ := docopt.ParseArgs(usage, nil, version)
  54. // don't require a config file for genpasswd or mksecret
  55. if arguments["genpasswd"].(bool) {
  56. var password string
  57. fd := int(os.Stdin.Fd())
  58. if terminal.IsTerminal(fd) {
  59. fmt.Print("Enter Password: ")
  60. password = getPassword()
  61. fmt.Print("\n")
  62. fmt.Print("Reenter Password: ")
  63. confirm := getPassword()
  64. fmt.Print("\n")
  65. if confirm != password {
  66. log.Fatal("passwords do not match")
  67. }
  68. } else {
  69. password = getPassword()
  70. }
  71. hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.MinCost)
  72. if err != nil {
  73. log.Fatal("encoding error:", err.Error())
  74. }
  75. fmt.Print(string(hash))
  76. if terminal.IsTerminal(fd) {
  77. fmt.Println()
  78. }
  79. return
  80. } else if arguments["mksecret"].(bool) {
  81. fmt.Println(utils.GenerateSecretKey())
  82. return
  83. }
  84. configfile := arguments["--conf"].(string)
  85. config, err := irc.LoadConfig(configfile)
  86. if err != nil {
  87. log.Fatal("Config file did not load successfully: ", err.Error())
  88. }
  89. logman, err := logger.NewManager(config.Logging)
  90. if err != nil {
  91. log.Fatal("Logger did not load successfully:", err.Error())
  92. }
  93. if arguments["initdb"].(bool) {
  94. irc.InitDB(config.Datastore.Path)
  95. if !arguments["--quiet"].(bool) {
  96. log.Println("database initialized: ", config.Datastore.Path)
  97. }
  98. } else if arguments["upgradedb"].(bool) {
  99. err = irc.UpgradeDB(config)
  100. if err != nil {
  101. log.Fatal("Error while upgrading db:", err.Error())
  102. }
  103. if !arguments["--quiet"].(bool) {
  104. log.Println("database upgraded: ", config.Datastore.Path)
  105. }
  106. } else if arguments["mkcerts"].(bool) {
  107. if !arguments["--quiet"].(bool) {
  108. log.Println("making self-signed certificates")
  109. }
  110. for name, conf := range config.Server.TLSListeners {
  111. if !arguments["--quiet"].(bool) {
  112. log.Printf(" making cert for %s listener\n", name)
  113. }
  114. host := config.Server.Name
  115. err := mkcerts.CreateCert("Oragono", host, conf.Cert, conf.Key)
  116. if err == nil {
  117. if !arguments["--quiet"].(bool) {
  118. log.Printf(" Certificate created at %s : %s\n", conf.Cert, conf.Key)
  119. }
  120. } else {
  121. log.Fatal(" Could not create certificate:", err.Error())
  122. }
  123. }
  124. } else if arguments["run"].(bool) {
  125. if !arguments["--quiet"].(bool) {
  126. logman.Info("server", fmt.Sprintf("Oragono v%s starting", irc.SemVer))
  127. if commit == "" {
  128. logman.Debug("server", fmt.Sprintf("Could not get current commit"))
  129. } else {
  130. logman.Info("server", fmt.Sprintf("Running commit %s", commit))
  131. }
  132. }
  133. // set current git commit
  134. irc.Commit = commit
  135. if commit != "" {
  136. irc.Ver = fmt.Sprintf("%s-%s", irc.Ver, commit)
  137. }
  138. // warning if running a non-final version
  139. if strings.Contains(irc.SemVer, "unreleased") {
  140. logman.Warning("server", "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.")
  141. }
  142. server, err := irc.NewServer(config, logman)
  143. if err != nil {
  144. logman.Error("server", fmt.Sprintf("Could not load server: %s", err.Error()))
  145. os.Exit(1)
  146. }
  147. if !arguments["--quiet"].(bool) {
  148. logman.Info("server", "Server running")
  149. defer logman.Info("server", fmt.Sprintf("Oragono v%s exiting", irc.SemVer))
  150. }
  151. server.Run()
  152. }
  153. }