本地聊天模块
UDP 发送消息
接口方法: HandleMessage
注意:指定消息发送的目标 IP 地址。应确保是有效的 IPv4 地址。
描述: 该接口通过 HandleMessage。接收 HTTP 请求并通过 UDP 协议将消息发送到指定的 IP 地址。
URL:
/localchat/message方法:
POST是否需要登录: false
请求参数:
| 参数名 | 类型 | 描述 | 是否必填 | 示例值 |
|---|---|---|---|---|
| hostname | string | 发送消息的主机名 | 否 | "my-host" |
| type | string | 消息的类型(文本、文件、图片等) | 是 | "notification" |
| time | time.Time | 消息发送的时间 | 否 | "2023-10-10T10:00:00Z" |
| ip | string | 目标接收方的 IP 地址 | 是 | "192.168.1.100" |
| message | any | 要发送的消息内容 | 是 | "Hello, World!" |
返回结果:
| 描述 | 返回示例 |
|---|---|
| 请求体无效(JSON解析失败) | "Invalid request body" |
| 获取主机名失败或发送消息失败 | "HandleMessage error" 或 "Failed to send message" |
示例:
curl -X POST http://api.example.com/localchat/message -H "Content-Type: application/json" -d '{"hostname": "my-host", "type": "notification", "time": "2023-10-10T10:00:00Z", "ip": "192.168.1.100", "message": "Hello, World!"}'成功响应示例
- HTTP 状态码: 200 OK
Text message sent successfully失败响应示例
- 请求体解析失败
- HTTP 状态码: 400 Bad Request
Invalid request body- 获取主机名失败或发送消息失败
- HTTP 状态码: 500 Internal Server Error
HandleMessage error发送文件
接口方法: HandlerApplySendFile
注意:确保请求体包含有效的 JSON 数据。以及 IP 地址是有效的 IPv4 地址。
描述: 该接口通过 HandlerApplySendFile 方法解析请求体中的 JSON 数据,获取消息内容,并将其发送到指定的 IP 地址。
URL:
/localchat/applyfile方法:
POST是否需要登录: false
请求参数:
| 参数名 | 类型 | 描述 | 是否必填 | 示例值 |
|---|---|---|---|---|
| hostname | string | 发送消息的主机名 | 否 | "my-host" |
| type | string | 消息的类型(文本、文件、图片等) | 是 | "notification" |
| time | time.Time | 消息发送的时间 | 否 | "2023-10-10T10:00:00Z" |
| ip | string | 目标接收方的 IP 地址 | 是 | "192.168.1.100" |
| message | any | 要发送的消息内容 | 是 | "Hello, World!" |
返回结果:
| 描述 | 返回示例 |
|---|---|
| 请求体无效(JSON解析失败) | "Invalid request body" |
| 获取主机名失败或发送消息失败 | "HandleMessage error" 或 "Failed to send message" |
示例:
curl -X POST http://api.example.com/localchat/applyfile -H "Content-Type: application/json" -d '{"hostname": "my-host", "type": "notification", "time": "2023-10-10T10:00:00Z", "ip": "192.168.1.100", "message": "Hello, World!"}'成功响应示例
- HTTP 状态码: 200 OK
File sent successfully失败响应示例
- 请求体解析失败
- HTTP 状态码: 400 Bad Request
Invalid request body- 获取主机名失败或发送消息失败
- HTTP 状态码: 500 Internal Server Error
HandleMessage error取消文件发送
接口方法: HandlerCannelFile
注意:确保请求体包含有效的 JSON 数据。以及 IP 地址是有效的 IPv4 地址。
描述: 该接口通过 HandlerCannelFile 方法处理取消文件发送的请求。通过解析请求体中的 JSON 数据,获取消息内容,并将其发送到指定的 IP 地址。
URL:
/localchat/cancelfile方法:
POST是否需要登录: false
请求参数:
| 参数名 | 类型 | 描述 | 是否必填 | 示例值 |
|---|---|---|---|---|
| hostname | string | 发送消息的主机名 | 否 | "my-host" |
| type | string | 消息的类型(文本、文件、图片等) | 是 | "notification" |
| time | time.Time | 消息发送的时间 | 否 | "2023-10-10T10:00:00Z" |
| ip | string | 目标接收方的 IP 地址 | 是 | "192.168.1.100" |
| message | any | 要发送的消息内容 | 是 | "Hello, World!" |
返回结果:
| 描述 | 返回示例 |
|---|---|
| 请求体无效(JSON解析失败) | "Invalid request body" |
| 获取主机名失败或发送消息失败 | "HandleMessage error" 或 "Failed to send message" |
示例:
curl -X POST http://api.example.com/localchat/cancelfile -H "Content-Type: application/json" -d '{"hostname": "my-host", "type": "notification", "time": "2023-10-10T10:00:00Z", "ip": "192.168.1.100", "message": "Hello, World!"}'成功响应示例
- HTTP 状态码: 200 OK
{
"message": "请求文件发送成功",
"code": 0
}失败响应示例
- 请求体解析失败
- HTTP 状态码: 400 Bad Request
{
"message": "Invalid request body",
"code": -1
}- 获取主机名失败或发送消息失败
- HTTP 状态码: 500 Internal Server Error
{
"message": "HandleMessage error",
"code": -1
}访问文件
接口方法: HandlerAccessFile
注意:确保请求体包含有效的 JSON 数据。以及 IP 地址是有效的 IPv4 地址。
描述: 该接口通过 HandlerAccessFile 方法处理访问文件的请求。通过解析请求体中的 JSON 数据,获取消息内容,并将其发送到指定的 IP 地址。
URL:
/localchat/accessfile方法:
POST是否需要登录: false
请求参数:
| 参数名 | 类型 | 描述 | 是否必填 | 示例值 |
|---|---|---|---|---|
| hostname | string | 发送消息的主机名 | 否 | "my-host" |
| type | string | 消息的类型(文本、文件、图片等) | 是 | "notification" |
| time | time.Time | 消息发送的时间 | 否 | "2023-10-10T10:00:00Z" |
| ip | string | 目标接收方的 IP 地址 | 是 | "192.168.1.100" |
| message | any | 要发送的消息内容 | 是 | "Hello, World!" |
返回结果:
| 描述 | 返回示例 |
|---|---|
| 请求体无效(JSON解析失败) | "Invalid request body" |
| 获取主机名失败或发送消息失败 | "HandleMessage error" 或 "Failed to send message" |
示例:
curl -X POST http://api.example.com/localchat/accessfile -H "Content-Type: application/json" -d '{"hostname": "my-host", "type": "notification", "time": "2023-10-10T10:00:00Z", "ip": "192.168.1.100", "message": "Hello, World!"}'成功响应示例
- HTTP 状态码: 200 OK
{
"message": "请求文件访问成功",
"code": 0
}失败响应示例
- 请求体解析失败
- HTTP 状态码: 400 Bad Request
{
"message": "Invalid request body",
"code": -1
}- 获取主机名失败或发送消息失败
- HTTP 状态码: 500 Internal Server Error
{
"message": "HandleMessage error",
"code": -1
}获取文件列表信息
接口方法: HandleGetFiles
注意:文件路径应是服务器上可访问的路径
描述: 该接口通过 HandleGetFiles 该接口用于接收一个包含文件路径的列表,并返回这些文件的详细信息。
URL:
/localchat/getfiles方法:
POST是否需要登录: false
请求参数:
| 参数名 | 类型 | 必填 | 描述 | 示例值 |
|---|---|---|---|---|
| Files | array | 是 | 文件路径的数组 | ["/path/to/file1", "/path/to/file2"] |
返回结果:
| 描述 | 返回示例 |
|---|---|
| 请求体无效(JSON解析失败) | "Invalid request body" |
| 获取主机名失败或发送消息失败 | "HandleMessage error" 或 "Failed to send message" |
示例:
curl -X POST http://api.example.com/localchat/getfiles -H "Content-Type: application/json" -d '{"Files": ["/path/to/file1", "/path/to/file2"]}'成功响应示例
- HTTP 状态码: 200 OK
{
"message": "请求文件列表成功",
"code": 0,
"data": [
{
"Path": "/path/to/file1",
"IsDir": false,
"Filename": "file1",
"WritePath": ""
},
{
"Path": "/path/to/file2",
"IsDir": true,
"Filename": "file2",
"WritePath": ""
}
]
}失败响应示例
- 请求方法不正确
- HTTP 状态码: 405 Method Not Allowed
{
"message": "Only POST method is allowed",
"code": -1
}- 请求体无效
- HTTP 状态码: 400 Bad Request
{
"message": "Invalid request body",
"code": -1
}- 获取操作系统目录失败
- HTTP 状态码: 500 Internal Server Error
{
"message": "Failed to get OS directory",
"code": -1
}- 文件状态获取失败
- HTTP 状态码: 500 Internal Server Error
{
"message": "Failed to stat file: [error details]",
"code": -1
}- 目录遍历失败
- HTTP 状态码: 500 Internal Server Error
{
"message": "Failed to traverse directory: [error details]",
"code": -1
}- 文件读取失败
- HTTP 状态码: 500 Internal Server Error
{
"message": "Failed to marshal file list",
"code": -1
}文件下载
接口方法: HandleServeFile
注意:确保提供的文件路径是有效的,并且不包含恶意路径输入(如目录遍历攻击 ../../)。
描述: 该接口通过 HandleServeFile 方法处理文件下载请求。通过从请求的 URL 中获取文件路径参数,验证文件的存在性和类型,然后将文件内容作为附件下载到客户端。
URL:
/localchat/servefile方法:
GET是否需要登录: false
请求参数:
| 参数名 | 类型 | 必填 | 描述 | 示例值 |
|---|---|---|---|---|
| path | string | 是 | 要下载的文件的路径 | "/path/to/file" |
返回结果:
| 类型 | 描述 | 返回示例 |
|---|---|---|
| application/octet-stream | 文件作为附件下载 | 文件内容的二进制流 |
示例:
curl -X GET http://api.example.com/localchat/servefile?path=/path/to/file成功响应示例
- HTTP 状态码: 200 OK
- 响应头:
Content-Disposition:attachment; filename="filename.ext"Content-Type:application/octet-streamContent-Length: 文件的字节大小
- 响应体: 文件的二进制内容
失败响应示例
- 缺少文件路径参数
- HTTP 状态码: 400 Bad Request
Missing filePath parameter- 文件路径指向目录
- HTTP 状态码: 400 Bad Request
Cannot download a directory- 文件状态获取失败
- HTTP 状态码: 500 Internal Server Error
Failed to stat file: [error details]- 文件打开失败
- HTTP 状态码: 500 Internal Server Error
Failed to open file: [error details]- 文件内容复制失败
- HTTP 状态码: 500 Internal Server Error
Failed to copy file content: [error details]接受图片路径
接口方法: HandlerSendImg
注意:请求体必须是有效的 JSON 格式,包含图片路径的数组。
描述: 该接口通过 HandlerSendImg 方法接收包含图片路径的请求体,将图片路径转换为绝对路径,并通过 UDP 协议发送到指定的 IP 地址。请求体必须是有效的 JSON 格式,包含图片路径的数组。
URL:
/localchat/sendimage方法:
POST是否需要登录: false
请求参数:
| 参数名 | 类型 | 必填 | 描述 | 示例值 |
|---|---|---|---|---|
| Message | array | 是 | 图片路径的数组 | ["/path/to/image1.jpg", "/path/to/image2.png"] |
返回结果:
| 描述 | 返回示例 |
|---|---|
| 请求体无效(JSON解析失败) | "Invalid request body" |
| 获取主机名失败或发送消息失败 | "HandleMessage error" 或 "Failed to send message" |
示例:
curl -X POST http://api.example.com/localchat/sendimage -H "Content-Type: application/json" -d '{"Message": ["/path/to/image1.jpg", "/path/to/image2.png"]}'成功响应示例
- HTTP 状态码: 200 OK
图片发送成功失败响应示例
- 请求体无效
- HTTP 状态码: 400 Bad Request
Invalid request body- 获取主机名失败或发送消息失败
- HTTP 状态码: 500 Internal Server Error
HandleMessage error- 消息格式错误
- HTTP 状态码: 500 Internal Server Error
HandleMessage message error预览图片
接口方法: HandleViewImg
注意:确保提供的文件路径是有效的,并且不包含恶意路径输入(如目录遍历攻击 ../../)。
描述: 该接口通过 HandleViewImg 方法处理图片预览请求。通过从请求的 URL 中获取图片路径参数,验证图片的存在性和类型,然后将图片内容作为附件下载到客户端。
URL:
/localchat/viewimage方法:
GET是否需要登录: false
请求参数:
| 参数名 | 类型 | 必填 | 描述 | 示例值 |
|---|---|---|---|---|
| img | string | 是 | 要查看的图片的路径 | "/path/to/image.jpg" |
返回结果:
| 类型 | 描述 | 返回示例 |
|---|---|---|
| image/jpeg | 图片作为附件下载 | 图片内容的二进制流 |
示例:
curl -X GET http://api.example.com/localchat/viewimage?img=/path/to/image.jpg成功响应示例
- HTTP 状态码: 200 OK
- 响应头:
Content-Disposition:attachment; filename="image.jpg"Content-Type:image/jpegContent-Length: 图片的字节大小
- 响应体: 图片的二进制内容
失败响应示例
- 缺少图片路径参数
- HTTP 状态码: 400 Bad Request
img is empty- 图片路径无效
- HTTP 状态码: 400 Bad Request
file not found- MIME 类型获取失败
- HTTP 状态码: 500 Internal Server Error
Failed to determine MIME type- 图片打开失败
- HTTP 状态码: 500 Internal Server Error
Failed to open file- 文件内容写入失败
- HTTP 状态码: 500 Internal Server Error
Failed to write file content设置IP
接口方法: HandleAddr
注意:IP 地址必须是有效的 IPv4 地址。
描述: 该接口通过 HandleAddr 方法接收和设置用户聊天 IP 配置。通过解析请求体中的 JSON 数据,更新系统的聊天 IP 设置,并异步检查在线状态。
URL:
/localchat/setting方法:
POST是否需要登录: false
请求参数:
| 参数名 | 类型 | 必填 | 描述 | 示例值 |
|---|---|---|---|---|
| ip | string | 是 | 聊天 IP 地址 | "192.168.1.100" |
返回结果:
| 描述 | 返回示例 |
|---|---|
| 请求成功 | "success" |
| 请求体无效(JSON解析失败) | "Invalid request body" |
| 获取主机名失败或发送消息失败 | "HandleMessage error" 或 "Failed to send message" |
示例:
curl -X POST http://api.example.com/localchat/setting -H "Content-Type: application/json" -d '{"ip": "192.168.1.100"}'成功响应示例
- HTTP 状态码: 200 OK
success失败响应示例
- 请求体无效
- HTTP 状态码: 400 Bad Request
Failed to parse request body