Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config.conf.example
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ port = 80
[fileserving]
enabled = false
webroot = www/
kiwiirc_config = "/etc/kiwiirc/client.json"

[transports]
websocket
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
9 changes: 9 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
}
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/webircgateway/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type Config struct {
RemoteOrigins []glob.Glob
ReverseProxies []net.IPNet
Webroot string
KiwiircConfig string
ClientRealname string
ClientUsername string
ClientHostname string
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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("")
}
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/webircgateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
}
}
Expand Down
42 changes: 42 additions & 0 deletions pkg/webircgateway/kiwiirc_config.go
Original file line number Diff line number Diff line change
@@ -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)
}