diff options
author | 2021-10-17 20:05:04 -0600 | |
---|---|---|
committer | 2021-10-17 20:38:02 -0600 | |
commit | 6d08d74c58ff78f02d438e21505f28ca823905ca (patch) | |
tree | f2110bf4038d869e59f81cf4af8d55e2a8ef022f /cmd | |
parent | wurgurboo: add wireguard-nt monitoring (diff) | |
download | irc-go-master.tar.xz irc-go-master.zip |
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/wurgurboo/main.go | 2 | ||||
-rw-r--r-- | cmd/wurgurboo/nt-driver-builder.go | 57 |
2 files changed, 59 insertions, 0 deletions
diff --git a/cmd/wurgurboo/main.go b/cmd/wurgurboo/main.go index e1a705d..1011bd7 100644 --- a/cmd/wurgurboo/main.go +++ b/cmd/wurgurboo/main.go @@ -66,6 +66,8 @@ func main() { Condition: func(b *hbot.Bot, m *hbot.Message) bool { return m.Command == "PRIVMSG" }, Action: banter.Handle, }) + ntDriverBuilderNotifier := NewNtDriverBuilderNotifier(channel, bot) + http.HandleFunc("/nt-driver-builder-notify", ntDriverBuilderNotifier.HandleRequest) c := make(chan os.Signal, 1) signal.Notify(c, syscall.SIGINT, syscall.SIGTERM) diff --git a/cmd/wurgurboo/nt-driver-builder.go b/cmd/wurgurboo/nt-driver-builder.go new file mode 100644 index 0000000..1c7eed7 --- /dev/null +++ b/cmd/wurgurboo/nt-driver-builder.go @@ -0,0 +1,57 @@ +/* SPDX-License-Identifier: GPL-2.0 + * + * Copyright (C) 2021 Jason A. Donenfeld. All Rights Reserved. + */ + +package main + +import ( + "crypto/hmac" + "encoding/base64" + "fmt" + "net/http" + "os" + + "golang.zx2c4.com/irc/hbot" +) + +type NtDriverBuilderNotifier struct { + bot *hbot.Bot + channel string + secret [32]byte +} + +func NewNtDriverBuilderNotifier(channel string, bot *hbot.Bot) *NtDriverBuilderNotifier { + notifier := new(NtDriverBuilderNotifier) + secret, err := base64.StdEncoding.DecodeString(os.Getenv("WURGURBOO_NTDRIVERBUILDERNOTIFIER_SECRET")) + if err != nil || len(secret) != 32 { + return notifier // Silently disable on failure + } + copy(notifier.secret[:], secret) + notifier.bot = bot + notifier.channel = channel + return notifier +} + +func isValidNotificationString(s string) bool { + for _, c := range []byte(s) { + if c < ' ' || c > '~' { + return false + } + } + return len(s) >= 3 && len(s) < 200 +} + +func (notifier *NtDriverBuilderNotifier) HandleRequest(w http.ResponseWriter, r *http.Request) { + w.Header().Add("Server", "WurGurBoo/1.0") + secret, _ := base64.StdEncoding.DecodeString(r.Header.Get("Secret")) + driver, action := r.Header.Get("Driver"), r.Header.Get("Action") + if r.Method != http.MethodPost || notifier.bot == nil || !hmac.Equal(secret, notifier.secret[:]) || + !isValidNotificationString(driver) || !isValidNotificationString(action) { + http.Redirect(w, r, "https://www.wireguard.com/", 302) + return + } + notifier.bot.Msg(notifier.channel, + fmt.Sprintf("rozmansi, zx2c4: \x0303nt-driver-builder\x0f %s \x0306%s\x0f", + action, driver)) +} |