2014-05-11 16:18:55 +00:00
|
|
|
/*
|
|
|
|
goircd -- minimalistic simple Internet Relay Chat (IRC) server
|
2021-01-05 17:48:29 +00:00
|
|
|
Copyright (C) 2014-2021 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
|
2019-09-27 08:32:53 +00:00
|
|
|
the Free Software Foundation, version 3 of the License.
|
2014-05-11 16:18:55 +00:00
|
|
|
|
|
|
|
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-06-08 01:16:33 +00:00
|
|
|
"io/ioutil"
|
2014-05-11 16:18:55 +00:00
|
|
|
"log"
|
|
|
|
"net"
|
2018-03-08 02:40:26 +00:00
|
|
|
"net/http"
|
2020-11-06 17:10:06 +00:00
|
|
|
"os"
|
2014-05-11 16:18:55 +00:00
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2020-11-06 17:10:06 +00:00
|
|
|
"strconv"
|
2014-05-11 16:18:55 +00:00
|
|
|
"strings"
|
2018-02-20 18:48:13 +00:00
|
|
|
"time"
|
|
|
|
|
2018-03-14 00:05:57 +00:00
|
|
|
healthchecking "github.com/heptiolabs/healthcheck"
|
2018-03-13 22:36:53 +00:00
|
|
|
"github.com/namsral/flag"
|
|
|
|
|
2018-02-20 18:48:13 +00:00
|
|
|
proxyproto "github.com/Freeaqingme/go-proxyproto"
|
2018-03-08 02:40:26 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
2014-05-11 16:18:55 +00:00
|
|
|
)
|
|
|
|
|
2018-03-06 21:53:04 +00:00
|
|
|
const (
|
2021-12-29 15:19:22 +00:00
|
|
|
PROXY_TIMEOUT = 5
|
2014-05-11 16:18:55 +00:00
|
|
|
)
|
|
|
|
|
2020-11-06 17:10:06 +00:00
|
|
|
const (
|
2020-12-04 10:28:53 +00:00
|
|
|
Version = "1.9.1"
|
2020-11-06 17:10:06 +00:00
|
|
|
|
|
|
|
StateTopicFilename = "topic"
|
|
|
|
StateKeyFilename = "key"
|
|
|
|
|
|
|
|
EventNew = iota
|
|
|
|
EventDel = iota
|
|
|
|
EventMsg = iota
|
|
|
|
EventTopic = iota
|
|
|
|
EventWho = iota
|
|
|
|
EventMode = iota
|
|
|
|
EventTerm = iota
|
|
|
|
EventTick = iota
|
|
|
|
)
|
|
|
|
|
|
|
|
type ClientEvent struct {
|
|
|
|
client *Client
|
|
|
|
eventType int
|
|
|
|
text string
|
|
|
|
}
|
|
|
|
|
|
|
|
type StateEvent struct {
|
|
|
|
where string
|
|
|
|
topic string
|
|
|
|
key string
|
|
|
|
}
|
2020-09-05 08:30:25 +00:00
|
|
|
|
2014-05-11 16:18:55 +00:00
|
|
|
var (
|
2018-03-06 21:53:04 +00:00
|
|
|
version string
|
|
|
|
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")
|
|
|
|
tlsBind = flag.String("tlsbind", "", "TLS address to bind to")
|
|
|
|
tlsPEM = flag.String("tlspem", "", "Path to TLS certificat+key PEM file")
|
2018-03-14 01:06:18 +00:00
|
|
|
tlsKEY = flag.String("tlskey", "", "Path to TLS key PEM as seperate file")
|
2018-03-08 17:37:26 +00:00
|
|
|
tlsonly = flag.Bool("tlsonly", false, "Disable listening on non tls-port")
|
2018-03-06 21:53:04 +00:00
|
|
|
proxyTimeout = flag.Uint("proxytimeout", PROXY_TIMEOUT, "Timeout when using proxy protocol")
|
2018-03-08 02:40:26 +00:00
|
|
|
metrics = flag.Bool("metrics", false, "Enable metrics export")
|
2018-03-06 21:53:04 +00:00
|
|
|
verbose = flag.Bool("v", false, "Enable verbose logging.")
|
2018-03-14 00:05:57 +00:00
|
|
|
healtcheck = flag.Bool("healthcheck", false, "Enable healthcheck endpoint.")
|
2018-04-22 08:45:02 +00:00
|
|
|
healtbind = flag.String("healthbind", "[::]:8086", "Healthcheck bind address and port.")
|
2018-03-08 02:40:26 +00:00
|
|
|
|
|
|
|
clients_tls_total = prometheus.NewCounter(
|
|
|
|
prometheus.CounterOpts{
|
|
|
|
Name: "clients_tls_connected_total",
|
|
|
|
Help: "Number of connected clients during the lifetime of the server.",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
clients_irc_total = prometheus.NewCounter(
|
|
|
|
prometheus.CounterOpts{
|
|
|
|
Name: "clients_irc_connected_total",
|
|
|
|
Help: "Number of connected irc clients during the lifetime of the server.",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
clients_irc_rooms_total = prometheus.NewCounterVec(
|
|
|
|
prometheus.CounterOpts{
|
|
|
|
Name: "clients_irc_rooms_connected_total",
|
|
|
|
Help: "Number of clients joined to rooms during the lifetime of the server.",
|
|
|
|
},
|
|
|
|
[]string{"room"},
|
|
|
|
)
|
|
|
|
|
|
|
|
clients_connected = prometheus.NewGauge(
|
|
|
|
prometheus.GaugeOpts{
|
|
|
|
Name: "clients_connected",
|
|
|
|
Help: "Number of connected clients.",
|
|
|
|
},
|
|
|
|
)
|
2014-05-11 16:18:55 +00:00
|
|
|
)
|
|
|
|
|
2020-11-06 17:10:06 +00:00
|
|
|
func permParse(s string) os.FileMode {
|
|
|
|
r, err := strconv.ParseUint(s, 8, 16)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
return os.FileMode(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func listenerLoop(ln net.Listener, events chan ClientEvent) {
|
2014-08-14 14:46:21 +00:00
|
|
|
for {
|
2020-11-06 17:10:06 +00:00
|
|
|
conn, err := ln.Accept()
|
2014-08-14 14:46:21 +00:00
|
|
|
if err != nil {
|
2020-11-06 17:10:06 +00:00
|
|
|
log.Println("error during accept", err)
|
2014-08-14 14:46:21 +00:00
|
|
|
continue
|
|
|
|
}
|
2018-03-06 21:53:04 +00:00
|
|
|
client := NewClient(conn)
|
2018-03-08 02:40:26 +00:00
|
|
|
clients_tls_total.Inc()
|
2014-08-14 14:46:21 +00:00
|
|
|
go client.Processor(events)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-06 17:10:06 +00:00
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
permStateDir = permParse(*permStateDirS)
|
|
|
|
permStateFile = permParse(*permStateFileS)
|
|
|
|
permLogFile = permParse(*permLogFileS)
|
|
|
|
|
|
|
|
if *timestamped {
|
|
|
|
log.SetFlags(log.Ldate | log.Lmicroseconds | log.Lshortfile)
|
|
|
|
} else {
|
|
|
|
log.SetFlags(log.Lshortfile)
|
|
|
|
}
|
|
|
|
log.SetOutput(os.Stdout)
|
2014-05-11 16:18:55 +00:00
|
|
|
|
|
|
|
if *logdir == "" {
|
|
|
|
// Dummy logger
|
|
|
|
go func() {
|
2020-11-06 17:10:06 +00:00
|
|
|
for range logSink {
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
} else {
|
|
|
|
if !path.IsAbs(*logdir) {
|
2020-11-06 17:10:06 +00:00
|
|
|
log.Fatalln("need absolute path for logdir")
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
2014-08-09 12:42:19 +00:00
|
|
|
go Logger(*logdir, logSink)
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if *statedir == "" {
|
|
|
|
// Dummy statekeeper
|
|
|
|
go func() {
|
2020-11-06 17:10:06 +00:00
|
|
|
for range stateSink {
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
} else {
|
|
|
|
if !path.IsAbs(*statedir) {
|
2020-11-06 17:10:06 +00:00
|
|
|
log.Fatalln("need absolute path for statedir")
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
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 {
|
2020-11-06 17:10:06 +00:00
|
|
|
log.Fatalln("can not read statedir", err)
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
|
|
|
for _, state := range states {
|
2020-11-06 17:10:06 +00:00
|
|
|
buf, err := ioutil.ReadFile(path.Join(state, StateTopicFilename))
|
2014-05-11 16:18:55 +00:00
|
|
|
if err != nil {
|
2020-11-06 17:10:06 +00:00
|
|
|
log.Fatalf(
|
|
|
|
"can not read state %s/%s: %v",
|
|
|
|
state, StateTopicFilename, err,
|
|
|
|
)
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
2020-11-06 17:10:06 +00:00
|
|
|
room := RoomRegister(path.Base(state))
|
|
|
|
room.topic = strings.TrimRight(string(buf), "\n")
|
|
|
|
buf, err = ioutil.ReadFile(path.Join(state, StateKeyFilename))
|
|
|
|
if err == nil {
|
|
|
|
room.key = strings.TrimRight(string(buf), "\n")
|
2014-06-08 01:16:33 +00:00
|
|
|
} else {
|
2020-11-06 17:10:06 +00:00
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
log.Fatalf(
|
|
|
|
"can not read state %s/%s: %v",
|
|
|
|
state, StateKeyFilename, err,
|
|
|
|
)
|
|
|
|
}
|
2014-06-08 01:16:33 +00:00
|
|
|
}
|
2020-11-06 17:10:06 +00:00
|
|
|
log.Println("loaded state for room:", room.name)
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
2020-11-06 17:10:06 +00:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
for event := range stateSink {
|
|
|
|
statePath := path.Join(*statedir, event.where)
|
|
|
|
if _, err := os.Stat(statePath); os.IsNotExist(err) {
|
|
|
|
if err := os.Mkdir(statePath, permStateDir); err != nil {
|
|
|
|
log.Printf("can not create state %s: %v", statePath, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
topicPath := path.Join(statePath, StateTopicFilename)
|
|
|
|
if err := ioutil.WriteFile(
|
|
|
|
topicPath,
|
|
|
|
[]byte(event.topic+"\n"),
|
|
|
|
permStateFile,
|
|
|
|
); err != nil {
|
|
|
|
log.Printf("can not write statefile %s: %v", topicPath, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
keyPath := path.Join(statePath, StateKeyFilename)
|
|
|
|
if err := ioutil.WriteFile(
|
|
|
|
keyPath,
|
|
|
|
[]byte(event.key+"\n"),
|
|
|
|
permStateFile,
|
|
|
|
); err != nil {
|
|
|
|
log.Printf("can not write statefile %s: %v", keyPath, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
|
|
|
|
2018-03-06 21:53:04 +00:00
|
|
|
proxyTimeout := time.Duration(uint(*proxyTimeout)) * time.Second
|
|
|
|
|
2018-03-08 17:37:26 +00:00
|
|
|
if *bind != "" && !*tlsonly {
|
2014-08-14 14:46:21 +00:00
|
|
|
listener, err := net.Listen("tcp", *bind)
|
2014-05-11 16:18:55 +00:00
|
|
|
if err != nil {
|
2020-11-06 17:10:06 +00:00
|
|
|
log.Fatalf("can not listen on %s: %v", *bind, err)
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
2018-03-06 21:53:04 +00:00
|
|
|
// Add PROXY-Protocol support
|
|
|
|
listener = &proxyproto.Listener{Listener: listener, ProxyHeaderTimeout: proxyTimeout}
|
|
|
|
|
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
|
|
|
}
|
2018-03-06 21:53:04 +00:00
|
|
|
|
2014-08-14 14:46:21 +00:00
|
|
|
if *tlsBind != "" {
|
2018-03-14 01:06:18 +00:00
|
|
|
if *tlsKEY == "" {
|
|
|
|
tlsKEY = tlsPEM
|
|
|
|
}
|
|
|
|
|
|
|
|
cert, err := tls.LoadX509KeyPair(*tlsPEM, *tlsKEY)
|
|
|
|
|
2014-08-14 14:46:21 +00:00
|
|
|
if err != nil {
|
2018-03-14 01:06:18 +00:00
|
|
|
log.Fatalf("Could not load Certificate and TLS keys from %s: %s", *tlsPEM, *tlsKEY, err)
|
2014-08-14 14:46:21 +00:00
|
|
|
}
|
|
|
|
config := tls.Config{Certificates: []tls.Certificate{cert}}
|
2018-03-06 21:53:04 +00:00
|
|
|
|
|
|
|
listenerTLS, err := net.Listen("tcp", *tlsBind)
|
2014-08-14 14:46:21 +00:00
|
|
|
if err != nil {
|
2020-11-06 17:10:06 +00:00
|
|
|
log.Fatalf("can not listen on %s: %v", *tlsBind, err)
|
2014-08-14 14:46:21 +00:00
|
|
|
}
|
|
|
|
log.Println("TLS listening on", *tlsBind)
|
2018-03-06 21:53:04 +00:00
|
|
|
|
|
|
|
// Add PROXY-Protocol support
|
|
|
|
|
|
|
|
listenerTLS = &proxyproto.Listener{Listener: listenerTLS, ProxyHeaderTimeout: proxyTimeout}
|
|
|
|
|
|
|
|
listenerTLS = tls.NewListener(listenerTLS, &config)
|
|
|
|
|
2014-08-14 14:46:21 +00:00
|
|
|
go listenerLoop(listenerTLS, events)
|
|
|
|
}
|
2018-03-08 02:40:26 +00:00
|
|
|
|
|
|
|
// Create endpoint for prometheus metrics export
|
|
|
|
if *metrics {
|
|
|
|
go prom_export()
|
|
|
|
}
|
2018-03-14 00:05:57 +00:00
|
|
|
if *healtcheck {
|
|
|
|
go health_endpoint()
|
2014-08-14 14:46:21 +00:00
|
|
|
}
|
2018-03-08 02:40:26 +00:00
|
|
|
|
2015-10-11 08:12:55 +00:00
|
|
|
Processor(events, make(chan struct{}))
|
2014-05-11 16:18:55 +00:00
|
|
|
}
|
|
|
|
|
2018-03-14 00:05:57 +00:00
|
|
|
func health_endpoint() {
|
|
|
|
health := healthchecking.NewHandler()
|
|
|
|
health.AddLivenessCheck("goroutine-threshold", healthchecking.GoroutineCountCheck(100))
|
2018-04-22 08:47:38 +00:00
|
|
|
log.Printf("Healthcheck listening on http://%s", *healtbind)
|
|
|
|
http.ListenAndServe(*healtbind, health)
|
2018-03-14 00:05:57 +00:00
|
|
|
}
|
|
|
|
|
2018-03-08 02:40:26 +00:00
|
|
|
func prom_export() {
|
|
|
|
prometheus.MustRegister(clients_tls_total)
|
|
|
|
prometheus.MustRegister(clients_irc_total)
|
|
|
|
prometheus.MustRegister(clients_irc_rooms_total)
|
|
|
|
prometheus.MustRegister(clients_connected)
|
|
|
|
|
|
|
|
http.Handle("/metrics", promhttp.Handler())
|
|
|
|
log.Fatal(http.ListenAndServe(":8080", nil))
|
|
|
|
}
|
|
|
|
|
2014-05-11 16:18:55 +00:00
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
Run()
|
|
|
|
}
|