Skip to content

Commit

Permalink
update examples and valid value of message attribute (#10984)
Browse files Browse the repository at this point in the history
  • Loading branch information
kayx23 authored Mar 4, 2024
1 parent 2caa9d8 commit 2e7c13c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 61 deletions.
40 changes: 10 additions & 30 deletions docs/en/latest/plugins/ua-restriction.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ A common scenario is to set crawler rules. `User-Agent` is the identity of the c
| bypass_missing | boolean | False | false | | When set to `true`, bypasses the check when the `User-Agent` header is missing. |
| allowlist | array[string] | False | | | List of allowed `User-Agent` headers. |
| denylist | array[string] | False | | | List of denied `User-Agent` headers. |
| message | string | False | "Not allowed" | [1, 1024] | Message with the reason for denial to be added to the response. |
| message | string | False | "Not allowed" | | Message with the reason for denial to be added to the response. |

:::note

Both `allowlist` and `denylist` can't be used at the same time.
`allowlist` and `denylist` can't be configured at the same time.

:::

Expand All @@ -64,56 +64,36 @@ curl http://127.0.0.1:9180/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f13
"plugins": {
"ua-restriction": {
"bypass_missing": true,
"allowlist": [
"my-bot1",
"(Baiduspider)/(\\d+)\\.(\\d+)"
],
"denylist": [
"my-bot2",
"(Twitterspider)/(\\d+)\\.(\\d+)"
]
],
"message": "Do you want to do something bad?"
}
}
}'
```

You can also configure the Plugin to respond with a custom rejection message:

```json
"plugins": {
"ua-restriction": {
"denylist": [
"my-bot2",
"(Twitterspider)/(\\d+)\\.(\\d+)"
],
"message": "Do you want to do something bad?"
}
}
```

## Example usage

After you have configured the Plugin as shown above, you can make a normal request which will get accepted:
Send a request to the route:

```shell
curl http://127.0.0.1:9080/index.html -i
```

```shell
HTTP/1.1 200 OK
...
```
You should receive an `HTTP/1.1 200 OK` response.

Now if the `User-Agent` header is in the `denylist` i.e the bot User-Agent:

```shell
curl http://127.0.0.1:9080/index.html --header 'User-Agent: Twitterspider/2.0'
```

```shell
HTTP/1.1 403 Forbidden
...
{"message":"Not allowed"}
You should receive an `HTTP/1.1 403 Forbidden` response with the following message:

```text
{"message":"Do you want to do something bad?"}
```

## Delete Plugin
Expand Down
38 changes: 7 additions & 31 deletions docs/zh/latest/plugins/ua-restriction.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ description: 本文介绍了 Apache APISIX ua-restriction 插件的使用方法
| --------- | ------------- | ------ | ------ | ------ | -------------------------------- |
| allowlist | array[string] || | | 加入白名单的 `User-Agent`|
| denylist | array[string] || | | 加入黑名单的 `User-Agent`|
| message | string || "Not allowed" | [1, 1024] | 当未允许的 `User-Agent` 访问时返回的信息。 |
| message | string || "Not allowed" | | 当未允许的 `User-Agent` 访问时返回的信息。 |
| bypass_missing | boolean || false | | 当设置为 `true` 时,如果 `User-Agent` 请求头不存在或格式有误时,将绕过检查。 |

:::note
Expand All @@ -64,33 +64,16 @@ curl http://127.0.0.1:9180/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f13
"plugins": {
"ua-restriction": {
"bypass_missing": true,
"allowlist": [
"my-bot1",
"(Baiduspider)/(\\d+)\\.(\\d+)"
],
"denylist": [
"my-bot2",
"(Twitterspider)/(\\d+)\\.(\\d+)"
]
],
"message": "Do you want to do something bad?"
}
}
}'
```

当未允许的 `User-Agent` 访问时,默认返回 `{"message":"Not allowed"}`。如果你想使用自定义的 `message`,可以在 `plugins` 部分进行配置:

```json
"plugins": {
"ua-restriction": {
"denylist": [
"my-bot2",
"(Twitterspider)/(\\d+)\\.(\\d+)"
],
"message": "Do you want to do something bad?"
}
}
```

## 测试插件

通过上述命令启用插件后,你可以先发起一个简单的请求测试:
Expand All @@ -99,25 +82,18 @@ curl http://127.0.0.1:9180/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f13
curl http://127.0.0.1:9080/index.html -i
```

返回的 HTTP 响应头中带有 `200` 状态码,代表请求成功:

```shell
HTTP/1.1 200 OK
...
```
你应当收到 `HTTP/1.1 200 OK` 的响应,表示请求成功。

接下来,请求的同时指定处于 `denylist` 中的 `User-Agent`,如 `Twitterspider/2.0`

```shell
curl http://127.0.0.1:9080/index.html --header 'User-Agent: Twitterspider/2.0'
```

返回的 HTTP 响应头中带有 `403` 状态码,请求失败,代表插件生效:
你应当收到 `HTTP/1.1 403 Forbidden` 的响应和以下报错,表示请求失败,代表插件生效:

```shell
HTTP/1.1 403 Forbidden
...
{"message":"Not allowed"}
```text
{"message":"Do you want to do something bad?"}
```

## 删除插件
Expand Down

0 comments on commit 2e7c13c

Please sign in to comment.