Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"miniflux.app/v2/internal/config"
"miniflux.app/v2/internal/database"
"miniflux.app/v2/internal/proxyrotator"
"miniflux.app/v2/internal/reader/rewrite"
"miniflux.app/v2/internal/storage"
"miniflux.app/v2/internal/ui/static"
"miniflux.app/v2/internal/version"
Expand Down Expand Up @@ -249,6 +250,12 @@ func Parse() {
}
}

if filePath := config.Opts.RefererOverrideFile(); filePath != "" {
if err := rewrite.LoadRefererOverrides(filePath); err != nil {
printErrorAndExit(fmt.Errorf("unable to load referer overrides: %v", err))
}
}

if flagRefreshFeeds {
refreshFeeds(store)
return
Expand Down
9 changes: 9 additions & 0 deletions internal/config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,11 @@ func NewConfigOptions() *configOptions {
return validateRange(rawValue, 1, 65535)
},
},
"REFERER_OVERRIDE_FILE": {
ParsedStringValue: "",
RawValue: "",
ValueType: stringType,
},
"RUN_MIGRATIONS": {
ParsedBoolValue: false,
RawValue: "0",
Expand Down Expand Up @@ -807,6 +812,10 @@ func (c *configOptions) LogFile() string {
return c.options["LOG_FILE"].ParsedStringValue
}

func (c *configOptions) RefererOverrideFile() string {
return c.options["REFERER_OVERRIDE_FILE"].ParsedStringValue
}

func (c *configOptions) LogDateTime() bool {
return c.options["LOG_DATE_TIME"].ParsedBoolValue
}
Expand Down
16 changes: 16 additions & 0 deletions internal/config/options_parsing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,22 @@ func TestLogFileOptionParsing(t *testing.T) {
}
}

func TestRefererOverrideFileOptionParsing(t *testing.T) {
configParser := NewConfigParser()

if configParser.options.RefererOverrideFile() != "" {
t.Fatalf("Expected REFERER_OVERRIDE_FILE to be empty by default")
}

if err := configParser.parseLines([]string{"REFERER_OVERRIDE_FILE=/path/to/referer-overrides.json"}); err != nil {
t.Fatalf("Unexpected error: %v", err)
}

if configParser.options.RefererOverrideFile() != "/path/to/referer-overrides.json" {
t.Fatalf("Expected REFERER_OVERRIDE_FILE to be '/path/to/referer-overrides.json'")
}
}

func TestLogFormatOptionParsing(t *testing.T) {
configParser := NewConfigParser()

Expand Down
64 changes: 40 additions & 24 deletions internal/reader/rewrite/referer_override.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,59 @@
package rewrite // import "miniflux.app/v2/internal/reader/rewrite"

import (
"encoding/json"
"net/url"
"os"
"strings"
)

var refererMappings = map[string]string{
"appinn.com": "https://appinn.com",
"bjp.org.cn": "https://bjp.org.cn",
"cdnfile.sspai.com": "https://sspai.com",
"f.video.weibocdn.com": "https://weibo.com",
"i.pximg.net": "https://www.pixiv.net",
"img.hellogithub.com": "https://hellogithub.com",
"moyu.im": "https://i.jandan.net",
"www.parkablogs.com": "https://www.parkablogs.com",
".cdninstagram.com": "https://www.instagram.com",
".moyu.im": "https://i.jandan.net",
".sinaimg.cn": "https://weibo.com",
}

func LoadRefererOverrides(externalFilePath string) error {
externalData, err := os.ReadFile(externalFilePath)
if err != nil {
return err
}
var externalMappings map[string]string
if err = json.Unmarshal(externalData, &externalMappings); err != nil {
return err
}
for k, v := range externalMappings {
refererMappings[k] = v
}

return nil
}

// GetRefererForURL returns the referer for the given URL if it exists, otherwise an empty string.
func GetRefererForURL(u string) string {
parsedUrl, err := url.Parse(u)
if err != nil {
return ""
}

switch parsedUrl.Hostname() {
case "appinn.com":
return "https://appinn.com"
case "bjp.org.cn":
return "https://bjp.org.cn"
case "cdnfile.sspai.com":
return "https://sspai.com"
case "f.video.weibocdn.com":
return "https://weibo.com"
case "i.pximg.net":
return "https://www.pixiv.net"
case "img.hellogithub.com":
return "https://hellogithub.com"
case "moyu.im":
return "https://i.jandan.net"
case "www.parkablogs.com":
return "https://www.parkablogs.com"
hostname := parsedUrl.Hostname()

if referer, ok := refererMappings[hostname]; ok {
return referer
}

switch {
case strings.HasSuffix(parsedUrl.Hostname(), ".cdninstagram.com"):
return "https://www.instagram.com"
case strings.HasSuffix(parsedUrl.Hostname(), ".moyu.im"):
return "https://i.jandan.net"
case strings.HasSuffix(parsedUrl.Hostname(), ".sinaimg.cn"):
return "https://weibo.com"
for suffix, referer := range refererMappings {
if strings.HasSuffix(hostname, suffix) {
return referer
}
}

return ""
Expand Down
7 changes: 7 additions & 0 deletions miniflux.1
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,13 @@ Override LISTEN_ADDR to 0.0.0.0:$PORT\&.
.br
Default is empty\&.
.TP
.B REFERER_OVERRIDE_FILE
Path to a JSON file containing referer override mappings\&.
.br
External mappings override embedded defaults for the same keys\&.
.br
Default is empty\&.
.TP
.B RUN_MIGRATIONS
Set to 1 to run database migrations\&.
.br
Expand Down