forked from ProjectWIND/ProjectWIND
Merge branch 'dev' of http://sheyiyuan.cn:3000/ProjectWIND/ProjectWIND into dev
This commit is contained in:
commit
2452df4da6
@ -1,6 +1,3 @@
|
||||
//go:build linux || darwin
|
||||
// +build linux darwin
|
||||
|
||||
package core
|
||||
|
||||
import (
|
||||
@ -9,7 +6,6 @@ import (
|
||||
"github.com/dop251/goja"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"plugin"
|
||||
)
|
||||
|
||||
var CmdMap = make(map[string]wba.Cmd)
|
||||
@ -39,38 +35,6 @@ func reloadAPP(file os.DirEntry, appsDir string) (totalDelta int, successDelta i
|
||||
}
|
||||
|
||||
ext := filepath.Ext(file.Name())
|
||||
if ext == ".so" {
|
||||
pluginPath := filepath.Join(appsDir, file.Name())
|
||||
p, err := plugin.Open(pluginPath)
|
||||
if err != nil {
|
||||
LOG.Error("打开应用 %s 时发生错误: %v", pluginPath, err)
|
||||
return 1, 0
|
||||
}
|
||||
AppInit, err := p.Lookup("AppInit")
|
||||
if err != nil {
|
||||
LOG.Error("找不到应用 %s 提供的 AppInit 接口: %v", pluginPath, err)
|
||||
return 1, 0
|
||||
}
|
||||
app := AppInit.(func() wba.AppInfo)()
|
||||
|
||||
err = app.Init(&AppApi)
|
||||
if err != nil {
|
||||
LOG.Error("初始化应用 %s 失败: %v", pluginPath, err)
|
||||
}
|
||||
|
||||
err = app.InitWSD(&DatabaseApi)
|
||||
if err != nil {
|
||||
LOG.Error("初始化应用 %s 数据库失败: %v", pluginPath, err)
|
||||
}
|
||||
|
||||
CmdMap = mergeMaps(CmdMap, app.Get().CmdMap)
|
||||
ScheduledTasks := app.Get().ScheduledTasks
|
||||
for _, task := range ScheduledTasks {
|
||||
RegisterCron(app.Get().Name, task)
|
||||
}
|
||||
LOG.Info("应用 %s 加载成功", pluginPath)
|
||||
return 1, 1
|
||||
}
|
||||
if ext == ".js" {
|
||||
pluginPath := filepath.Join(appsDir, file.Name())
|
||||
jsCode, err := os.ReadFile(pluginPath)
|
@ -2,6 +2,7 @@ package core
|
||||
|
||||
import (
|
||||
"ProjectWIND/LOG"
|
||||
"ProjectWIND/typed"
|
||||
"ProjectWIND/wba"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
@ -10,16 +11,14 @@ import (
|
||||
"net/url"
|
||||
)
|
||||
|
||||
var gProtocolAddr string
|
||||
var gToken string
|
||||
var gProtocol typed.Protocol
|
||||
|
||||
// WebSocketHandler 接收WebSocket连接处的消息并处理
|
||||
func WebSocketHandler(protocolAddr string, token string) error {
|
||||
func WebSocketHandler(protocol typed.Protocol) error {
|
||||
// 保存全局变量
|
||||
gProtocolAddr = protocolAddr
|
||||
gToken = token
|
||||
gProtocol = protocol
|
||||
// 解析连接URL
|
||||
u, err := url.Parse(protocolAddr)
|
||||
u, err := url.Parse(protocol.Addr)
|
||||
if err != nil {
|
||||
LOG.Error("Parse URL error: %v", err)
|
||||
return err
|
||||
@ -30,7 +29,7 @@ func WebSocketHandler(protocolAddr string, token string) error {
|
||||
if err != nil {
|
||||
LOG.Fatal("创建请求出错:%v", err)
|
||||
}
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
req.Header.Set("Authorization", "Bearer "+protocol.Token)
|
||||
// 配置WebSocket连接升级器
|
||||
dialer := websocket.DefaultDialer
|
||||
// 使用升级器建立WebSocket连接
|
||||
@ -126,7 +125,7 @@ func wsAPI(body wba.APIRequestInfo) (Response wba.APIResponseInfo, err error) {
|
||||
return wba.APIResponseInfo{}, err
|
||||
}
|
||||
// 解析连接URL
|
||||
u, err := url.Parse(gProtocolAddr)
|
||||
u, err := url.Parse(gProtocol.Addr)
|
||||
if err != nil {
|
||||
LOG.Error("Parse URL error: %v", err)
|
||||
return wba.APIResponseInfo{}, err
|
||||
@ -136,7 +135,7 @@ func wsAPI(body wba.APIRequestInfo) (Response wba.APIResponseInfo, err error) {
|
||||
if err != nil {
|
||||
LOG.Fatal("创建请求出错:%v", err)
|
||||
}
|
||||
req.Header.Set("Authorization", "Bearer "+gToken)
|
||||
req.Header.Set("Authorization", "Bearer "+gProtocol.Token)
|
||||
// 配置WebSocket连接升级器
|
||||
dialer := websocket.DefaultDialer
|
||||
// 使用升级器建立WebSocket连接
|
||||
|
15
readme.md
15
readme.md
@ -14,8 +14,8 @@ TODO:
|
||||
- ✅ 文件初始化
|
||||
- ✅ 事件处理
|
||||
- ✅ 数据库交互
|
||||
- ❌ 插件系统
|
||||
- ❌ 用户系统
|
||||
- ✅ 插件系统
|
||||
- ✅ 用户系统
|
||||
- ❌ web ui
|
||||
- ❌ 文档编写
|
||||
|
||||
@ -30,14 +30,19 @@ WIND(全称:WIND is not dice)是一个基于 Go 语言开发的bot框架
|
||||
## 1. goja
|
||||
- **库名称**:goja
|
||||
- **仓库地址**:[https://github.com/dop251/goja](https://github.com/dop251/goja)
|
||||
- **用途说明**:goja 作为一款强大的 JavaScript 解释器,在本项目中承担了处理动态的 JavaScript 脚本逻辑,为项目提供了灵活的脚本扩展能力,使得我们能够在 Go 项目中嵌入 JavaScript 代码来实现一些特定的业务规则处理等功能。它极大地丰富了项目的功能和灵活性,让我们能够更高效地开发出具有特色的功能模块。
|
||||
- **用途说明**:goja 作为一款强大的 JavaScript 解释器,在本项目中承担了处理动态的 JavaScript 脚本逻辑,为项目提供了灵活的脚本扩展能力,使得我们能够在项目中实现js插件的功能。它极大地丰富了项目的功能和灵活性,让跨平台的插件开发变得更加容易。
|
||||
|
||||
## 2. gocron
|
||||
- **库名称**:gocron
|
||||
- **仓库地址**:[https://github.com/go-co-op/gocron](https://github.com/go-co-op/gocron)
|
||||
- **用途说明**:gocron 是一个出色的任务调度库。在本项目里,它被用于[详细的任务调度应用场景,比如定期执行数据同步任务、定时清理临时文件或缓存数据等],确保了项目中的各种定时任务能够精准、可靠地执行。其简洁易用的 API 设计大大降低了我们实现复杂任务调度逻辑的难度,为项目的稳定运行提供了有力保障。
|
||||
- **用途说明**:gocron 是一个出色的任务调度库。在本项目里,它被用作定时任务的调度器,确保了项目中的各种定时任务能够精准、可靠地执行。其简洁易用的 API 设计大大降低了我们实现复杂任务调度逻辑的难度,为项目的稳定运行提供了有力保障。
|
||||
|
||||
非常感谢 `goja` 和 `gocron` 项目团队的开源贡献,使得我们的项目开发能够借助这些优秀的工具快速推进,为用户带来更好的体验。
|
||||
## 3. hertz
|
||||
- **库名称**:hertz
|
||||
- **仓库地址**:[https://github.com/cloudwego/hertz](https://github.com/cloudwego/hertz)
|
||||
- **用途说明**:hertz 是一个基于 Go 语言开发的高性能 HTTP 路由器。在本项目中,它被用作项目的 HTTP 服务器,为项目提供了快速、高效的 HTTP 请求处理能力。让我们能够灵活地对 HTTP 请求进行处理。
|
||||
|
||||
非常感谢以上项目团队的开源贡献,使得我们的项目开发能够借助这些优秀的工具快速推进,为用户带来更好的体验。
|
||||
|
||||
---
|
||||
后面没有了,开发者很懒,什么都没写。
|
@ -1,17 +1,17 @@
|
||||
package typed
|
||||
|
||||
type CoreConfigInfo struct {
|
||||
CoreName string `json:"core_name"`
|
||||
Protocols []Protocol `json:"protocols"`
|
||||
WebUIPort uint16 `json:"webui_port"`
|
||||
PasswordHash string `json:"password_hash"`
|
||||
ServiceName string `json:"service_name"`
|
||||
CoreName string `json:"core_name"`
|
||||
Protocol Protocol `json:"protocol"`
|
||||
WebUIPort uint16 `json:"webui_port"`
|
||||
PasswordHash string `json:"password_hash"`
|
||||
ServiceName string `json:"service_name"`
|
||||
}
|
||||
|
||||
type Protocol struct {
|
||||
ProtocolName string `json:"protocol_name"`
|
||||
ProtocolPlatform string `json:"protocol_platform"`
|
||||
ProtocolAddr string `json:"protocol_addr"`
|
||||
Token string `json:"token"`
|
||||
Enable bool `json:"enable"`
|
||||
Name string `json:"protocol_name"`
|
||||
Platform string `json:"protocol_platform"`
|
||||
Addr string `json:"protocol_addr"`
|
||||
Token string `json:"token"`
|
||||
Enable bool `json:"enable"`
|
||||
}
|
||||
|
75
utils.go
75
utils.go
@ -87,16 +87,16 @@ func checkAndUpdateConfig(configPath string) error {
|
||||
var coreConfig typed.CoreConfigInfo
|
||||
|
||||
var defaultProtocol typed.Protocol
|
||||
defaultProtocol.ProtocolName = "EXAMPLE"
|
||||
defaultProtocol.ProtocolPlatform = "在这里输入协议平台"
|
||||
defaultProtocol.ProtocolAddr = "在这里输入协议地址,如'ws://127.0.0.1:8080'"
|
||||
defaultProtocol.Name = "EXAMPLE"
|
||||
defaultProtocol.Platform = "在这里输入协议平台"
|
||||
defaultProtocol.Addr = "在这里输入协议地址,如'ws://127.0.0.1:8080'"
|
||||
defaultProtocol.Token = "在这里输入协议的Token"
|
||||
defaultProtocol.Enable = true
|
||||
|
||||
var defaultConfig typed.CoreConfigInfo
|
||||
defaultConfig.CoreName = "windCore"
|
||||
defaultConfig.WebUIPort = 3211
|
||||
defaultConfig.Protocols = []typed.Protocol{defaultProtocol}
|
||||
defaultConfig.Protocol = defaultProtocol
|
||||
defaultConfig.ServiceName = "wind"
|
||||
// 读取配置文件
|
||||
file, err := os.Open(configPath)
|
||||
@ -120,11 +120,8 @@ func checkAndUpdateConfig(configPath string) error {
|
||||
}
|
||||
|
||||
// 检查并更新配置
|
||||
//if coreConfig.ProtocolAddr == "" {
|
||||
// coreConfig.ProtocolAddr = defaultConfig.ProtocolAddr
|
||||
//}
|
||||
if coreConfig.Protocols == nil || len(coreConfig.Protocols) == 0 {
|
||||
coreConfig.Protocols = defaultConfig.Protocols
|
||||
if coreConfig.Protocol == (typed.Protocol{}) {
|
||||
coreConfig.Protocol = defaultConfig.Protocol
|
||||
}
|
||||
if coreConfig.WebUIPort == 0 {
|
||||
coreConfig.WebUIPort = defaultConfig.WebUIPort
|
||||
@ -311,44 +308,30 @@ func startProtocol() {
|
||||
LOG.Fatal("连接协议时,解析配置文件 ./data/core.json 失败: %v", err)
|
||||
}
|
||||
LOG.Info("正在启动WebSocket链接程序...")
|
||||
protocolNum := 0
|
||||
breakNum := 0
|
||||
UnenableProtocolNum := 0
|
||||
for _, protocol := range config.Protocols {
|
||||
protocolName := protocol.ProtocolName
|
||||
if protocolName == "EXAMPLE" {
|
||||
continue
|
||||
}
|
||||
if protocolName == "" {
|
||||
LOG.Warn("连接协议 %s 时,协议名称为空,跳过该协议", protocolName)
|
||||
breakNum++
|
||||
continue
|
||||
}
|
||||
//获取协议地址
|
||||
protocolAddr := protocol.ProtocolAddr
|
||||
if protocolAddr == "" {
|
||||
LOG.Warn("连接协议 %s 时,协议地址为空,跳过该协议", protocolName)
|
||||
breakNum++
|
||||
continue
|
||||
}
|
||||
if protocol.Enable == false {
|
||||
LOG.Warn("连接协议 %s 时,协议已禁用,跳过该协议", protocolName)
|
||||
UnenableProtocolNum++
|
||||
continue
|
||||
}
|
||||
//获取token
|
||||
token := protocol.Token
|
||||
// 启动 WebSocket 处理程序
|
||||
go func() {
|
||||
err := core.WebSocketHandler(protocolAddr, token)
|
||||
if err != nil {
|
||||
LOG.Error("连接协议时,启动 WebSocket 处理程序失败: %v", err)
|
||||
}
|
||||
}()
|
||||
protocolNum++
|
||||
protocol := config.Protocol
|
||||
if protocol.Name == "EXAMPLE" {
|
||||
LOG.Warn("未找到协议配置信息")
|
||||
return
|
||||
}
|
||||
if protocol.Name == "" {
|
||||
LOG.Warn("连接协议 %s 时,协议名称为空", protocol.Name)
|
||||
return
|
||||
}
|
||||
//获取协议地址
|
||||
protocolAddr := protocol.Addr
|
||||
if protocolAddr == "" {
|
||||
LOG.Warn("连接协议 %s 时,协议地址为空", protocol.Name)
|
||||
return
|
||||
}
|
||||
if protocol.Enable == false {
|
||||
LOG.Warn("连接协议 %s 时,协议已禁用", protocol.Name)
|
||||
return
|
||||
}
|
||||
// 启动 WebSocket 处理程序
|
||||
err = core.WebSocketHandler(protocol)
|
||||
if err != nil {
|
||||
LOG.Error("连接协议时 %s,启动 WebSocket 处理程序失败: %v", protocol.Name, err)
|
||||
}
|
||||
LOG.Info(" %d 个协议服务启动完成, %d 个协议服务已禁用, %d 个协议服务因为配置错误被跳过。", protocolNum, UnenableProtocolNum, breakNum)
|
||||
select {}
|
||||
}
|
||||
|
||||
func ReloadApps() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user