利用免费的ESA访问Stack Overflow无需梯子

本站开启邀请码注册,最新邀请码获取: https://t.me/tearcc

Gutou

Member
博客上的教程地址:





创建一个边缘函数

代码换为以下代码


'use strict'

const ASSET_URL = ' '
const JS_VER = 10
const MAX_RETRY = 1

/** @Type {RequestInit} */
const PREFLIGHT_INIT = {
status: 204,
headers: new Headers({
'access-control-allow-origin': '*',
'access-control-allow-methods': 'GET,POST,PUT,PATCH,TRACE,DELETE,HEAD,OPTIONS',
'access-control-max-age': '1728000',
}),
}

/** 工具函数:生成响应 */
function makeRes(body, status = 200, headers = {}) {
headers['--ver'] = JS_VER
headers['access-control-allow-origin'] = '*'
return new Response(body, { status, headers })
}

/** 工具函数:解析 URL */
function newUrl(urlStr) {
try {
return new URL(urlStr)
} catch (err) {
return null
}
}

/** 入口函数:模块方式导出 */
export default {
async fetch(req, env, ctx) {
return fetchHandler(req).catch(err =>
makeRes('cfworker error:\n' + err.stack, 502)
)
}
}

/** 主处理函数 */
async function fetchHandler(req) {
const urlStr = req.url
const urlObj = new URL(urlStr)
const path = urlObj.href.substr(urlObj.origin.length)

// 强制 HTTPS 跳转
if (urlObj.protocol === 'http:') {
urlObj.protocol = 'https:'
return makeRes('', 301, {
'strict-transport-security': 'max-age=99999999; includeSubDomains; preload',
'location': urlObj.href,
})
}

// 代理请求
if (path.startsWith('/http/')) {
return httpHandler(req, path.substr(6))
}

// 特殊路径处理
switch (path) {
case '/http':
return makeRes('请更新到最新版本!')
case '/ws':
return makeRes('not support', 400)
case '/works':
return makeRes('it works')
default:
return fetch(ASSET_URL + path)
}
}

/** HTTP 代理处理逻辑 */
function httpHandler(req, pathname) {
const reqHdrRaw = req.headers
if (reqHdrRaw.has('x-jsproxy')) {
return Response.error()
}

if (
req.method === 'OPTIONS' &&
reqHdrRaw.has('access-control-request-headers')
) {
return new Response(null, PREFLIGHT_INIT)
}

let acehOld = false
let rawSvr = ''
let rawLen = ''
let rawEtag = ''

const reqHdrNew = new Headers(reqHdrRaw)
reqHdrNew.set('x-jsproxy', '1')

const refer = reqHdrNew.get('referer') || ''
const query = refer.includes('?') ? refer.split('?')[1] : ''
if (!query) {
return makeRes('missing params', 403)
}

const param = new URLSearchParams(query)
for (const [k, v] of param.entries()) {
if (k.startsWith('--')) {
switch (k.slice(2)) {
case 'aceh':
acehOld = true
break
case 'raw-info':
[rawSvr, rawLen, rawEtag] = v.split('|')
break
}
} else {
if (v) {
reqHdrNew.set(k, v)
} else {
reqHdrNew.delete(k)
}
}
}

if (!param.has('referer')) {
reqHdrNew.delete('referer')
}

const urlStr = pathname.replace(/^(https?):\/+/, '$1://')
const urlObj = newUrl(urlStr)
if (!urlObj) {
return makeRes('invalid proxy url: ' + urlStr, 403)
}

const reqInit = {
method: req.method,
headers: reqHdrNew,
redirect: 'manual',
body: req.method === 'POST' ? req.body : undefined,
}

return proxy(urlObj, reqInit, acehOld, rawLen, 0)
}

/** 发起代理请求 */
async function proxy(urlObj, reqInit, acehOld, rawLen, retryTimes) {
const res = await fetch(urlObj.href, reqInit)
const resHdrOld = res.headers
const resHdrNew = new Headers(resHdrOld)

let expose = '*'

for (const [k, v] of resHdrOld.entries()) {
if (
k === 'access-control-allow-origin' ||
k === 'access-control-expose-headers' ||
k === 'location' ||
k === 'set-cookie'
) {
const x = '--' + k
resHdrNew.set(x, v)
if (acehOld) {
expose += ',' + x
}
resHdrNew.delete(k)
} else if (
acehOld &&
!['cache-control', 'content-language', 'content-type', 'expires', 'last-modified', 'pragma'].includes(k)
) {
expose += ',' + k
}
}

if (acehOld) {
expose += ',--s'
resHdrNew.set('--t', '1')
}

if (rawLen) {
const newLen = resHdrOld.get('content-length') || ''
const badLen = rawLen !== newLen

if (badLen) {
if (retryTimes < MAX_RETRY) {
const newUrlObj = await parseYtVideoRedir(urlObj, newLen, res)
if (newUrlObj) {
return proxy(newUrlObj, reqInit, acehOld, rawLen, retryTimes + 1)
}
}

return makeRes(res.body, 400, {
'--error': `bad len: ${newLen}, expect: ${rawLen}`,
'access-control-expose-headers': '--error',
})
}

if (retryTimes > 1) {
resHdrNew.set('--retry', retryTimes)
}
}

let status = res.status
resHdrNew.set('access-control-expose-headers', expose)
resHdrNew.set('access-control-allow-origin', '*')
resHdrNew.set('--s', status)
resHdrNew.set('--ver', JS_VER)

resHdrNew.delete('content-security-policy')
resHdrNew.delete('content-security-policy-report-only')
resHdrNew.delete('clear-site-data')

if ([301, 302, 303, 307, 308].includes(status)) {
status += 10
}

return new Response(res.body, {
status,
headers: resHdrNew,
})
}

/** 判断是否为 有图比 视频 URL */
function isYtUrl(urlObj) {
return (
urlObj.host.endsWith('.googlevideo.com') &&
urlObj.pathname.startsWith('/videoplayback')
)
}

/** 处理 有图比 视频跳转解析 */
async function parseYtVideoRedir(urlObj, newLen, res) {
if (parseInt(newLen) > 2000 || !isYtUrl(urlObj)) {
return null
}
try {
const data = await res.text()
const newUrl = newUrl(data)
return isYtUrl(newUrl) ? newUrl : null
} catch {
return null
}
}复制代码

然后点击快速发布,再点击触发器绑定你的域名。搞定

这个速度正常上网是够用了

AgACAgUAAyEGAASbUJWQAAMRaIHVAkbtvlVtQsFVPodgrfZj6hgAAj3LMRt2NBFU03mR_W1VLvkBAAMCAAN5AAM2BA.png


此教程仅供学习使用,使用者需要自行承担相关责任。



精彩评论


郑爽 发表于4 分钟前

你不是入籍新加坡吗,还需要翻?
van 发表于4 分钟前

我只能说666
 
后退
顶部