選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

oragono.go 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. "strings"
  10. "syscall"
  11. "github.com/docopt/docopt-go"
  12. "github.com/oragono/oragono/irc"
  13. "github.com/oragono/oragono/irc/logger"
  14. "github.com/oragono/oragono/irc/mkcerts"
  15. "golang.org/x/crypto/bcrypt"
  16. "golang.org/x/crypto/ssh/terminal"
  17. )
  18. var commit = ""
  19. // get a password from stdin from the user
  20. func getPassword() string {
  21. bytePassword, err := terminal.ReadPassword(int(syscall.Stdin))
  22. if err != nil {
  23. log.Fatal("Error reading password:", err.Error())
  24. }
  25. return string(bytePassword)
  26. }
  27. func main() {
  28. version := irc.SemVer
  29. usage := `oragono.
  30. Usage:
  31. oragono initdb [--conf <filename>] [--quiet]
  32. oragono upgradedb [--conf <filename>] [--quiet]
  33. oragono genpasswd [--conf <filename>] [--quiet]
  34. oragono mkcerts [--conf <filename>] [--quiet]
  35. oragono run [--conf <filename>] [--quiet]
  36. oragono -h | --help
  37. oragono --version
  38. Options:
  39. --conf <filename> Configuration file to use [default: ircd.yaml].
  40. --quiet Don't show startup/shutdown lines.
  41. -h --help Show this screen.
  42. --version Show version.`
  43. arguments, _ := docopt.ParseArgs(usage, nil, version)
  44. // don't require a config file for genpasswd
  45. if arguments["genpasswd"].(bool) {
  46. fmt.Print("Enter Password: ")
  47. password := getPassword()
  48. fmt.Print("\n")
  49. fmt.Print("Reenter Password: ")
  50. confirm := getPassword()
  51. fmt.Print("\n")
  52. if confirm != password {
  53. log.Fatal("passwords do not match")
  54. }
  55. hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.MinCost)
  56. if err != nil {
  57. log.Fatal("encoding error:", err.Error())
  58. }
  59. fmt.Println(string(hash))
  60. return
  61. }
  62. configfile := arguments["--conf"].(string)
  63. config, err := irc.LoadConfig(configfile)
  64. if err != nil {
  65. log.Fatal("Config file did not load successfully: ", err.Error())
  66. }
  67. logman, err := logger.NewManager(config.Logging)
  68. if err != nil {
  69. log.Fatal("Logger did not load successfully:", err.Error())
  70. }
  71. 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. if !arguments["--quiet"].(bool) {
  104. logman.Info("startup", fmt.Sprintf("Oragono v%s starting", irc.SemVer))
  105. if commit == "" {
  106. logman.Debug("startup", fmt.Sprintf("Could not get current commit"))
  107. } else {
  108. logman.Info("startup", fmt.Sprintf("Running commit %s", commit))
  109. }
  110. }
  111. // set current git commit
  112. irc.Commit = commit
  113. if commit != "" {
  114. irc.Ver = fmt.Sprintf("%s-%s", irc.Ver, commit)
  115. }
  116. // warning if running a non-final version
  117. if strings.Contains(irc.SemVer, "unreleased") {
  118. 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.")
  119. }
  120. server, err := irc.NewServer(config, logman)
  121. if err != nil {
  122. logman.Error("startup", fmt.Sprintf("Could not load server: %s", err.Error()))
  123. return
  124. }
  125. if !arguments["--quiet"].(bool) {
  126. logman.Info("startup", "Server running")
  127. defer logman.Info("shutdown", fmt.Sprintf("Oragono v%s exiting", irc.SemVer))
  128. }
  129. server.Run()
  130. }
  131. }