2014-05-11 16:18:55 +00:00
|
|
|
/*
|
|
|
|
goircd -- minimalistic simple Internet Relay Chat (IRC) server
|
2018-03-25 14:57:05 +00:00
|
|
|
Copyright (C) 2014-2018 | wn Sergey Matveev <stargrave@stargrave.org>
|
2014-05-11 16:18:55 +00:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2015-05-09 15:27:06 +00:00
|
|
|
|
2014-05-11 16:18:55 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2014-06-08 00:08:13 +00:00
|
|
|
"crypto/tls"
|
2014-05-11 16:18:55 +00:00
|
|
|
"flag"
|
2014-06-08 01:16:33 +00:00
|
|
|
"io/ioutil"
|
2014-05-11 16:18:55 +00:00
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2014-08-14 10:31:34 +00:00
|
|
|
version string
|
2014-08-14 10:01:54 +00:00
|
|
|
hostname = flag.String("hostname", "localhost", "Hostname")
|
|
|
|
bind = flag.String("bind", ":6667", "Address to bind to")
|
|
|
|
motd = flag.String("motd", "", "Path to MOTD file")
|
|
|
|
logdir = flag.String("logdir", "", "Absolute path to directory for logs")
|
|
|
|
statedir = flag.String("statedir", "", "Absolute path to directory for states")
|
|
|
|
passwords = flag.String("passwords", "", "Optional path to passwords file")
|
2015-10-11 08:12:55 +00:00
|
|
|
tlsBind = flag.String("tlsbind", "", "TLS address to bind to")
|
|
|
|
tlsPEM = flag.String("tlspem", "", "Path to TLS certificat+key PEM file")
|
|
|
|
verbose = flag.Bool("v", false, "Enable verbose logging.")
|
2014-05-11 16:18:55 +00:00
|
|
|
)
|
|
|
|
|
2015-10-11 08:12:55 +00:00
|
|
|
func listenerLoop(sock net.Listener, events chan ClientEvent) {
|
2014-08-14 14:46:21 +00:00
|
|
|
for {
|
|
|
|
conn, err := sock.Accept()
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Error during accepting connection", err)
|
|
|
|
continue
|
|
|
|
}
|
2015-10-11 08:12:55 +00:00
|
|
|
client := NewClient(conn)
|
2014-08-14 14:46:21 +00:00
|
|
|
go client.Processor(events)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-11 16:18:55 +00:00
|
|
|
func Run() {
|
|
|
|
events := make(chan ClientEvent)
|
|
|
|
log.SetFlags(log.Ldate | log.Lmicroseconds | log.Lshortfile)
|
|
|
|
|
|
|
|
if *logdir == "" {
|
|
|
|
// Dummy logger
|
|
|
|
go func() {
|
2014-08-09 12:42:19 +00:00
|
|
|
for _ = range logSink {
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
} else {
|
|
|
|
if !path.IsAbs(*logdir) {
|
|
|
|
log.Fatalln("Need absolute path for logdir")
|
|
|
|
}
|
2014-08-09 12:42:19 +00:00
|
|
|
go Logger(*logdir, logSink)
|
2014-05-11 16:18:55 +00:00
|
|
|
log.Println(*logdir, "logger initialized")
|
|
|
|
}
|
|
|
|
|
2015-10-11 08:12:55 +00:00
|
|
|
log.Println("goircd " + version + " is starting")
|
2014-05-11 16:18:55 +00:00
|
|
|
if *statedir == "" {
|
|
|
|
// Dummy statekeeper
|
|
|
|
go func() {
|
2014-08-09 12:42:19 +00:00
|
|
|
for _ = range stateSink {
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
} else {
|
|
|
|
if !path.IsAbs(*statedir) {
|
|
|
|
log.Fatalln("Need absolute path for statedir")
|
|
|
|
}
|
2014-06-08 01:16:33 +00:00
|
|
|
states, err := filepath.Glob(path.Join(*statedir, "#*"))
|
2014-05-11 16:18:55 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("Can not read statedir", err)
|
|
|
|
}
|
|
|
|
for _, state := range states {
|
2014-06-08 01:16:33 +00:00
|
|
|
buf, err := ioutil.ReadFile(state)
|
2014-05-11 16:18:55 +00:00
|
|
|
if err != nil {
|
2014-06-08 01:16:33 +00:00
|
|
|
log.Fatalf("Can not read state %s: %v", state, err)
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
2015-10-11 08:12:55 +00:00
|
|
|
room, _ := RoomRegister(path.Base(state))
|
2014-05-11 16:18:55 +00:00
|
|
|
contents := strings.Split(string(buf), "\n")
|
2014-06-08 01:16:33 +00:00
|
|
|
if len(contents) < 2 {
|
2015-10-11 08:12:55 +00:00
|
|
|
log.Printf("State corrupted for %s: %q", *room.name, contents)
|
2014-06-08 01:16:33 +00:00
|
|
|
} else {
|
2015-10-11 08:12:55 +00:00
|
|
|
room.topic = &contents[0]
|
|
|
|
room.key = &contents[1]
|
|
|
|
log.Println("Loaded state for room", *room.name)
|
2014-06-08 01:16:33 +00:00
|
|
|
}
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
2014-08-09 12:42:19 +00:00
|
|
|
go StateKeeper(*statedir, stateSink)
|
2014-05-11 16:18:55 +00:00
|
|
|
log.Println(*statedir, "statekeeper initialized")
|
|
|
|
}
|
|
|
|
|
2014-08-14 14:46:21 +00:00
|
|
|
if *bind != "" {
|
|
|
|
listener, err := net.Listen("tcp", *bind)
|
2014-05-11 16:18:55 +00:00
|
|
|
if err != nil {
|
2014-08-14 14:46:21 +00:00
|
|
|
log.Fatalf("Can not listen on %s: %v", *bind, err)
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
2014-08-14 14:46:21 +00:00
|
|
|
log.Println("Raw listening on", *bind)
|
|
|
|
go listenerLoop(listener, events)
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
2014-08-14 14:46:21 +00:00
|
|
|
if *tlsBind != "" {
|
2014-08-14 19:08:41 +00:00
|
|
|
cert, err := tls.LoadX509KeyPair(*tlsPEM, *tlsPEM)
|
2014-08-14 14:46:21 +00:00
|
|
|
if err != nil {
|
2014-08-14 19:08:41 +00:00
|
|
|
log.Fatalf("Could not load TLS keys from %s: %s", *tlsPEM, err)
|
2014-08-14 14:46:21 +00:00
|
|
|
}
|
|
|
|
config := tls.Config{Certificates: []tls.Certificate{cert}}
|
|
|
|
listenerTLS, err := tls.Listen("tcp", *tlsBind, &config)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Can not listen on %s: %v", *tlsBind, err)
|
|
|
|
}
|
|
|
|
log.Println("TLS listening on", *tlsBind)
|
|
|
|
go listenerLoop(listenerTLS, events)
|
|
|
|
}
|
2015-10-11 08:12:55 +00:00
|
|
|
Processor(events, make(chan struct{}))
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
Run()
|
|
|
|
}
|