Sfoglia il codice sorgente

:art: Add kernel API `/api/network/forwardProxy` https://github.com/siyuan-note/siyuan/issues/8724

Daniel 2 anni fa
parent
commit
5d7c6d47bf
3 ha cambiato i file con 106 aggiunte e 9 eliminazioni
  1. 48 0
      API.md
  2. 48 0
      API_zh_CN.md
  3. 10 9
      kernel/api/network.go

+ 48 - 0
API.md

@@ -52,6 +52,8 @@
 * [Notification](#Notification)
     * [Push message](#Push-message)
     * [Push error message](#Push-error-message)
+* [Network](#Network)
+    * [Forward proxy](#Forward-proxy)
 * [System](#System)
     * [Get boot progress](#Get-boot-progress)
     * [Get system version](#Get-system-version)
@@ -1207,6 +1209,52 @@ View API token in <kbd>Settings - About</kbd>, request header: `Authorization: T
   ```
     * `id`: Message ID
 
+## Network
+
+### Forward proxy
+
+* `/api/network/forkProxy`
+* Parameters
+
+  ```json
+  {
+    "url": "https://b3log.org/siyuan/",
+    "method": "GET",
+    "timeout": 7000,
+    "contentType": "text/html",
+    "headers": [
+        {
+            "Cookie": ""
+        }
+    ],
+    "payload": {}
+  }
+  ```
+
+    * `url`: URL to forward
+    * `method`: HTTP method, default is `POST`
+    * `timeout`: timeout in milliseconds, default is `7000`
+    * `contentType`: Content-Type, default is `application/json`
+    * `headers`: HTTP headers
+    * `payload`: HTTP payload, object or string
+* Return value
+
+  ```json
+  {
+    "code": 0,
+    "msg": "",
+    "data": {
+      "body": "",
+      "contentType": "text/html",
+      "elapsed": 1976,
+      "headers": {
+      },
+      "status": 200,
+      "url": "https://b3log.org/siyuan"
+    }
+  }
+  ```
+
 ## System
 
 ### Get boot progress

+ 48 - 0
API_zh_CN.md

@@ -52,6 +52,8 @@
 * [通知](#通知)
     * [推送消息](#推送消息)
     * [推送报错消息](#推送报错消息)
+* [网络](#网络)
+    * [正向代理](#正向代理)
 * [系统](#系统)
     * [获取启动进度](#获取启动进度)
     * [获取系统版本](#获取系统版本)
@@ -1197,6 +1199,52 @@
   ```
     * `id`:消息 ID
 
+## 网络
+
+### 正向代理
+
+* `/api/network/forkProxy`
+* 参数
+
+  ```json
+  {
+    "url": "https://b3log.org/siyuan/",
+    "method": "GET",
+    "timeout": 7000,
+    "contentType": "text/html",
+    "headers": [
+        {
+            "Cookie": ""
+        }
+    ],
+    "payload": {}
+  }
+  ```
+
+    * `url`:转发的 URL
+    * `method`:HTTP 方法,默认为 `GET`
+    * `timeout`:超时时间,单位为毫秒,默认为 `7000` 毫秒
+    * `contentType`:HTTP Content-Type,默认为 `application/json`
+    * `headers`:HTTP 请求标头
+    * `payload`:HTTP 请求体,对象或者是字符串
+* 返回值
+
+  ```json
+  {
+    "code": 0,
+    "msg": "",
+    "data": {
+      "body": "",
+      "contentType": "text/html",
+      "elapsed": 1976,
+      "headers": {
+      },
+      "status": 200,
+      "url": "https://b3log.org/siyuan"
+    }
+  }
+  ```
+
 ## 系统
 
 ### 获取启动进度

+ 10 - 9
kernel/api/network.go

@@ -46,10 +46,12 @@ func forwardProxy(c *gin.Context) {
 		return
 	}
 
-	method := strings.ToUpper(arg["method"].(string))
-	timeoutArg := arg["timeout"]
+	method := "POST"
+	if methodArg := arg["method"]; nil != methodArg {
+		method = strings.ToUpper(methodArg.(string))
+	}
 	timeout := 7 * 1000
-	if nil != timeoutArg {
+	if timeoutArg := arg["timeout"]; nil != timeoutArg {
 		timeout = int(timeoutArg.(float64))
 		if 1 > timeout {
 			timeout = 7 * 1000
@@ -66,14 +68,13 @@ func forwardProxy(c *gin.Context) {
 		}
 	}
 
-	contentType := arg["contentType"]
-	if nil != contentType && "" != contentType {
-		request.SetHeader("Content-Type", contentType.(string))
+	contentType := "application/json"
+	if contentTypeArg := arg["contentType"]; nil != contentTypeArg {
+		contentType = contentTypeArg.(string)
 	}
+	request.SetHeader("Content-Type", contentType)
 
-	if "POST" == method {
-		request.SetBody(arg["payload"])
-	}
+	request.SetBody(arg["payload"])
 
 	started := time.Now()
 	resp, err := request.Send(method, destURL)