Skip to content
This repository was archived by the owner on Nov 5, 2024. It is now read-only.

Commit 4ca7096

Browse files
committed
[proxy]添加简单的分组管理
1 parent 669995e commit 4ca7096

28 files changed

Lines changed: 478 additions & 19 deletions

File tree

teaconfigs/server_group.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package teaconfigs
2+
3+
import (
4+
"github.com/iwind/TeaGo/lists"
5+
stringutil "github.com/iwind/TeaGo/utils/string"
6+
)
7+
8+
type ServerGroup struct {
9+
Id string `yaml:"id" json:"id"`
10+
IsOn bool `yaml:"isOn" json:"isOn"`
11+
Name string `yaml:"name" json:"name"`
12+
ServerIds []string `yaml:"serverIds" json:"serverIds"`
13+
}
14+
15+
func NewServerGroup() *ServerGroup {
16+
return &ServerGroup{
17+
Id: stringutil.Rand(16),
18+
IsOn: true,
19+
ServerIds: []string{},
20+
}
21+
}
22+
23+
func (this *ServerGroup) Add(serverId ...string) {
24+
for _, id := range serverId {
25+
if lists.ContainsString(this.ServerIds, id) {
26+
continue
27+
}
28+
this.ServerIds = append(this.ServerIds, id)
29+
}
30+
}
31+
32+
func (this *ServerGroup) Remove(serverId string) {
33+
result := []string{}
34+
for _, id := range this.ServerIds {
35+
if id == serverId {
36+
continue
37+
}
38+
result = append(result, id)
39+
}
40+
this.ServerIds = result
41+
}

teaconfigs/server_group_list.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package teaconfigs
2+
3+
import (
4+
"github.com/TeaWeb/code/teaconfigs/shared"
5+
"github.com/go-yaml/yaml"
6+
"github.com/iwind/TeaGo/Tea"
7+
"github.com/iwind/TeaGo/logs"
8+
"io/ioutil"
9+
"os"
10+
)
11+
12+
type ServerGroupList struct {
13+
Groups []*ServerGroup `yaml:"groups" json:"groups"`
14+
}
15+
16+
func SharedServerGroupList() *ServerGroupList {
17+
list := &ServerGroupList{
18+
Groups: []*ServerGroup{},
19+
}
20+
filename := "servergrouplist.conf"
21+
data, err := ioutil.ReadFile(Tea.ConfigFile(filename))
22+
if err != nil {
23+
if !os.IsNotExist(err) {
24+
logs.Error(err)
25+
}
26+
return list
27+
}
28+
29+
err = yaml.Unmarshal(data, list)
30+
if err != nil {
31+
logs.Error(err)
32+
return list
33+
}
34+
35+
return list
36+
}
37+
38+
func (this *ServerGroupList) Add(group *ServerGroup) {
39+
this.Groups = append(this.Groups, group)
40+
}
41+
42+
func (this *ServerGroupList) Remove(id string) {
43+
result := []*ServerGroup{}
44+
for _, group := range this.Groups {
45+
if group.Id == id {
46+
continue
47+
}
48+
result = append(result, group)
49+
}
50+
this.Groups = result
51+
}
52+
53+
// 查找分组
54+
func (this *ServerGroupList) Find(id string) *ServerGroup {
55+
for _, group := range this.Groups {
56+
if group.Id == id {
57+
return group
58+
}
59+
}
60+
return nil
61+
}
62+
63+
// 检查是否包含某个代理服务ID
64+
func (this *ServerGroupList) ContainsServer(serverId string) bool {
65+
for _, group := range this.Groups {
66+
for _, id := range group.ServerIds {
67+
if id == serverId {
68+
return true
69+
}
70+
}
71+
}
72+
return false
73+
}
74+
75+
// 保存
76+
func (this *ServerGroupList) Save() error {
77+
data, err := yaml.Marshal(this)
78+
if err != nil {
79+
return err
80+
}
81+
82+
shared.Locker.Lock()
83+
defer shared.Locker.WriteUnlock()
84+
85+
filename := "servergrouplist.conf"
86+
return ioutil.WriteFile(Tea.ConfigFile(filename), data, 0666)
87+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package teaconfigs
2+
3+
import (
4+
"github.com/iwind/TeaGo/logs"
5+
"testing"
6+
)
7+
8+
func TestSharedServerGroupList(t *testing.T) {
9+
logs.PrintAsJSON(SharedServerGroupList(), t)
10+
}

teaweb/actions/default/cache/helper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ type Helper struct {
1212
// 缓存相关Helper
1313
func (this *Helper) BeforeAction(action *actions.ActionObject) {
1414
if action.Request.Method == http.MethodGet {
15-
proxyutils.AddServerMenu(action)
15+
proxyutils.AddServerMenu(action, false)
1616
}
1717
}

teaweb/actions/default/proxy/certs/helper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ func (this *Helper) BeforeAction(action *actions.ActionObject) {
1313
if action.Request.Method != http.MethodGet {
1414
return
1515
}
16-
proxyutils.AddServerMenu(action)
16+
proxyutils.AddServerMenu(action, false)
1717
}

teaweb/actions/default/proxy/helper.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ type Helper struct {
1111

1212
func (this *Helper) BeforeAction(action *actions.ActionObject) {
1313
if action.Request.Method == http.MethodGet && !action.HasPrefix("/proxy/status") {
14-
proxyutils.AddServerMenu(action)
14+
if action.HasPrefix("/proxy/add") {
15+
proxyutils.AddServerMenu(action, false)
16+
} else {
17+
proxyutils.AddServerMenu(action, true)
18+
}
1519
}
1620
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package proxy
2+
3+
import (
4+
_ "github.com/TeaWeb/code/teaweb/actions/default/proxy/servergroups"
5+
)

teaweb/actions/default/proxy/locations/waf.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func (this *WafAction) RunGet(params struct {
2222

2323
_, location := locationutils.SetCommonInfo(this, params.ServerId, params.LocationId, "waf")
2424

25-
proxyutils.AddServerMenu(this)
25+
proxyutils.AddServerMenu(this, true)
2626

2727
wafList := []maps.Map{}
2828
for _, waf := range teaconfigs.SharedWAFList().FindAllConfigs() {

teaweb/actions/default/proxy/log/day.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func (this *DayAction) Run(params struct {
4646
}
4747
this.Data["searchIP"] = params.SearchIP
4848

49-
proxyutils.AddServerMenu(this)
49+
proxyutils.AddServerMenu(this, true)
5050

5151
// 检查数据库连接
5252
this.Data["mongoError"] = ""

teaweb/actions/default/proxy/log/history.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func (this *HistoryAction) Run(params struct {
2626
"id": server.Id,
2727
}
2828

29-
proxyutils.AddServerMenu(this)
29+
proxyutils.AddServerMenu(this, true)
3030

3131
// 检查MongoDB连接
3232
this.Data["mongoError"] = ""

0 commit comments

Comments
 (0)