mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-05-02 18:32:28 +08:00
feat(api): improve /api/network/forwardProxy
(#9110)
This commit is contained in:
parent
49c648f9be
commit
d99ac84092
30
API.md
30
API.md
@ -1288,7 +1288,9 @@ View API token in <kbd>Settings - About</kbd>, request header: `Authorization: T
|
|||||||
"Cookie": ""
|
"Cookie": ""
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"payload": {}
|
"payload": {},
|
||||||
|
"payloadEncoding": "text",
|
||||||
|
"responseEncoding": "text"
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -1298,6 +1300,22 @@ View API token in <kbd>Settings - About</kbd>, request header: `Authorization: T
|
|||||||
* `contentType`: Content-Type, default is `application/json`
|
* `contentType`: Content-Type, default is `application/json`
|
||||||
* `headers`: HTTP headers
|
* `headers`: HTTP headers
|
||||||
* `payload`: HTTP payload, object or string
|
* `payload`: HTTP payload, object or string
|
||||||
|
* `payloadEncoding`: The encoding scheme used by `pyaload`, default is `text`, optional values are as follows
|
||||||
|
|
||||||
|
* `text`
|
||||||
|
* `base64` | `base64-std`
|
||||||
|
* `base64-url`
|
||||||
|
* `base32` | `base32-std`
|
||||||
|
* `base32-hex`
|
||||||
|
* `hex`
|
||||||
|
* `responseEncoding`: The encoding scheme used by `body` in response data, default is `text`, optional values are as follows
|
||||||
|
|
||||||
|
* `text`
|
||||||
|
* `base64` | `base64-std`
|
||||||
|
* `base64-url`
|
||||||
|
* `base32` | `base32-std`
|
||||||
|
* `base32-hex`
|
||||||
|
* `hex`
|
||||||
* Return value
|
* Return value
|
||||||
|
|
||||||
```json
|
```json
|
||||||
@ -1306,6 +1324,7 @@ View API token in <kbd>Settings - About</kbd>, request header: `Authorization: T
|
|||||||
"msg": "",
|
"msg": "",
|
||||||
"data": {
|
"data": {
|
||||||
"body": "",
|
"body": "",
|
||||||
|
"bodyEncoding": "text",
|
||||||
"contentType": "text/html",
|
"contentType": "text/html",
|
||||||
"elapsed": 1976,
|
"elapsed": 1976,
|
||||||
"headers": {
|
"headers": {
|
||||||
@ -1316,6 +1335,15 @@ View API token in <kbd>Settings - About</kbd>, request header: `Authorization: T
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
* `bodyEncoding`:The encoding scheme used by `body`, is consistent with field `responseEncoding` in request, default is `text`, optional values are as follows
|
||||||
|
|
||||||
|
* `text`
|
||||||
|
* `base64` | `base64-std`
|
||||||
|
* `base64-url`
|
||||||
|
* `base32` | `base32-std`
|
||||||
|
* `base32-hex`
|
||||||
|
* `hex`
|
||||||
|
|
||||||
## System
|
## System
|
||||||
|
|
||||||
### Get boot progress
|
### Get boot progress
|
||||||
|
30
API_zh_CN.md
30
API_zh_CN.md
@ -1278,7 +1278,9 @@
|
|||||||
"Cookie": ""
|
"Cookie": ""
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"payload": {}
|
"payload": {},
|
||||||
|
"payloadEncoding": "text",
|
||||||
|
"responseEncoding": "text"
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -1288,6 +1290,22 @@
|
|||||||
* `contentType`:HTTP Content-Type,默认为 `application/json`
|
* `contentType`:HTTP Content-Type,默认为 `application/json`
|
||||||
* `headers`:HTTP 请求标头
|
* `headers`:HTTP 请求标头
|
||||||
* `payload`:HTTP 请求体,对象或者是字符串
|
* `payload`:HTTP 请求体,对象或者是字符串
|
||||||
|
* `payloadEncoding`:`pyaload` 所使用的编码方案,默认为 `text`,可选值如下所示
|
||||||
|
|
||||||
|
* `text`
|
||||||
|
* `base64` | `base64-std`
|
||||||
|
* `base64-url`
|
||||||
|
* `base32` | `base32-std`
|
||||||
|
* `base32-hex`
|
||||||
|
* `hex`
|
||||||
|
* `responseEncoding`:响应数据中 `body` 字段所使用的编码方案,默认为 `text`,可选值如下所示
|
||||||
|
|
||||||
|
* `text`
|
||||||
|
* `base64` | `base64-std`
|
||||||
|
* `base64-url`
|
||||||
|
* `base32` | `base32-std`
|
||||||
|
* `base32-hex`
|
||||||
|
* `hex`
|
||||||
* 返回值
|
* 返回值
|
||||||
|
|
||||||
```json
|
```json
|
||||||
@ -1296,6 +1314,7 @@
|
|||||||
"msg": "",
|
"msg": "",
|
||||||
"data": {
|
"data": {
|
||||||
"body": "",
|
"body": "",
|
||||||
|
"bodyEncoding": "text",
|
||||||
"contentType": "text/html",
|
"contentType": "text/html",
|
||||||
"elapsed": 1976,
|
"elapsed": 1976,
|
||||||
"headers": {
|
"headers": {
|
||||||
@ -1306,6 +1325,15 @@
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
* `bodyEncoding`:`body` 所使用的编码方案,与请求中 `responseEncoding` 字段一致,默认为 `text`,可能的值如下所示
|
||||||
|
|
||||||
|
* `text`
|
||||||
|
* `base64` | `base64-std`
|
||||||
|
* `base64-url`
|
||||||
|
* `base32` | `base32-std`
|
||||||
|
* `base32-hex`
|
||||||
|
* `hex`
|
||||||
|
|
||||||
## 系统
|
## 系统
|
||||||
|
|
||||||
### 获取启动进度
|
### 获取启动进度
|
||||||
|
@ -17,6 +17,9 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/base32"
|
||||||
|
"encoding/base64"
|
||||||
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -74,7 +77,60 @@ func forwardProxy(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
request.SetHeader("Content-Type", contentType)
|
request.SetHeader("Content-Type", contentType)
|
||||||
|
|
||||||
|
payloadEncoding := "json"
|
||||||
|
if payloadEncodingArg := arg["payloadEncoding"]; nil != payloadEncodingArg {
|
||||||
|
payloadEncoding = payloadEncodingArg.(string)
|
||||||
|
}
|
||||||
|
|
||||||
|
switch payloadEncoding {
|
||||||
|
case "base64":
|
||||||
|
fallthrough
|
||||||
|
case "base64-std":
|
||||||
|
if payload, err := base64.StdEncoding.DecodeString(arg["payload"].(string)); nil != err {
|
||||||
|
ret.Code = -2
|
||||||
|
ret.Msg = "decode base64-std payload failed: " + err.Error()
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
request.SetBody(payload)
|
||||||
|
}
|
||||||
|
case "base64-url":
|
||||||
|
if payload, err := base64.URLEncoding.DecodeString(arg["payload"].(string)); nil != err {
|
||||||
|
ret.Code = -2
|
||||||
|
ret.Msg = "decode base64-url payload failed: " + err.Error()
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
request.SetBody(payload)
|
||||||
|
}
|
||||||
|
case "base32":
|
||||||
|
fallthrough
|
||||||
|
case "base32-std":
|
||||||
|
if payload, err := base32.StdEncoding.DecodeString(arg["payload"].(string)); nil != err {
|
||||||
|
ret.Code = -2
|
||||||
|
ret.Msg = "decode base32-std payload failed: " + err.Error()
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
request.SetBody(payload)
|
||||||
|
}
|
||||||
|
case "base32-hex":
|
||||||
|
if payload, err := base32.HexEncoding.DecodeString(arg["payload"].(string)); nil != err {
|
||||||
|
ret.Code = -2
|
||||||
|
ret.Msg = "decode base32-hex payload failed: " + err.Error()
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
request.SetBody(payload)
|
||||||
|
}
|
||||||
|
case "hex":
|
||||||
|
if payload, err := hex.DecodeString(arg["payload"].(string)); nil != err {
|
||||||
|
ret.Code = -2
|
||||||
|
ret.Msg = "decode hex payload failed: " + err.Error()
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
request.SetBody(payload)
|
||||||
|
}
|
||||||
|
case "text":
|
||||||
|
default:
|
||||||
request.SetBody(arg["payload"])
|
request.SetBody(arg["payload"])
|
||||||
|
}
|
||||||
|
|
||||||
started := time.Now()
|
started := time.Now()
|
||||||
resp, err := request.Send(method, destURL)
|
resp, err := request.Send(method, destURL)
|
||||||
@ -90,14 +146,43 @@ func forwardProxy(c *gin.Context) {
|
|||||||
ret.Msg = "read response body failed: " + err.Error()
|
ret.Msg = "read response body failed: " + err.Error()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
body := string(bodyData)
|
|
||||||
elapsed := time.Now().Sub(started)
|
elapsed := time.Now().Sub(started)
|
||||||
|
|
||||||
|
responseEncoding := "text"
|
||||||
|
if responseEncodingArg := arg["responseEncoding"]; nil != responseEncodingArg {
|
||||||
|
responseEncoding = responseEncodingArg.(string)
|
||||||
|
}
|
||||||
|
|
||||||
|
body := ""
|
||||||
|
switch responseEncoding {
|
||||||
|
case "base64":
|
||||||
|
fallthrough
|
||||||
|
case "base64-std":
|
||||||
|
body = base64.StdEncoding.EncodeToString(bodyData)
|
||||||
|
case "base64-url":
|
||||||
|
body = base64.URLEncoding.EncodeToString(bodyData)
|
||||||
|
case "base32":
|
||||||
|
fallthrough
|
||||||
|
case "base32-std":
|
||||||
|
body = base32.StdEncoding.EncodeToString(bodyData)
|
||||||
|
case "base32-hex":
|
||||||
|
body = base32.HexEncoding.EncodeToString(bodyData)
|
||||||
|
case "hex":
|
||||||
|
body = hex.EncodeToString(bodyData)
|
||||||
|
case "text":
|
||||||
|
fallthrough
|
||||||
|
default:
|
||||||
|
responseEncoding = "text"
|
||||||
|
body = string(bodyData)
|
||||||
|
}
|
||||||
|
|
||||||
data := map[string]interface{}{
|
data := map[string]interface{}{
|
||||||
"url": destURL,
|
"url": destURL,
|
||||||
"status": resp.StatusCode,
|
"status": resp.StatusCode,
|
||||||
"contentType": resp.GetHeader("content-type"),
|
"contentType": resp.GetHeader("content-type"),
|
||||||
"body": body,
|
"body": body,
|
||||||
|
"bodyEncoding": responseEncoding,
|
||||||
"headers": resp.Header,
|
"headers": resp.Header,
|
||||||
"elapsed": elapsed.Milliseconds(),
|
"elapsed": elapsed.Milliseconds(),
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user