|
注册免广告
您需要 登录 才可以下载或查看,没有账号?注册
×
- import { serve } from "https://deno.land/[email protected]/http/server.ts"
- const suffixWhiteList = [
- 'shuzijumin.com',
- 'localhost',
- '127.0.0.1',
- ]
- serve(async (req: Request) => {
- const url = new URL(req.url)
- const targetUrl = url.href.replace(`${url.origin}/`, '')
- let urlObj: URL
- try {
- urlObj = new URL(targetUrl)
- let ifHostInWhiteList = false
- for (const suffix of suffixWhiteList) {
- if (urlObj.hostname.toLowerCase().endsWith(suffix)) {
- ifHostInWhiteList = true
- break
- }
- }
- if (!ifHostInWhiteList) {
- return new Response(`hostname suffix not in white list`, {status: 403})
- }
- if (['http:', 'https:'].indexOf(urlObj?.protocol) > -1) {
- let res = await fetch(targetUrl, {
- headers: req.headers,
- method: req.method,
- body: req.body,
- })
- let headers = {}
- res.headers.forEach((value, key) => {
- if (key.toLowerCase()!=='access-control-allow-origin') {
- headers[key] = value
- }
- })
- headers['Access-Control-Allow-Origin'] = '*'
- return new Response(res.body, { headers, status: res.status })
- }
- } catch (e) {
- console.error(e.message)
- }
- return new Response(`Usage: ${url.origin}/https://deno.com/deploy/docs/pricing-and-limits`)
- })
复制代码
|
|