实现websocket基本功能

This commit is contained in:
刘祥超
2020-09-26 19:54:02 +08:00
parent 2e0c8a3160
commit 4a8498db10
22 changed files with 487 additions and 23 deletions

View File

@@ -36,6 +36,7 @@ type RPCClient struct {
httpCachePolicyClients []pb.HTTPCachePolicyServiceClient
httpFirewallPolicyClients []pb.HTTPFirewallPolicyServiceClient
httpLocationClients []pb.HTTPLocationServiceClient
httpWebsocketClients []pb.HTTPWebsocketServiceClient
}
func NewRPCClient(apiConfig *configs.APIConfig) (*RPCClient, error) {
@@ -61,6 +62,7 @@ func NewRPCClient(apiConfig *configs.APIConfig) (*RPCClient, error) {
httpCachePolicyClients := []pb.HTTPCachePolicyServiceClient{}
httpFirewallPolicyClients := []pb.HTTPFirewallPolicyServiceClient{}
httpLocationClients := []pb.HTTPLocationServiceClient{}
httpWebsocketClients := []pb.HTTPWebsocketServiceClient{}
conns := []*grpc.ClientConn{}
for _, endpoint := range apiConfig.RPC.Endpoints {
@@ -94,6 +96,7 @@ func NewRPCClient(apiConfig *configs.APIConfig) (*RPCClient, error) {
httpCachePolicyClients = append(httpCachePolicyClients, pb.NewHTTPCachePolicyServiceClient(conn))
httpFirewallPolicyClients = append(httpFirewallPolicyClients, pb.NewHTTPFirewallPolicyServiceClient(conn))
httpLocationClients = append(httpLocationClients, pb.NewHTTPLocationServiceClient(conn))
httpWebsocketClients = append(httpWebsocketClients, pb.NewHTTPWebsocketServiceClient(conn))
}
return &RPCClient{
@@ -116,6 +119,7 @@ func NewRPCClient(apiConfig *configs.APIConfig) (*RPCClient, error) {
httpCachePolicyClients: httpCachePolicyClients,
httpFirewallPolicyClients: httpFirewallPolicyClients,
httpLocationClients: httpLocationClients,
httpWebsocketClients: httpWebsocketClients,
}, nil
}
@@ -245,6 +249,13 @@ func (this *RPCClient) HTTPLocationRPC() pb.HTTPLocationServiceClient {
return nil
}
func (this *RPCClient) HTTPWebsocketRPC() pb.HTTPWebsocketServiceClient {
if len(this.httpWebsocketClients) > 0 {
return this.httpWebsocketClients[rands.Int(0, len(this.httpWebsocketClients)-1)]
}
return nil
}
func (this *RPCClient) Context(adminId int64) context.Context {
ctx := context.Background()
m := maps.Map{

View File

@@ -54,84 +54,81 @@ func (this *LocationHelper) createMenus(serverIdString string, locationIdString
"isActive": secondMenuItem == "basic",
},
}
menuItems = append(menuItems, maps.Map{
"name": "HTTP",
"url": "/servers/server/settings/locations/http?serverId=" + serverIdString + "&locationId=" + locationIdString,
"isActive": secondMenuItem == "http",
})
menuItems = append(menuItems, maps.Map{
"name": "Web设置",
"url": "/servers/server/settings/locations/web?serverId=" + serverIdString + "&locationId=" + locationIdString,
"isActive": secondMenuItem == "web",
})
menuItems = append(menuItems, maps.Map{
"name": "反向代理",
"url": "/servers/server/settings/locations/reverseProxy?serverId=" + serverIdString + "&locationId=" + locationIdString,
"isActive": secondMenuItem == "reverseProxy",
})
menuItems = append(menuItems, maps.Map{
"name": "重写规则",
"url": "/servers/server/settings/locations/rewrite?serverId=" + serverIdString + "&locationId=" + locationIdString,
"isActive": secondMenuItem == "rewrite",
})
menuItems = append(menuItems, maps.Map{
"name": "访问控制",
"url": "/servers/server/settings/locations/access?serverId=" + serverIdString + "&locationId=" + locationIdString,
"isActive": secondMenuItem == "access",
})
menuItems = append(menuItems, maps.Map{
"name": "WAF",
"url": "/servers/server/settings/locations/waf?serverId=" + serverIdString + "&locationId=" + locationIdString,
"isActive": secondMenuItem == "waf",
})
menuItems = append(menuItems, maps.Map{
"name": "缓存",
"url": "/servers/server/settings/locations/cache?serverId=" + serverIdString + "&locationId=" + locationIdString,
"isActive": secondMenuItem == "cache",
})
menuItems = append(menuItems, maps.Map{
"name": "-",
"url": "",
"isActive": false,
})
menuItems = append(menuItems, maps.Map{
"name": "字符编码",
"url": "/servers/server/settings/locations/charset?serverId=" + serverIdString + "&locationId=" + locationIdString,
"isActive": secondMenuItem == "charset",
})
menuItems = append(menuItems, maps.Map{
"name": "访问日志",
"url": "/servers/server/settings/locations/accessLog?serverId=" + serverIdString + "&locationId=" + locationIdString,
"isActive": secondMenuItem == "accessLog",
})
menuItems = append(menuItems, maps.Map{
"name": "统计",
"url": "/servers/server/settings/locations/stat?serverId=" + serverIdString + "&locationId=" + locationIdString,
"isActive": secondMenuItem == "stat",
})
menuItems = append(menuItems, maps.Map{
"name": "Gzip压缩",
"url": "/servers/server/settings/locations/gzip?serverId=" + serverIdString + "&locationId=" + locationIdString,
"isActive": secondMenuItem == "gzip",
})
menuItems = append(menuItems, maps.Map{
"name": "特殊页面",
"url": "/servers/server/settings/locations/pages?serverId=" + serverIdString + "&locationId=" + locationIdString,
"isActive": secondMenuItem == "pages",
})
menuItems = append(menuItems, maps.Map{
"name": "HTTP Header",
"url": "/servers/server/settings/locations/headers?serverId=" + serverIdString + "&locationId=" + locationIdString,
"isActive": secondMenuItem == "header",
})
menuItems = append(menuItems, maps.Map{
"name": "Websocket",
"url": "/servers/server/settings/locations/websocket?serverId=" + serverIdString + "&locationId=" + locationIdString,
"isActive": secondMenuItem == "websocket",
})
return menuItems
}

View File

@@ -0,0 +1,20 @@
package rewrite
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
)
type IndexAction struct {
actionutils.ParentAction
}
func (this *IndexAction) Init() {
}
func (this *IndexAction) RunGet(params struct {
LocationId int64
}) {
// TODO
this.Show()
}

View File

@@ -0,0 +1,21 @@
package rewrite
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/locations/locationutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/serverutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/helpers"
"github.com/iwind/TeaGo"
)
func init() {
TeaGo.BeforeStart(func(server *TeaGo.Server) {
server.
Helper(helpers.NewUserMustAuth()).
Helper(locationutils.NewLocationHelper()).
Helper(serverutils.NewServerHelper()).
Data("tinyMenuItem", "rewrite").
Prefix("/servers/server/settings/locations/rewrite").
Get("", new(IndexAction)).
EndAll()
})
}

View File

@@ -0,0 +1,112 @@
package websocket
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/webutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/iwind/TeaGo/actions"
)
type IndexAction struct {
actionutils.ParentAction
}
func (this *IndexAction) Init() {
}
func (this *IndexAction) RunGet(params struct {
LocationId int64
}) {
webConfig, err := webutils.FindWebConfigWithLocationId(this.Parent(), params.LocationId)
if err != nil {
this.ErrorPage(err)
return
}
this.Data["webId"] = webConfig.Id
this.Data["websocketRef"] = webConfig.WebsocketRef
this.Data["websocketConfig"] = webConfig.Websocket
this.Show()
}
func (this *IndexAction) RunPost(params struct {
WebId int64
WebsocketRefJSON []byte
WebsocketJSON []byte
Must *actions.Must
}) {
// TODO 检查配置
websocketRef := &serverconfigs.HTTPWebsocketRef{}
err := json.Unmarshal(params.WebsocketRefJSON, websocketRef)
if err != nil {
this.ErrorPage(err)
return
}
websocketConfig := &serverconfigs.HTTPWebsocketConfig{}
err = json.Unmarshal(params.WebsocketJSON, websocketConfig)
if err != nil {
this.ErrorPage(err)
return
}
err = websocketConfig.Init()
if err != nil {
this.ErrorPage(err)
return
}
// 创建
handshakeTimeoutJSON, err := json.Marshal(websocketConfig.HandshakeTimeout)
if err != nil {
this.ErrorPage(err)
return
}
// 创建或修改
if websocketConfig.Id <= 0 {
createResp, err := this.RPC().HTTPWebsocketRPC().CreateHTTPWebsocket(this.AdminContext(), &pb.CreateHTTPWebsocketRequest{
HandshakeTimeoutJSON: handshakeTimeoutJSON,
AllowAllOrigins: websocketConfig.AllowAllOrigins,
AllowedOrigins: websocketConfig.AllowedOrigins,
RequestSameOrigin: websocketConfig.RequestSameOrigin,
RequestOrigin: websocketConfig.RequestOrigin,
})
if err != nil {
this.ErrorPage(err)
return
}
websocketConfig.Id = createResp.WebsocketId
} else {
_, err = this.RPC().HTTPWebsocketRPC().UpdateHTTPWebsocket(this.AdminContext(), &pb.UpdateHTTPWebsocketRequest{
WebsocketId: websocketConfig.Id,
HandshakeTimeoutJSON: handshakeTimeoutJSON,
AllowAllOrigins: websocketConfig.AllowAllOrigins,
AllowedOrigins: websocketConfig.AllowedOrigins,
RequestSameOrigin: websocketConfig.RequestSameOrigin,
RequestOrigin: websocketConfig.RequestOrigin,
})
}
websocketRef.WebsocketId = websocketConfig.Id
websocketRefJSON, err := json.Marshal(websocketRef)
if err != nil {
this.ErrorPage(err)
return
}
_, err = this.RPC().HTTPWebRPC().UpdateHTTPWebWebsocket(this.AdminContext(), &pb.UpdateHTTPWebWebsocketRequest{
WebId: params.WebId,
WebsocketJSON: websocketRefJSON,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}

View File

@@ -0,0 +1,21 @@
package websocket
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/locations/locationutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/serverutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/helpers"
"github.com/iwind/TeaGo"
)
func init() {
TeaGo.BeforeStart(func(server *TeaGo.Server) {
server.
Helper(helpers.NewUserMustAuth()).
Helper(locationutils.NewLocationHelper()).
Helper(serverutils.NewServerHelper()).
Data("tinyMenuItem", "websocket").
Prefix("/servers/server/settings/locations/websocket").
GetPost("", new(IndexAction)).
EndAll()
})
}

View File

@@ -0,0 +1,22 @@
package rewrite
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
)
type IndexAction struct {
actionutils.ParentAction
}
func (this *IndexAction) Init() {
this.Nav("", "setting", "index")
this.SecondMenu("rewrite")
}
func (this *IndexAction) RunGet(params struct {
ServerId int64
}) {
// TODO
this.Show()
}

View File

@@ -0,0 +1,18 @@
package rewrite
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/serverutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/helpers"
"github.com/iwind/TeaGo"
)
func init() {
TeaGo.BeforeStart(func(server *TeaGo.Server) {
server.
Helper(helpers.NewUserMustAuth()).
Helper(serverutils.NewServerHelper()).
Prefix("/servers/server/settings/rewrite").
Get("", new(IndexAction)).
EndAll()
})
}

View File

@@ -0,0 +1,32 @@
package websocket
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/iwind/TeaGo/actions"
)
// 添加来源域
type CreateOriginAction struct {
actionutils.ParentAction
}
func (this *CreateOriginAction) Init() {
this.Nav("", "", "")
}
func (this *CreateOriginAction) RunGet(params struct{}) {
this.Show()
}
func (this *CreateOriginAction) RunPost(params struct {
Origin string
Must *actions.Must
}) {
params.Must.
Field("origin", params.Origin).
Require("请输入域名")
this.Data["origin"] = params.Origin
this.Success()
}

View File

@@ -0,0 +1,161 @@
Vue.component("http-websocket-box", {
props: ["v-websocket-ref", "v-websocket-config", "v-is-location"],
data: function () {
let websocketRef = this.vWebsocketRef
if (websocketRef == null) {
websocketRef = {
isPrior: false,
isOn: true,
websocketId: 0
}
}
let websocketConfig = this.vWebsocketConfig
if (websocketConfig == null) {
websocketConfig = {
id: 0,
isOn: true,
handshakeTimeout: {
count: 30,
unit: "second"
},
allowAllOrigins: true,
allowedOrigins: [],
requestSameOrigin: true,
requestOrigin: ""
}
} else {
if (websocketConfig.handshakeTimeout == null) {
websocketConfig.handshakeTimeout = {
count: 30,
unit: "second",
}
}
if (websocketConfig.allowedOrigins == null) {
websocketConfig.allowedOrigins = []
}
}
return {
websocketRef: websocketRef,
websocketConfig: websocketConfig,
handshakeTimeoutCountString: websocketConfig.handshakeTimeout.count.toString(),
advancedVisible: false
}
},
watch: {
handshakeTimeoutCountString: function (v) {
let count = parseInt(v)
if (!isNaN(count) && count >= 0) {
this.websocketConfig.handshakeTimeout.count = count
} else {
this.websocketConfig.handshakeTimeout.count = 0
}
}
},
methods: {
isOn: function () {
return (!this.vIsLocation || this.websocketRef.isPrior) && this.websocketRef.isOn
},
changeAdvancedVisible: function (v) {
this.advancedVisible = v
},
createOrigin: function () {
let that = this
teaweb.popup("/servers/server/settings/websocket/createOrigin", {
height: "12.5em",
callback: function (resp) {
that.websocketConfig.allowedOrigins.push(resp.data.origin)
}
})
},
removeOrigin: function (index) {
this.websocketConfig.allowedOrigins.$remove(index)
}
},
template: `<div>
<input type="hidden" name="websocketRefJSON" :value="JSON.stringify(websocketRef)"/>
<input type="hidden" name="websocketJSON" :value="JSON.stringify(websocketConfig)"/>
<table class="ui table definition selectable">
<prior-checkbox :v-config="websocketRef" v-if="vIsLocation"></prior-checkbox>
<tbody v-show="(!this.vIsLocation || this.websocketRef.isPrior)">
<tr>
<td class="title">是否启用配置</td>
<td>
<div class="ui checkbox">
<input type="checkbox" v-model="websocketRef.isOn"/>
<label></label>
</div>
</td>
</tr>
</tbody>
<tbody v-show="isOn()">
<tr>
<td>允许所有来源域<em>Origin</em></td>
<td>
<div class="ui checkbox">
<input type="checkbox" v-model="websocketConfig.allowAllOrigins"/>
<label></label>
</div>
<p class="comment">选中表示允许所有的来源域。</p>
</td>
</tr>
</tbody>
<tbody v-show="isOn() && !websocketConfig.allowAllOrigins">
<tr>
<td>允许的来源域列表<em>Origin</em></td>
<td>
<div v-if="websocketConfig.allowedOrigins.length > 0">
<div class="ui label tiny" v-for="(origin, index) in websocketConfig.allowedOrigins">
{{origin}} <a href="" title="删除" @click.prevent="removeOrigin(index)"><i class="icon remove"></i></a>
</div>
<div class="ui divider"></div>
</div>
<button class="ui button tiny" type="button" @click.prevent="createOrigin()">+</button>
<p class="comment">只允许在列表中的来源域名访问Websocket服务。</p>
</td>
</tr>
</tbody>
<more-options-tbody @change="changeAdvancedVisible" v-show="isOn()"></more-options-tbody>
<tbody v-show="isOn() && advancedVisible">
<tr>
<td>是否传递请求来源域</td>
<td>
<div class="ui checkbox">
<input type="checkbox" v-model="websocketConfig.requestSameOrigin"/>
<label></label>
</div>
<p class="comment">选中表示把接收到的请求中的<span class="ui label tiny">Origin</span>字段传递到源站。</p>
</td>
</tr>
</tbody>
<tbody v-show="isOn() && advancedVisible && !websocketConfig.requestSameOrigin">
<tr>
<td>指定传递的来源域</td>
<td>
<input type="text" v-model="websocketConfig.requestOrigin" maxlength="200"/>
<p class="comment">指定向源站传递的<span class="ui label tiny">Origin</span>字段值。</p>
</td>
</tr>
</tbody>
<!-- TODO 这个选项暂时保留 -->
<tbody v-show="isOn() && false">
<tr>
<td>握手超时时间<em>Handshake</em></td>
<td>
<div class="ui fields inline">
<div class="ui field">
<input type="text" maxlength="10" v-model="handshakeTimeoutCountString" style="width:6em"/>
</div>
<div class="ui field">
</div>
</div>
<p class="comment">0表示使用默认的时间设置。</p>
</td>
</tr>
</tbody>
</table>
<div class="margin"></div>
</div>`
})

View File

@@ -39,9 +39,6 @@
.left-box.disabled {
opacity: 0.1;
}
.left-box.disabled:hover {
opacity: 1.0;
}
.left-box.tiny {
top: 10.5em;
}

File diff suppressed because one or more lines are too long

View File

@@ -52,10 +52,6 @@
opacity: 0.1;
}
.left-box.disabled:hover {
opacity: 1.0;
}
.left-box.tiny {
top: 10.5em;
}

View File

@@ -0,0 +1,11 @@
{$layout}
{$template "/left_menu"}
<div class="right-box">
{$template "../location_menu"}
{$template "../left_menu"}
<div class="right-box tiny">
<p class="ui message">此功能暂未开放,敬请期待。</p>
</div>
</div>

View File

@@ -1,5 +1,4 @@
{$layout}
{$template "/left_menu"}
<div class="right-box">

View File

@@ -1,5 +1,4 @@
{$layout}
{$template "/left_menu"}
<div class="right-box">

View File

@@ -0,0 +1,17 @@
{$layout}
{$template "/left_menu"}
<div class="right-box">
{$template "../location_menu"}
{$template "../left_menu"}
<div class="right-box tiny">
<form class="ui form" data-tea-action="$" data-tea-success="success">
<input type="hidden" name="webId" :value="webId"/>
<http-websocket-box :v-websocket-ref="websocketRef"
:v-websocket-config="websocketConfig"
:v-is-location="true"></http-websocket-box>
<submit-btn></submit-btn>
</form>
</div>
</div>

View File

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

View File

@@ -0,0 +1,6 @@
{$layout}
{$template "/left_menu"}
<div class="right-box">
<p class="ui message">此功能暂未开放,敬请期待。</p>
</div>

View File

@@ -0,0 +1,15 @@
{$layout "layout_popup"}
<h3>添加来源域</h3>
<form class="ui form" data-tea-success="success" data-tea-action="$">
<table class="ui table definition selectable">
<tr>
<td class="title">来源域 *</td>
<td>
<input type="text" name="origin" maxlength="200" placeholder="比如 example.com" ref="focus"/>
<p class="comment">允许客户端通过Websocket可以访问的域名可以使用星号通配符*来表示匹配一组域名比如www.example.com *.google.com</p>
</td>
</tr>
</table>
<submit-btn></submit-btn>
</form>

View File

@@ -0,0 +1,3 @@
Tea.context(function () {
this.success = NotifyPopup
})

View File

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