支持UDP代理

This commit is contained in:
GoEdgeLab
2021-06-07 15:43:43 +08:00
parent eb7eafe67c
commit b13f9c5dfe
11 changed files with 158 additions and 6 deletions

View File

@@ -52,6 +52,7 @@ func (this *AddOriginPopupAction) RunPost(params struct {
PortRange: port,
},
Description: "",
Weight: 10,
IsOn: true,
})
if err != nil {

View File

@@ -161,6 +161,34 @@ func (this *CreateAction) RunPost(params struct {
tlsConfig.AddListen(addr)
}
}
case serverconfigs.ServerTypeUDPProxy:
// 在DEMO模式下不能创建
if teaconst.IsDemo {
this.Fail("DEMO模式下不能创建UDP反向代理")
}
listen := []*serverconfigs.NetworkAddressConfig{}
err := json.Unmarshal([]byte(params.Addresses), &listen)
if err != nil {
this.Fail("端口地址解析失败:" + err.Error())
}
if len(listen) == 0 {
this.Fail("至少需要绑定一个端口")
}
for _, addr := range listen {
switch addr.Protocol.Primary() {
case serverconfigs.ProtocolUDP:
if udpConfig == nil {
udpConfig = &serverconfigs.UDPProtocolConfig{
BaseProtocol: serverconfigs.BaseProtocol{
IsOn: true,
},
}
}
udpConfig.AddListen(addr)
}
}
default:
this.Fail("请选择正确的服务类型")
}
@@ -237,7 +265,7 @@ func (this *CreateAction) RunPost(params struct {
// 源站地址
reverseProxyRefJSON := []byte{}
switch params.ServerType {
case serverconfigs.ServerTypeHTTPProxy, serverconfigs.ServerTypeTCPProxy:
case serverconfigs.ServerTypeHTTPProxy, serverconfigs.ServerTypeTCPProxy, serverconfigs.ServerTypeUDPProxy:
originConfigs := []*serverconfigs.OriginConfig{}
err := json.Unmarshal([]byte(params.Origins), &originConfigs)
if err != nil {

View File

@@ -2,6 +2,8 @@ package server
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"strconv"
)
@@ -18,5 +20,19 @@ func (this *IndexAction) RunGet(params struct {
ServerId int64
}) {
// TODO 等看板实现后,需要跳转到看板
// TCP & UDP跳转到设置
serverTypeResp, err := this.RPC().ServerRPC().FindEnabledServerType(this.AdminContext(), &pb.FindEnabledServerTypeRequest{ServerId: params.ServerId})
if err != nil {
this.ErrorPage(err)
return
}
serverType := serverTypeResp.Type
if serverType == serverconfigs.ServerTypeTCPProxy || serverType == serverconfigs.ServerTypeUDPProxy {
this.RedirectURL("/servers/server/settings?serverId=" + strconv.FormatInt(params.ServerId, 10))
return
}
// HTTP跳转到访问日志
this.RedirectURL("/servers/server/log?serverId=" + strconv.FormatInt(params.ServerId, 10))
}

View File

@@ -1,9 +1,15 @@
package udp
import (
"encoding/json"
"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/serverconfigs"
"github.com/iwind/TeaGo/actions"
)
// IndexAction UDP设置
type IndexAction struct {
actionutils.ParentAction
}
@@ -16,7 +22,71 @@ func (this *IndexAction) Init() {
func (this *IndexAction) RunGet(params struct {
ServerId int64
}) {
// TODO
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
this.Show()
}
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()
}

View File

@@ -13,7 +13,7 @@ func init() {
Helper(helpers.NewUserMustAuth(configloaders.AdminModuleCodeServer)).
Helper(serverutils.NewServerHelper()).
Prefix("/servers/server/settings/udp").
Get("", new(IndexAction)).
GetPost("", new(IndexAction)).
EndAll()
})
}

View File

@@ -93,7 +93,9 @@ func (this *ServerHelper) createLeftMenu(action *actions.ActionObject) {
tabbar := actionutils.NewTabbar()
tabbar.Add("服务列表", "", "/servers", "", false)
//tabbar.Add("看板", "", "/servers/server/board?serverId="+serverIdString, "dashboard", selectedTabbar == "board")
tabbar.Add("日志", "", "/servers/server/log?serverId="+serverIdString, "history", selectedTabbar == "log")
if family == "http" {
tabbar.Add("日志", "", "/servers/server/log?serverId="+serverIdString, "history", selectedTabbar == "log")
}
if family == "http" {
tabbar.Add("统计", "", "/servers/server/stat?serverId="+serverIdString, "chart area", selectedTabbar == "stat")
}
@@ -350,6 +352,12 @@ func (this *ServerHelper) createSettingsMenu(secondMenuItem string, serverIdStri
"isActive": secondMenuItem == "udp",
"isOn": serverConfig.UDP != nil && serverConfig.UDP.IsOn && len(serverConfig.UDP.Listen) > 0,
})
menuItems = append(menuItems, maps.Map{
"name": "反向代理",
"url": "/servers/server/settings/reverseProxy?serverId=" + serverIdString,
"isActive": secondMenuItem == "reverseProxy",
"isOn": serverConfig.ReverseProxyRef != nil && serverConfig.ReverseProxyRef.IsOn,
})
}
return menuItems

View File

@@ -18,6 +18,11 @@
<option value="tcp">TCP</option>
<option value="tls">TLS</option>
</select>
<!-- UDP -->
<select class="ui dropdown auto-width" name="protocol" v-if="serverType == 'udpProxy'">
<option value="udp">UDP</option>
</select>
</td>
</tr>
<tr>

View File

@@ -20,6 +20,11 @@
<option value="tcp">TCP</option>
<option value="tls">TLS</option>
</select>
<!-- UDP -->
<select class="ui dropdown auto-width" name="protocol" v-if="serverType == 'udpProxy'">
<option value="udp">UDP</option>
</select>
</td>
</tr>
<tr>

View File

@@ -22,6 +22,11 @@
<option value="tcp">TCP</option>
<option value="tls">TLS</option>
</select>
<!-- UDP -->
<select class="ui dropdown auto-width" name="protocol" v-model="origin.protocol" v-if="serverType == 'udpProxy'">
<option value="udp">UDP</option>
</select>
</td>
</tr>
<tr>

View File

@@ -1,7 +1,18 @@
{$layout}
{$template "/left_menu"}
<div class="right-box">
<p class="ui message">此功能暂未开放,敬请期待。</p>
<form method="post" class="ui form" data-tea-action="$" data-tea-success="success">
<input type="hidden" name="serverId" :value="serverId"/>
<input type="hidden" name="serverType" :value="serverType"/>
<table class="ui table selectable definition">
<tr>
<td class="title">绑定端口 *</td>
<td>
<network-addresses-box :v-server-type="serverType" :v-addresses="udpConfig.listen" :v-protocol="'udp'"></network-addresses-box>
</td>
</tr>
</table>
<submit-btn></submit-btn>
</form>
</div>

View File

@@ -0,0 +1,3 @@
Tea.context(function () {
this.success = NotifyReloadSuccess("保存成功")
})