Files

95 lines
2.2 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package protocol
import (
"strings"
"unicode/utf8"
)
func isEndpointIDChar(c rune) bool {
return (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '_' || c == '.' || c == '-'
}
func isMessageIDChar(c rune) bool {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_' || c == '.' || c == '-'
}
// ValidEndpointID 校验端编号或群编号(小写字母、数字、_ . -,1–64)。
func ValidEndpointID(id string) bool {
n := len(id)
if n < MinIDLen || n > MaxIDLen {
return false
}
for _, c := range id {
if !isEndpointIDChar(c) {
return false
}
}
return true
}
// ValidMessageID 校验消息号(字母数字 _ . -,区分大小写,1–64)。
func ValidMessageID(id string) bool {
n := len(id)
if n < MinIDLen || n > MaxIDLen {
return false
}
for _, c := range id {
if !isMessageIDChar(c) {
return false
}
}
return true
}
// ValidLoginPassword 校验登录密码:8–128 字符,且不能以 nst_ 开头。空串表示由服务器生成,视为合法。
func ValidLoginPassword(pw string) bool {
if pw == "" {
return true
}
if strings.HasPrefix(pw, SessionTokenPrefix) {
return false
}
n := utf8.RuneCountInString(pw)
return n >= MinLoginPasswordLen && n <= MaxLoginPasswordLen
}
// LoginPasswordForbiddenPrefix 报告密码是否因 nst_ 前缀非法。
func LoginPasswordForbiddenPrefix(pw string) bool {
return strings.HasPrefix(pw, SessionTokenPrefix)
}
// ValidTalkPassword 校验对话密码:空表示清除/不设;否则 4–64 字符。
func ValidTalkPassword(pw string) bool {
if pw == "" {
return true
}
n := utf8.RuneCountInString(pw)
return n >= MinTalkPasswordLen && n <= MaxTalkPasswordLen
}
// ValidName 校验名称:最多 64 个 Unicode 字符。
func ValidName(name string) bool {
return utf8.RuneCountInString(name) <= MaxNameChars
}
func requireRID(rid string) error {
if rid == "" {
return badRequest("missing rid")
}
return nil
}
func requireVersion(v int) error {
if v != Version {
return badRequest("unsupported version")
}
return nil
}
func requireType(got, want string) error {
if got != want {
return badRequest("wrong type")
}
return nil
}