From c5cf9242c9b0c7fae1ec8267dcdb5a0dc6dc578e Mon Sep 17 00:00:00 2001 From: ItsOnlyBinary Date: Sat, 18 Apr 2020 17:01:02 +0100 Subject: [PATCH] Make webircgateway handle kiwiirc config.json --- config.conf.example | 1 + go.mod | 1 + go.sum | 2 ++ main.go | 9 +++++++ pkg/webircgateway/config.go | 3 +++ pkg/webircgateway/gateway.go | 4 +++ pkg/webircgateway/kiwiirc_config.go | 42 +++++++++++++++++++++++++++++ 7 files changed, 62 insertions(+) create mode 100644 pkg/webircgateway/kiwiirc_config.go diff --git a/config.conf.example b/config.conf.example index 6393f87..9eb6852 100644 --- a/config.conf.example +++ b/config.conf.example @@ -58,6 +58,7 @@ port = 80 [fileserving] enabled = false webroot = www/ +kiwiirc_config = "/etc/kiwiirc/client.json" [transports] websocket diff --git a/go.mod b/go.mod index ce8aab8..a7fe1d2 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ require ( github.com/OneOfOne/xxhash v1.2.4 github.com/cespare/xxhash v1.1.0 // indirect github.com/dgrijalva/jwt-go v3.2.0+incompatible + github.com/flynn/json5 v0.0.0-20160717195620-7620272ed633 github.com/gobwas/glob v0.2.3 github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e // indirect github.com/gorilla/websocket v1.4.0 // indirect diff --git a/go.sum b/go.sum index e3000d3..847740f 100644 --- a/go.sum +++ b/go.sum @@ -5,6 +5,8 @@ github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= +github.com/flynn/json5 v0.0.0-20160717195620-7620272ed633 h1:xJMmr4GMYIbALX5edyoDIOQpc2bOQTeJiWMeCl9lX/8= +github.com/flynn/json5 v0.0.0-20160717195620-7620272ed633/go.mod h1:NJDK3/o7abx6PP54EOe0G0n0RLmhCo9xv61gUYpI0EY= github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y= github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8= github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e h1:JKmoR8x90Iww1ks85zJ1lfDGgIiMDuIptTOhJq+zKyg= diff --git a/main.go b/main.go index 4f9eb6d..06d7c20 100644 --- a/main.go +++ b/main.go @@ -61,6 +61,12 @@ func runGateway(configFile string, function string) { log.Printf("Config file error: %s", configErr.Error()) os.Exit(1) } + if gateway.Config.Webroot != "" && gateway.Config.KiwiircConfig != "" { + err := gateway.LoadKiwiircConfig() + if err != nil { + log.Printf("Kiwi IRC config file error: %s", err.Error()) + } + } pluginsQuit := &sync.WaitGroup{} loadPlugins(gateway, pluginsQuit) @@ -83,6 +89,9 @@ func watchForSignals(gateway *webircgateway.Gateway) { case syscall.SIGHUP: fmt.Println("Recieved SIGHUP, reloading config file") gateway.Config.Load() + if gateway.Config.Webroot != "" && gateway.Config.KiwiircConfig != "" { + gateway.LoadKiwiircConfig() + } } } } diff --git a/pkg/webircgateway/config.go b/pkg/webircgateway/config.go index af1f032..a3fae2d 100644 --- a/pkg/webircgateway/config.go +++ b/pkg/webircgateway/config.go @@ -67,6 +67,7 @@ type Config struct { RemoteOrigins []glob.Glob ReverseProxies []net.IPNet Webroot string + KiwiircConfig string ClientRealname string ClientUsername string ClientHostname string @@ -139,6 +140,7 @@ func (c *Config) Load() error { c.GatewayWhitelist = []glob.Glob{} c.ReverseProxies = []net.IPNet{} c.Webroot = "" + c.KiwiircConfig = "" c.ReCaptchaSecret = "" c.ReCaptchaKey = "" c.RequiresVerification = false @@ -198,6 +200,7 @@ func (c *Config) Load() error { if strings.Index(section.Name(), "fileserving") == 0 { if section.Key("enabled").MustBool(false) { c.Webroot = section.Key("webroot").MustString("") + c.KiwiircConfig = section.Key("kiwiirc_config").MustString("") } } diff --git a/pkg/webircgateway/gateway.go b/pkg/webircgateway/gateway.go index 1a4e97b..8361a8a 100644 --- a/pkg/webircgateway/gateway.go +++ b/pkg/webircgateway/gateway.go @@ -104,6 +104,10 @@ func (s *Gateway) maybeStartStaticFileServer() { if s.Config.Webroot != "" { webroot := s.Config.ResolvePath(s.Config.Webroot) s.Log(2, "Serving files from %s", webroot) + if s.Config.KiwiircConfig != "" { + s.Log(2, "Serving Kiwi IRC config from %s", s.Config.ResolvePath(s.Config.KiwiircConfig)) + s.HttpRouter.HandleFunc("/static/config.json", sendKiwiircConfig) + } s.HttpRouter.Handle("/", http.FileServer(http.Dir(webroot))) } } diff --git a/pkg/webircgateway/kiwiirc_config.go b/pkg/webircgateway/kiwiirc_config.go new file mode 100644 index 0000000..646ed6b --- /dev/null +++ b/pkg/webircgateway/kiwiirc_config.go @@ -0,0 +1,42 @@ +package webircgateway + +import ( + "encoding/json" + "io/ioutil" + "net/http" + "sync" + + "github.com/flynn/json5" +) + +var kiwiircConfig []byte +var kiwiircConfigLock sync.RWMutex + +func (s *Gateway) LoadKiwiircConfig() error { + configPath := s.Config.ResolvePath(s.Config.KiwiircConfig) + file, err := ioutil.ReadFile(configPath) + if err != nil { + return err + } + config := make(map[string]interface{}) + + if err = json5.Unmarshal([]byte(file), &config); err != nil { + return err + } + config["kiwiServer"] = "/webirc/kiwiirc/" + + kiwiircConfigLock.Lock() + if kiwiircConfig, err = json.Marshal(config); err != nil { + kiwiircConfigLock.Unlock() + return err + } + kiwiircConfigLock.Unlock() + + return nil +} + +func sendKiwiircConfig(w http.ResponseWriter, r *http.Request) { + kiwiircConfigLock.RLock() + defer kiwiircConfigLock.RUnlock() + w.Write(kiwiircConfig) +}