3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-12-12 13:59:25 +01:00
ergo/oragono.go

164 lines
4.6 KiB
Go
Raw Normal View History

// Copyright (c) 2012-2014 Jeremy Latt
// Copyright (c) 2014-2015 Edmund Huber
2017-03-27 14:15:02 +02:00
// Copyright (c) 2016-2017 Daniel Oaks <daniel@danieloaks.net>
// released under the MIT license
2014-02-08 22:18:11 +01:00
package main
import (
2019-02-02 22:21:46 +01:00
"bufio"
2014-02-24 07:21:39 +01:00
"fmt"
2014-02-09 16:53:42 +01:00
"log"
2019-02-02 22:21:46 +01:00
"os"
"strings"
"syscall"
2014-03-13 02:57:00 +01:00
"github.com/docopt/docopt-go"
2017-06-14 20:00:53 +02:00
"github.com/oragono/oragono/irc"
"github.com/oragono/oragono/irc/logger"
2018-02-03 20:34:26 +01:00
"github.com/oragono/oragono/irc/mkcerts"
"golang.org/x/crypto/bcrypt"
"golang.org/x/crypto/ssh/terminal"
)
2014-03-13 02:57:00 +01:00
2018-04-09 02:08:54 +02:00
var commit = ""
// get a password from stdin from the user
func getPassword() string {
2019-02-02 22:21:46 +01:00
fd := int(os.Stdin.Fd())
if terminal.IsTerminal(fd) {
bytePassword, err := terminal.ReadPassword(int(syscall.Stdin))
if err != nil {
log.Fatal("Error reading password:", err.Error())
}
return string(bytePassword)
}
2019-02-02 22:21:46 +01:00
reader := bufio.NewReader(os.Stdin)
text, _ := reader.ReadString('\n')
2019-02-02 23:08:12 +01:00
return strings.TrimSpace(text)
}
func main() {
2016-10-13 09:36:44 +02:00
version := irc.SemVer
usage := `oragono.
Usage:
2016-09-19 14:30:45 +02:00
oragono initdb [--conf <filename>] [--quiet]
oragono upgradedb [--conf <filename>] [--quiet]
oragono genpasswd [--conf <filename>] [--quiet]
oragono mkcerts [--conf <filename>] [--quiet]
oragono run [--conf <filename>] [--quiet]
oragono -h | --help
oragono --version
Options:
--conf <filename> Configuration file to use [default: ircd.yaml].
2016-09-19 14:30:45 +02:00
--quiet Don't show startup/shutdown lines.
-h --help Show this screen.
--version Show version.`
arguments, _ := docopt.ParseArgs(usage, nil, version)
2018-12-28 19:45:55 +01:00
// don't require a config file for genpasswd
if arguments["genpasswd"].(bool) {
2019-02-02 22:21:46 +01:00
var password string
fd := int(os.Stdin.Fd())
if terminal.IsTerminal(fd) {
fmt.Print("Enter Password: ")
password = getPassword()
fmt.Print("\n")
fmt.Print("Reenter Password: ")
confirm := getPassword()
fmt.Print("\n")
if confirm != password {
log.Fatal("passwords do not match")
}
} else {
password = getPassword()
}
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.MinCost)
if err != nil {
2017-03-06 06:50:23 +01:00
log.Fatal("encoding error:", err.Error())
}
2019-02-02 22:21:46 +01:00
fmt.Print(string(hash))
if terminal.IsTerminal(fd) {
fmt.Println()
}
2018-12-28 19:45:55 +01:00
return
}
configfile := arguments["--conf"].(string)
config, err := irc.LoadConfig(configfile)
if err != nil {
log.Fatal("Config file did not load successfully: ", err.Error())
}
logman, err := logger.NewManager(config.Logging)
if err != nil {
log.Fatal("Logger did not load successfully:", err.Error())
}
if arguments["initdb"].(bool) {
irc.InitDB(config.Datastore.Path)
2016-09-19 14:30:45 +02:00
if !arguments["--quiet"].(bool) {
log.Println("database initialized: ", config.Datastore.Path)
}
} else if arguments["upgradedb"].(bool) {
err = irc.UpgradeDB(config)
if err != nil {
log.Fatal("Error while upgrading db:", err.Error())
}
2016-09-19 14:30:45 +02:00
if !arguments["--quiet"].(bool) {
log.Println("database upgraded: ", config.Datastore.Path)
}
} else if arguments["mkcerts"].(bool) {
2016-09-19 14:30:45 +02:00
if !arguments["--quiet"].(bool) {
log.Println("making self-signed certificates")
}
2016-06-15 11:31:39 +02:00
for name, conf := range config.Server.TLSListeners {
2017-09-11 01:15:39 +02:00
if !arguments["--quiet"].(bool) {
log.Printf(" making cert for %s listener\n", name)
}
2016-06-15 11:31:39 +02:00
host := config.Server.Name
2016-08-12 23:40:58 +02:00
err := mkcerts.CreateCert("Oragono", host, conf.Cert, conf.Key)
if err == nil {
2016-09-19 14:30:45 +02:00
if !arguments["--quiet"].(bool) {
log.Printf(" Certificate created at %s : %s\n", conf.Cert, conf.Key)
}
2016-08-12 23:40:58 +02:00
} else {
log.Fatal(" Could not create certificate:", err.Error())
2016-06-15 11:31:39 +02:00
}
}
} else if arguments["run"].(bool) {
2017-03-06 11:15:28 +01:00
if !arguments["--quiet"].(bool) {
logman.Info("server", fmt.Sprintf("Oragono v%s starting", irc.SemVer))
2018-04-09 02:08:54 +02:00
if commit == "" {
logman.Debug("server", fmt.Sprintf("Could not get current commit"))
2018-04-09 02:08:54 +02:00
} else {
logman.Info("server", fmt.Sprintf("Running commit %s", commit))
2018-04-09 02:08:54 +02:00
}
}
// set current git commit
irc.Commit = commit
if commit != "" {
irc.Ver = fmt.Sprintf("%s-%s", irc.Ver, commit)
2017-03-06 11:15:28 +01:00
}
2017-04-30 04:35:07 +02:00
// warning if running a non-final version
if strings.Contains(irc.SemVer, "unreleased") {
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.")
}
2017-11-19 01:42:40 +01:00
server, err := irc.NewServer(config, logman)
2017-03-06 06:50:23 +01:00
if err != nil {
logman.Error("server", fmt.Sprintf("Could not load server: %s", err.Error()))
2019-02-03 03:15:41 +01:00
os.Exit(1)
}
2016-09-19 14:30:45 +02:00
if !arguments["--quiet"].(bool) {
logman.Info("server", "Server running")
defer logman.Info("server", fmt.Sprintf("Oragono v%s exiting", irc.SemVer))
2016-09-19 14:30:45 +02:00
}
2014-03-13 02:57:00 +01:00
server.Run()
}
2014-02-08 22:18:11 +01:00
}