2020-08-21 21:09:42 +08:00
|
|
|
|
package http
|
2020-08-21 12:32:16 +08:00
|
|
|
|
|
|
|
|
|
|
import (
|
2020-09-16 09:09:10 +08:00
|
|
|
|
"encoding/json"
|
2020-11-17 15:41:43 +08:00
|
|
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/oplogs"
|
2020-08-21 12:32:16 +08:00
|
|
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
2020-09-16 09:09:10 +08:00
|
|
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/serverutils"
|
2021-01-03 20:25:15 +08:00
|
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/dao"
|
2020-09-16 09:09:10 +08:00
|
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
|
|
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
|
|
|
|
|
"github.com/iwind/TeaGo/actions"
|
|
|
|
|
|
"github.com/iwind/TeaGo/maps"
|
2021-05-23 20:03:14 +08:00
|
|
|
|
"github.com/iwind/TeaGo/types"
|
|
|
|
|
|
"regexp"
|
2020-08-21 12:32:16 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type IndexAction struct {
|
|
|
|
|
|
actionutils.ParentAction
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (this *IndexAction) Init() {
|
|
|
|
|
|
this.Nav("", "setting", "index")
|
|
|
|
|
|
this.SecondMenu("http")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (this *IndexAction) RunGet(params struct {
|
|
|
|
|
|
ServerId int64
|
|
|
|
|
|
}) {
|
2020-09-21 19:51:50 +08:00
|
|
|
|
server, _, isOk := serverutils.FindServer(this.Parent(), params.ServerId)
|
2020-09-16 09:09:10 +08:00
|
|
|
|
if !isOk {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
httpConfig := &serverconfigs.HTTPProtocolConfig{}
|
|
|
|
|
|
if len(server.HttpJSON) > 0 {
|
|
|
|
|
|
err := json.Unmarshal(server.HttpJSON, httpConfig)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
httpConfig.IsOn = true
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
this.Data["serverType"] = server.Type
|
|
|
|
|
|
this.Data["httpConfig"] = maps.Map{
|
|
|
|
|
|
"isOn": httpConfig.IsOn,
|
|
|
|
|
|
"addresses": httpConfig.Listen,
|
|
|
|
|
|
}
|
2020-08-21 21:09:42 +08:00
|
|
|
|
|
2020-09-23 18:43:38 +08:00
|
|
|
|
// 跳转相关设置
|
2021-01-03 20:25:15 +08:00
|
|
|
|
webConfig, err := dao.SharedHTTPWebDAO.FindWebConfigWithServerId(this.AdminContext(), params.ServerId)
|
2020-09-23 18:43:38 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
this.Data["webId"] = webConfig.Id
|
|
|
|
|
|
this.Data["redirectToHTTPSConfig"] = webConfig.RedirectToHttps
|
|
|
|
|
|
|
2020-08-21 12:32:16 +08:00
|
|
|
|
this.Show()
|
|
|
|
|
|
}
|
2020-09-16 09:09:10 +08:00
|
|
|
|
|
|
|
|
|
|
func (this *IndexAction) RunPost(params struct {
|
|
|
|
|
|
ServerId int64
|
2020-09-28 16:25:26 +08:00
|
|
|
|
IsOn bool
|
2020-09-16 09:09:10 +08:00
|
|
|
|
Addresses string
|
|
|
|
|
|
|
2020-09-23 18:43:38 +08:00
|
|
|
|
WebId int64
|
|
|
|
|
|
RedirectToHTTPSJSON []byte
|
|
|
|
|
|
|
2020-09-16 09:09:10 +08:00
|
|
|
|
Must *actions.Must
|
|
|
|
|
|
}) {
|
2020-11-17 15:41:43 +08:00
|
|
|
|
// 记录日志
|
2020-11-20 15:32:42 +08:00
|
|
|
|
defer this.CreateLog(oplogs.LevelInfo, "修改服务 %d 的HTTP设置", params.ServerId)
|
2020-11-17 15:41:43 +08:00
|
|
|
|
|
2022-06-08 20:11:38 +08:00
|
|
|
|
var addresses = []*serverconfigs.NetworkAddressConfig{}
|
2020-09-16 09:09:10 +08:00
|
|
|
|
err := json.Unmarshal([]byte(params.Addresses), &addresses)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
this.Fail("端口地址解析失败:" + err.Error())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-06-08 20:11:38 +08:00
|
|
|
|
// 如果启用HTTP时没有填写端口,则默认为80
|
|
|
|
|
|
if params.IsOn && len(addresses) == 0 {
|
|
|
|
|
|
addresses = []*serverconfigs.NetworkAddressConfig{
|
|
|
|
|
|
{
|
|
|
|
|
|
Protocol: serverconfigs.ProtocolHTTP,
|
|
|
|
|
|
PortRange: "80",
|
|
|
|
|
|
},
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-23 20:03:14 +08:00
|
|
|
|
// 检查端口地址是否正确
|
|
|
|
|
|
for _, addr := range addresses {
|
|
|
|
|
|
err = addr.Init()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
this.Fail("绑定端口校验失败:" + err.Error())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if regexp.MustCompile(`^\d+$`).MatchString(addr.PortRange) {
|
|
|
|
|
|
port := types.Int(addr.PortRange)
|
|
|
|
|
|
if port > 65535 {
|
|
|
|
|
|
this.Fail("绑定的端口地址不能大于65535")
|
|
|
|
|
|
}
|
|
|
|
|
|
if port == 443 {
|
|
|
|
|
|
this.Fail("端口443通常是HTTPS的端口,不能用在HTTP上")
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-21 19:51:50 +08:00
|
|
|
|
server, _, isOk := serverutils.FindServer(this.Parent(), params.ServerId)
|
2020-09-16 09:09:10 +08:00
|
|
|
|
if !isOk {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2022-06-08 20:11:38 +08:00
|
|
|
|
var httpConfig = &serverconfigs.HTTPProtocolConfig{}
|
2020-09-16 09:09:10 +08:00
|
|
|
|
if len(server.HttpJSON) > 0 {
|
|
|
|
|
|
err = json.Unmarshal(server.HttpJSON, httpConfig)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-28 16:25:26 +08:00
|
|
|
|
httpConfig.IsOn = params.IsOn
|
2020-09-16 09:09:10 +08:00
|
|
|
|
httpConfig.Listen = addresses
|
|
|
|
|
|
configData, err := json.Marshal(httpConfig)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_, err = this.RPC().ServerRPC().UpdateServerHTTP(this.AdminContext(), &pb.UpdateServerHTTPRequest{
|
|
|
|
|
|
ServerId: params.ServerId,
|
2020-12-23 09:52:31 +08:00
|
|
|
|
HttpJSON: configData,
|
2020-09-16 09:09:10 +08:00
|
|
|
|
})
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-23 18:43:38 +08:00
|
|
|
|
// 设置跳转到HTTPS
|
|
|
|
|
|
// TODO 校验设置
|
|
|
|
|
|
_, err = this.RPC().HTTPWebRPC().UpdateHTTPWebRedirectToHTTPS(this.AdminContext(), &pb.UpdateHTTPWebRedirectToHTTPSRequest{
|
2021-11-24 11:58:01 +08:00
|
|
|
|
HttpWebId: params.WebId,
|
2020-09-23 18:43:38 +08:00
|
|
|
|
RedirectToHTTPSJSON: params.RedirectToHTTPSJSON,
|
|
|
|
|
|
})
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-16 09:09:10 +08:00
|
|
|
|
this.Success()
|
|
|
|
|
|
}
|