使用 openresty 对 websocket 进行消息频次限制的尝试

in #cn5 months ago

最近我的 BTS 见证人总是收到丢块报警,排查了原因,大概率是因为 API 和 出块都使用同一个程序的原因。

从 Abit 那里了解到,API 请求和出块并没有做线程优先级,所以在有大量 API 请求进入或者长耗时操作的时候,都会对出块造成影响。

对于底层的 C++ 改起来太麻烦,主要我也不会改,所以思路变换一下,那么就在入口位置进行拦截。

最初尝试使用 nginx 的 limit_conn 来限流,但是没有效果。

主要原因是 Websocket 握手建立连接后,就不会再触发规则了,而所有的请求都以 Websocket 的消息形式与后端交互了。

于是换成了 openresty,通过 lua 来建立一个 Websocket 的消息转发机制,符合条件的放行,不符合的拦截。

最终代码越写越复杂,成型的规则就是在 IP 黑名单中的,且 method 也在黑名单里,那么就限流。

具体的代码实现在这里:https://github.com/ety001/openresty/blob/master/scripts/lua/bts/limit_req.lua,可以作为参考。

Sort:  

不符合的IP放到iptables rule里拦截是否可行

可以,在 nginx 里用 deny 指令也可以,但是这就意味着彻底不对这个 ip 进行服务了。我的目标是对黑名单 IP 进行有限的服务,主要是这些是机器人,拉取数据的量太狠。

Hello.
I'm looking for help. @justyy told me to ask you (@ety001).
My main account (https://steemitwallet.com/ @gi2nee/transfers) was hacked on January 10.

The password was also changed by the hacker. So I can't do anything. I applied for restoration on steem. But I haven't heard back yet.

And still the hacker is making a claim. I think the hacker updated my account again (12, Jan) but I don't know what to do. Is there anything I can do other than just wait for a recovery mail from @steem?

Hacker also cancelled a. Maybe he will power down from January 15th.
Please help me.

你好。
寻求帮助。 @justyyy通知你咨询一下。
我的主账号(https://steemitwallet.com/ @gi2nee/transfers)在1月10日被黑客入侵。

密码也被黑客换了。 所以我什么都做不了。 我在steem上申请了修复。 但是还没有答复。

而且黑客还在索赔。 黑客好像又更新了我的账户(12,Jan),不知道该怎么办。 我除了在@steem等恢复邮件发过来以外,没有其他可以做的吗?

黑客还取消了a。 也许他会从1月15日开始力量下降。
求求你帮帮我吧。

不好意思。 我不太懂英语和中文,所以用翻译器翻译了。
I'm sorry. I didn't know English and Chinese well, so I used it as a translator.

@ety001

Hi, The steemit still has the API issue. Who's in charge of the maintenance of steemit? I've heard that you are engaging in the maintenance.

Why does steemit neglect this issue for more than 3 days?

Coin Marketplace

STEEM 0.30
TRX 0.12
JST 0.032
BTC 58439.16
ETH 2965.47
USDT 1.00
SBD 3.69