2016-06-15 13:50:56 +02:00
// 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>
2016-06-15 13:50:56 +02:00
// released under the MIT license
2014-02-08 22:18:11 +01:00
package main
import (
2014-02-24 07:21:39 +01:00
"fmt"
2014-02-09 16:53:42 +01:00
"log"
2017-01-14 10:52:47 +01:00
"math/rand"
2017-06-30 23:07:48 +02:00
"strings"
2016-04-12 15:00:09 +02:00
"syscall"
2017-01-14 10:52:47 +01:00
"time"
2014-03-13 02:57:00 +01:00
2016-04-12 15:00:09 +02:00
"github.com/docopt/docopt-go"
2017-06-14 20:00:53 +02:00
"github.com/oragono/oragono/irc"
2017-06-22 23:21:45 +02:00
cloak "github.com/oragono/oragono/irc/cloaking"
2017-06-14 20:00:53 +02:00
"github.com/oragono/oragono/irc/logger"
"github.com/oragono/oragono/mkcerts"
2017-04-30 04:35:07 +02:00
stackimpact "github.com/stackimpact/stackimpact-go"
2016-04-12 15:00:09 +02:00
"golang.org/x/crypto/ssh/terminal"
)
2014-03-13 02:57:00 +01:00
2016-04-12 15:00:09 +02:00
func main ( ) {
2016-10-13 09:36:44 +02:00
version := irc . SemVer
2016-04-13 00:55:37 +02:00
usage := ` oragono .
2016-04-12 15:00:09 +02:00
Usage :
2016-09-19 14:30:45 +02:00
oragono initdb [ -- conf < filename > ] [ -- quiet ]
oragono upgradedb [ -- conf < filename > ] [ -- quiet ]
oragono genpasswd [ -- conf < filename > ] [ -- quiet ]
2017-09-06 07:51:55 +02:00
oragono genkeys
2016-09-19 14:30:45 +02:00
oragono mkcerts [ -- conf < filename > ] [ -- quiet ]
oragono run [ -- conf < filename > ] [ -- quiet ]
2016-04-13 00:55:37 +02:00
oragono - h | -- help
oragono -- version
2016-04-12 15:00:09 +02:00
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 .
2016-04-12 15:00:09 +02:00
- h -- help Show this screen .
-- version Show version . `
arguments , _ := docopt . Parse ( usage , nil , true , version , false )
2017-09-06 07:51:55 +02:00
// load config and logger for everything but genkeys
var err error
2016-04-12 15:00:09 +02:00
configfile := arguments [ "--conf" ] . ( string )
2017-09-06 07:51:55 +02:00
var config * irc . Config
var logman * logger . Manager
if ! arguments [ "genkeys" ] . ( bool ) {
config , err = irc . LoadConfig ( configfile )
if err != nil {
log . Fatal ( "Config file did not load successfully:" , err . Error ( ) )
}
2014-03-13 02:57:00 +01:00
2017-09-06 07:51:55 +02:00
// assemble separate log configs
var logConfigs [ ] logger . Config
for _ , lConfig := range config . Logging {
logConfigs = append ( logConfigs , logger . Config {
MethodStdout : lConfig . MethodStdout ,
MethodStderr : lConfig . MethodStderr ,
MethodFile : lConfig . MethodFile ,
Filename : lConfig . Filename ,
Level : lConfig . Level ,
Types : lConfig . Types ,
ExcludedTypes : lConfig . ExcludedTypes ,
} )
}
2017-03-10 13:02:08 +01:00
2017-09-06 07:51:55 +02:00
logman , err = logger . NewManager ( logConfigs ... )
if err != nil {
log . Fatal ( "Logger did not load successfully:" , err . Error ( ) )
}
2017-03-06 06:50:23 +01:00
}
2016-04-12 15:00:09 +02:00
if arguments [ "genpasswd" ] . ( bool ) {
fmt . Print ( "Enter Password: " )
bytePassword , err := terminal . ReadPassword ( int ( syscall . Stdin ) )
if err != nil {
log . Fatal ( "Error reading password:" , err . Error ( ) )
}
password := string ( bytePassword )
encoded , err := irc . GenerateEncodedPassword ( password )
2014-03-02 00:10:04 +01:00
if err != nil {
2017-03-06 06:50:23 +01:00
log . Fatal ( "encoding error:" , err . Error ( ) )
2014-03-02 00:10:04 +01:00
}
2016-04-12 15:00:09 +02:00
fmt . Print ( "\n" )
2014-03-02 00:10:04 +01:00
fmt . Println ( encoded )
2017-09-06 07:51:55 +02:00
} else if arguments [ "genkeys" ] . ( bool ) {
fmt . Println ( "Here are your cloak keys:" )
// generate IPv4 keys
keyA , err := cloak . GenerateCloakKey ( )
if err != nil {
log . Fatal ( "Error generating cloak keys:" , err )
}
keyB , err := cloak . GenerateCloakKey ( )
if err != nil {
log . Fatal ( "Error generating cloak keys:" , err )
}
keyC , err := cloak . GenerateCloakKey ( )
if err != nil {
log . Fatal ( "Error generating cloak keys:" , err )
}
keyD , err := cloak . GenerateCloakKey ( )
if err != nil {
log . Fatal ( "Error generating cloak keys:" , err )
}
fmt . Println ( fmt . Sprintf ( ` ipv4-keys: ["%s", "%s", "%s", "%s"] ` , keyA , keyB , keyC , keyD ) )
// generate IPv6 keys
keyA , err = cloak . GenerateCloakKey ( )
if err != nil {
log . Fatal ( "Error generating cloak keys:" , err )
}
keyB , err = cloak . GenerateCloakKey ( )
if err != nil {
log . Fatal ( "Error generating cloak keys:" , err )
}
keyC , err = cloak . GenerateCloakKey ( )
if err != nil {
log . Fatal ( "Error generating cloak keys:" , err )
}
keyD , err = cloak . GenerateCloakKey ( )
if err != nil {
log . Fatal ( "Error generating cloak keys:" , err )
}
keyE , err := cloak . GenerateCloakKey ( )
if err != nil {
log . Fatal ( "Error generating cloak keys:" , err )
}
keyF , err := cloak . GenerateCloakKey ( )
if err != nil {
log . Fatal ( "Error generating cloak keys:" , err )
}
keyG , err := cloak . GenerateCloakKey ( )
if err != nil {
log . Fatal ( "Error generating cloak keys:" , err )
}
keyH , err := cloak . GenerateCloakKey ( )
if err != nil {
log . Fatal ( "Error generating cloak keys:" , err )
}
fmt . Println ( fmt . Sprintf ( ` ipv6-keys: ["%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s"] ` , keyA , keyB , keyC , keyD , keyE , keyF , keyG , keyH ) )
2016-04-12 15:00:09 +02:00
} else if arguments [ "initdb" ] . ( bool ) {
2016-09-17 13:23:04 +02:00
irc . InitDB ( config . Datastore . Path )
2016-09-19 14:30:45 +02:00
if ! arguments [ "--quiet" ] . ( bool ) {
log . Println ( "database initialized: " , config . Datastore . Path )
}
2016-04-12 15:00:09 +02:00
} else if arguments [ "upgradedb" ] . ( bool ) {
2016-09-17 13:23:04 +02:00
irc . UpgradeDB ( config . Datastore . Path )
2016-09-19 14:30:45 +02:00
if ! arguments [ "--quiet" ] . ( bool ) {
log . Println ( "database upgraded: " , config . Datastore . Path )
}
2016-08-13 10:17:40 +02:00
} 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 {
2016-08-13 10:17:40 +02:00
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
}
}
2016-04-12 15:00:09 +02:00
} else if arguments [ "run" ] . ( bool ) {
2017-01-14 10:52:47 +01:00
rand . Seed ( time . Now ( ) . UTC ( ) . UnixNano ( ) )
2017-03-06 11:15:28 +01:00
if ! arguments [ "--quiet" ] . ( bool ) {
2017-09-06 07:51:55 +02:00
logman . Info ( "startup" , fmt . Sprintf ( "Oragono v%s starting" , irc . SemVer ) )
2017-03-06 11:15:28 +01:00
}
2017-04-30 04:35:07 +02:00
// profiling
if config . Debug . StackImpact . Enabled {
if config . Debug . StackImpact . AgentKey == "" || config . Debug . StackImpact . AppName == "" {
2017-09-06 07:51:55 +02:00
logman . Error ( "startup" , "Could not start StackImpact - agent-key or app-name are undefined" )
2017-04-30 04:35:07 +02:00
return
}
agent := stackimpact . NewAgent ( )
agent . Start ( stackimpact . Options { AgentKey : config . Debug . StackImpact . AgentKey , AppName : config . Debug . StackImpact . AppName } )
defer agent . RecordPanic ( )
2017-09-06 07:51:55 +02:00
logman . Info ( "startup" , fmt . Sprintf ( "StackImpact profiling started as %s" , config . Debug . StackImpact . AppName ) )
2017-04-30 04:35:07 +02:00
}
2017-06-30 23:07:48 +02:00
// warning if running a non-final version
if strings . Contains ( irc . SemVer , "unreleased" ) {
2017-09-06 07:51:55 +02: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-06-30 23:07:48 +02:00
}
2017-09-06 07:51:55 +02:00
server , err := irc . NewServer ( configfile , config , logman )
2017-03-06 06:50:23 +01:00
if err != nil {
2017-09-06 07:51:55 +02:00
logman . Error ( "startup" , fmt . Sprintf ( "Could not load server: %s" , err . Error ( ) ) )
2016-11-06 04:47:13 +01:00
return
}
2016-09-19 14:30:45 +02:00
if ! arguments [ "--quiet" ] . ( bool ) {
2017-09-06 07:51:55 +02:00
logman . Info ( "startup" , "Server running" )
defer logman . Info ( "shutdown" , 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
}