【霏艺所思】Cloudflare的ngx_brotli_module压缩模块

Cloudflare下的开源项目精华真多
https://github.com/cloudflare/ngx_brotli_module
这个,我觉得就可以很适合初学者入门nginx 的module开发

整个开源项目 ,就一个代码文件 ngx_http_brotli_filter_module.c

入口函数 叫 ngx_http_brotli_filter_init
给一句忠告 ,一般你自己写mod ,都是从 postconfiguration 这个函数指针写起的
这里用到了 装饰器 设计模式,会python的话,就理解为 @

ngx_http_brotli_header_filter  针对 http的 报文头
说白了就是修改 Content-Encoding 字段 为 br

ngx_http_brotli_body_filter 针对 http的 body
说白了 就是对原来的body 进行 ngx_http_brotli_filter_compress 压缩

这样,http的body就是br压缩的后的buffer了~
2
分享 2020-05-16

1 个评论

霏艺Faye 图书管理员
说到nginx了,就多说些吧
像cloudflare这样的装饰器模式,自然可以完成很多需求,但是有时候不能!!!
比如,我希望针对某一个单一的 请求做特殊处理!

https://xxxx/index.html 就是 a操作
https://xxxx/fjkdlsalhgfjasdkhgfda 我希望是一个websocket操作
怎么办呢?

自然有办法!
搜索 src/http/ngx_http_core_module.c文件里的
ngx_http_core_content_phase函数

看到 ngx_http_finalize_request(r, r->content_handler(r)); 这句关键代码
也就是 content_handler 函数指针
只有匹配了特定的 location以后,才会对content_handler赋值!
我们需要实现自己的 content_handler 函数指针指向的函数

例如 src/http/modules/ngx_http_proxy_module.c
看到 ngx_http_proxy_commands 里 有 proxy_pass 这个字符串了没?
它 绑定了一个 函数 ngx_http_proxy_pass
翻译成人话版本:
如果,在 ngin短网址nf 文件 的 location里 看到了 proxy_pass 这个字符串 ,就会执行 ngx_http_proxy_pass 这个函数
我们点进去 看看这个函数在干嘛
关键 函数 clcf->handler = ngx_http_proxy_handler;
这个 ngx_http_proxy_handler 就是在这里被指向了 content_handler !!!
【不严谨了,,,, 为了你们好,真实情况 ngx_http_core_find_location 根据 location 去查找 clcf,然后把clcf的handler 设置给 了content_handler 】


-----------------------------------------------
无聊的人,可以自己写个mod给nginx,这样nginx就可以完成v2ray的工作
【VPS上只跑nginx就可以翻墙,不需要同时运行nginx和v2ray了~】
复制 ngx_http_proxy_module.c 文件,取名 ngx_http_faye_module.c
表示我的mod
然后,根据我刚才的话,去实现ngx_http_faye_handler

这里,我觉得正常程序员都应该可以做到的!!!
难得地方来了~
websocket协议的实现不会。。。

好在我会抄袭。。。
https://github.com/signalwire/freeswitch/tree/master/src/mod/endpoints/mod_verto
使用 ws.c里的代码【websocket的缩写】,就可以完成建立 ws的握手和 数据读写

接下来是第二步socks5协议的解析
https://github.com/rofl0r/microsocks/blob/master/sockssrv.c 里的代码,就可以完成socks5

这样,就可以几乎没写什么代码的情况下
完成了,nginx的mod开发,支持了v2ray的 socks5 over wss来翻墙!!!
这里涉及了
ngx_http_proxy_module.c
ws.c
sockssrv.c
3个文件!

对了,光教你们写代码实现,没告诉你们怎么编译这个末端。。。。
Use the "--add-module=" when configuring NGINX to enable the module.
--------------------------------
希望你们有朝一日可以自己实现自己的翻墙软件吧。。。

我自己是对研究这些代码一分钱兴趣都没有了!!!
懒得再写教程了!!!

我就想安安静静的发音乐!!!
你们能不能多回复下我的音乐。。。我需要葱!!!

要发言请先登录注册

要发言请先登录注册