mirror of
https://github.com/yhl452493373/frps-panel.git
synced 2026-04-04 14:27:00 +08:00
update readme;
optimize logic code;
This commit is contained in:
@@ -290,7 +290,7 @@ func (c *HandleController) MakeQueryTokensFunc() func(context *gin.Context) {
|
||||
func (c *HandleController) MakeAddTokenFunc() func(context *gin.Context) {
|
||||
return func(context *gin.Context) {
|
||||
info := TokenInfo{
|
||||
Status: true,
|
||||
Enable: true,
|
||||
}
|
||||
response := OperationResponse{
|
||||
Success: true,
|
||||
@@ -315,7 +315,7 @@ func (c *HandleController) MakeAddTokenFunc() func(context *gin.Context) {
|
||||
}
|
||||
|
||||
info.Comment = cleanString(info.Comment)
|
||||
info.Ports = cleanStrings(info.Ports)
|
||||
info.Ports = cleanPorts(info.Ports)
|
||||
info.Domains = cleanStrings(info.Domains)
|
||||
info.Subdomains = cleanStrings(info.Subdomains)
|
||||
|
||||
@@ -373,7 +373,7 @@ func (c *HandleController) MakeUpdateTokensFunc() func(context *gin.Context) {
|
||||
}
|
||||
|
||||
after.Comment = cleanString(after.Comment)
|
||||
after.Ports = cleanStrings(after.Ports)
|
||||
after.Ports = cleanPorts(after.Ports)
|
||||
after.Domains = cleanStrings(after.Domains)
|
||||
after.Subdomains = cleanStrings(after.Subdomains)
|
||||
|
||||
@@ -467,7 +467,7 @@ func (c *HandleController) MakeDisableTokensFunc() func(context *gin.Context) {
|
||||
|
||||
for _, user := range disable.Users {
|
||||
token := c.Tokens[user.User]
|
||||
token.Status = false
|
||||
token.Enable = false
|
||||
c.Tokens[user.User] = token
|
||||
}
|
||||
|
||||
@@ -515,7 +515,7 @@ func (c *HandleController) MakeEnableTokensFunc() func(context *gin.Context) {
|
||||
|
||||
for _, user := range enable.Users {
|
||||
token := c.Tokens[user.User]
|
||||
token.Status = true
|
||||
token.Enable = true
|
||||
c.Tokens[user.User] = token
|
||||
}
|
||||
|
||||
|
||||
@@ -50,7 +50,7 @@ func (c *HandleController) JudgeToken(user string, token string) plugin.Response
|
||||
res.Reject = true
|
||||
res.RejectReason = "user or meta token can not be empty"
|
||||
} else if info, exist := c.Tokens[user]; exist {
|
||||
if !info.Status {
|
||||
if !info.Enable {
|
||||
res.Reject = true
|
||||
res.RejectReason = fmt.Sprintf("user [%s] is disabled", user)
|
||||
} else {
|
||||
@@ -77,7 +77,6 @@ func (c *HandleController) JudgePort(content *plugin.NewProxyContent) plugin.Res
|
||||
"tcp", "tcpmux", "udp", "http", "https",
|
||||
}
|
||||
proxyType := content.ProxyType
|
||||
|
||||
if stringContains(proxyType, supportProxyTypes) {
|
||||
log.Printf("proxy type [%v] not support, plugin do nothing", proxyType)
|
||||
res.Unchange = true
|
||||
@@ -94,36 +93,45 @@ func (c *HandleController) JudgePort(content *plugin.NewProxyContent) plugin.Res
|
||||
portAllowed = false
|
||||
if token, exist := c.Tokens[user]; exist {
|
||||
for _, port := range token.Ports {
|
||||
if strings.Contains(port, "-") {
|
||||
allowedRanges := strings.Split(port, "-")
|
||||
if len(allowedRanges) != 2 {
|
||||
portErr = fmt.Errorf("user [%v] port range [%v] format error", user, port)
|
||||
break
|
||||
}
|
||||
start, err := strconv.Atoi(trimString(allowedRanges[0]))
|
||||
if err != nil {
|
||||
portErr = fmt.Errorf("user [%v] port rang [%v] start port [%v] is not a number", user, port, allowedRanges[0])
|
||||
break
|
||||
}
|
||||
end, err := strconv.Atoi(trimString(allowedRanges[1]))
|
||||
if err != nil {
|
||||
portErr = fmt.Errorf("user [%v] port rang [%v] end port [%v] is not a number", user, port, allowedRanges[0])
|
||||
break
|
||||
}
|
||||
if max(userPort, start) == userPort && min(userPort, end) == userPort {
|
||||
portAllowed = true
|
||||
break
|
||||
if str, ok := port.(string); ok {
|
||||
if strings.Contains(str, "-") {
|
||||
allowedRanges := strings.Split(str, "-")
|
||||
if len(allowedRanges) != 2 {
|
||||
portErr = fmt.Errorf("user [%v] port range [%v] format error", user, port)
|
||||
break
|
||||
}
|
||||
start, err := strconv.Atoi(trimString(allowedRanges[0]))
|
||||
if err != nil {
|
||||
portErr = fmt.Errorf("user [%v] port rang [%v] start port [%v] is not a number", user, port, allowedRanges[0])
|
||||
break
|
||||
}
|
||||
end, err := strconv.Atoi(trimString(allowedRanges[1]))
|
||||
if err != nil {
|
||||
portErr = fmt.Errorf("user [%v] port rang [%v] end port [%v] is not a number", user, port, allowedRanges[0])
|
||||
break
|
||||
}
|
||||
if max(userPort, start) == userPort && min(userPort, end) == userPort {
|
||||
portAllowed = true
|
||||
break
|
||||
}
|
||||
} else {
|
||||
allowed, err := strconv.Atoi(str)
|
||||
if err != nil {
|
||||
portErr = fmt.Errorf("user [%v] allowed port [%v] is not a number", user, port)
|
||||
}
|
||||
if allowed == userPort {
|
||||
portAllowed = true
|
||||
break
|
||||
}
|
||||
}
|
||||
} else {
|
||||
allowed, err := strconv.Atoi(port)
|
||||
if err != nil {
|
||||
portErr = fmt.Errorf("user [%v] allowed port [%v] is not a number", user, port)
|
||||
}
|
||||
allowed := port
|
||||
if allowed == userPort {
|
||||
portAllowed = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
} else {
|
||||
portAllowed = true
|
||||
|
||||
@@ -121,13 +121,15 @@ func (c *HandleController) verifyToken(token TokenInfo, operate int) OperationRe
|
||||
|
||||
if validatePorts {
|
||||
for _, port := range token.Ports {
|
||||
trimmedPort := trimString(port)
|
||||
if trimmedPort != "" && !portsFormatSingle.MatchString(trimmedPort) && !portsFormatRange.MatchString(trimmedPort) {
|
||||
response.Success = false
|
||||
response.Code = PortsFormatError
|
||||
response.Message = fmt.Sprintf("operate failed, ports [%v] format error", token.Ports)
|
||||
log.Printf(response.Message)
|
||||
return response
|
||||
if str, ok := port.(string); ok {
|
||||
trimmedPort := trimString(str)
|
||||
if trimmedPort != "" && !portsFormatSingle.MatchString(trimmedPort) && !portsFormatRange.MatchString(trimmedPort) {
|
||||
response.Success = false
|
||||
response.Code = PortsFormatError
|
||||
response.Message = fmt.Sprintf("operate failed, ports [%v] format error", token.Ports)
|
||||
log.Printf(response.Message)
|
||||
return response
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -161,6 +163,19 @@ func (c *HandleController) verifyToken(token TokenInfo, operate int) OperationRe
|
||||
return response
|
||||
}
|
||||
|
||||
func cleanPorts(ports []any) []any {
|
||||
cleanedPorts := make([]any, len(ports))
|
||||
for i, port := range ports {
|
||||
if str, ok := port.(string); ok {
|
||||
cleanedPorts[i] = cleanString(str)
|
||||
} else {
|
||||
//float64, for JSON numbers
|
||||
cleanedPorts[i] = int(port.(float64))
|
||||
}
|
||||
}
|
||||
return cleanedPorts
|
||||
}
|
||||
|
||||
func cleanStrings(originalStrings []string) []string {
|
||||
cleanedStrings := make([]string, len(originalStrings))
|
||||
for i, str := range originalStrings {
|
||||
@@ -170,7 +185,7 @@ func cleanStrings(originalStrings []string) []string {
|
||||
}
|
||||
|
||||
func cleanString(originalString string) string {
|
||||
return trimAllSpace.ReplaceAllString(originalString, "")
|
||||
return trimString(originalString)
|
||||
}
|
||||
|
||||
func stringContains(element string, data []string) bool {
|
||||
@@ -194,7 +209,9 @@ func (c *HandleController) saveToken() error {
|
||||
log.Printf("error to crate file %v: %v", c.TokensFile, err)
|
||||
}
|
||||
|
||||
if err = toml.NewEncoder(tokenFile).Encode(tokensList(c.Tokens)); err != nil {
|
||||
encoder := toml.NewEncoder(tokenFile)
|
||||
encoder.Indent = " "
|
||||
if err = encoder.Encode(tokensList(c.Tokens)); err != nil {
|
||||
log.Printf("error to encode tokens: %v", err)
|
||||
}
|
||||
if err = tokenFile.Close(); err != nil {
|
||||
|
||||
@@ -84,10 +84,10 @@ type TokenInfo struct {
|
||||
User string `toml:"user" json:"user" form:"user"`
|
||||
Token string `toml:"token" json:"token" form:"token"`
|
||||
Comment string `toml:"comment" json:"comment" form:"comment"`
|
||||
Ports []string `toml:"ports" json:"ports" from:"ports"`
|
||||
Ports []any `toml:"ports" json:"ports" from:"ports"`
|
||||
Domains []string `toml:"domains" json:"domains" from:"domains"`
|
||||
Subdomains []string `toml:"subdomains" json:"subdomains" from:"subdomains"`
|
||||
Status bool `toml:"status" json:"status" form:"status"`
|
||||
Enable bool `toml:"enable" json:"enable" form:"enable"`
|
||||
}
|
||||
|
||||
type TokenResponse struct {
|
||||
|
||||
Reference in New Issue
Block a user