2021-10-29 17:58:14 +00:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2023-01-28 04:10:59 +00:00
|
|
|
"bytes"
|
2022-02-02 04:39:57 +00:00
|
|
|
"encoding/base64"
|
2022-06-02 15:59:22 +00:00
|
|
|
"encoding/json"
|
2021-12-17 01:33:01 +00:00
|
|
|
"errors"
|
2021-11-08 14:24:34 +00:00
|
|
|
"fmt"
|
2023-02-07 21:20:49 +00:00
|
|
|
"golang.org/x/time/rate"
|
2022-01-23 05:54:18 +00:00
|
|
|
"io"
|
2021-10-29 17:58:14 +00:00
|
|
|
"math/rand"
|
2022-10-07 21:16:20 +00:00
|
|
|
"net/netip"
|
2021-10-29 17:58:14 +00:00
|
|
|
"os"
|
2022-01-06 00:04:56 +00:00
|
|
|
"regexp"
|
|
|
|
"strconv"
|
2021-12-17 01:33:01 +00:00
|
|
|
"strings"
|
2021-12-07 21:03:01 +00:00
|
|
|
"sync"
|
2021-10-29 17:58:14 +00:00
|
|
|
"time"
|
2022-10-05 20:42:07 +00:00
|
|
|
|
|
|
|
"github.com/gabriel-vasile/mimetype"
|
|
|
|
"golang.org/x/term"
|
2021-10-29 17:58:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
randomStringCharset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-01-06 13:45:23 +00:00
|
|
|
random = rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
|
|
randomMutex = sync.Mutex{}
|
|
|
|
sizeStrRegex = regexp.MustCompile(`(?i)^(\d+)([gmkb])?$`)
|
2021-12-22 12:46:17 +00:00
|
|
|
errInvalidPriority = errors.New("invalid priority")
|
2022-06-21 03:03:16 +00:00
|
|
|
noQuotesRegex = regexp.MustCompile(`^[-_./:@a-zA-Z0-9]+$`)
|
2021-10-29 17:58:14 +00:00
|
|
|
)
|
|
|
|
|
2022-12-31 21:08:49 +00:00
|
|
|
// Errors for UnmarshalJSON and UnmarshalJSONWithLimit functions
|
2022-12-29 14:57:42 +00:00
|
|
|
var (
|
2022-12-31 21:08:49 +00:00
|
|
|
ErrUnmarshalJSON = errors.New("unmarshalling JSON failed")
|
|
|
|
ErrTooLargeJSON = errors.New("too large JSON")
|
2022-12-29 14:57:42 +00:00
|
|
|
)
|
|
|
|
|
2021-12-07 16:45:15 +00:00
|
|
|
// FileExists checks if a file exists, and returns true if it does
|
2021-10-29 17:58:14 +00:00
|
|
|
func FileExists(filename string) bool {
|
|
|
|
stat, _ := os.Stat(filename)
|
|
|
|
return stat != nil
|
|
|
|
}
|
|
|
|
|
2022-10-01 19:50:48 +00:00
|
|
|
// Contains returns true if needle is contained in haystack
|
|
|
|
func Contains[T comparable](haystack []T, needle T) bool {
|
2021-12-09 03:13:59 +00:00
|
|
|
for _, s := range haystack {
|
|
|
|
if s == needle {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-10-07 21:16:20 +00:00
|
|
|
// ContainsIP returns true if any one of the of prefixes contains the ip.
|
|
|
|
func ContainsIP(haystack []netip.Prefix, needle netip.Addr) bool {
|
2022-10-05 20:42:07 +00:00
|
|
|
for _, s := range haystack {
|
|
|
|
if s.Contains(needle) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-10-01 19:50:48 +00:00
|
|
|
// ContainsAll returns true if all needles are contained in haystack
|
|
|
|
func ContainsAll[T comparable](haystack []T, needles []T) bool {
|
2021-12-21 20:22:27 +00:00
|
|
|
matches := 0
|
|
|
|
for _, s := range haystack {
|
|
|
|
for _, needle := range needles {
|
|
|
|
if s == needle {
|
|
|
|
matches++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return matches == len(needles)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SplitNoEmpty splits a string using strings.Split, but filters out empty strings
|
|
|
|
func SplitNoEmpty(s string, sep string) []string {
|
|
|
|
res := make([]string, 0)
|
|
|
|
for _, r := range strings.Split(s, sep) {
|
|
|
|
if r != "" {
|
|
|
|
res = append(res, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2022-04-19 13:14:32 +00:00
|
|
|
// SplitKV splits a string into a key/value pair using a separator, and trimming space. If the separator
|
|
|
|
// is not found, key is empty.
|
|
|
|
func SplitKV(s string, sep string) (key string, value string) {
|
|
|
|
kv := strings.SplitN(strings.TrimSpace(s), sep, 2)
|
|
|
|
if len(kv) == 2 {
|
|
|
|
return strings.TrimSpace(kv[0]), strings.TrimSpace(kv[1])
|
|
|
|
}
|
|
|
|
return "", strings.TrimSpace(kv[0])
|
|
|
|
}
|
|
|
|
|
2022-06-16 19:31:09 +00:00
|
|
|
// LastString returns the last string in a slice, or def if s is empty
|
|
|
|
func LastString(s []string, def string) string {
|
|
|
|
if len(s) == 0 {
|
|
|
|
return def
|
|
|
|
}
|
|
|
|
return s[len(s)-1]
|
|
|
|
}
|
|
|
|
|
2021-10-29 17:58:14 +00:00
|
|
|
// RandomString returns a random string with a given length
|
|
|
|
func RandomString(length int) string {
|
2023-01-22 04:15:22 +00:00
|
|
|
return RandomStringPrefix("", length)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RandomStringPrefix returns a random string with a given length, with a prefix
|
|
|
|
func RandomStringPrefix(prefix string, length int) string {
|
2021-12-07 21:03:01 +00:00
|
|
|
randomMutex.Lock() // Who would have thought that random.Intn() is not thread-safe?!
|
|
|
|
defer randomMutex.Unlock()
|
2023-01-22 04:15:22 +00:00
|
|
|
b := make([]byte, length-len(prefix))
|
2021-10-29 17:58:14 +00:00
|
|
|
for i := range b {
|
|
|
|
b[i] = randomStringCharset[random.Intn(len(randomStringCharset))]
|
|
|
|
}
|
2023-01-22 04:15:22 +00:00
|
|
|
return prefix + string(b)
|
2021-10-29 17:58:14 +00:00
|
|
|
}
|
2021-11-08 14:24:34 +00:00
|
|
|
|
2022-02-26 20:57:10 +00:00
|
|
|
// ValidRandomString returns true if the given string matches the format created by RandomString
|
|
|
|
func ValidRandomString(s string, length int) bool {
|
|
|
|
if len(s) != length {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for _, c := range strings.Split(s, "") {
|
|
|
|
if !strings.Contains(randomStringCharset, c) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-12-19 19:27:26 +00:00
|
|
|
// ParsePriority parses a priority string into its equivalent integer value
|
2021-12-17 01:33:01 +00:00
|
|
|
func ParsePriority(priority string) (int, error) {
|
2022-07-01 13:28:42 +00:00
|
|
|
p := strings.TrimSpace(strings.ToLower(priority))
|
|
|
|
switch p {
|
2021-12-17 01:33:01 +00:00
|
|
|
case "":
|
|
|
|
return 0, nil
|
|
|
|
case "1", "min":
|
|
|
|
return 1, nil
|
|
|
|
case "2", "low":
|
|
|
|
return 2, nil
|
|
|
|
case "3", "default":
|
|
|
|
return 3, nil
|
|
|
|
case "4", "high":
|
|
|
|
return 4, nil
|
|
|
|
case "5", "max", "urgent":
|
|
|
|
return 5, nil
|
|
|
|
default:
|
2022-07-01 13:28:42 +00:00
|
|
|
// Ignore new HTTP Priority header (see https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-priority)
|
|
|
|
// Cloudflare adds this to requests when forwarding to the backend (ntfy), so we just ignore it.
|
|
|
|
if strings.HasPrefix(p, "u=") {
|
|
|
|
return 3, nil
|
|
|
|
}
|
2021-12-17 01:33:01 +00:00
|
|
|
return 0, errInvalidPriority
|
|
|
|
}
|
|
|
|
}
|
2021-12-18 19:43:27 +00:00
|
|
|
|
2021-12-24 23:13:09 +00:00
|
|
|
// PriorityString converts a priority number to a string
|
|
|
|
func PriorityString(priority int) (string, error) {
|
|
|
|
switch priority {
|
|
|
|
case 0:
|
|
|
|
return "default", nil
|
|
|
|
case 1:
|
|
|
|
return "min", nil
|
|
|
|
case 2:
|
|
|
|
return "low", nil
|
|
|
|
case 3:
|
|
|
|
return "default", nil
|
|
|
|
case 4:
|
|
|
|
return "high", nil
|
|
|
|
case 5:
|
2021-12-24 23:57:02 +00:00
|
|
|
return "max", nil
|
2021-12-24 23:13:09 +00:00
|
|
|
default:
|
|
|
|
return "", errInvalidPriority
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-23 20:04:17 +00:00
|
|
|
// ShortTopicURL shortens the topic URL to be human-friendly, removing the http:// or https://
|
|
|
|
func ShortTopicURL(s string) string {
|
|
|
|
return strings.TrimPrefix(strings.TrimPrefix(s, "https://"), "http://")
|
|
|
|
}
|
2022-01-04 18:45:29 +00:00
|
|
|
|
2022-01-10 18:38:51 +00:00
|
|
|
// DetectContentType probes the byte array b and returns mime type and file extension.
|
|
|
|
// The filename is only used to override certain special cases.
|
|
|
|
func DetectContentType(b []byte, filename string) (mimeType string, ext string) {
|
|
|
|
if strings.HasSuffix(strings.ToLower(filename), ".apk") {
|
|
|
|
return "application/vnd.android.package-archive", ".apk"
|
2022-01-04 18:45:29 +00:00
|
|
|
}
|
2022-01-10 18:38:51 +00:00
|
|
|
m := mimetype.Detect(b)
|
|
|
|
mimeType, ext = m.String(), m.Extension()
|
|
|
|
if ext == "" {
|
|
|
|
ext = ".bin"
|
|
|
|
}
|
|
|
|
return
|
2022-01-04 18:45:29 +00:00
|
|
|
}
|
2022-01-06 00:04:56 +00:00
|
|
|
|
|
|
|
// ParseSize parses a size string like 2K or 2M into bytes. If no unit is found, e.g. 123, bytes is assumed.
|
|
|
|
func ParseSize(s string) (int64, error) {
|
|
|
|
matches := sizeStrRegex.FindStringSubmatch(s)
|
|
|
|
if matches == nil {
|
|
|
|
return -1, fmt.Errorf("invalid size %s", s)
|
|
|
|
}
|
|
|
|
value, err := strconv.Atoi(matches[1])
|
|
|
|
if err != nil {
|
|
|
|
return -1, fmt.Errorf("cannot convert number %s", matches[1])
|
|
|
|
}
|
|
|
|
switch strings.ToUpper(matches[2]) {
|
|
|
|
case "G":
|
|
|
|
return int64(value) * 1024 * 1024 * 1024, nil
|
|
|
|
case "M":
|
|
|
|
return int64(value) * 1024 * 1024, nil
|
|
|
|
case "K":
|
|
|
|
return int64(value) * 1024, nil
|
|
|
|
default:
|
|
|
|
return int64(value), nil
|
|
|
|
}
|
|
|
|
}
|
2022-01-23 05:54:18 +00:00
|
|
|
|
2023-02-07 03:38:22 +00:00
|
|
|
// FormatSize formats bytes into a human-readable notation, e.g. 2.1 MB
|
|
|
|
func FormatSize(b int64) string {
|
|
|
|
const unit = 1024
|
|
|
|
if b < unit {
|
|
|
|
return fmt.Sprintf("%d bytes", b)
|
|
|
|
}
|
|
|
|
div, exp := int64(unit), 0
|
|
|
|
for n := b / unit; n >= unit; n /= unit {
|
|
|
|
div *= unit
|
|
|
|
exp++
|
|
|
|
}
|
2023-02-09 20:24:12 +00:00
|
|
|
return fmt.Sprintf("%.1f %cB", float64(b)/float64(div), "KMGTPE"[exp])
|
2023-02-07 03:38:22 +00:00
|
|
|
}
|
|
|
|
|
2022-01-23 05:54:18 +00:00
|
|
|
// ReadPassword will read a password from STDIN. If the terminal supports it, it will not print the
|
|
|
|
// input characters to the screen. If not, it'll just read using normal readline semantics (useful for testing).
|
|
|
|
func ReadPassword(in io.Reader) ([]byte, error) {
|
|
|
|
// If in is a file and a character device (a TTY), use term.ReadPassword
|
|
|
|
if f, ok := in.(*os.File); ok {
|
|
|
|
stat, err := f.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if (stat.Mode() & os.ModeCharDevice) == os.ModeCharDevice {
|
|
|
|
password, err := term.ReadPassword(int(f.Fd())) // This is always going to be 0
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return password, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fallback: Manually read util \n if found, see #69 for details why this is so manual
|
|
|
|
password := make([]byte, 0)
|
|
|
|
buf := make([]byte, 1)
|
|
|
|
for {
|
|
|
|
_, err := in.Read(buf)
|
|
|
|
if err == io.EOF || buf[0] == '\n' {
|
|
|
|
break
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if len(password) > 10240 {
|
|
|
|
return nil, errors.New("passwords this long are not supported")
|
|
|
|
}
|
|
|
|
password = append(password, buf[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
return password, nil
|
|
|
|
}
|
2022-02-02 04:39:57 +00:00
|
|
|
|
|
|
|
// BasicAuth encodes the Authorization header value for basic auth
|
|
|
|
func BasicAuth(user, pass string) string {
|
|
|
|
return fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", user, pass))))
|
|
|
|
}
|
2022-06-02 15:59:22 +00:00
|
|
|
|
2022-12-29 00:55:11 +00:00
|
|
|
// BearerAuth encodes the Authorization header value for a bearer/token auth
|
|
|
|
func BearerAuth(token string) string {
|
|
|
|
return fmt.Sprintf("Bearer %s", token)
|
|
|
|
}
|
|
|
|
|
2022-06-02 15:59:22 +00:00
|
|
|
// MaybeMarshalJSON returns a JSON string of the given object, or "<cannot serialize>" if serialization failed.
|
|
|
|
// This is useful for logging purposes where a failure doesn't matter that much.
|
2022-10-01 20:31:48 +00:00
|
|
|
func MaybeMarshalJSON(v any) string {
|
2022-06-02 15:59:22 +00:00
|
|
|
jsonBytes, err := json.MarshalIndent(v, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return "<cannot serialize>"
|
|
|
|
}
|
|
|
|
if len(jsonBytes) > 5000 {
|
|
|
|
return string(jsonBytes)[:5000]
|
|
|
|
}
|
|
|
|
return string(jsonBytes)
|
|
|
|
}
|
2022-06-21 03:03:16 +00:00
|
|
|
|
|
|
|
// QuoteCommand combines a command array to a string, quoting arguments that need quoting.
|
|
|
|
// This function is naive, and sometimes wrong. It is only meant for lo pretty-printing a command.
|
|
|
|
//
|
|
|
|
// Warning: Never use this function with the intent to run the resulting command.
|
|
|
|
//
|
|
|
|
// Example:
|
2022-09-27 16:37:02 +00:00
|
|
|
//
|
|
|
|
// []string{"ls", "-al", "Document Folder"} -> ls -al "Document Folder"
|
2022-06-21 03:03:16 +00:00
|
|
|
func QuoteCommand(command []string) string {
|
|
|
|
var quoted []string
|
|
|
|
for _, c := range command {
|
|
|
|
if noQuotesRegex.MatchString(c) {
|
|
|
|
quoted = append(quoted, c)
|
|
|
|
} else {
|
|
|
|
quoted = append(quoted, fmt.Sprintf(`"%s"`, c))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return strings.Join(quoted, " ")
|
|
|
|
}
|
2022-12-29 00:55:11 +00:00
|
|
|
|
2022-12-31 21:08:49 +00:00
|
|
|
// UnmarshalJSON reads the given io.ReadCloser into a struct
|
|
|
|
func UnmarshalJSON[T any](body io.ReadCloser) (*T, error) {
|
2022-12-29 00:55:11 +00:00
|
|
|
var obj T
|
|
|
|
if err := json.NewDecoder(body).Decode(&obj); err != nil {
|
2022-12-31 21:08:49 +00:00
|
|
|
return nil, ErrUnmarshalJSON
|
2022-12-29 00:55:11 +00:00
|
|
|
}
|
|
|
|
return &obj, nil
|
|
|
|
}
|
|
|
|
|
2022-12-31 21:08:49 +00:00
|
|
|
// UnmarshalJSONWithLimit reads the given io.ReadCloser into a struct, but only until limit is reached
|
2023-01-28 04:10:59 +00:00
|
|
|
func UnmarshalJSONWithLimit[T any](r io.ReadCloser, limit int, allowEmpty bool) (*T, error) {
|
2022-12-29 14:57:42 +00:00
|
|
|
defer r.Close()
|
|
|
|
p, err := Peek(r, limit)
|
2022-12-29 00:55:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-12-29 14:57:42 +00:00
|
|
|
} else if p.LimitReached {
|
|
|
|
return nil, ErrTooLargeJSON
|
2022-12-29 00:55:11 +00:00
|
|
|
}
|
|
|
|
var obj T
|
2023-01-28 04:10:59 +00:00
|
|
|
if len(bytes.TrimSpace(p.PeekedBytes)) == 0 && allowEmpty {
|
|
|
|
return &obj, nil
|
|
|
|
} else if err := json.NewDecoder(p).Decode(&obj); err != nil {
|
2022-12-31 21:08:49 +00:00
|
|
|
return nil, ErrUnmarshalJSON
|
2022-12-29 00:55:11 +00:00
|
|
|
}
|
|
|
|
return &obj, nil
|
|
|
|
}
|
2023-01-23 03:21:30 +00:00
|
|
|
|
|
|
|
// Retry executes function f until if succeeds, and then returns t. If f fails, it sleeps
|
|
|
|
// and tries again. The sleep durations are passed as the after params.
|
|
|
|
func Retry[T any](f func() (*T, error), after ...time.Duration) (t *T, err error) {
|
|
|
|
for _, delay := range after {
|
|
|
|
if t, err = f(); err == nil {
|
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
time.Sleep(delay)
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-01-25 15:05:54 +00:00
|
|
|
|
2023-01-27 03:57:18 +00:00
|
|
|
// MinMax returns value if it is between min and max, or either
|
|
|
|
// min or max if it is out of range
|
|
|
|
func MinMax[T int | int64](value, min, max T) T {
|
|
|
|
if value < min {
|
|
|
|
return min
|
|
|
|
} else if value > max {
|
|
|
|
return max
|
|
|
|
}
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
2023-02-07 21:20:49 +00:00
|
|
|
// Max returns the maximum value of the two given values
|
|
|
|
func Max[T int | int64 | rate.Limit](a, b T) T {
|
|
|
|
if a > b {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2023-01-25 15:05:54 +00:00
|
|
|
// String turns a string into a pointer of a string
|
|
|
|
func String(v string) *string {
|
|
|
|
return &v
|
|
|
|
}
|
|
|
|
|
|
|
|
// Int turns a string into a pointer of an int
|
|
|
|
func Int(v int) *int {
|
|
|
|
return &v
|
|
|
|
}
|
2023-01-28 04:10:59 +00:00
|
|
|
|
|
|
|
// Time turns a time.Time into a pointer
|
|
|
|
func Time(v time.Time) *time.Time {
|
|
|
|
return &v
|
|
|
|
}
|