Skip to content

Commit 0d417c3

Browse files
committed
mv gmc_config to plugins
1 parent b60cf84 commit 0d417c3

File tree

7 files changed

+496
-67
lines changed

7 files changed

+496
-67
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,5 @@ captcha.jpg
4545
/gmc_config.json
4646
cuberbot
4747
tmp
48-
dist/
48+
dist/
49+
plugins/

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ require (
4444
go.uber.org/atomic v1.9.0 // indirect
4545
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 // indirect
4646
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
47-
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
47+
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e // indirect
4848
golang.org/x/text v0.3.6 // indirect
4949
)
5050

go.sum

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,9 @@ golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7w
123123
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
124124
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
125125
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
126-
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I=
127126
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
127+
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e h1:WUoyKPm6nCo1BnNUvPGnFG3T5DUVem42yDJZZ4CNxMA=
128+
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
128129
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
129130
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
130131
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=

pkg/bot/remote.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,22 @@ var (
3535

3636
type WsServer struct {
3737
*safe_ws.SafeWebSocket // 线程安全的ws
38-
*config.ServerGroup // 服务器组配置
38+
*config.Plugin // 服务器组配置
3939
wsUrl string // 随机抽中的url
4040
regexp *regexp.Regexp
4141
}
4242

4343
func ConnectUniversal(cli *client.QQClient) {
4444
botServers := map[string]*WsServer{}
4545
RemoteServers.Store(cli.Uin, botServers)
46-
for _, group := range config.Conf.ServerGroups {
46+
47+
plugins := make([]*config.Plugin, 0)
48+
config.Plugins.Range(func(key string, value *config.Plugin) bool {
49+
plugins = append(plugins, value)
50+
return true
51+
})
52+
53+
for _, group := range plugins {
4754
if group.Disabled || group.Urls == nil || len(group.Urls) < 1 {
4855
continue
4956
}
@@ -79,7 +86,7 @@ func ConnectUniversal(cli *client.QQClient) {
7986
})
8087
botServers[serverGroup.Name] = &WsServer{
8188
SafeWebSocket: safeWs,
82-
ServerGroup: &serverGroup,
89+
Plugin: &serverGroup,
8390
wsUrl: serverUrl,
8491
regexp: nil,
8592
}

pkg/config/config.go

Lines changed: 24 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,39 @@
11
package config
22

3-
import (
4-
"encoding/json"
5-
6-
"github.com/pkg/errors"
7-
)
8-
3+
//go:generate go run github.com/a8m/syncmap -o "gen_plugin_map.go" -pkg config -name PluginMap "map[string]*Plugin"
94
var (
105
Fragment = false // 是否分片
116
Port = "9000"
127
SMS = false
138
Device = ""
14-
Conf = &GmcConfig{
15-
//SMS: false,
16-
//Port: "9000",
17-
ServerGroups: []*ServerGroup{
18-
{
19-
Name: "default",
20-
Disabled: false,
21-
Json: false,
22-
Urls: []string{"ws://localhost:8081/ws/cq/"},
23-
EventFilter: []int32{},
24-
RegexFilter: "",
25-
RegexReplace: "",
26-
ExtraHeader: map[string][]string{
27-
"User-Agent": {"GMC"},
28-
},
29-
},
30-
},
31-
}
9+
Plugins = &PluginMap{}
3210
HttpAuth = map[string]string{}
3311
)
3412

35-
type GmcConfig struct {
36-
//Port string `json:"port"` // 管理端口
37-
//SMS bool `json:"sms"` // 设备锁是否优先使用短信认证
38-
ServerGroups []*ServerGroup `json:"server_groups"` // 服务器组
13+
func init() {
14+
Plugins.Store("default", &Plugin{
15+
Name: "default",
16+
Disabled: false,
17+
Json: false,
18+
Urls: []string{"ws://localhost:8081/ws/cq/"},
19+
EventFilter: []int32{},
20+
RegexFilter: "",
21+
RegexReplace: "",
22+
ExtraHeader: map[string][]string{
23+
"User-Agent": {"GMC"},
24+
},
25+
})
3926
}
4027

41-
type ServerGroup struct {
42-
Name string `json:"name"` // 功能名称
28+
func ClearPlugins(pluginMap *PluginMap) {
29+
pluginMap.Range(func(key string, value *Plugin) bool {
30+
pluginMap.Delete(key)
31+
return true
32+
})
33+
}
34+
35+
type Plugin struct {
36+
Name string `json:"-"` // 功能名称
4337
Disabled bool `json:"disabled"` // 不填false默认启用
4438
Json bool `json:"json"` // json上报
4539
Urls []string `json:"urls"` // 服务器列表
@@ -49,17 +43,3 @@ type ServerGroup struct {
4943
ExtraHeader map[string][]string `json:"extra_header"` // 自定义请求头
5044
// TODO event filter, msg filter, regex filter, prefix filter, suffix filter
5145
}
52-
53-
func (g *GmcConfig) ReadJson(d []byte) error {
54-
var fileConfig GmcConfig
55-
if err := json.Unmarshal(d, &fileConfig); err != nil {
56-
return errors.Wrap(err, "failed to unmarshal json GmcConfig")
57-
}
58-
*g = fileConfig
59-
return nil
60-
}
61-
62-
func (g *GmcConfig) ToJson() []byte {
63-
b, _ := json.MarshalIndent(g, "", " ")
64-
return b
65-
}

0 commit comments

Comments
 (0)