diff --git a/internal/web/actions/default/servers/server/settings/locations/locationutils/location_helper.go b/internal/web/actions/default/servers/server/settings/locations/locationutils/location_helper.go index a7ebad87..94074a40 100644 --- a/internal/web/actions/default/servers/server/settings/locations/locationutils/location_helper.go +++ b/internal/web/actions/default/servers/server/settings/locations/locationutils/location_helper.go @@ -52,9 +52,7 @@ func (this *LocationHelper) BeforeAction(actionPtr actions.ActionWrapper) { } func (this *LocationHelper) createMenus(serverIdString string, locationIdString string, secondMenuItem string, locationConfig *serverconfigs.HTTPLocationConfig) []maps.Map { - menuItems := []maps.Map{ - - } + menuItems := []maps.Map{} menuItems = append(menuItems, maps.Map{ "name": "基本信息", "url": "/servers/server/settings/locations/location?serverId=" + serverIdString + "&locationId=" + locationIdString, @@ -163,6 +161,19 @@ func (this *LocationHelper) createMenus(serverIdString string, locationIdString "isOn": locationConfig != nil && locationConfig.Web != nil && locationConfig.Web.FastcgiRef != nil && locationConfig.Web.FastcgiRef.IsPrior, }) + menuItems = append(menuItems, maps.Map{ + "name": "-", + "url": "", + "isActive": false, + }) + + menuItems = append(menuItems, maps.Map{ + "name": "访客IP地址", + "url": "/servers/server/settings/locations/remoteAddr?serverId=" + serverIdString + "&locationId=" + locationIdString, + "isActive": secondMenuItem == "remoteAddr", + "isOn": locationConfig != nil && locationConfig.Web != nil && locationConfig.Web.RemoteAddr != nil && locationConfig.Web.RemoteAddr.IsOn, + }) + return menuItems } diff --git a/internal/web/actions/default/servers/server/settings/locations/remoteAddr/index.go b/internal/web/actions/default/servers/server/settings/locations/remoteAddr/index.go new file mode 100644 index 00000000..4b16cb2b --- /dev/null +++ b/internal/web/actions/default/servers/server/settings/locations/remoteAddr/index.go @@ -0,0 +1,60 @@ +// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved. + +package remoteAddr + +import ( + "encoding/json" + "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" + "github.com/TeaOSLab/EdgeCommon/pkg/rpc/dao" + "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 := dao.SharedHTTPWebDAO.FindWebConfigWithLocationId(this.AdminContext(), params.LocationId) + if err != nil { + this.ErrorPage(err) + return + } + + this.Data["webId"] = webConfig.Id + this.Data["remoteAddrConfig"] = webConfig.RemoteAddr + + this.Show() +} + +func (this *IndexAction) RunPost(params struct { + WebId int64 + RemoteAddrJSON []byte + + Must *actions.Must + CSRF *actionutils.CSRF +}) { + var remoteAddrConfig = &serverconfigs.HTTPRemoteAddrConfig{} + err := json.Unmarshal(params.RemoteAddrJSON, remoteAddrConfig) + if err != nil { + this.Fail("参数校验失败:" + err.Error()) + } + + _, err = this.RPC().HTTPWebRPC().UpdateHTTPWebRemoteAddr(this.AdminContext(), &pb.UpdateHTTPWebRemoteAddrRequest{ + WebId: params.WebId, + RemoteAddrJSON: params.RemoteAddrJSON, + }) + if err != nil { + this.ErrorPage(err) + return + } + + this.Success() +} diff --git a/internal/web/actions/default/servers/server/settings/locations/remoteAddr/init.go b/internal/web/actions/default/servers/server/settings/locations/remoteAddr/init.go new file mode 100644 index 00000000..700611b1 --- /dev/null +++ b/internal/web/actions/default/servers/server/settings/locations/remoteAddr/init.go @@ -0,0 +1,22 @@ +package remoteAddr + +import ( + "github.com/TeaOSLab/EdgeAdmin/internal/configloaders" + "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(configloaders.AdminModuleCodeServer)). + Helper(locationutils.NewLocationHelper()). + Helper(serverutils.NewServerHelper()). + Data("tinyMenuItem", "remoteAddr"). + Prefix("/servers/server/settings/locations/remoteAddr"). + GetPost("", new(IndexAction)). + EndAll() + }) +} diff --git a/internal/web/actions/default/servers/server/settings/remoteAddr/index.go b/internal/web/actions/default/servers/server/settings/remoteAddr/index.go new file mode 100644 index 00000000..85b4ce90 --- /dev/null +++ b/internal/web/actions/default/servers/server/settings/remoteAddr/index.go @@ -0,0 +1,61 @@ +// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved. + +package remoteAddr + +import ( + "encoding/json" + "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" + "github.com/TeaOSLab/EdgeCommon/pkg/rpc/dao" + "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() { + this.Nav("", "setting", "index") + this.SecondMenu("remoteAddr") +} + +func (this *IndexAction) RunGet(params struct { + ServerId int64 +}) { + webConfig, err := dao.SharedHTTPWebDAO.FindWebConfigWithServerId(this.AdminContext(), params.ServerId) + if err != nil { + this.ErrorPage(err) + return + } + + this.Data["webId"] = webConfig.Id + this.Data["remoteAddrConfig"] = webConfig.RemoteAddr + + this.Show() +} + +func (this *IndexAction) RunPost(params struct { + WebId int64 + RemoteAddrJSON []byte + + Must *actions.Must + CSRF *actionutils.CSRF +}) { + var remoteAddrConfig = &serverconfigs.HTTPRemoteAddrConfig{} + err := json.Unmarshal(params.RemoteAddrJSON, remoteAddrConfig) + if err != nil { + this.Fail("参数校验失败:" + err.Error()) + } + + _, err = this.RPC().HTTPWebRPC().UpdateHTTPWebRemoteAddr(this.AdminContext(), &pb.UpdateHTTPWebRemoteAddrRequest{ + WebId: params.WebId, + RemoteAddrJSON: params.RemoteAddrJSON, + }) + if err != nil { + this.ErrorPage(err) + return + } + + this.Success() +} diff --git a/internal/web/actions/default/servers/server/settings/remoteAddr/init.go b/internal/web/actions/default/servers/server/settings/remoteAddr/init.go new file mode 100644 index 00000000..1d091394 --- /dev/null +++ b/internal/web/actions/default/servers/server/settings/remoteAddr/init.go @@ -0,0 +1,19 @@ +package remoteAddr + +import ( + "github.com/TeaOSLab/EdgeAdmin/internal/configloaders" + "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(configloaders.AdminModuleCodeServer)). + Helper(serverutils.NewServerHelper()). + Prefix("/servers/server/settings/remoteAddr"). + GetPost("", new(IndexAction)). + EndAll() + }) +} diff --git a/internal/web/actions/default/servers/serverutils/server_helper.go b/internal/web/actions/default/servers/serverutils/server_helper.go index 4d0c529f..8bee336d 100644 --- a/internal/web/actions/default/servers/serverutils/server_helper.go +++ b/internal/web/actions/default/servers/serverutils/server_helper.go @@ -328,12 +328,26 @@ func (this *ServerHelper) createSettingsMenu(secondMenuItem string, serverIdStri "isActive": secondMenuItem == "webp", "isOn": serverConfig.Web != nil && serverConfig.Web.WebP != nil && serverConfig.Web.WebP.IsOn, }) + menuItems = append(menuItems, maps.Map{ "name": "Fastcgi", "url": "/servers/server/settings/fastcgi?serverId=" + serverIdString, "isActive": secondMenuItem == "fastcgi", "isOn": serverConfig.Web != nil && serverConfig.Web.FastcgiRef != nil && serverConfig.Web.FastcgiRef.IsOn, }) + + menuItems = append(menuItems, maps.Map{ + "name": "-", + "url": "", + "isActive": false, + }) + + menuItems = append(menuItems, maps.Map{ + "name": "访客IP地址", + "url": "/servers/server/settings/remoteAddr?serverId=" + serverIdString, + "isActive": secondMenuItem == "remoteAddr", + "isOn": serverConfig.Web != nil && serverConfig.Web.RemoteAddr != nil && serverConfig.Web.RemoteAddr.IsOn, + }) } else if serverConfig.IsTCPFamily() { menuItems = append(menuItems, maps.Map{ "name": "TCP", diff --git a/internal/web/import.go b/internal/web/import.go index 05816170..b744db2d 100644 --- a/internal/web/import.go +++ b/internal/web/import.go @@ -77,6 +77,7 @@ import ( _ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/locations/http" _ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/locations/location" _ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/locations/pages" + _ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/locations/remoteAddr" _ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/locations/reverseProxy" _ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/locations/rewrite" _ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/locations/stat" @@ -87,6 +88,7 @@ import ( _ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/origins" _ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/pages" _ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/redirects" + _ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/remoteAddr" _ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/reverseProxy" _ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/rewrite" _ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/serverNames" diff --git a/web/public/js/components/server/http-remote-addr-config-box.js b/web/public/js/components/server/http-remote-addr-config-box.js new file mode 100644 index 00000000..ed4e625b --- /dev/null +++ b/web/public/js/components/server/http-remote-addr-config-box.js @@ -0,0 +1,49 @@ +Vue.component("http-remote-addr-config-box", { + props: ["v-remote-addr-config", "v-is-location"], + data: function () { + let config = this.vRemoteAddrConfig + if (config == null) { + config = { + isPrior: false, + isOn: false, + value: "${remoteAddr}" + } + } + return { + config: config + } + }, + methods: { + isOn: function () { + return (!this.vIsLocation || this.config.isPrior) && this.config.isOn + } + }, + template: `
| 是否启用 | +
+
+
+
+
+ 选中后表示使用自定义的请求变量获取客户端IP。 + |
+
| 请求变量 | +
+
+ 通过此变量获取用户的IP地址。具体可用的请求变量列表可参考官方网站文档。 + |
+