├── .gitattributes ├── .gitignore ├── DeployToHeroku.md ├── Procfile ├── cloudflare_worker.js ├── favicon.ico ├── readme.md ├── requirements.txt ├── vercel.json └── wsgi.py /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ################# 2 | ## Eclipse 3 | ################# 4 | 5 | *.pydevproject 6 | .project 7 | .metadata 8 | bin/ 9 | tmp/ 10 | *.tmp 11 | *.bak 12 | *.swp 13 | *~.nib 14 | local.properties 15 | .classpath 16 | .settings/ 17 | .loadpath 18 | 19 | # External tool builders 20 | .externalToolBuilders/ 21 | 22 | # Locally stored "Eclipse launch configurations" 23 | *.launch 24 | 25 | # CDT-specific 26 | .cproject 27 | 28 | # PDT-specific 29 | .buildpath 30 | 31 | 32 | ################# 33 | ## Visual Studio 34 | ################# 35 | 36 | ## Ignore Visual Studio temporary files, build results, and 37 | ## files generated by popular Visual Studio add-ons. 38 | 39 | # User-specific files 40 | *.suo 41 | *.user 42 | *.sln.docstates 43 | 44 | # Build results 45 | 46 | [Dd]ebug/ 47 | [Rr]elease/ 48 | x64/ 49 | build/ 50 | [Bb]in/ 51 | [Oo]bj/ 52 | 53 | # MSTest test Results 54 | [Tt]est[Rr]esult*/ 55 | [Bb]uild[Ll]og.* 56 | 57 | *_i.c 58 | *_p.c 59 | *.ilk 60 | *.meta 61 | *.obj 62 | *.pch 63 | *.pdb 64 | *.pgc 65 | *.pgd 66 | *.rsp 67 | *.sbr 68 | *.tlb 69 | *.tli 70 | *.tlh 71 | *.tmp 72 | *.tmp_proj 73 | *.log 74 | *.vspscc 75 | *.vssscc 76 | .builds 77 | *.pidb 78 | *.log 79 | *.scc 80 | 81 | # Visual C++ cache files 82 | ipch/ 83 | *.aps 84 | *.ncb 85 | *.opensdf 86 | *.sdf 87 | *.cachefile 88 | 89 | # Visual Studio profiler 90 | *.psess 91 | *.vsp 92 | *.vspx 93 | 94 | # Guidance Automation Toolkit 95 | *.gpState 96 | 97 | # ReSharper is a .NET coding add-in 98 | _ReSharper*/ 99 | *.[Rr]e[Ss]harper 100 | 101 | # TeamCity is a build add-in 102 | _TeamCity* 103 | 104 | # DotCover is a Code Coverage Tool 105 | *.dotCover 106 | 107 | # NCrunch 108 | *.ncrunch* 109 | .*crunch*.local.xml 110 | 111 | # Installshield output folder 112 | [Ee]xpress/ 113 | 114 | # DocProject is a documentation generator add-in 115 | DocProject/buildhelp/ 116 | DocProject/Help/*.HxT 117 | DocProject/Help/*.HxC 118 | DocProject/Help/*.hhc 119 | DocProject/Help/*.hhk 120 | DocProject/Help/*.hhp 121 | DocProject/Help/Html2 122 | DocProject/Help/html 123 | 124 | # Click-Once directory 125 | publish/ 126 | 127 | # Publish Web Output 128 | *.Publish.xml 129 | *.pubxml 130 | 131 | # NuGet Packages Directory 132 | ## TODO: If you have NuGet Package Restore enabled, uncomment the next line 133 | #packages/ 134 | 135 | # Windows Azure Build Output 136 | csx 137 | *.build.csdef 138 | 139 | # Windows Store app package directory 140 | AppPackages/ 141 | 142 | # Others 143 | sql/ 144 | *.Cache 145 | ClientBin/ 146 | [Ss]tyle[Cc]op.* 147 | ~$* 148 | *~ 149 | *.dbmdl 150 | *.[Pp]ublish.xml 151 | *.pfx 152 | *.publishsettings 153 | 154 | # RIA/Silverlight projects 155 | Generated_Code/ 156 | 157 | # Backup & report files from converting an old project file to a newer 158 | # Visual Studio version. Backup files are not needed, because we have git ;-) 159 | _UpgradeReport_Files/ 160 | Backup*/ 161 | UpgradeLog*.XML 162 | UpgradeLog*.htm 163 | 164 | # SQL Server files 165 | App_Data/*.mdf 166 | App_Data/*.ldf 167 | 168 | ############# 169 | ## Windows detritus 170 | ############# 171 | 172 | # Windows image file caches 173 | Thumbs.db 174 | ehthumbs.db 175 | 176 | # Folder config file 177 | Desktop.ini 178 | 179 | # Recycle Bin used on file shares 180 | $RECYCLE.BIN/ 181 | 182 | # Mac crap 183 | .DS_Store 184 | 185 | 186 | ############# 187 | ## Python 188 | ############# 189 | 190 | *.py[co] 191 | 192 | # Packages 193 | *.egg 194 | *.egg-info 195 | dist/ 196 | build/ 197 | eggs/ 198 | parts/ 199 | var/ 200 | sdist/ 201 | develop-eggs/ 202 | .installed.cfg 203 | 204 | # Installer logs 205 | pip-log.txt 206 | 207 | # Unit test / coverage reports 208 | .coverage 209 | .tox 210 | 211 | #Translations 212 | *.mo 213 | 214 | #Mr Developer 215 | .mr.developer.cfg 216 | -------------------------------------------------------------------------------- /DeployToHeroku.md: -------------------------------------------------------------------------------- 1 | #部署Forwarder到Heroku的教程 2 | 3 | > 此教程包含两种部署方法,第一种最简单,什么软件都不需要安装;第二种为部署的标准方法,需要安装一些工具软件,自己选择吧。 4 | 5 | ##傻瓜步骤(使用网页直接部署) 6 | 1. Fork [Forwarder](https://github.com/cdhigh/Forwarder) 到你的Github账户 7 | 8 | 2. 登录 [Heroku](https://www.heroku.com/),没有账号就申请一个 9 | 10 | 3. 点击网页右上角的加号,'Create new app',创建应用后会自动跳转至dashboard 11 | 12 | 4. 在dashboard页面上的 'Deploy' | 'Deployment method' 区段选择 'Github' 13 | 14 | 5. 点击 'Connect to Github' 按钮,授权Heroku访问你的Github账户 15 | 16 | 6. 授权后就可以在 'Connect to Github' 的 'repo-name' 输入你的Github仓库名比如:Forwarder,点搜索后再点'Connect'即可 17 | 18 | 7. 使用 'Manual deploy' 区段的按钮 'Deploy Branch' 按钮将Github代码直接部署到Heroku的服务器 19 | 20 | 8. 用浏览器访问你的应用网页地址测试是否部署成功 21 | 22 | ##标准步骤(使用Heroku工具套件) 23 | 1. 安装git (针对没有git的机器) 24 | `sudo apt-get install git` 25 | *(如果是Windows直接下载git安装包安装即可)* 26 | 27 | 2. 安装ruby 28 | `sudo apt-get install ruby` 29 | 30 | 3. 安装heroku 31 | `sudo gem install heroku` 32 | 33 | 4. 确认应用目录下必须有这两个文件:requirements.txt 和 Procfile. 34 | 35 | 5. 在应用目录下新建git仓库 36 | `git init` 37 | `git add .` 38 | `git commit -m “initial”` 39 | 40 | 6. 登录heroku 41 | `heroku login` 42 | 43 | 7. 增加publickey,如果没有的话 44 | `heroku keys:add` 45 | 46 | 8. 新建远端heroku应用 47 | `heroku create --stack cedar` 48 | 49 | 9. 确认远端git库的名字,一般是heroku,有时候可能是origin 50 | `git remote -v` 51 | 如果不对,可以增加: 52 | `heroku git:remote -a falling-wind-1624` 53 | 或 54 | `git remote add heroku git@heroku.com:kforwarder.git` 55 | 56 | 10. 上传代码(这里面的heroku就是上一步查询出来的git库名字) 57 | `git push heroku master` 58 | 59 | 11. 如果需要,修改应用的进程数 60 | `heroku scale web=1` 61 | 62 | 12. 其他几个有用到命令 63 | `heroku ps` 64 | `heroku logs` 65 | -------------------------------------------------------------------------------- /Procfile: -------------------------------------------------------------------------------- 1 | web: python ./wsgi.py $PORT -------------------------------------------------------------------------------- /cloudflare_worker.js: -------------------------------------------------------------------------------- 1 | const ALLOW_KEYS = 'xzSlE'; 2 | 3 | export default { 4 | async fetch(request, env, ctx) { 5 | return await handleRequest(request); 6 | } 7 | } 8 | 9 | async function handleRequest(request) { 10 | const url = new URL(request.url); 11 | const params = new URLSearchParams(url.search); 12 | 13 | const u = params.get('u'); // 需要转发的url 14 | const k = params.get('k'); // AUTHKEY 15 | const timeout = parseInt(params.get('t') || '30') * 1000; // 超时时间,默认30秒 16 | 17 | if (!k || k !== ALLOW_KEYS) { 18 | return new Response('Auth Key is invalid!', { status: 403 }); 19 | } 20 | 21 | if (!u || !k) { 22 | return new Response('Forwarder UrlForwarder(1.2.4) : thisurl?k=AUTHKEY&t=timeout&u=url', { 23 | headers: { 'Content-Type': 'text/html' }, 24 | }); 25 | } 26 | 27 | const referer = new URL(u).origin; 28 | 29 | try { 30 | // 设置超时的 Promise,用于处理超时情况 31 | const controller = new AbortController(); 32 | const timeoutId = setTimeout(() => controller.abort(), timeout); 33 | 34 | // 转发请求 35 | const response = await fetch(u, { 36 | method: 'GET', 37 | headers: { 38 | 'User-Agent': 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)', 39 | 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 40 | 'Accept-Language': 'en,*', 41 | 'Referer': referer 42 | }, 43 | signal: controller.signal, 44 | }); 45 | 46 | clearTimeout(timeoutId); 47 | 48 | // 提取并传递所有非 hop-by-hop 的 headers 49 | const newHeaders = new Headers(response.headers); 50 | ['connection', 'keep-alive', 'proxy-authenticate', 'proxy-authorization', 'te', 'trailer', 'transfer-encoding', 'upgrade'].forEach(header => { 51 | newHeaders.delete(header); 52 | }); 53 | 54 | return new Response(await response.text(), { 55 | status: response.status, 56 | headers: newHeaders, 57 | }); 58 | } catch (e) { 59 | console.error("Error: ", e); 60 | return new Response('Request failed!
' + e, { status: 400 }); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cdhigh/Forwarder/c2a324f8708fc56eac4914b89e2478c651759f7a/favicon.ico -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # 描述: 2 | 3 | 这是一个很简单的HTTP请求转发服务器,没有任何外部依赖,可以很简单的部署到任何支持Python WSGI的 4 | 云端服务器,或部署为cloudflare的worker。 5 | 用于中转KindleEar的个别HTTP请求,以便绕过墙或绕过部分网站对GAE的IP的封锁。 6 | 7 | # 使用方法: 8 | 在KindleEar的recipe文件里面将需要转发的feed地址修改为以下的调用格式: 9 | `http://example.com/?k=xzSlE&t=TIMEOUT&u=URL` 10 | 其中xzS1E为验证码,TIMEOUT为超时时间,可省略,默认为30s,URL则为要转发的URL. 11 | 12 | # 一键部署到Vercel: 13 | [![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fcdhigh%2FForwarder&env=ALLOW_KEY) 14 | 15 | # 部署到cloudflare: 16 | 1. 在cloudflare的dashboard页面点击左侧的 `Workers & Pages` 导航栏。 17 | 2. 点击 `Create Worker`,输入一个域名,点击 `Deploy`。 18 | 3. 部署完成后点击对应Worker右上角的 `Edit Code` ,将 `cloudflare_worker.js` 的内容拷贝粘贴过去,再点击右上角的 `Deploy`,完成部署。 19 | 20 | # 部署到Heroku: 21 | 参见 [DeployToHeroku.md](https://github.com/cdhigh/forwarder/blob/master/DeployToHeroku.md) 22 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /vercel.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": 2, 3 | "builds": [ 4 | { 5 | "src": "wsgi.py", 6 | "use": "@vercel/python" 7 | } 8 | ], 9 | "routes": [ 10 | { 11 | "src": "/(.*)", 12 | "dest": "wsgi.py" 13 | } 14 | ] 15 | } 16 | -------------------------------------------------------------------------------- /wsgi.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding:utf-8 -*- 3 | """ python网络请求转发器,没有任何外部依赖,可以轻易将其部署到任何一个支持WSGI的托管空间,兼容Python2/3。 4 | 用法: 5 | http://hostedurl?k=AUTHKEY&t=TIMEOUT&u=URL 6 | 解析: 7 | hostedurl: 转发服务器的URL 8 | URL: 需要转发的url,需要先使用urllib.quote转义,特别是如果有&符号 9 | AUTHKEY: 为了防止滥用,需要提供一个key 10 | TIMEOUT: [可选]超时时间,默认为30s 11 | """ 12 | import os, socket 13 | from wsgiref.util import is_hop_by_hop 14 | try: 15 | from urllib.parse import unquote, urlparse 16 | from urllib.request import Request, urlopen 17 | from urllib.error import URLError 18 | except ImportError: 19 | from urllib import unquote, urlparse 20 | import urllib2 21 | Request = urllib2.Request 22 | urlopen = urllib2.urlopen 23 | URLError = urllib2.URLError 24 | 25 | ALLOW_KEY = os.environ.get('ALLOW_KEY') or 'xzSlE' 26 | DEFAULT_TIMEOUT = 30 27 | __Version__ = "1.4" 28 | 29 | def msg(txt): 30 | return "Url Forwarder

Url Forwarder v{}

{}".format(__Version__, txt).encode('utf-8') 31 | 32 | def application(environ, start_response): 33 | #from wsgiref.util import setup_testing_defaults 34 | #setup_testing_defaults(environ) 35 | 36 | # Parse query parameters 37 | query_string = environ.get('QUERY_STRING', '') 38 | query_params = dict(qc.split("=", 1) for qc in query_string.split("&") if "=" in qc) 39 | 40 | url = query_params.get('u') 41 | key = query_params.get('k') 42 | try: 43 | timeout = int(query_params.get('t', DEFAULT_TIMEOUT)) 44 | except: 45 | timeout = DEFAULT_TIMEOUT 46 | 47 | if key and key != ALLOW_KEY: 48 | start_response('403 FORBIDDEN', [('Content-Type', 'text/html')]) 49 | return [msg('Auth Key is invalid!')] 50 | 51 | if not url or not key: 52 | start_response('200 OK', [('Content-Type', 'text/html')]) 53 | return [msg('Usage: thisurl?k=AUTHKEY&t=TIMEOUT&u=URL')] 54 | 55 | url = unquote(url).replace(' ', r'%20') 56 | parts = urlparse(url) 57 | referer = f'{parts.scheme}://{parts.netloc}' 58 | 59 | try: 60 | req = Request(url) 61 | req.add_header('User-Agent', "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)") 62 | req.add_header('Accept', "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8") 63 | req.add_header('Accept-Language', "en,*") 64 | req.add_header('Referer', referer) 65 | ret = urlopen(req, timeout=timeout) 66 | content = ret.read() 67 | headers = [(n, v) for n, v in ret.info().items() if not is_hop_by_hop(n)] 68 | start_response('200 OK', headers) 69 | return [content] 70 | except socket.timeout: 71 | start_response('504 GATEWAY TIMEOUT', [('Content-Type', 'text/html')]) 72 | return [msg('Timeout error')] 73 | except Exception as e: 74 | err_msg = str(e).replace("<", "<").replace(">", ">") 75 | print("ERROR : {} : {}".format(type(e), str(e))) 76 | start_response('400 BAD REQUEST', [('Content-Type', 'text/html')]) 77 | return [msg('Error occurred:
{}'.format(err_msg))] 78 | 79 | app = application 80 | 81 | if __name__ == '__main__': 82 | from wsgiref.simple_server import make_server 83 | port = int(os.environ.get('PORT', 5000)) 84 | with make_server('0.0.0.0', port, application) as httpd: 85 | print("Serving on port {}...".format(port)) 86 | httpd.serve_forever() 87 | --------------------------------------------------------------------------------