/* goircd -- minimalistic simple Internet Relay Chat (IRC) server Copyright (C) 2014-2021 Sergey Matveev 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, version 3 of the License. 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 . */ package main import ( "crypto/tls" "io/ioutil" "log" "net" "net/http" "os" "path" "path/filepath" "strconv" "strings" "time" healthchecking "github.com/heptiolabs/healthcheck" "github.com/namsral/flag" proxyproto "github.com/Freeaqingme/go-proxyproto" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" ) const ( PROXY_TIMEOUT = 5 ) const ( Version = "1.9.1" 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 } var ( 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") tlsKEY = flag.String("tlskey", "", "Path to TLS key PEM as seperate file") tlsonly = flag.Bool("tlsonly", false, "Disable listening on non tls-port") proxyTimeout = flag.Uint("proxytimeout", PROXY_TIMEOUT, "Timeout when using proxy protocol") metrics = flag.Bool("metrics", false, "Enable metrics export") verbose = flag.Bool("v", false, "Enable verbose logging.") healtcheck = flag.Bool("healthcheck", false, "Enable healthcheck endpoint.") healtbind = flag.String("healthbind", "[::]:8086", "Healthcheck bind address and port.") 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.", }, ) ) 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) { for { conn, err := ln.Accept() if err != nil { log.Println("error during accept", err) continue } client := NewClient(conn) clients_tls_total.Inc() go client.Processor(events) } } 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) if *logdir == "" { // Dummy logger go func() { for range logSink { } }() } else { if !path.IsAbs(*logdir) { log.Fatalln("need absolute path for logdir") } go Logger(*logdir, logSink) } if *statedir == "" { // Dummy statekeeper go func() { for range stateSink { } }() } else { if !path.IsAbs(*statedir) { log.Fatalln("need absolute path for statedir") } states, err := filepath.Glob(path.Join(*statedir, "#*")) if err != nil { log.Fatalln("can not read statedir", err) } for _, state := range states { buf, err := ioutil.ReadFile(path.Join(state, StateTopicFilename)) if err != nil { log.Fatalf( "can not read state %s/%s: %v", state, StateTopicFilename, err, ) } 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") } else { if !os.IsNotExist(err) { log.Fatalf( "can not read state %s/%s: %v", state, StateKeyFilename, err, ) } } log.Println("loaded state for room:", room.name) } 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) } } }() } proxyTimeout := time.Duration(uint(*proxyTimeout)) * time.Second if *bind != "" && !*tlsonly { listener, err := net.Listen("tcp", *bind) if err != nil { log.Fatalf("can not listen on %s: %v", *bind, err) } // Add PROXY-Protocol support listener = &proxyproto.Listener{Listener: listener, ProxyHeaderTimeout: proxyTimeout} log.Println("Raw listening on", *bind) go listenerLoop(listener, events) } if *tlsBind != "" { if *tlsKEY == "" { tlsKEY = tlsPEM } cert, err := tls.LoadX509KeyPair(*tlsPEM, *tlsKEY) if err != nil { log.Fatalf("Could not load Certificate and TLS keys from %s: %s", *tlsPEM, *tlsKEY, err) } config := tls.Config{Certificates: []tls.Certificate{cert}} listenerTLS, err := net.Listen("tcp", *tlsBind) if err != nil { log.Fatalf("can not listen on %s: %v", *tlsBind, err) } log.Println("TLS listening on", *tlsBind) // Add PROXY-Protocol support listenerTLS = &proxyproto.Listener{Listener: listenerTLS, ProxyHeaderTimeout: proxyTimeout} listenerTLS = tls.NewListener(listenerTLS, &config) go listenerLoop(listenerTLS, events) } // Create endpoint for prometheus metrics export if *metrics { go prom_export() } if *healtcheck { go health_endpoint() } Processor(events, make(chan struct{})) } func health_endpoint() { health := healthchecking.NewHandler() health.AddLivenessCheck("goroutine-threshold", healthchecking.GoroutineCountCheck(100)) log.Printf("Healthcheck listening on http://%s", *healtbind) http.ListenAndServe(*healtbind, health) } 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)) } func main() { flag.Parse() Run() }