diff --git a/cmd/main.go b/cmd/main.go index fcf25cb..8c6d172 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -212,5 +212,6 @@ func getNewApp(log logger.T, v config.SubConfigT) (app.Option, error) { C: cch, L: logger.NewLayer(log, fmt.Sprintf("[%s] app", v.Name)), DefaultVideoHeight: v.DefaultVideoHeight, + MaxVideoHeight: v.MaxVideoHeight, }, nil } diff --git a/config.example.jsonc b/config.example.jsonc index fbc66d3..61d50d7 100644 --- a/config.example.jsonc +++ b/config.example.jsonc @@ -9,9 +9,12 @@ // web server listen port. // DEFAULT 8080 "port": 8080, - // used if video height not set in request + // used if video height not set in request. // DEFAULT 720 "default-video-height": 360, + // restrict maximum video height. + // DEFAULT 720 + "max-video-height": 1080, // logger config "log": { // log level @@ -139,6 +142,7 @@ "my.streamer.example" ], "default-video-height": 1080, + "max-video-height": 1080, "extractor": { "path": "direct" }, diff --git a/lib/app/app.go b/lib/app/app.go index afcf51b..aaa5204 100644 --- a/lib/app/app.go +++ b/lib/app/app.go @@ -28,7 +28,8 @@ type app struct { name string sites []string log logger.T - defaultVideoHeight uint16 + defaultVideoHeight uint64 + maxVideoHeight uint64 } type AppLogic struct { @@ -45,7 +46,8 @@ type Option struct { S streamer.T C cache.T L logger.T - DefaultVideoHeight uint16 + DefaultVideoHeight uint64 + MaxVideoHeight uint64 } func New(log logger.T, def Option, opts []Option) *AppLogic { @@ -63,6 +65,7 @@ func (t *AppLogic) set(log logger.T, def Option, opts []Option) { extractor: def.X, streamer: def.S, defaultVideoHeight: def.DefaultVideoHeight, + maxVideoHeight: def.MaxVideoHeight, } t.appList = make([]app, 0) @@ -75,6 +78,7 @@ func (t *AppLogic) set(log logger.T, def Option, opts []Option) { sites: v.Sites, log: v.L, defaultVideoHeight: v.DefaultVideoHeight, + maxVideoHeight: v.MaxVideoHeight, }) } } @@ -199,11 +203,19 @@ func parseQuery(query string) (string, uint64, string) { } func (t *app) fixRequest(link string, height uint64, format string) extractor_config.RequestT { - var h string - if height == 0 { - h = fmt.Sprintf("%d", t.defaultVideoHeight) - } else { - h = fmt.Sprintf("%d", height) + var ( + h string + toS = func(d uint64) string { + return fmt.Sprintf("%d", d) + } + ) + switch { + case height == 0: + h = toS(t.defaultVideoHeight) + case height > t.maxVideoHeight: + h = toS(t.maxVideoHeight) + default: + h = toS(height) } return extractor_config.RequestT{ URL: link, diff --git a/lib/config/config.go b/lib/config/config.go index 801fe41..beabfa4 100644 --- a/lib/config/config.go +++ b/lib/config/config.go @@ -15,7 +15,8 @@ import ( type ConfigT struct { PortInt uint16 `json:"port"` Host string `json:"host"` - DefaultVideoHeight uint16 `json:"default-video-height"` + DefaultVideoHeight uint64 `json:"default-video-height"` + MaxVideoHeight uint64 `json:"max-video-height"` Streamer streamer.ConfigT `json:"streamer"` Extractor extractor_config.ConfigT `json:"extractor"` Log logger_config.ConfigT `json:"log"` @@ -53,6 +54,7 @@ func defaultConfig() ConfigT { PortInt: 8080, Host: "0.0.0.0", DefaultVideoHeight: 720, + MaxVideoHeight: 720, Streamer: streamer.ConfigT{ EnableErrorHeaders: &fls, IgnoreMissingHeaders: &fls, @@ -96,6 +98,9 @@ func appendConfig(src ConfigT, dst ConfigT) ConfigT { if dst.DefaultVideoHeight == 0 { dst.DefaultVideoHeight = src.DefaultVideoHeight } + if dst.MaxVideoHeight == 0 { + dst.MaxVideoHeight = src.MaxVideoHeight + } // streamer if dst.Streamer.EnableErrorHeaders == nil { dst.Streamer.EnableErrorHeaders = src.Streamer.EnableErrorHeaders