forked from DIYgod/RSSHub
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: internal media proxy (DIYgod#14593)
* feat: image proxy * feat: use Map * docs: add docs * fix: change config name * Add lightnovel.us as a media source * fix: break hostname extraction * feat: add psl for more accurate domain extraction * feat: split referer map * perf(deps): replace `psl` with `tldts` * fix: add indienova.com to referer map
- Loading branch information
Showing
10 changed files
with
100 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
const got = require('@/utils/got'); | ||
const config = require('@/config').value; | ||
const { getDomain } = require('tldts'); | ||
const { refererMap } = require('./referer-map'); | ||
|
||
module.exports = async (ctx) => { | ||
if (!config.feature.mediaProxyKey) { | ||
ctx.throw(403, 'Internal media proxy is disabled.'); | ||
} | ||
|
||
const { key } = ctx.params; | ||
if (key !== config.feature.mediaProxyKey) { | ||
ctx.throw(401, 'Invalid media proxy key.'); | ||
} | ||
|
||
const url = decodeURIComponent(ctx.params.url); | ||
const requestUrl = new URL(url); | ||
const { hostname, origin } = requestUrl; | ||
|
||
const domain = getDomain(hostname); | ||
|
||
let referer = refererMap.get(domain); | ||
referer ||= origin; | ||
|
||
const { headers } = await got.head(url, { | ||
headers: { | ||
referer, | ||
}, | ||
}); | ||
|
||
const cacheControl = headers['cache-control']; | ||
const contentType = headers['content-type']; | ||
const contentLength = headers['content-length']; | ||
|
||
if (!contentType.startsWith('image/') || headers.server === 'RSSHub') { | ||
return ctx.redirect(url); | ||
} | ||
|
||
ctx.set({ | ||
'cache-control': cacheControl || `public, max-age=${config.cache.contentExpire}`, | ||
'content-length': contentLength, | ||
'content-type': contentType, | ||
server: 'RSSHub', | ||
}); | ||
|
||
ctx.body = await got.stream(url, { | ||
headers: { | ||
referer, | ||
}, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const refererMap = new Map([ | ||
['fbcdn.net', 'https://www.facebook.com/'], | ||
['cdninstagram.com', 'https://www.instagram.com/'], | ||
['moyu.im', 'https://jandan.net/'], | ||
['lightnovel.us', 'https://www.lightnovel.us/'], | ||
['indienova.com', 'https://indienova.com/'], | ||
['pximg.net', 'https://www.pixiv.net/'], | ||
['me8gs.app', 'https://www.sehuatang.net/'], | ||
['rxn30.app', 'https://www.sehuatang.net/'], | ||
['sex.com', 'https://www.sex.com/'], | ||
['sinaimg.cn', 'https://weibo.com/'], | ||
]); | ||
|
||
module.exports = { | ||
refererMap, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters