util

package
v1.18.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 20, 2026 License: Apache-2.0 Imports: 48 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// IPSignupLimiter: max 3 signups per hour per IP address.
	IPSignupLimiter = NewSignupRateLimiter(3, 1*time.Hour)

	// DomainSignupLimiter: max 10 signups per hour per email domain.
	DomainSignupLimiter = NewSignupRateLimiter(10, 1*time.Hour)
)

Global signup rate limiters.

View Source
var (
	ReWhiteSpace        *regexp.Regexp
	ReFieldWhiteList    *regexp.Regexp
	ReUserName          *regexp.Regexp
	ReUserNameWithEmail *regexp.Regexp
)
View Source
var (
	Version      = "v1.0.2"
	CommitId     = "80e0b132b78c33ee01c599c83c938860d8eb5767"
	CommitOffset = 3
)

Functions

func AppendWebConfigCookie

func AppendWebConfigCookie(ctx *context.Context) error

func AuthzRuleToSlice

func AuthzRuleToSlice(rule AuthzRule) []string

func BoolToString

func BoolToString(b bool) string

func CamelToSnakeCase

func CamelToSnakeCase(camel string) string

CamelToSnakeCase This function transform camelcase in snakecase LoremIpsum in lorem_ipsum

func DeleteVal

func DeleteVal(values []string, val string) []string

func FileExist

func FileExist(path string) bool

func FilterField

func FilterField(field string) bool

func FilterQuery

func FilterQuery(urlString string, blackList []string) string

func GenerateClientId

func GenerateClientId() string

func GenerateClientSecret

func GenerateClientSecret() string

func GenerateId

func GenerateId() string

func GeneratePasswordSalt

func GeneratePasswordSalt() string

func GenerateSimpleTimeId

func GenerateSimpleTimeId() string

func GenerateTimeId

func GenerateTimeId() string

func GenerateTwoUniqueRandomStrings

func GenerateTwoUniqueRandomStrings() (string, string, error)

func GenerateVerboseReason

func GenerateVerboseReason(ruleId string, expressionReason string, customReason string) string

GenerateVerboseReason creates a detailed reason message for verbose mode

func GetBaseDomain

func GetBaseDomain(domain string) (string, error)

func GetCertExpireTime

func GetCertExpireTime(s string) (string, error)

func GetClientIp

func GetClientIp(r *http.Request) string

func GetClientIpFromRequest

func GetClientIpFromRequest(req *http.Request) string

func GetCountryCode

func GetCountryCode(prefix string, phone string) (string, error)

func GetCurrentTime

func GetCurrentTime() string

func GetCurrentTimeEx

func GetCurrentTimeEx(timestamp string) string

func GetCurrentUnixTime

func GetCurrentUnixTime() string

func GetE164Number

func GetE164Number(phone string, countryCode string) (string, bool)

func GetEndPoint

func GetEndPoint(endpoint string) string

GetEndPoint remove scheme from url

func GetHmacSha1

func GetHmacSha1(keyStr, value string) string

func GetHmacSha256

func GetHmacSha256(key string, data string) string

func GetHostname

func GetHostname() string

func GetId

func GetId(owner, name string) string

func GetMaskedEmail

func GetMaskedEmail(email string) string

func GetMaskedPhone

func GetMaskedPhone(phone string) string

func GetMd5Hash

func GetMd5Hash(text string) string

func GetOwnerAndNameAndOtherFromId

func GetOwnerAndNameAndOtherFromId(id string) (string, string, string)

func GetOwnerAndNameFromIdNoCheck

func GetOwnerAndNameFromIdNoCheck(id string) (string, string)

func GetOwnerAndNameFromIdWithError

func GetOwnerAndNameFromIdWithError(id string) (string, string, error)

func GetOwnerFromId

func GetOwnerFromId(id string) string

func GetPath

func GetPath(path string) string

func GetRandomName

func GetRandomName() string

func GetSeperatedPhone

func GetSeperatedPhone(phone string) string

func GetSessionId

func GetSessionId(owner, name, application string) string

func GetSharedOrgFromApp

func GetSharedOrgFromApp(rawName string) (name string, organization string)

func GetUnobfuscatedPassword

func GetUnobfuscatedPassword(passwordObfuscatorType string, passwordObfuscatorKey string, passwordCipher string) (string, error)

func GetUploadXlsxPath

func GetUploadXlsxPath(fileId string) string

func GetUrlHost

func GetUrlHost(urlString string) string

func GetUrlPath

func GetUrlPath(urlString string) string

func GetUsernameFromEmail

func GetUsernameFromEmail(email string) string

func GetValueFromDataSourceName

func GetValueFromDataSourceName(key string, dataSourceName string) string

func HasString

func HasString(strs []string, str string) bool

HasString reports if slice has input string.

func HasTagInSlice

func HasTagInSlice(slice []string, userTag string) bool

HasTagInSlice checks if a comma-separated tag string contains any tag that matches the slice. For example, if userTag is "default-policy,project-admin" and slice is ["default-policy", "project-admin"], it will return true because "default-policy" and "project-admin" are both in the slice.

func HaveIntersection

func HaveIntersection(arr1 []string, arr2 []string) bool

func InSlice

func InSlice(slice []string, elem string) bool

func IsChinese

func IsChinese(str string) bool

func IsEmailValid

func IsEmailValid(email string) bool

func IsHostIntranet

func IsHostIntranet(ip string) bool

func IsInternetIp

func IsInternetIp(ip string) bool

func IsIntranetIp

func IsIntranetIp(ip string) bool

func IsInvitationCodeMatch

func IsInvitationCodeMatch(pattern string, invitationCode string) (bool, error)

func IsPhoneAllowInRegin

func IsPhoneAllowInRegin(countryCode string, allowRegions []string) bool

func IsPhoneValid

func IsPhoneValid(phone string, countryCode string) bool

func IsRegexp

func IsRegexp(s string) (bool, error)

func IsStringsEmpty

func IsStringsEmpty(strs ...string) bool

func IsTokenExpired

func IsTokenExpired(createdTime string, expiresIn int) (bool, string)

func IsValidOrigin

func IsValidOrigin(origin string) (bool, error)

func JsonToStruct

func JsonToStruct(data string, v interface{}) error

func ListFiles

func ListFiles(path string) []string

func LoadCACertPool

func LoadCACertPool(cert string) (*x509.CertPool, error)

LoadCACertPool loads a CA certificate pool from either a file path or a PEM-encoded string. The cert parameter can be: - A file path to a PEM-encoded certificate file - A PEM-encoded certificate string (starts with "-----BEGIN") Returns nil pool and nil error if the cert is empty.

func LogInfo

func LogInfo(ctx *context.Context, f string, v ...interface{})

func LogWarning

func LogWarning(ctx *context.Context, f string, v ...interface{})

func ParseBool

func ParseBool(s string) bool

func ParseFloat

func ParseFloat(s string) float64

func ParseIdToString

func ParseIdToString(input interface{}) (string, error)

func ParseInt

func ParseInt(s string) int

func ParseIntWithError

func ParseIntWithError(s string) (int, error)

func PingUrl

func PingUrl(url string) (bool, string)

func ReadStringFromPath

func ReadStringFromPath(path string) string

func RemoveExt

func RemoveExt(filename string) string

func ReplaceVal

func ReplaceVal(values []string, oldVal string, newVal string) []string

func ResolveDomainToIp

func ResolveDomainToIp(domain string) string

func ReturnAnyNotEmpty

func ReturnAnyNotEmpty(strs ...string) string

func SafeGoroutine

func SafeGoroutine(fn func())

func SnakeString

func SnakeString(s string) string

SnakeString transform XxYy to xx_yy

func SnakeToCamel

func SnakeToCamel(snake string) string

func SpaceToCamel

func SpaceToCamel(name string) string

func StopOldInstance

func StopOldInstance(port int) error

func String2Time

func String2Time(timestamp string) time.Time

func StringToInterfaceArray

func StringToInterfaceArray(array []string) []interface{}

func StringToInterfaceArray2d

func StringToInterfaceArray2d(arrays [][]string) [][]interface{}

func StructToJson

func StructToJson(v interface{}) string

func StructToJsonFormatted

func StructToJsonFormatted(v interface{}) string

func Time2String

func Time2String(timestamp time.Time) string

func TryJsonToAnonymousStruct

func TryJsonToAnonymousStruct(j string) (interface{}, error)

func UrlJoin

func UrlJoin(base string, path string) string

func WriteStringToPath

func WriteStringToPath(s string, path string)

Types

type AuthzRule added in v1.14.21

type AuthzRule = authzstore.AuthzRule

AuthzRule is the canonical Hanzo name for a policy row. It aliases the row type owned by the object/authzstore package so callers in util and controllers can manipulate policies without importing the adapter implementation directly.

func MatrixToAuthzRules

func MatrixToAuthzRules(Ptype string, policies [][]string) []*AuthzRule

type SignupRateLimiter

type SignupRateLimiter struct {
	// contains filtered or unexported fields
}

SignupRateLimiter enforces per-key sliding-window rate limits for signups.

func NewSignupRateLimiter

func NewSignupRateLimiter(limit int, window time.Duration) *SignupRateLimiter

NewSignupRateLimiter creates a rate limiter that allows at most limit events per window duration for each key.

func (*SignupRateLimiter) Allow

func (r *SignupRateLimiter) Allow(key string) error

Allow checks whether the given key is within the rate limit. Returns nil if allowed, or an error describing the limit.

func (*SignupRateLimiter) Cleanup

func (r *SignupRateLimiter) Cleanup()

Cleanup removes stale entries older than the window. Call periodically to prevent memory growth in long-running processes.

type Stack

type Stack struct {
	// contains filtered or unexported fields
}

Stack is a stack data structure implemented using a slice

func NewStack

func NewStack() *Stack

NewStack creates a new stack

func (*Stack) IsEmpty

func (s *Stack) IsEmpty() bool

IsEmpty checks if the stack is empty

func (*Stack) Peek

func (s *Stack) Peek() interface{}

Peek returns the last item from the stack without removing it

func (*Stack) Pop

func (s *Stack) Pop() (interface{}, bool)

Pop removes and returns the last item from the stack

func (*Stack) Push

func (s *Stack) Push(item interface{})

Push adds an item to the stack

func (*Stack) Size

func (s *Stack) Size() int

Size returns the number of items in the stack

type SystemInfo

type SystemInfo struct {
	CpuUsage    []float64 `json:"cpuUsage"`
	MemoryUsed  uint64    `json:"memoryUsed"`
	MemoryTotal uint64    `json:"memoryTotal"`
}

func GetSystemInfo

func GetSystemInfo() (*SystemInfo, error)

type VersionInfo

type VersionInfo struct {
	Version      string `json:"version"`
	CommitId     string `json:"commitId"`
	CommitOffset int    `json:"commitOffset"`
}

func GetBuiltInVersionInfo

func GetBuiltInVersionInfo() *VersionInfo

func GetVersionInfo

func GetVersionInfo() (*VersionInfo, error)

GetVersionInfo get git current commit and repo release version Falls back to built-in values when .git is not available (e.g., in Docker)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL