Skip to content

Commit

Permalink
fix: 修改webui实现
Browse files Browse the repository at this point in the history
适配webui
配置文件操作移动到config.go
  • Loading branch information
huoxue1 committed Sep 21, 2021
1 parent 8b9e659 commit 5c580c7
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 158 deletions.
90 changes: 90 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package leafBot

import (
_ "embed"
"fmt"
"github.com/huoxue1/leafBot/utils"
log "github.com/sirupsen/logrus"
"os"
"strconv"
"time"
)

//go:embed config/default_config.yaml
var defaultConfig []byte

type Config struct {
NickName []string `json:"nick_name" yaml:"nick_name" hjson:"nick_name"`
Admin int `json:"admin" yaml:"admin" hjson:"admin"`
Host string `json:"host" yaml:"host" hjson:"host"`
Port int `json:"port" yaml:"port" hjson:"port"`
LogLevel string `json:"log_level" yaml:"log_level" hjson:"log_level"`
SuperUser []int `json:"super_user" yaml:"super_user" hjson:"super_user"`
CommandStart []string `json:"command_start" yaml:"command_start" hjson:"command_start"`
EnablePlaywright bool `json:"enable_playwright" yaml:"enable_playwright" hjson:"enable_playwright"`
Plugins struct {
FlashGroupID int `json:"flash_group_id" yaml:"flash_group_id" hjson:"flash_group_id"`
AlApiToken string `json:"al_api_token" yaml:"al_api_token" hjson:"al_api_token"`
EnableReplyTome bool `json:"enable_reply_tome" yaml:"enable_reply_tome" hjson:"enable_reply_tome"`
Welcome []struct {
GroupId int `json:"group_id" yaml:"group_id" hjson:"group_id"`
Message string `json:"message" yaml:"message" hjson:"message"`
} `json:"welcome" yaml:"welcome" hjson:"welcome"`
GithubToken string `json:"github_token" yaml:"github_token" hjson:"github_token"`
AutoPassFriendRequest []string `json:"auto_pass_friend_request" yaml:"auto_pass_friend_request" hjson:"auto_pass_friend_request"`
} `json:"plugins" yaml:"plugins" hjson:"plugins"`
Datas map[string]interface{} `json:"datas" yaml:"datas" hjson:"datas"`
}

var (
DefaultConfig = new(Config)
hook *utils.LogHook
)

func LoadConfig() {

input := ""
log.Infoln("请输入机器人账号")
_, err := fmt.Scanln(&input)
if err != nil {
log.Panicln(err)
}

selfID, err := strconv.Atoi(input)
if err != nil {
log.Errorln("输入有误")
}
writeGoConfig(selfID)
_, err = os.Stat("./config")
if err != nil {
err := os.Mkdir("./config", 0666)
if err != nil {
log.Errorln("创建config文件夹失败")
return
}
}
file, err := os.OpenFile("./config/config.yml", os.O_CREATE|os.O_RDWR, 0666)
if err != nil {
log.Println("打开config.yml文件失败\n" + err.Error())
return
}
_, err = file.Write(defaultConfig)
if err != nil {
log.Infoln("写入配置到文件失败")
}
log.Infoln("成功写入默认配置到config.yml")
log.Infoln("程序将在五秒后重启")
time.Sleep(5000)
os.Exit(3)
//ui.Close()
}

func writeGoConfig(selfID int) {
err := os.WriteFile(".config/gocq_config.yml", []byte(fmt.Sprintf(gocqConfig, selfID)), 0666)
if err != nil {
return
}
log.Infoln("已生成gocq配置文件gocq_config.yml,将其复制到gocq目录并重命名为config.yml即可使用")
}

var gocqConfig = "# go-cqhttp 默认配置文件\n\naccount: # 账号相关\n uin: %d # QQ账号\n password: '' # 密码为空时使用扫码登录\n encrypt: false # 是否开启密码加密\n status: 0 # 在线状态 请参考 https://github.com/Mrs4s/go-cqhttp/blob/dev/docs/config.md#在线状态\n relogin: # 重连设置\n disabled: false\n delay: 3 # 重连延迟, 单位秒\n interval: 0 # 重连间隔\n max-times: 0 # 最大重连次数, 0为无限制\n\n # 是否使用服务器下发的新地址进行重连\n # 注意, 此设置可能导致在海外服务器上连接情况更差\n use-sso-address: true\n\nheartbeat:\n disabled: false # 是否开启心跳事件上报\n # 心跳频率, 单位秒\n # -1 为关闭心跳\n interval: 5\n\nmessage:\n # 上报数据类型\n # 可选: string,array\n post-format: array\n # 是否忽略无效的CQ码, 如果为假将原样发送\n ignore-invalid-cqcode: false\n # 是否强制分片发送消息\n # 分片发送将会带来更快的速度\n # 但是兼容性会有些问题\n force-fragment: false\n # 是否将url分片发送\n fix-url: false\n # 下载图片等请求网络代理\n proxy-rewrite: ''\n # 是否上报自身消息\n report-self-message: false\n # 移除服务端的Reply附带的At\n remove-reply-at: false\n # 为Reply附加更多信息\n extra-reply-data: false\n\noutput:\n # 日志等级 trace,debug,info,warn,error\n log-level: warn\n # 是否启用 DEBUG\n debug: false # 开启调试模式\n\n# 默认中间件锚点\ndefault-middlewares: &default\n # 访问密钥, 强烈推荐在公网的服务器设置\n access-token: ''\n # 事件过滤器文件目录\n filter: ''\n # API限速设置\n # 该设置为全局生效\n # 原 cqhttp 虽然启用了 rate_limit 后缀, 但是基本没插件适配\n # 目前该限速设置为令牌桶算法, 请参考:\n # https://baike.baidu.com/item/%E4%BB%A4%E7%89%8C%E6%A1%B6%E7%AE%97%E6%B3%95/6597000?fr=aladdin\n rate-limit:\n enabled: false # 是否启用限速\n frequency: 1 # 令牌回复频率, 单位秒\n bucket: 1 # 令牌桶大小\n\nservers:\n # HTTP 通信设置\n - http:\n # 是否关闭正向HTTP服务器\n disabled: true\n # 服务端监听地址\n host: 127.0.0.1\n # 服务端监听端口\n port: 5700\n # 反向HTTP超时时间, 单位秒\n # 最小值为5,小于5将会忽略本项设置\n timeout: 5\n middlewares:\n <<: *default # 引用默认中间件\n # 反向HTTP POST地址列表\n post:\n #- url: '' # 地址\n # secret: '' # 密钥\n #- url: 127.0.0.1:5701 # 地址\n # secret: '' # 密钥\n\n # 正向WS设置\n - ws:\n # 是否禁用正向WS服务器\n disabled: true\n # 正向WS服务器监听地址\n host: 127.0.0.1\n # 正向WS服务器监听端口\n port: 6700\n middlewares:\n <<: *default # 引用默认中间件\n\n - ws-reverse:\n # 是否禁用当前反向WS服务\n disabled: false\n # 反向WS Universal 地址\n # 注意 设置了此项地址后下面两项将会被忽略\n universal: ws://127.0.0.1:8080/cqhttp/ws\n # 反向WS API 地址\n api: ws://your_websocket_api.server\n # 反向WS Event 地址\n event: ws://your_websocket_event.server\n # 重连间隔 单位毫秒\n reconnect-interval: 3000\n middlewares:\n <<: *default # 引用默认中间件\n # pprof 性能分析服务器, 一般情况下不需要启用.\n # 如果遇到性能问题请上传报告给开发者处理\n # 注意: pprof服务不支持中间件、不支持鉴权. 请不要开放到公网\n - pprof:\n # 是否禁用pprof性能分析服务器\n disabled: true\n # pprof服务器监听地址\n host: 127.0.0.1\n # pprof服务器监听端口\n port: 7700\n\n # 可添加更多\n #- ws-reverse:\n #- ws:\n #- http:\n #- pprof:\n\ndatabase: # 数据库相关设置\n leveldb:\n # 是否启用内置leveldb数据库\n # 启用将会增加10-20MB的内存占用和一定的磁盘空间\n # 关闭将无法使用 撤回 回复 get_msg 等上下文相关功能\n enable: true\n"
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/hjson/hjson-go v3.1.0+incompatible
github.com/huoxue1/gg v1.3.1-0.20210909022355-795dba57682a
github.com/huoxue1/lorca v0.1.11
github.com/huoxue1/test3 v0.0.0-20210921063422-c43e6876777d // indirect
github.com/jonboulle/clockwork v0.2.2 // indirect
github.com/json-iterator/go v1.1.10
github.com/kr/pretty v0.1.0 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ github.com/huoxue1/gg v1.3.1-0.20210909022355-795dba57682a h1:4ul0TDmJw/TWwvhyGJ
github.com/huoxue1/gg v1.3.1-0.20210909022355-795dba57682a/go.mod h1:fUbidWJbgI9Gc/FNaCATEcM0KjDGRGSdEVDoLSmqHJk=
github.com/huoxue1/lorca v0.1.11 h1:B5x4Xta8rauGCERoCHY7nWg2z1GUrksf9ycZGHwLs4Q=
github.com/huoxue1/lorca v0.1.11/go.mod h1:eEI3aGkmMladREqFJwUhd8cLlSUgG13wvuNprVjaVaY=
github.com/huoxue1/test3 v0.0.0-20210921062027-d3eb76e40e29 h1:CYXhxqBtddC98W9EeXSWiOhilLsT7cFWTZn3JOpI6Fc=
github.com/huoxue1/test3 v0.0.0-20210921062027-d3eb76e40e29/go.mod h1:6ausQYwJPfruk1GDGhqf7J5ptnOrlakAXHwNuaMrpvo=
github.com/huoxue1/test3 v0.0.0-20210921062653-86eb8dc09a26 h1:EUidOuoCdj+rXFxRDEPlfTlNKd9C52bhHUui+zzn+OI=
github.com/huoxue1/test3 v0.0.0-20210921062653-86eb8dc09a26/go.mod h1:6ausQYwJPfruk1GDGhqf7J5ptnOrlakAXHwNuaMrpvo=
github.com/huoxue1/test3 v0.0.0-20210921063123-b62eac168e68 h1:u5NNNKtEWUUYQxVzmTJ6UKSY7SAB/7FnoDJKBNwb30I=
github.com/huoxue1/test3 v0.0.0-20210921063123-b62eac168e68/go.mod h1:6ausQYwJPfruk1GDGhqf7J5ptnOrlakAXHwNuaMrpvo=
github.com/huoxue1/test3 v0.0.0-20210921063422-c43e6876777d h1:wCAgKH32OqyCkV6K/E6unTW2KEuBzkB9VRjWyxBkGPQ=
github.com/huoxue1/test3 v0.0.0-20210921063422-c43e6876777d/go.mod h1:6ausQYwJPfruk1GDGhqf7J5ptnOrlakAXHwNuaMrpvo=
github.com/jonboulle/clockwork v0.2.2 h1:UOGuzwb1PwsrDAObMuhUnj0p5ULPj8V/xJ7Kx9qUBdQ=
github.com/jonboulle/clockwork v0.2.2/go.mod h1:Pkfl5aHPm1nk2H9h0bjmnJD/BcgbGXUBGnn1kMkgxc8=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
Expand Down
141 changes: 101 additions & 40 deletions gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,23 @@ package leafBot

import ( //nolint:gci

"embed"
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
"github.com/huoxue1/leafBot/message"
"github.com/huoxue1/leafBot/utils"
"github.com/huoxue1/lorca"
"github.com/huoxue1/test3"
log "github.com/sirupsen/logrus"
"io"
"net/http" //nolint:gci
"os"
"os/signal"
"sort"
"strconv"
)

//go:embed gui/static
var static embed.FS

var upGrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
Expand Down Expand Up @@ -79,41 +78,42 @@ func InitWindow() {
}
log.Infoln("web页面:http://127.0.0.1:3000")
engine := gin.New()
engine.StaticFS("/static", http.FS(static))
engine.Use(Cors())
engine.StaticFS("/dist", http.FS(test3.Dist))
engine.POST("/get_config", GetConfig)
engine.POST("/get_group_list", GetGroupList)
engine.POST("/get_friend_list", GetFriendList)
engine.GET("/", func(context *gin.Context) {
context.Redirect(http.StatusMovedPermanently, "/static/gui/static/html/default.html")
context.Redirect(http.StatusMovedPermanently, "/dist/dist/default.html")
})
engine.POST("/update_plugin_states", func(context *gin.Context) {
id := context.PostForm("id")
status, err := strconv.ParseBool(context.PostForm("status"))
if err != nil {
log.Errorln("改变插件状态出错" + err.Error())
}
if status {
StartPluginByID(id)
} else {
BanPluginByID(id)
}
context.JSON(200, nil)
})

engine.POST("/get_plugins", func(context *gin.Context) {
list := GetHandleList()
var pluginList []BaseHandle

for _, handles := range list {
pluginList = append(pluginList, handles...)
}
sort.SliceStable(pluginList, func(i, j int) bool {
id1, _ := strconv.Atoi(pluginList[i].ID)
id2, _ := strconv.Atoi(pluginList[j].ID)
return id1 < id2
})
context.JSON(200, pluginList)
})
//engine.POST("/update_plugin_states", func(context *gin.Context) {
// id := context.PostForm("id")
// status, err := strconv.ParseBool(context.PostForm("status"))
// if err != nil {
// log.Errorln("改变插件状态出错" + err.Error())
// }
// if status {
// StartPluginByID(id)
// } else {
// BanPluginByID(id)
// }
// context.JSON(200, nil)
//})
//
//engine.POST("/get_plugins", func(context *gin.Context) {
// list := GetHandleList()
// var pluginList []BaseHandle
//
// for _, handles := range list {
// pluginList = append(pluginList, handles...)
// }
// sort.SliceStable(pluginList, func(i, j int) bool {
// id1, _ := strconv.Atoi(pluginList[i].ID)
// id2, _ := strconv.Atoi(pluginList[j].ID)
// return id1 < id2
// })
// context.JSON(200, pluginList)
//})

engine.GET("/get_log", func(context *gin.Context) {
conn, err := upGrader.Upgrade(context.Writer, context.Request, nil)
Expand Down Expand Up @@ -193,21 +193,48 @@ func GetConfig(ctx *gin.Context) {
func GetGroupList(ctx *gin.Context) {
selfID, err := strconv.Atoi(ctx.PostForm("self_id"))
if err != nil {
return

var data map[string]interface{}
err := ctx.BindJSON(&data)
if err != nil {
log.Errorln(err.Error())
return
}
selfID = int(data["self_id"].(float64))
}

bot := GetBotById(selfID)
list := bot.GetGroupList()
ctx.JSON(200, list)
var resp []interface{}
list := bot.GetGroupList().String()
err = json.Unmarshal([]byte(list), &resp)
if err != nil {
log.Errorln(err.Error())
}
ctx.JSON(200, resp)
}

func GetFriendList(ctx *gin.Context) {
selfID, err := strconv.Atoi(ctx.PostForm("self_id"))
if err != nil {
return
log.Errorln(err.Error())
var data map[string]interface{}
err := ctx.BindJSON(&data)
if err != nil {
log.Errorln(err.Error())
log.Errorln("绑定错误")
return
}
selfID = int(data["self_id"].(float64))
}
bot := GetBotById(selfID)
list := bot.GetFriendList()
ctx.JSON(200, list)
var resp []interface{}
list := bot.GetFriendList().String()
err = json.Unmarshal([]byte(list), &resp)
if err != nil {
log.Errorln(err.Error())
log.Errorln("解析json错误")
}
ctx.JSON(200, resp)
}

func CallApi(ctx *gin.Context) {
Expand All @@ -222,3 +249,37 @@ func CallApi(ctx *gin.Context) {
msgID := bot.SendMsg(messageType, id, id, message.ParseMessageFromString(message1))
ctx.JSON(200, msgID)
}

func Cors() gin.HandlerFunc {
return func(c *gin.Context) {
method := c.Request.Method
origin := c.Request.Header.Get("Origin") //请求头部
if origin != "" {
//接收客户端发送的origin (重要!)
c.Writer.Header().Set("Access-Control-Allow-Origin", origin)
//服务器支持的所有跨域请求的方法
c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE,UPDATE")
//允许跨域设置可以返回其他子段,可以自定义字段
c.Header("Access-Control-Allow-Headers", "Authorization, Content-Length, X-CSRF-Token, Token,session, Content-Type")
// 允许浏览器(客户端)可以解析的头部 (重要)
c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers")
//设置缓存时间
c.Header("Access-Control-Max-Age", "172800")
//允许客户端传递校验信息比如 cookie (重要)
c.Header("Access-Control-Allow-Credentials", "true")
}

//允许类型校验
if method == "OPTIONS" {
c.JSON(http.StatusOK, "ok!")
}

defer func() {
if err := recover(); err != nil {
log.Printf("Panic info is: %v", err)
}
}()

c.Next()
}
}
5 changes: 3 additions & 2 deletions handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,9 @@ type (
)

type Plugin struct {
Name string
Helps []map[string]string
Name string
Helps []map[string]string
handles []BaseHandle
}

var (
Expand Down
Loading

0 comments on commit 5c580c7

Please sign in to comment.