Skip to content

Commit

Permalink
new blog post
Browse files Browse the repository at this point in the history
  • Loading branch information
ginuerzh committed Sep 4, 2024
1 parent f47e644 commit d8aafac
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 3 deletions.
53 changes: 53 additions & 0 deletions docs/blog/posts/limiter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
authors:
- ginuerzh
categories:
- Limiter
- Observer
readtime: 10
date: 2024-09-04
comments: true
---

# 用户级别的流量统计和动态限流方案

GOST中的[观测器](https://gost.run/concepts/observer/)组件可以用来对服务的连接和流量进行统计,当服务配置使用了观测器,则会周期性以事件(Event)的方式上报此服务的接收(inputBytes)和发送数据(outputBytes)总字节数。而[限制器](https://gost.run/concepts/limiter/)组件则可以用来限制服务的连接和流量。

有些时候可能需要对流量进行更加精细化管理。例如一个支持认证的代理服务,需要按用户进行流量统计或限速,更进一步可能还需要根据用户的实时流量来做动态限流。由于不同的使用场景可能会有比较复杂的处理逻辑,为了获得更高的灵活性和更强的扩展性,GOST本身并没有提供用户级别的限流功能,而是通过插件的方式开放给使用者来实现自己的逻辑。

<!-- more -->

对于支持认证的处理器(HTTP,HTTP2,SOCKS4,SOCKS5,Relay),观测器和流量限制器可以用在这些处理器上,再结合[认证器](https://gost.run/concepts/auth/)组件,就可以实现比较灵活的用户级别动态限流功能。

对于处理器上的观测器,会根据认证器返回的用户标识对流量进行分组统计并上报,通过观测器插件就可以得到用户级别的流量信息。对于处理器上的流量限制器,也会根据认证器返回的用户标识向插件请求用户级别的限流配置。流量限制器插件也可以选择结合观测器接收到的用户流量统计信息来动态调整单个用户的限速配置。


![Limiter](../../images/limiter.png)

```yaml
services:
- name: service-0
addr: :8080
handler:
type: http
auther: auther-0
observer: observer-0
limiter: limiter-0
listener:
type: tcp
authers:
- name: auther-0
plugin:
type: http
addr: http://localhost:8000/auther
observers:
- name: observer-0
plugin:
type: http
addr: http://localhost:8001/observer
limiters:
- name: limiter-0
plugin:
type: http
addr: http://localhost:8002/limiter
```
2 changes: 1 addition & 1 deletion docs/concepts/limiter.md
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ curl -XPOST http://127.0.0.1:8000/limiter \

## 处理器(Handler)上的限制器

对于支持认证的处理器(HTTP,HTTP2,SOCKS4,SOCKS5,Relay, Tunnel),流量速率限制器也可以用在这些类型的处理器上。
对于支持认证的处理器(HTTP,HTTP2,SOCKS4,SOCKS5,RelayTunnel),流量速率限制器也可以用在这些类型的处理器上。

```yaml hl_lines="6"
services:
Expand Down
Binary file added docs/images/limiter.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 5 additions & 1 deletion docs/tutorials/redirect.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ comments: true

通过`sniffing`选项开启流量嗅探,默认不开启。通过`sniffing.timeout`选项设置嗅探超时时长。

对于HTTPS流量如果未嗅探到SNI信息,可以开启`sniffing.fallback`选项,再次使用目标地址尝试连接。

## REDIRECT

采用REDIRECT方式的透明代理可以选择给数据包打标记(Mark)。使用Mark需要管理员权限运行。
Expand All @@ -38,6 +40,7 @@ comments: true
metadata:
sniffing: true
sniffing.timeout: 5s
sniffing.fallback: true
listener:
type: red
chains:
Expand Down Expand Up @@ -163,9 +166,10 @@ comments: true
handler:
type: red
metadata:
tproxy: true
sniffing: true
sniffing.timeout: 5s
tproxy: true
sniffing.fallback: true
listener:
type: red
metadata:
Expand Down
8 changes: 7 additions & 1 deletion en/docs/tutorials/redirect.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Transparent proxy supports two modes: REDIRECT and TPROXY. The REDIRECT mode onl

Traffic sniffing is enabled through the `sniffing` option, which is not enabled by default.

If the SNI information is not sniffed for HTTPS traffic, you can enable the `sniffing.fallback` option and try to connect again using the original target address.

## REDIRECT

Transparent proxy using REDIRECT can choose to mark packets. Using Mark requires administrator privileges to run.
Expand All @@ -37,6 +39,8 @@ Transparent proxy using REDIRECT can choose to mark packets. Using Mark requires
chain: chain-0
metadata:
sniffing: true
sniffing.timeout: 5s
sniffing.fallback: true
listener:
type: red
chains:
Expand Down Expand Up @@ -162,8 +166,10 @@ Set the mark value via the `so_mark` (command line) or `sockopts` (config file)
handler:
type: red
metadata:
sniffing: true
tproxy: true
sniffing: true
sniffing.timeout: 5s
sniffing.fallback: true
listener:
type: red
metadata:
Expand Down

0 comments on commit d8aafac

Please sign in to comment.