From 5f290af917cd4afff6f890d61a7ba86e7b547115 Mon Sep 17 00:00:00 2001 From: mejgun Date: Wed, 25 Sep 2024 15:02:18 +0300 Subject: [PATCH] add sites option to default config. fix #34 this adds ability to restrict the app to specified sites only --- CHANGELOG.md | 4 ++++ cmd/main.go | 11 +++++++---- config.example.jsonc | 6 ++++++ lib/app/app.go | 17 +++++++++++++---- lib/config/config.go | 4 ++-- 5 files changed, 32 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9697c55..7eab3c4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,8 +5,12 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). ## [Unreleased] + +## 2.2.0 - 2024-09-25 ### Added - default video height config setting +- max video height setting +- ability to restrict the app to specified sites ## 2.1.0 - 2024-09-25 ### Fixed diff --git a/cmd/main.go b/cmd/main.go index 8c6d172..debae1a 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -17,7 +17,7 @@ import ( streamer "lib/streamer" ) -const appVersion = "2.0.0" +const appVersion = "2.2.0" type flagsT struct { version bool @@ -124,9 +124,12 @@ func readConfig(conf_file string) (config.ConfigT, app.Option, []app.Option, defapp, err := getNewApp(log, config.SubConfigT{ ConfigT: config.ConfigT{ - Streamer: conf.Streamer, - Extractor: conf.Extractor, - Cache: conf.Cache, + Streamer: conf.Streamer, + Extractor: conf.Extractor, + Cache: conf.Cache, + DefaultVideoHeight: conf.DefaultVideoHeight, + MaxVideoHeight: conf.MaxVideoHeight, + Sites: conf.Sites, }, Name: "default", }) diff --git a/config.example.jsonc b/config.example.jsonc index 61d50d7..a3892c2 100644 --- a/config.example.jsonc +++ b/config.example.jsonc @@ -15,6 +15,12 @@ // restrict maximum video height. // DEFAULT 720 "max-video-height": 1080, + // sites (hosts) list. + // if not empty, only the specified sites will work, + // others will be forbidden. + // see sub-config part for a detailed explaination. + // DEFAULT [] + "sites": [], // logger config "log": { // log level diff --git a/lib/app/app.go b/lib/app/app.go index aaa5204..ca6c815 100644 --- a/lib/app/app.go +++ b/lib/app/app.go @@ -66,6 +66,7 @@ func (t *AppLogic) set(log logger.T, def Option, opts []Option) { streamer: def.S, defaultVideoHeight: def.DefaultVideoHeight, maxVideoHeight: def.MaxVideoHeight, + sites: def.Sites, } t.appList = make([]app, 0) @@ -83,16 +84,19 @@ func (t *AppLogic) set(log logger.T, def Option, opts []Option) { } } -func (t *AppLogic) selectApp(rawURL string) app { +func (t *AppLogic) selectApp(rawURL string) (app, error) { host, err := parseUrlHost(rawURL) if err == nil { for _, v := range t.appList { if slices.Contains(v.sites, host) { - return v + return v, nil } } } - return t.defaultApp + if len(t.defaultApp.sites) == 0 || slices.Contains(t.defaultApp.sites, host) { + return t.defaultApp, nil + } + return app{}, fmt.Errorf("host %s did not match any sites in config or sub-configs", host) } func parseUrlHost(rawURL string) (string, error) { @@ -110,7 +114,12 @@ func (t *AppLogic) Run(w http.ResponseWriter, r *http.Request) { } now := time.Now() link, height, format := parseQuery(r.RequestURI) - resapp := t.selectApp(link) + resapp, err := t.selectApp(link) + if err != nil { + t.log.LogWarning("", err) + w.WriteHeader(http.StatusForbidden) + return + } req := resapp.fixRequest(link, height, format) t.log.LogInfo("Request", req, "app", resapp.name) if res, ok, expired := resapp.cacheCheck(req, now); ok { diff --git a/lib/config/config.go b/lib/config/config.go index beabfa4..ae9b275 100644 --- a/lib/config/config.go +++ b/lib/config/config.go @@ -17,6 +17,7 @@ type ConfigT struct { Host string `json:"host"` DefaultVideoHeight uint64 `json:"default-video-height"` MaxVideoHeight uint64 `json:"max-video-height"` + Sites []string `json:"sites"` Streamer streamer.ConfigT `json:"streamer"` Extractor extractor_config.ConfigT `json:"extractor"` Log logger_config.ConfigT `json:"log"` @@ -25,8 +26,7 @@ type ConfigT struct { } type SubConfigT struct { - Name string `json:"name"` - Sites []string `json:"sites"` + Name string `json:"name"` ConfigT }