V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
yzhen123
V2EX  ›  Node.js

如何用 10 行 nodejs 代码开发一个 google 镜像站

  •  2
     
  •   yzhen123 · May 31, 2017 · 6028 views
    This topic created in 3253 days ago, the information mentioned may be changed or developed.

    嘿嘿,其实是有点标题党了。。

    Google 的首页的静态资源并没有使用 CDN,实现起来其实是比较简单的,如果有 CDN 的话还需要用正则替换静态链接。这里使用了 koa-proxy,虽然是下载之后再转发可能会带来一定的性能影响,不过可以使用 koa 的中间件对响应进行修改,如果用 koa-http-proxy 的话就没这么容易了。。

    
    const Koa = require('koa')
    const proxy = require('koa-proxy')
    const mount = require('koa-mount')
    const convert = require('koa-convert')
    const app = new Koa();
    const zlib = require('zlib')
    
    app.use(convert(function * (next) {
      yield next
      if (this.path === '/' || this.path === '/search') {
        const data = yield zlib.gunzip.bind(zlib, this.body)
        const str = data.toString('utf-8').replace(/<body([^>]*)>/, `<body$1>
      <div style="font-size: 14px; text-align: center; padding: 3px 10px;">Powered By <a href="https://www.lets-ss.com">lets-ss.com</a></div>
    `)
        this.body = yield zlib.gzip.bind(zlib, str)
      }
    }))
    
    app.use(convert(proxy({
      host: 'https://www.google.com.hk', // 目标站 点
      jar: true, // 转发 cookie
      followRedirect: false, // co-request 的参数,不跟随跳转
    })));
    app.listen(80);
    
    

    测试可以使用 proxychains 让命令行走代理

    proxychains node ./index.js
    

    顺便分享一下我搭建的站点: https://g.lets-ss.com

    16 replies    2017-06-02 13:47:17 +08:00
    ixiaohei
        1
    ixiaohei  
       May 31, 2017
    反代,不过你网站速度真慢
    Showfom
        2
    Showfom  
    PRO
       May 31, 2017
    这样公开域名很容易被墙的
    yzhen123
        3
    yzhen123  
    OP
       May 31, 2017
    @ixiaohei 额,可能是有些广告或者统计链接还是走了 google 的域名
    des
        4
    des  
       May 31, 2017 via Android
    数了一下,不止 10 行,lol
    yzhen123
        5
    yzhen123  
    OP
       May 31, 2017
    @des 其实 如果去掉修改网页内容的代码,加起来也只有 10 行多一点
    yzhen123
        6
    yzhen123  
    OP
       May 31, 2017
    @Showfom 是吗?谢谢关系,不过没关系,现在节点比较多,玩完了就撤。。
    yzhen123
        7
    yzhen123  
    OP
       May 31, 2017
    @Showfom 谢谢关心。。
    KIDJourney
        8
    KIDJourney  
       May 31, 2017
    说什么一共 10 行,你考虑过 Koa 的感受吗?
    yzhen123
        9
    yzhen123  
    OP
       May 31, 2017
    @KIDJourney 嘿嘿,其实是有点标题党哈,第一行就已经承认了,求各位大佬轻喷
    est
        10
    est  
       May 31, 2017
    如何一行代码都不写用 nginx 实现无痛人流

    /t/341913
    douglas1997
        11
    douglas1997  
       Jun 1, 2017
    package.json 可以共享一下吗?
    eccstartup
        12
    eccstartup  
       Jun 1, 2017 via iPhone
    @douglas1997 这是什么鬼
    douglas1997
        13
    douglas1997  
       Jun 1, 2017
    @eccstartup ?? nodejs 程序不是需要 packages.json 然后安装依赖?不知道楼主 koa 是什么版本。
    loadinger
        14
    loadinger  
       Jun 1, 2017
    nodejs 会被你们这样搞臭的.别取这样的名字好么..
    Dye8
        15
    Dye8  
       Jun 1, 2017
    10 行还不简单 楼主快去几个换行
    470326964
        16
    470326964  
       Jun 2, 2017
    @Dye8 标题党。。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3379 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 57ms · UTC 12:53 · PVG 20:53 · LAX 05:53 · JFK 08:53
    ♥ Do have faith in what you're doing.