1.4.5.2
This commit is contained in:
89
EdgeAdmin/internal/plus/components.go
Normal file
89
EdgeAdmin/internal/plus/components.go
Normal file
@@ -0,0 +1,89 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
//go:build plus
|
||||
|
||||
package plus
|
||||
|
||||
type ComponentCode = string
|
||||
|
||||
const (
|
||||
ComponentCodeUser ComponentCode = "user"
|
||||
ComponentCodeScheduling ComponentCode = "scheduling"
|
||||
ComponentCodeMonitor ComponentCode = "monitor"
|
||||
ComponentCodeLog ComponentCode = "log"
|
||||
ComponentCodeReporter ComponentCode = "reporter"
|
||||
ComponentCodePlan ComponentCode = "plan"
|
||||
ComponentCodeFinance ComponentCode = "finance"
|
||||
ComponentCodeNS ComponentCode = "ns"
|
||||
ComponentCodeL2Node ComponentCode = "l2node"
|
||||
ComponentCodeComputing ComponentCode = "computing"
|
||||
ComponentCodeTicket ComponentCode = "ticket"
|
||||
ComponentCodeAntiDDoS ComponentCode = "antiDDoS"
|
||||
)
|
||||
|
||||
type ComponentDefinition struct {
|
||||
Name string `json:"name"`
|
||||
Code ComponentCode `json:"code"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
|
||||
func FindAllComponents() []*ComponentDefinition {
|
||||
return []*ComponentDefinition{
|
||||
{
|
||||
Name: "多租户",
|
||||
Code: ComponentCodeUser,
|
||||
},
|
||||
{
|
||||
Name: "智能调度",
|
||||
Code: ComponentCodeScheduling,
|
||||
},
|
||||
{
|
||||
Name: "监控",
|
||||
Code: ComponentCodeMonitor,
|
||||
},
|
||||
{
|
||||
Name: "日志",
|
||||
Code: ComponentCodeLog,
|
||||
},
|
||||
{
|
||||
Name: "区域监控",
|
||||
Code: ComponentCodeReporter,
|
||||
},
|
||||
{
|
||||
Name: "套餐",
|
||||
Code: ComponentCodePlan,
|
||||
},
|
||||
{
|
||||
Name: "财务",
|
||||
Code: ComponentCodeFinance,
|
||||
},
|
||||
{
|
||||
Name: "智能DNS",
|
||||
Code: ComponentCodeNS,
|
||||
},
|
||||
{
|
||||
Name: "L2节点",
|
||||
Code: ComponentCodeL2Node,
|
||||
},
|
||||
{
|
||||
Name: "边缘计算",
|
||||
Code: ComponentCodeComputing,
|
||||
},
|
||||
{
|
||||
Name: "工单系统",
|
||||
Code: ComponentCodeTicket,
|
||||
},
|
||||
{
|
||||
Name: "高防IP",
|
||||
Code: ComponentCodeAntiDDoS,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func CheckComponent(code string) bool {
|
||||
for _, c := range FindAllComponents() {
|
||||
if c.Code == code {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
6
EdgeAdmin/internal/plus/error.go
Normal file
6
EdgeAdmin/internal/plus/error.go
Normal file
@@ -0,0 +1,6 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
//go:build plus
|
||||
|
||||
package plus
|
||||
|
||||
var ErrString = ""
|
||||
51
EdgeAdmin/internal/plus/helper.go
Normal file
51
EdgeAdmin/internal/plus/helper.go
Normal file
@@ -0,0 +1,51 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
//go:build plus
|
||||
|
||||
package plus
|
||||
|
||||
import (
|
||||
teaconst "github.com/TeaOSLab/EdgeAdmin/internal/const"
|
||||
"github.com/iwind/TeaGo/actions"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func NewHelper(component ComponentCode) *Helper {
|
||||
return &Helper{component: component}
|
||||
}
|
||||
|
||||
func NewBasicHelper() *Helper {
|
||||
return NewHelper("")
|
||||
}
|
||||
|
||||
type Helper struct {
|
||||
component string
|
||||
}
|
||||
|
||||
func (this *Helper) BeforeAction(actionPtr actions.ActionWrapper) (goNext bool) {
|
||||
if !teaconst.IsPlus || !AllowComponent(this.component) {
|
||||
var writer = actionPtr.Object().ResponseWriter
|
||||
writer.WriteHeader(http.StatusForbidden)
|
||||
writer.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
_, _ = writer.Write([]byte(`<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>权限错误</title>
|
||||
<link rel="stylesheet" type="text/css" href="/css/semantic.min.css?v=bRafhK" media="all"/>
|
||||
<style type="text/css">
|
||||
div { max-width: 960px; margin: 4em auto; }
|
||||
h3 { font-weight: normal; }
|
||||
p { font-size: 14px; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div>
|
||||
<h3>权限错误</h3>
|
||||
<p>此操作需要商业版用户权限。如果你已经是商业用户,请重新访问 <a href="/settings/authority">商业版认证页</a> 以便激活权限。</p>
|
||||
</div>
|
||||
</body>
|
||||
</html>`))
|
||||
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
98
EdgeAdmin/internal/plus/utils.go
Normal file
98
EdgeAdmin/internal/plus/utils.go
Normal file
@@ -0,0 +1,98 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
//go:build plus
|
||||
|
||||
package plus
|
||||
|
||||
import (
|
||||
teaconst "github.com/TeaOSLab/EdgeAdmin/internal/const"
|
||||
plusutils "github.com/TeaOSLab/EdgePlus/pkg/utils"
|
||||
"github.com/iwind/TeaGo/lists"
|
||||
"net"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// 已经开通的组件
|
||||
var plusLocker = sync.Mutex{}
|
||||
var plusComponents = []string{}
|
||||
|
||||
var PlusEdition = ""
|
||||
|
||||
// UpdateComponents 修改开通的组件
|
||||
func UpdateComponents(edition string, components []string) {
|
||||
plusLocker.Lock()
|
||||
PlusEdition = edition
|
||||
plusComponents = components
|
||||
plusLocker.Unlock()
|
||||
}
|
||||
|
||||
// AllowComponent 检查是否允许使用某个组件
|
||||
func AllowComponent(component ComponentCode) bool {
|
||||
if !teaconst.IsPlus {
|
||||
return false
|
||||
}
|
||||
|
||||
// 允许不指定组件,表示只要是plus用户就可以
|
||||
if len(component) == 0 {
|
||||
return true
|
||||
}
|
||||
|
||||
plusLocker.Lock()
|
||||
defer plusLocker.Unlock()
|
||||
|
||||
if len(plusComponents) == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
for _, c := range plusComponents {
|
||||
if c == "*" || c == component {
|
||||
// 如果不是最新版本的授权,则忽略一些权限
|
||||
if c == "*" && (len(PlusEdition) == 0 || plusutils.CompareEdition(PlusEdition, plusutils.EditionPro) < 0) && component == ComponentCodeAntiDDoS {
|
||||
continue
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// ValidateMac 校验MAC地址
|
||||
func ValidateMac(mac string) bool {
|
||||
if len(mac) == 0 || mac == "*" {
|
||||
return true
|
||||
}
|
||||
|
||||
for _, addr := range FindAllMacAddresses() {
|
||||
if addr == mac {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// FindAllMacAddresses 获取MAC地址
|
||||
func FindAllMacAddresses() []string {
|
||||
var macAddresses = []string{}
|
||||
interfaces, _ := net.Interfaces()
|
||||
for _, i := range interfaces {
|
||||
var addr = i.HardwareAddr.String()
|
||||
if len(addr) == 0 {
|
||||
continue
|
||||
}
|
||||
if lists.ContainsString(macAddresses, addr) {
|
||||
continue
|
||||
}
|
||||
if i.Flags&net.FlagLoopback == 1 {
|
||||
continue
|
||||
}
|
||||
if i.Flags&net.FlagUp == 0 {
|
||||
continue
|
||||
}
|
||||
ipAddrs, _ := i.Addrs()
|
||||
if len(ipAddrs) == 0 {
|
||||
continue
|
||||
}
|
||||
macAddresses = append(macAddresses, addr)
|
||||
}
|
||||
return macAddresses
|
||||
}
|
||||
Reference in New Issue
Block a user