mirror of
https://github.com/TeaOSLab/EdgeAdmin.git
synced 2025-11-13 03:40:27 +08:00
修改反向代理实现
This commit is contained in:
@@ -74,7 +74,6 @@ func (this *CreateAction) RunPost(params struct {
|
|||||||
var unixConfig *serverconfigs.UnixProtocolConfig = nil
|
var unixConfig *serverconfigs.UnixProtocolConfig = nil
|
||||||
var udpConfig *serverconfigs.UDPProtocolConfig = nil
|
var udpConfig *serverconfigs.UDPProtocolConfig = nil
|
||||||
var webId int64 = 0
|
var webId int64 = 0
|
||||||
var reverseProxyId int64 = 0
|
|
||||||
|
|
||||||
switch params.ServerType {
|
switch params.ServerType {
|
||||||
case serverconfigs.ServerTypeHTTPProxy, serverconfigs.ServerTypeHTTPWeb:
|
case serverconfigs.ServerTypeHTTPProxy, serverconfigs.ServerTypeHTTPWeb:
|
||||||
@@ -157,6 +156,7 @@ func (this *CreateAction) RunPost(params struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 源站地址
|
// 源站地址
|
||||||
|
reverseProxyRefJSON := []byte{}
|
||||||
switch params.ServerType {
|
switch params.ServerType {
|
||||||
case serverconfigs.ServerTypeHTTPProxy, serverconfigs.ServerTypeTCPProxy:
|
case serverconfigs.ServerTypeHTTPProxy, serverconfigs.ServerTypeTCPProxy:
|
||||||
origins := []*serverconfigs.OriginServerConfig{}
|
origins := []*serverconfigs.OriginServerConfig{}
|
||||||
@@ -174,7 +174,15 @@ func (this *CreateAction) RunPost(params struct {
|
|||||||
this.ErrorPage(err)
|
this.ErrorPage(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
reverseProxyId = resp.ReverseProxyId
|
reverseProxyRef := &serverconfigs.ReverseProxyRef{
|
||||||
|
IsOn: true,
|
||||||
|
ReverseProxyId: resp.ReverseProxyId,
|
||||||
|
}
|
||||||
|
reverseProxyRefJSON, err = json.Marshal(reverseProxyRef)
|
||||||
|
if err != nil {
|
||||||
|
this.ErrorPage(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Web地址
|
// Web地址
|
||||||
@@ -215,7 +223,7 @@ func (this *CreateAction) RunPost(params struct {
|
|||||||
IncludeNodesJSON: includeNodesJSON,
|
IncludeNodesJSON: includeNodesJSON,
|
||||||
ExcludeNodesJSON: excludeNodesJSON,
|
ExcludeNodesJSON: excludeNodesJSON,
|
||||||
WebId: webId,
|
WebId: webId,
|
||||||
ReverseProxyId: reverseProxyId,
|
ReverseProxyJSON: reverseProxyRefJSON,
|
||||||
}
|
}
|
||||||
if httpConfig != nil {
|
if httpConfig != nil {
|
||||||
data, err := json.Marshal(httpConfig)
|
data, err := json.Marshal(httpConfig)
|
||||||
|
|||||||
@@ -17,5 +17,6 @@ func (this *IndexAction) Init() {
|
|||||||
func (this *IndexAction) RunGet(params struct {
|
func (this *IndexAction) RunGet(params struct {
|
||||||
ServerId int64
|
ServerId int64
|
||||||
}) {
|
}) {
|
||||||
this.RedirectURL("/servers/server/board?serverId=" + strconv.FormatInt(params.ServerId, 10))
|
// TODO 等看板实现后,需要跳转到看板
|
||||||
|
this.RedirectURL("/servers/server/settings?serverId=" + strconv.FormatInt(params.ServerId, 10))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,11 +2,10 @@ package reverseProxy
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
|
||||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/serverutils"
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||||
|
"github.com/iwind/TeaGo/actions"
|
||||||
"github.com/iwind/TeaGo/maps"
|
"github.com/iwind/TeaGo/maps"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -22,67 +21,76 @@ func (this *IndexAction) Init() {
|
|||||||
func (this *IndexAction) RunGet(params struct {
|
func (this *IndexAction) RunGet(params struct {
|
||||||
ServerId int64
|
ServerId int64
|
||||||
}) {
|
}) {
|
||||||
server, _, isOk := serverutils.FindServer(&this.ParentAction, params.ServerId)
|
serverTypeResp, err := this.RPC().ServerRPC().FindEnabledServerType(this.AdminContext(), &pb.FindEnabledServerTypeRequest{ServerId: params.ServerId})
|
||||||
if !isOk {
|
if err != nil {
|
||||||
|
this.ErrorPage(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
this.Data["serverType"] = server.Type
|
serverType := serverTypeResp.Type
|
||||||
this.Data["reverseProxyId"] = server.ReverseProxyId
|
|
||||||
|
|
||||||
isOn := false
|
reverseProxyResp, err := this.RPC().ServerRPC().FindAndInitServerReverseProxyConfig(this.AdminContext(), &pb.FindAndInitServerReverseProxyConfigRequest{ServerId: params.ServerId})
|
||||||
if server.ReverseProxyId > 0 {
|
if err != nil {
|
||||||
reverseProxyResp, err := this.RPC().ReverseProxyRPC().FindEnabledReverseProxy(this.AdminContext(), &pb.FindEnabledReverseProxyRequest{ReverseProxyId: server.ReverseProxyId})
|
this.ErrorPage(err)
|
||||||
if err != nil {
|
return
|
||||||
this.ErrorPage(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
reverseProxy := reverseProxyResp.ReverseProxy
|
|
||||||
if reverseProxy == nil {
|
|
||||||
// TODO 应该在界面上提示用户开启
|
|
||||||
this.ErrorPage(errors.New("reverse proxy should not be nil"))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
isOn = true
|
|
||||||
|
|
||||||
primaryOrigins := []*serverconfigs.OriginServerConfig{}
|
|
||||||
backupOrigins := []*serverconfigs.OriginServerConfig{}
|
|
||||||
if len(reverseProxy.PrimaryOriginsJSON) > 0 {
|
|
||||||
err = json.Unmarshal(reverseProxy.PrimaryOriginsJSON, &primaryOrigins)
|
|
||||||
if err != nil {
|
|
||||||
this.ErrorPage(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(reverseProxy.BackupOriginsJSON) > 0 {
|
|
||||||
err = json.Unmarshal(reverseProxy.BackupOriginsJSON, &backupOrigins)
|
|
||||||
if err != nil {
|
|
||||||
this.ErrorPage(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
primaryOriginMaps := []maps.Map{}
|
|
||||||
backupOriginMaps := []maps.Map{}
|
|
||||||
for _, originConfig := range primaryOrigins {
|
|
||||||
m := maps.Map{
|
|
||||||
"id": originConfig.Id,
|
|
||||||
"weight": originConfig.Weight,
|
|
||||||
"addr": originConfig.Addr.Protocol.String() + "://" + originConfig.Addr.Host + ":" + originConfig.Addr.PortRange,
|
|
||||||
}
|
|
||||||
primaryOriginMaps = append(primaryOriginMaps, m)
|
|
||||||
}
|
|
||||||
for _, originConfig := range backupOrigins {
|
|
||||||
m := maps.Map{
|
|
||||||
"id": originConfig.Id,
|
|
||||||
"weight": originConfig.Weight,
|
|
||||||
"addr": originConfig.Addr.Protocol.String() + "://" + originConfig.Addr.Host + ":" + originConfig.Addr.PortRange,
|
|
||||||
}
|
|
||||||
backupOriginMaps = append(backupOriginMaps, m)
|
|
||||||
}
|
|
||||||
this.Data["primaryOrigins"] = primaryOriginMaps
|
|
||||||
this.Data["backupOrigins"] = backupOriginMaps
|
|
||||||
}
|
}
|
||||||
this.Data["isOn"] = isOn
|
reverseProxyRef := &serverconfigs.ReverseProxyRef{}
|
||||||
|
err = json.Unmarshal(reverseProxyResp.ReverseProxyRef, reverseProxyRef)
|
||||||
|
if err != nil {
|
||||||
|
this.ErrorPage(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
reverseProxy := &serverconfigs.ReverseProxyConfig{}
|
||||||
|
err = json.Unmarshal(reverseProxyResp.ReverseProxy, reverseProxy)
|
||||||
|
if err != nil {
|
||||||
|
this.ErrorPage(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
this.Data["serverType"] = serverType
|
||||||
|
this.Data["reverseProxyRef"] = reverseProxyRef
|
||||||
|
this.Data["reverseProxyConfig"] = reverseProxy
|
||||||
|
|
||||||
|
primaryOriginMaps := []maps.Map{}
|
||||||
|
backupOriginMaps := []maps.Map{}
|
||||||
|
for _, originConfig := range reverseProxy.PrimaryOrigins {
|
||||||
|
m := maps.Map{
|
||||||
|
"id": originConfig.Id,
|
||||||
|
"weight": originConfig.Weight,
|
||||||
|
"addr": originConfig.Addr.Protocol.String() + "://" + originConfig.Addr.Host + ":" + originConfig.Addr.PortRange,
|
||||||
|
}
|
||||||
|
primaryOriginMaps = append(primaryOriginMaps, m)
|
||||||
|
}
|
||||||
|
for _, originConfig := range reverseProxy.BackupOrigins {
|
||||||
|
m := maps.Map{
|
||||||
|
"id": originConfig.Id,
|
||||||
|
"weight": originConfig.Weight,
|
||||||
|
"addr": originConfig.Addr.Protocol.String() + "://" + originConfig.Addr.Host + ":" + originConfig.Addr.PortRange,
|
||||||
|
}
|
||||||
|
backupOriginMaps = append(backupOriginMaps, m)
|
||||||
|
}
|
||||||
|
this.Data["primaryOrigins"] = primaryOriginMaps
|
||||||
|
this.Data["backupOrigins"] = backupOriginMaps
|
||||||
|
|
||||||
this.Show()
|
this.Show()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *IndexAction) RunPost(params struct {
|
||||||
|
ServerId int64
|
||||||
|
ReverseProxyRefJSON []byte
|
||||||
|
|
||||||
|
Must *actions.Must
|
||||||
|
}) {
|
||||||
|
// TODO 校验配置
|
||||||
|
|
||||||
|
_, err := this.RPC().ServerRPC().UpdateServerReverseProxy(this.AdminContext(), &pb.UpdateServerReverseProxyRequest{
|
||||||
|
ServerId: params.ServerId,
|
||||||
|
ReverseProxyJSON: params.ReverseProxyRefJSON,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
this.ErrorPage(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
this.Success()
|
||||||
|
}
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ func init() {
|
|||||||
Data("mainTab", "setting").
|
Data("mainTab", "setting").
|
||||||
Data("secondMenuItem", "reverseProxy").
|
Data("secondMenuItem", "reverseProxy").
|
||||||
Prefix("/servers/server/settings/reverseProxy").
|
Prefix("/servers/server/settings/reverseProxy").
|
||||||
Get("", new(IndexAction)).
|
GetPost("", new(IndexAction)).
|
||||||
GetPost("/scheduling", new(SchedulingAction)).
|
GetPost("/scheduling", new(SchedulingAction)).
|
||||||
GetPost("/updateSchedulingPopup", new(UpdateSchedulingPopupAction)).
|
GetPost("/updateSchedulingPopup", new(UpdateSchedulingPopupAction)).
|
||||||
Post("/updateOn", new(UpdateOnAction)).
|
Post("/updateOn", new(UpdateOnAction)).
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/serverutils"
|
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/schedulingconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/schedulingconfigs"
|
||||||
@@ -21,31 +20,18 @@ func (this *SchedulingAction) Init() {
|
|||||||
func (this *SchedulingAction) RunGet(params struct {
|
func (this *SchedulingAction) RunGet(params struct {
|
||||||
ServerId int64
|
ServerId int64
|
||||||
}) {
|
}) {
|
||||||
server, _, isOk := serverutils.FindServer(&this.ParentAction, params.ServerId)
|
reverseProxyResp, err := this.RPC().ServerRPC().FindAndInitServerReverseProxyConfig(this.AdminContext(), &pb.FindAndInitServerReverseProxyConfigRequest{ServerId: params.ServerId})
|
||||||
if !isOk {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if server.ReverseProxyId <= 0 {
|
|
||||||
// TODO 在界面上提示用户未开通,并提供开通按钮,用户点击后开通
|
|
||||||
this.WriteString("此服务尚未开通反向代理功能")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
this.Data["reverseProxyId"] = server.ReverseProxyId
|
|
||||||
|
|
||||||
reverseProxyResp, err := this.RPC().ReverseProxyRPC().FindEnabledReverseProxyConfig(this.AdminContext(), &pb.FindEnabledReverseProxyConfigRequest{
|
|
||||||
ReverseProxyId: server.ReverseProxyId,
|
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
this.ErrorPage(err)
|
this.ErrorPage(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
reverseProxy := &serverconfigs.ReverseProxyConfig{}
|
reverseProxy := &serverconfigs.ReverseProxyConfig{}
|
||||||
err = json.Unmarshal(reverseProxyResp.Config, reverseProxy)
|
err = json.Unmarshal(reverseProxyResp.ReverseProxy, reverseProxy)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
this.ErrorPage(err)
|
this.ErrorPage(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
this.Data["reverseProxyId"] = reverseProxy.Id
|
||||||
|
|
||||||
schedulingCode := reverseProxy.FindSchedulingConfig().Code
|
schedulingCode := reverseProxy.FindSchedulingConfig().Code
|
||||||
schedulingMap := schedulingconfigs.FindSchedulingType(schedulingCode)
|
schedulingMap := schedulingconfigs.FindSchedulingType(schedulingCode)
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
package reverseProxy
|
package reverseProxy
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||||
)
|
)
|
||||||
|
|
||||||
type UpdateOnAction struct {
|
type UpdateOnAction struct {
|
||||||
@@ -29,10 +31,21 @@ func (this *UpdateOnAction) RunPost(params struct {
|
|||||||
this.ErrorPage(err)
|
this.ErrorPage(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
reverseProxyId := resp.ReverseProxyId
|
reverseProxyId := resp.ReverseProxyId
|
||||||
_, err = this.RPC().ServerRPC().UpdateServerReverseProxy(this.AdminContext(), &pb.UpdateServerReverseProxyRequest{
|
ref := &serverconfigs.ReverseProxyRef{
|
||||||
ServerId: params.ServerId,
|
IsOn: true,
|
||||||
ReverseProxyId: reverseProxyId,
|
ReverseProxyId: reverseProxyId,
|
||||||
|
}
|
||||||
|
refJSON, err := json.Marshal(ref)
|
||||||
|
if err != nil {
|
||||||
|
this.ErrorPage(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = this.RPC().ServerRPC().UpdateServerReverseProxy(this.AdminContext(), &pb.UpdateServerReverseProxyRequest{
|
||||||
|
ServerId: params.ServerId,
|
||||||
|
ReverseProxyJSON: refJSON,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
this.ErrorPage(err)
|
this.ErrorPage(err)
|
||||||
|
|||||||
@@ -76,9 +76,9 @@ func (this *ServerHelper) createLeftMenu(action *actions.ActionObject) {
|
|||||||
selectedTabbar, _ := action.Data["mainTab"]
|
selectedTabbar, _ := action.Data["mainTab"]
|
||||||
tabbar := actionutils.NewTabbar()
|
tabbar := actionutils.NewTabbar()
|
||||||
tabbar.Add("当前服务:"+serverConfig.Name, "", "/servers", "left long alternate arrow", false)
|
tabbar.Add("当前服务:"+serverConfig.Name, "", "/servers", "left long alternate arrow", false)
|
||||||
tabbar.Add("看板", "", "/servers/server/board?serverId="+serverIdString, "dashboard", selectedTabbar == "board")
|
//tabbar.Add("看板", "", "/servers/server/board?serverId="+serverIdString, "dashboard", selectedTabbar == "board")
|
||||||
tabbar.Add("日志", "", "/servers/server/log?serverId="+serverIdString, "history", selectedTabbar == "log")
|
tabbar.Add("日志", "", "/servers/server/log?serverId="+serverIdString, "history", selectedTabbar == "log")
|
||||||
tabbar.Add("统计", "", "/servers/server/stat?serverId="+serverIdString, "chart area", selectedTabbar == "stat")
|
//tabbar.Add("统计", "", "/servers/server/stat?serverId="+serverIdString, "chart area", selectedTabbar == "stat")
|
||||||
tabbar.Add("设置", "", "/servers/server/settings?serverId="+serverIdString, "setting", selectedTabbar == "setting")
|
tabbar.Add("设置", "", "/servers/server/settings?serverId="+serverIdString, "setting", selectedTabbar == "setting")
|
||||||
tabbar.Add("删除", "", "/servers/server/delete?serverId="+serverIdString, "trash", selectedTabbar == "delete")
|
tabbar.Add("删除", "", "/servers/server/delete?serverId="+serverIdString, "trash", selectedTabbar == "delete")
|
||||||
|
|
||||||
|
|||||||
@@ -2,12 +2,29 @@
|
|||||||
{$template "/left_menu"}
|
{$template "/left_menu"}
|
||||||
|
|
||||||
<div class="right-box">
|
<div class="right-box">
|
||||||
{$ if not .isOn}
|
{$template "menu"}
|
||||||
<div class="ui message">暂未启用反向代理功能,<a href="" @click.prevent="updateOn(true)">[启用]</a>。</div>
|
|
||||||
{$end}
|
|
||||||
{$ if .isOn}
|
|
||||||
{$template "menu"}
|
|
||||||
|
|
||||||
<origin-list-box :v-primary-origins="primaryOrigins" :v-backup-origins="backupOrigins" :v-server-type="serverType" :v-params="'type=server&serverId=' + serverId + '&reverseProxyId=' + reverseProxyId"></origin-list-box>
|
<div class="margin"></div>
|
||||||
{$end}
|
<form class="ui form" data-tea-action="$" data-tea-success="success">
|
||||||
|
<input type="hidden" name="serverId" :value="serverId"/>
|
||||||
|
<input type="hidden" name="reverseProxyRefJSON" :value="JSON.stringify(reverseProxyRef)"/>
|
||||||
|
<table class="ui table selectable definition">
|
||||||
|
<tr>
|
||||||
|
<td class="title">是否启用</td>
|
||||||
|
<td>
|
||||||
|
<div class="ui checkbox">
|
||||||
|
<input type="checkbox" v-model="reverseProxyRef.isOn"/>
|
||||||
|
<label></label>
|
||||||
|
</div>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
<submit-btn></submit-btn>
|
||||||
|
</form>
|
||||||
|
|
||||||
|
<div v-if="reverseProxyRef.isOn">
|
||||||
|
<div class="ui divider"></div>
|
||||||
|
<origin-list-box :v-primary-origins="primaryOrigins" :v-backup-origins="backupOrigins" :v-server-type="serverType"
|
||||||
|
:v-params="'type=server&serverId=' + serverId + '&reverseProxyId=' + reverseProxyConfig.id"></origin-list-box>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -1,4 +1,6 @@
|
|||||||
Tea.context(function () {
|
Tea.context(function () {
|
||||||
|
this.success = NotifyReloadSuccess("保存成功")
|
||||||
|
|
||||||
this.updateOn = function (b) {
|
this.updateOn = function (b) {
|
||||||
teaweb.confirm(b ? "确定要启用反向代理服务吗?" : "确定要停用反向代理服务吗?", function () {
|
teaweb.confirm(b ? "确定要启用反向代理服务吗?" : "确定要停用反向代理服务吗?", function () {
|
||||||
this.$post(".updateOn")
|
this.$post(".updateOn")
|
||||||
|
|||||||
Reference in New Issue
Block a user