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

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