2021-10-22 19:33:57 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
"regexp"
|
2024-06-13 14:38:59 +00:00
|
|
|
"strings"
|
2021-10-22 19:33:57 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2021-11-25 22:48:16 +00:00
|
|
|
|
2024-06-13 15:45:04 +00:00
|
|
|
"github.com/Luzifer/go_helpers/v2/str"
|
2024-06-12 19:26:39 +00:00
|
|
|
"github.com/Luzifer/twitch-bot/v3/pkg/twitch"
|
2022-11-02 21:38:14 +00:00
|
|
|
"github.com/Luzifer/twitch-bot/v3/plugins"
|
2021-10-22 19:33:57 +00:00
|
|
|
)
|
|
|
|
|
2023-07-14 14:15:58 +00:00
|
|
|
const frontendNotifyTypeReload = "configReload"
|
|
|
|
|
2024-06-13 14:38:59 +00:00
|
|
|
type (
|
|
|
|
configEditorLoginResponse struct {
|
|
|
|
ExpiresAt time.Time `json:"expiresAt"`
|
|
|
|
Token string `json:"token"`
|
|
|
|
User string `json:"user"`
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2023-07-14 14:15:58 +00:00
|
|
|
var frontendNotifyHooks = newHooker()
|
2023-02-06 22:41:54 +00:00
|
|
|
|
2023-04-02 12:55:27 +00:00
|
|
|
//nolint:funlen // Just contains a collection of objects
|
2021-10-22 19:33:57 +00:00
|
|
|
func registerEditorGlobalMethods() {
|
|
|
|
for _, rd := range []plugins.HTTPRouteRegistrationArgs{
|
|
|
|
{
|
|
|
|
Description: "Returns the documentation for available actions",
|
|
|
|
HandlerFunc: configEditorGlobalGetActions,
|
|
|
|
Method: http.MethodGet,
|
2023-07-14 14:15:58 +00:00
|
|
|
Module: moduleConfigEditor,
|
2021-10-22 19:33:57 +00:00
|
|
|
Name: "Get available actions",
|
|
|
|
Path: "/actions",
|
|
|
|
ResponseType: plugins.HTTPRouteResponseTypeJSON,
|
|
|
|
},
|
2024-06-12 19:26:39 +00:00
|
|
|
{
|
|
|
|
Description: "Exchanges the Twitch token against an internal Bearer token",
|
|
|
|
HandlerFunc: configEditorGlobalLogin,
|
|
|
|
Method: http.MethodPost,
|
|
|
|
Module: moduleConfigEditor,
|
|
|
|
Name: "Authorize on Config-Editor",
|
|
|
|
Path: "/login",
|
|
|
|
ResponseType: plugins.HTTPRouteResponseTypeJSON,
|
|
|
|
},
|
2021-10-23 15:22:58 +00:00
|
|
|
{
|
|
|
|
Description: "Returns all available modules for auth",
|
|
|
|
HandlerFunc: configEditorGlobalGetModules,
|
|
|
|
Method: http.MethodGet,
|
2023-07-14 14:15:58 +00:00
|
|
|
Module: moduleConfigEditor,
|
2021-10-23 15:22:58 +00:00
|
|
|
Name: "Get available modules",
|
|
|
|
Path: "/modules",
|
|
|
|
ResponseType: plugins.HTTPRouteResponseTypeJSON,
|
|
|
|
},
|
2021-10-22 19:33:57 +00:00
|
|
|
{
|
|
|
|
Description: "Returns information about a Twitch user to properly display bot editors",
|
|
|
|
HandlerFunc: configEditorGlobalGetUser,
|
|
|
|
Method: http.MethodGet,
|
2023-07-14 14:15:58 +00:00
|
|
|
Module: moduleConfigEditor,
|
2021-10-22 19:33:57 +00:00
|
|
|
Name: "Get user information",
|
|
|
|
Path: "/user",
|
|
|
|
QueryParams: []plugins.HTTPRouteParamDocumentation{
|
|
|
|
{
|
|
|
|
Description: "The user to query the information for",
|
|
|
|
Name: "user",
|
|
|
|
Required: true,
|
|
|
|
Type: "string",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
RequiresEditorsAuth: true,
|
|
|
|
ResponseType: plugins.HTTPRouteResponseTypeJSON,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Description: "Subscribe for configuration changes",
|
|
|
|
HandlerFunc: configEditorGlobalSubscribe,
|
|
|
|
Method: http.MethodGet,
|
2023-07-14 14:15:58 +00:00
|
|
|
Module: moduleConfigEditor,
|
2021-10-22 19:33:57 +00:00
|
|
|
Name: "Websocket: Subscribe config changes",
|
|
|
|
Path: "/notify-config",
|
|
|
|
ResponseType: plugins.HTTPRouteResponseTypeTextPlain,
|
|
|
|
},
|
2024-06-13 14:38:59 +00:00
|
|
|
{
|
|
|
|
Description: "Takes the authorization token present in the request and returns a new one for the same user",
|
|
|
|
HandlerFunc: configEditorGlobalRefreshToken,
|
|
|
|
Method: http.MethodGet,
|
|
|
|
Module: moduleConfigEditor,
|
|
|
|
Name: "Refresh Auth-Token",
|
|
|
|
Path: "/refreshToken",
|
|
|
|
RequiresEditorsAuth: true,
|
|
|
|
ResponseType: plugins.HTTPRouteResponseTypeJSON,
|
|
|
|
},
|
2021-10-22 19:33:57 +00:00
|
|
|
{
|
|
|
|
Description: "Validate a cron expression and return the next executions",
|
|
|
|
HandlerFunc: configEditorGlobalValidateCron,
|
|
|
|
Method: http.MethodPut,
|
2023-07-14 14:15:58 +00:00
|
|
|
Module: moduleConfigEditor,
|
2021-10-22 19:33:57 +00:00
|
|
|
Name: "Validate cron expression",
|
|
|
|
Path: "/validate-cron",
|
|
|
|
QueryParams: []plugins.HTTPRouteParamDocumentation{
|
|
|
|
{
|
|
|
|
Description: "The cron expression to test",
|
|
|
|
Name: "cron",
|
|
|
|
Required: true,
|
|
|
|
Type: "string",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Description: "Check cron with last execution of auto-message",
|
|
|
|
Name: "uuid",
|
|
|
|
Required: false,
|
|
|
|
Type: "string",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ResponseType: plugins.HTTPRouteResponseTypeJSON,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Description: "Validate a regular expression against the RE2 regex parser",
|
|
|
|
HandlerFunc: configEditorGlobalValidateRegex,
|
|
|
|
Method: http.MethodPut,
|
2023-07-14 14:15:58 +00:00
|
|
|
Module: moduleConfigEditor,
|
2021-10-22 19:33:57 +00:00
|
|
|
Name: "Validate regular expression",
|
|
|
|
Path: "/validate-regex",
|
|
|
|
QueryParams: []plugins.HTTPRouteParamDocumentation{
|
|
|
|
{
|
|
|
|
Description: "The regular expression to test",
|
|
|
|
Name: "regexp",
|
|
|
|
Required: true,
|
|
|
|
Type: "string",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ResponseType: plugins.HTTPRouteResponseTypeTextPlain,
|
|
|
|
},
|
2023-04-02 12:55:27 +00:00
|
|
|
{
|
|
|
|
Description: "Validate a template expression against the built in template function library",
|
|
|
|
HandlerFunc: configEditorGlobalValidateTemplate,
|
|
|
|
Method: http.MethodPut,
|
2023-07-14 14:15:58 +00:00
|
|
|
Module: moduleConfigEditor,
|
2023-04-02 12:55:27 +00:00
|
|
|
Name: "Validate template expression",
|
|
|
|
Path: "/validate-template",
|
|
|
|
QueryParams: []plugins.HTTPRouteParamDocumentation{
|
|
|
|
{
|
|
|
|
Description: "The template expression to test",
|
|
|
|
Name: "template",
|
|
|
|
Required: true,
|
|
|
|
Type: "string",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ResponseType: plugins.HTTPRouteResponseTypeTextPlain,
|
|
|
|
},
|
2021-10-22 19:33:57 +00:00
|
|
|
} {
|
|
|
|
if err := registerRoute(rd); err != nil {
|
|
|
|
log.WithError(err).Fatal("Unable to register config editor route")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-24 21:32:00 +00:00
|
|
|
func configEditorGlobalGetActions(w http.ResponseWriter, _ *http.Request) {
|
2021-10-22 19:33:57 +00:00
|
|
|
availableActorDocsLock.Lock()
|
|
|
|
defer availableActorDocsLock.Unlock()
|
|
|
|
|
|
|
|
if err := json.NewEncoder(w).Encode(availableActorDocs); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-24 21:32:00 +00:00
|
|
|
func configEditorGlobalGetModules(w http.ResponseWriter, _ *http.Request) {
|
2021-10-23 15:22:58 +00:00
|
|
|
if err := json.NewEncoder(w).Encode(knownModules); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-22 19:33:57 +00:00
|
|
|
func configEditorGlobalGetUser(w http.ResponseWriter, r *http.Request) {
|
2024-01-01 16:52:18 +00:00
|
|
|
usr, err := twitchClient.GetUserInformation(r.Context(), r.FormValue("user"))
|
2021-10-22 19:33:57 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.NewEncoder(w).Encode(usr); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-12 19:26:39 +00:00
|
|
|
func configEditorGlobalLogin(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var payload struct {
|
|
|
|
Token string `json:"token"`
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
tc := twitch.New(cfg.TwitchClient, cfg.TwitchClientSecret, payload.Token, "")
|
|
|
|
id, user, err := tc.GetAuthorizedUser(r.Context())
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "access denied", http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-06-13 15:45:04 +00:00
|
|
|
if !str.StringInSlice(user, config.BotEditors) && !str.StringInSlice(id, config.BotEditors) {
|
|
|
|
// That user is none of our editors: Deny access
|
|
|
|
http.Error(w, "access denied", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-06-12 19:26:39 +00:00
|
|
|
tok, expiresAt, err := editorTokenService.CreateLoginToken(id, user)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-06-13 14:38:59 +00:00
|
|
|
if err := json.NewEncoder(w).Encode(configEditorLoginResponse{
|
|
|
|
ExpiresAt: expiresAt,
|
|
|
|
Token: tok,
|
|
|
|
User: user,
|
|
|
|
}); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func configEditorGlobalRefreshToken(w http.ResponseWriter, r *http.Request) {
|
|
|
|
tokenType, token, found := strings.Cut(r.Header.Get("Authorization"), " ")
|
|
|
|
if !found || !strings.EqualFold(tokenType, "bearer") {
|
|
|
|
http.Error(w, "invalid renew request", http.StatusBadRequest)
|
|
|
|
}
|
|
|
|
|
|
|
|
id, user, _, err := editorTokenService.ValidateLoginToken(token)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
|
|
|
|
tok, expiresAt, err := editorTokenService.CreateLoginToken(id, user)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.NewEncoder(w).Encode(configEditorLoginResponse{
|
|
|
|
ExpiresAt: expiresAt,
|
|
|
|
Token: tok,
|
|
|
|
User: user,
|
2024-06-12 19:26:39 +00:00
|
|
|
}); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-22 19:33:57 +00:00
|
|
|
func configEditorGlobalSubscribe(w http.ResponseWriter, r *http.Request) {
|
|
|
|
conn, err := upgrader.Upgrade(w, r, nil)
|
|
|
|
if err != nil {
|
2022-02-09 23:30:40 +00:00
|
|
|
log.WithError(err).Error("Unable to initialize websocket")
|
2021-10-22 19:33:57 +00:00
|
|
|
return
|
|
|
|
}
|
2024-01-01 16:52:18 +00:00
|
|
|
defer conn.Close() //nolint:errcheck
|
2021-10-22 19:33:57 +00:00
|
|
|
|
|
|
|
var (
|
2023-07-14 14:15:58 +00:00
|
|
|
frontendNotify = make(chan string, 1)
|
|
|
|
pingTimer = time.NewTicker(websocketPingInterval)
|
|
|
|
unsubscribe = frontendNotifyHooks.Register(func(payload any) { frontendNotify <- payload.(string) })
|
2021-10-22 19:33:57 +00:00
|
|
|
)
|
|
|
|
defer unsubscribe()
|
|
|
|
|
|
|
|
type socketMsg struct {
|
|
|
|
MsgType string `json:"msg_type"`
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
2023-07-14 14:15:58 +00:00
|
|
|
case msgType := <-frontendNotify:
|
2021-10-22 19:33:57 +00:00
|
|
|
if err := conn.WriteJSON(socketMsg{
|
2023-07-14 14:15:58 +00:00
|
|
|
MsgType: msgType,
|
2021-10-22 19:33:57 +00:00
|
|
|
}); err != nil {
|
|
|
|
log.WithError(err).Debug("Unable to send websocket notification")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
case <-pingTimer.C:
|
|
|
|
if err := conn.WriteJSON(socketMsg{
|
|
|
|
MsgType: "ping",
|
|
|
|
}); err != nil {
|
|
|
|
log.WithError(err).Debug("Unable to send websocket ping")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func configEditorGlobalValidateCron(w http.ResponseWriter, r *http.Request) {
|
|
|
|
sched, err := cronParser.Parse(r.FormValue("cron"))
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
lt = time.Now()
|
|
|
|
out []time.Time
|
|
|
|
)
|
|
|
|
|
|
|
|
if id := r.FormValue("uuid"); id != "" {
|
|
|
|
for _, a := range config.AutoMessages {
|
|
|
|
if a.ID() != id {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
lt = a.lastMessageSent
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
lt = sched.Next(lt)
|
|
|
|
out = append(out, lt)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.NewEncoder(w).Encode(out); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func configEditorGlobalValidateRegex(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if _, err := regexp.Compile(r.FormValue("regexp")); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
}
|
2023-04-02 12:55:27 +00:00
|
|
|
|
|
|
|
func configEditorGlobalValidateTemplate(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if err := validateTemplate(r.FormValue("template")); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
}
|