ircd/oragono.go

158 lines
4.8 KiB
Go
Raw Normal View History

// Copyright (c) 2012-2014 Jeremy Latt
// Copyright (c) 2014-2015 Edmund Huber
2017-03-27 12:15:02 +00:00
// Copyright (c) 2016-2017 Daniel Oaks <daniel@danieloaks.net>
// released under the MIT license
2014-02-08 21:18:11 +00:00
package main
import (
2014-02-24 06:21:39 +00:00
"fmt"
2014-02-09 15:53:42 +00:00
"log"
2017-01-14 09:52:47 +00:00
"math/rand"
"strings"
"syscall"
2017-01-14 09:52:47 +00:00
"time"
2014-03-13 01:57:00 +00:00
"github.com/docopt/docopt-go"
2017-06-14 18:00:53 +00:00
"github.com/oragono/oragono/irc"
"github.com/oragono/oragono/irc/logger"
2018-02-03 19:34:26 +00:00
"github.com/oragono/oragono/irc/mkcerts"
2017-10-05 14:03:53 +00:00
"github.com/oragono/oragono/irc/passwd"
2017-04-30 02:35:07 +00:00
stackimpact "github.com/stackimpact/stackimpact-go"
"golang.org/x/crypto/ssh/terminal"
)
2014-03-13 01:57:00 +00:00
2018-04-09 00:08:54 +00:00
var commit = ""
// get a password from stdin from the user
func getPassword() string {
bytePassword, err := terminal.ReadPassword(int(syscall.Stdin))
if err != nil {
log.Fatal("Error reading password:", err.Error())
}
return string(bytePassword)
}
func main() {
2016-10-13 07:36:44 +00:00
version := irc.SemVer
usage := `oragono.
Usage:
2016-09-19 12:30:45 +00: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 12:30:45 +00:00
--quiet Don't show startup/shutdown lines.
-h --help Show this screen.
--version Show version.`
arguments, _ := docopt.Parse(usage, nil, true, version, false)
configfile := arguments["--conf"].(string)
config, err := irc.LoadConfig(configfile)
2014-03-13 01:57:00 +00:00
if err != nil {
2018-01-23 05:06:33 +00:00
log.Fatal("Config file did not load successfully: ", err.Error())
2014-03-13 01:57:00 +00:00
}
2017-11-19 00:42:40 +00:00
logman, err := logger.NewManager(config.Logging)
2017-03-06 05:50:23 +00:00
if err != nil {
log.Fatal("Logger did not load successfully:", err.Error())
}
if arguments["genpasswd"].(bool) {
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")
}
2017-10-05 14:03:53 +00:00
encoded, err := passwd.GenerateEncodedPassword(password)
if err != nil {
2017-03-06 05:50:23 +00:00
log.Fatal("encoding error:", err.Error())
}
fmt.Println(encoded)
} else if arguments["initdb"].(bool) {
irc.InitDB(config.Datastore.Path)
2016-09-19 12:30:45 +00:00
if !arguments["--quiet"].(bool) {
log.Println("database initialized: ", config.Datastore.Path)
}
} else if arguments["upgradedb"].(bool) {
irc.UpgradeDB(config)
2016-09-19 12:30:45 +00:00
if !arguments["--quiet"].(bool) {
log.Println("database upgraded: ", config.Datastore.Path)
}
} else if arguments["mkcerts"].(bool) {
2016-09-19 12:30:45 +00:00
if !arguments["--quiet"].(bool) {
log.Println("making self-signed certificates")
}
2016-06-15 09:31:39 +00:00
for name, conf := range config.Server.TLSListeners {
2017-09-10 23:15:39 +00:00
if !arguments["--quiet"].(bool) {
log.Printf(" making cert for %s listener\n", name)
}
2016-06-15 09:31:39 +00:00
host := config.Server.Name
2016-08-12 21:40:58 +00:00
err := mkcerts.CreateCert("Oragono", host, conf.Cert, conf.Key)
if err == nil {
2016-09-19 12:30:45 +00:00
if !arguments["--quiet"].(bool) {
log.Printf(" Certificate created at %s : %s\n", conf.Cert, conf.Key)
}
2016-08-12 21:40:58 +00:00
} else {
log.Fatal(" Could not create certificate:", err.Error())
2016-06-15 09:31:39 +00:00
}
}
} else if arguments["run"].(bool) {
2017-01-14 09:52:47 +00:00
rand.Seed(time.Now().UTC().UnixNano())
2017-03-06 10:15:28 +00:00
if !arguments["--quiet"].(bool) {
2017-11-19 00:42:40 +00:00
logman.Info("startup", fmt.Sprintf("Oragono v%s starting", irc.SemVer))
2018-04-09 00:08:54 +00:00
if commit == "" {
logman.Debug("startup", fmt.Sprintf("Could not get current commit"))
} else {
logman.Info("startup", fmt.Sprintf("Running commit %s", commit))
}
}
// set current git commit
irc.Commit = commit
if commit != "" {
irc.Ver = fmt.Sprintf("%s-%s", irc.Ver, commit)
2017-03-06 10:15:28 +00:00
}
2017-04-30 02:35:07 +00:00
// profiling
if config.Debug.StackImpact.Enabled {
if config.Debug.StackImpact.AgentKey == "" || config.Debug.StackImpact.AppName == "" {
2017-11-19 00:42:40 +00:00
logman.Error("startup", "Could not start StackImpact - agent-key or app-name are undefined")
2017-04-30 02:35:07 +00:00
return
}
agent := stackimpact.NewAgent()
agent.Start(stackimpact.Options{AgentKey: config.Debug.StackImpact.AgentKey, AppName: config.Debug.StackImpact.AppName})
defer agent.RecordPanic()
2017-11-19 00:42:40 +00:00
logman.Info("startup", fmt.Sprintf("StackImpact profiling started as %s", config.Debug.StackImpact.AppName))
2017-04-30 02:35:07 +00:00
}
// warning if running a non-final version
if strings.Contains(irc.SemVer, "unreleased") {
2017-11-19 00:42:40 +00:00
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.")
}
2017-11-19 00:42:40 +00:00
server, err := irc.NewServer(config, logman)
2017-03-06 05:50:23 +00:00
if err != nil {
2017-11-19 00:42:40 +00:00
logman.Error("startup", fmt.Sprintf("Could not load server: %s", err.Error()))
return
}
2016-09-19 12:30:45 +00:00
if !arguments["--quiet"].(bool) {
2017-11-19 00:42:40 +00:00
logman.Info("startup", "Server running")
defer logman.Info("shutdown", fmt.Sprintf("Oragono v%s exiting", irc.SemVer))
2016-09-19 12:30:45 +00:00
}
2014-03-13 01:57:00 +00:00
server.Run()
}
2014-02-08 21:18:11 +00:00
}