添加四层转发规则¶
1. 接口描述¶
接口请求路径:POST /prod-api/cdn/layer4/rules/insertRule
insertRule 用于在指定 TCP/UDP(四层)套餐下批量添加转发规则。
Token 获取方式见 密钥鉴权。
2. 输入参数¶
以下请求参数列表仅列出了接口请求参数和部分公共参数,完整公共参数列表见 公共请求参数。
| 参数名称 | 必选 | 类型 | 描述 |
|---|---|---|---|
| oid | 是 | String | 套餐号 示例值: 202304241245319372 |
| rules | 是 | [Layer4RuleItem] | 转发规则列表,至少包含一条 |
3. 示例¶
示例1 添加转发规则¶
输入示例¶
{
"oid": "202304241245319372",
"rules": [
{
"originAddress": "1.1.1.1",
"protocolType": "tcp",
"types": "off",
"limitConn": 20,
"hyPort": "6665",
"listenPort": "6666",
"remark": "",
"proxyProtocol": "off",
"node": "line_1"
}
]
}
代码调用¶
请将 {登录域名}、token 替换为实际值。
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class InsertRuleTest {
public static void main(String[] args) throws Exception {
String url = "https://{登录域名}/prod-api/cdn/layer4/rules/insertRule";
String token = "eyJhbGciOiJIUzUxMiJ9.xxxxxx";
String requestBody = "{"
+ "\"oid\":\"202304241245319372\","
+ "\"rules\":[{"
+ "\"originAddress\":\"1.1.1.1\","
+ "\"protocolType\":\"tcp\","
+ "\"types\":\"off\","
+ "\"limitConn\":20,"
+ "\"hyPort\":\"6665\","
+ "\"listenPort\":\"6666\","
+ "\"remark\":\"\","
+ "\"proxyProtocol\":\"off\","
+ "\"node\":\"line_1\""
+ "}]"
+ "}";
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Authorization", "Bearer " + token);
httpPost.setEntity(new StringEntity(requestBody, ContentType.APPLICATION_JSON));
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
int statusCode = response.getStatusLine().getStatusCode();
String body = EntityUtils.toString(response.getEntity(), "UTF-8");
System.out.println("Status Code: " + statusCode);
System.out.println("Body: " + body);
}
}
}
}
package main
import (
"bytes"
"fmt"
"io"
"net/http"
)
func main() {
url := "https://{登录域名}/prod-api/cdn/layer4/rules/insertRule"
token := "eyJhbGciOiJIUzUxMiJ9.xxxxxx"
requestBody := []byte(`{
"oid": "202304241245319372",
"rules": [
{
"originAddress": "1.1.1.1",
"protocolType": "tcp",
"types": "off",
"limitConn": 20,
"hyPort": "6665",
"listenPort": "6666",
"remark": "",
"proxyProtocol": "off",
"node": "line_1"
}
]
}`)
req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(requestBody))
if err != nil {
panic(err)
}
req.Header.Set("Authorization", "Bearer "+token)
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
fmt.Println("Status Code:", resp.StatusCode)
fmt.Println("Body:", string(body))
}
输出示例¶
4. 输出参数¶
| 参数名称 | 类型 | 描述 |
|---|---|---|
| code | Integer | 状态码。示例值:200 |
| msg | String | 提示信息。示例值:保存成功 |
5. 错误码¶
| HTTP 状态码 / 业务提示 | 描述 |
|---|---|
| 200 | 成功 |
| 401 | 未鉴权或 Token 无效 |
| 403 | 无权限 |
| 请传入转发规则参数 | 请求体为空 |
| 请至少传入一条转发规则 | rules 为空 |
| 套餐信息不存,请刷新后重试 | 套餐不存在或不属于四层套餐 |
| 套餐已关闭,无法继续添加转发规则 | 套餐状态为关闭 |
| 套餐已过期,无法继续添加转发规则 | 套餐已过期 |
| 请勿添加重复的转发端口 | 同协议、同实例下存在重复转发端口 |
| 保存失败 | 保存未成功 |
6. 数据模型¶
Layer4RuleItem¶
转发规则(请求)。新增时无需传入 ruleId。
| 参数名称 | 必选 | 类型 | 描述 |
|---|---|---|---|
| originAddress | 是 | String | 源站地址,多个地址用英文分号 ; 分隔示例值: 1.1.1.1 |
| protocolType | 是 | String | 转发协议,支持以下值:tcp:TCPudp:UDP示例值: tcp |
| types | 是 | String | 会话保持,支持以下值:off:否on:是示例值: off |
| limitConn | 否 | Integer | 频率限速(连接数限制) 示例值: 20 |
| listenPort | 是 | String | 转发端口(边缘监听端口) 示例值: 6666 |
| hyPort | 是 | String | 源站端口 示例值: 6665 |
| remark | 否 | String | 备注 |
| proxyProtocol | 是 | String | Proxy Protocol,支持以下值:off:禁用on:启用示例值: off |
| node | 是 | String | 实例标识,如 line_1示例值: line_1 |
| ruleId | 否 | Long | 规则 ID。新增时不传 |