Files
EdgeAdmin/internal/web/actions/default/servers/server/settings/udp/index.go

93 lines
2.0 KiB
Go
Raw Normal View History

2020-08-21 21:09:42 +08:00
package udp
import (
2021-06-07 15:43:43 +08:00
"encoding/json"
2020-08-21 21:09:42 +08:00
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
2021-06-07 15:43:43 +08:00
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/serverutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/iwind/TeaGo/actions"
2020-08-21 21:09:42 +08:00
)
2021-06-07 15:43:43 +08:00
// IndexAction UDP设置
2020-08-21 21:09:42 +08:00
type IndexAction struct {
actionutils.ParentAction
}
func (this *IndexAction) Init() {
this.Nav("", "setting", "index")
this.SecondMenu("udp")
}
func (this *IndexAction) RunGet(params struct {
ServerId int64
}) {
2021-06-07 15:43:43 +08:00
server, _, isOk := serverutils.FindServer(this.Parent(), params.ServerId)
if !isOk {
return
}
udpConfig := &serverconfigs.UDPProtocolConfig{}
if len(server.UdpJSON) > 0 {
err := json.Unmarshal(server.UdpJSON, udpConfig)
if err != nil {
this.ErrorPage(err)
}
} else {
udpConfig.IsOn = true
}
this.Data["serverType"] = server.Type
this.Data["udpConfig"] = udpConfig
2020-08-21 21:09:42 +08:00
this.Show()
}
2021-06-07 15:43:43 +08:00
func (this *IndexAction) RunPost(params struct {
ServerId int64
ServerType string
Addresses string
Must *actions.Must
}) {
defer this.CreateLogInfo("修改代理服务 %d UDP设置", params.ServerId)
server, _, isOk := serverutils.FindServer(this.Parent(), params.ServerId)
if !isOk {
return
}
addresses := []*serverconfigs.NetworkAddressConfig{}
err := json.Unmarshal([]byte(params.Addresses), &addresses)
if err != nil {
this.Fail("端口地址解析失败:" + err.Error())
}
udpConfig := &serverconfigs.UDPProtocolConfig{}
if len(server.UdpJSON) > 0 {
err := json.Unmarshal(server.UdpJSON, udpConfig)
if err != nil {
this.ErrorPage(err)
}
} else {
udpConfig.IsOn = true
}
udpConfig.Listen = addresses
configData, err := json.Marshal(udpConfig)
if err != nil {
this.ErrorPage(err)
return
}
_, err = this.RPC().ServerRPC().UpdateServerUDP(this.AdminContext(), &pb.UpdateServerUDPRequest{
ServerId: params.ServerId,
UdpJSON: configData,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}