Rewrite v2
This commit is contained in:
64
src/util.go
64
src/util.go
@@ -4,27 +4,29 @@ import (
|
||||
"crypto/sha1"
|
||||
_ "embed"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/valyala/fasthttp"
|
||||
)
|
||||
|
||||
var (
|
||||
//go:embed default-icon.png
|
||||
//go:embed icon.png
|
||||
defaultIconBytes []byte
|
||||
ipAddressRegExp = regexp.MustCompile("^\\d{1,3}(\\.\\d{1,3}){3}$")
|
||||
)
|
||||
|
||||
var (
|
||||
ErrNoAddressMatch = errors.New("address does not match any known format")
|
||||
)
|
||||
func Contains[T comparable](arr []T, v T) bool {
|
||||
for _, value := range arr {
|
||||
if v == value {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func GetBlockedServerList() error {
|
||||
resp, err := http.Get("https://sessionserver.mojang.com/blockedservers")
|
||||
@@ -100,7 +102,7 @@ func ParseAddress(address string, defaultPort uint16) (string, uint16, error) {
|
||||
result := strings.SplitN(address, ":", 2)
|
||||
|
||||
if len(result) < 1 {
|
||||
return "", 0, ErrNoAddressMatch
|
||||
return "", 0, fmt.Errorf("'%s' does not match any known address", address)
|
||||
}
|
||||
|
||||
if len(result) < 2 {
|
||||
@@ -115,47 +117,3 @@ func ParseAddress(address string, defaultPort uint16) (string, uint16, error) {
|
||||
|
||||
return result[0], uint16(port), nil
|
||||
}
|
||||
|
||||
func WriteError(ctx *fasthttp.RequestCtx, err error, statusCode int, body ...string) {
|
||||
ctx.SetStatusCode(statusCode)
|
||||
|
||||
if len(body) > 0 {
|
||||
ctx.SetBodyString(body[0])
|
||||
} else {
|
||||
ctx.SetBodyString(http.StatusText(statusCode))
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
func IsCacheEnabled(ctx *fasthttp.RequestCtx, cacheType, host string, port uint16) (bool, string, error) {
|
||||
key := fmt.Sprintf("%s:%s-%d", cacheType, host, port)
|
||||
|
||||
if authKey := ctx.Request.Header.Peek("Authorization"); len(authKey) > 0 {
|
||||
exists, err := r.Exists(fmt.Sprintf("auth_key:%s", authKey))
|
||||
|
||||
if err != nil {
|
||||
WriteError(ctx, err, http.StatusInternalServerError)
|
||||
|
||||
return config.CacheEnable, key, err
|
||||
}
|
||||
|
||||
if exists {
|
||||
return false, key, nil
|
||||
}
|
||||
}
|
||||
|
||||
return config.CacheEnable, key, nil
|
||||
}
|
||||
|
||||
func Contains[T comparable](arr []T, x T) bool {
|
||||
for _, v := range arr {
|
||||
if v == x {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user