Skip to content

Commit

Permalink
add sites option to default config. fix #34
Browse files Browse the repository at this point in the history
this adds ability to restrict the app to specified sites only
  • Loading branch information
mejgun committed Sep 25, 2024
1 parent bc076be commit 5f290af
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 7 additions & 4 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
streamer "lib/streamer"
)

const appVersion = "2.0.0"
const appVersion = "2.2.0"

type flagsT struct {
version bool
Expand Down Expand Up @@ -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",
})
Expand Down
6 changes: 6 additions & 0 deletions config.example.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 13 additions & 4 deletions lib/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions lib/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -25,8 +26,7 @@ type ConfigT struct {
}

type SubConfigT struct {
Name string `json:"name"`
Sites []string `json:"sites"`
Name string `json:"name"`
ConfigT
}

Expand Down

0 comments on commit 5f290af

Please sign in to comment.