2023-10-02 20:33:13 +02:00
|
|
|
package main
|
|
|
|
|
2023-10-03 20:41:03 +02:00
|
|
|
import (
|
2023-10-03 23:20:31 +02:00
|
|
|
"crypto/rand"
|
|
|
|
"encoding/hex"
|
2023-12-28 00:02:50 +01:00
|
|
|
"fmt"
|
2023-10-04 01:49:14 +02:00
|
|
|
"log"
|
2023-10-03 23:20:31 +02:00
|
|
|
mathrand "math/rand"
|
2023-12-28 00:02:50 +01:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2023-10-03 20:41:03 +02:00
|
|
|
)
|
2023-10-02 20:33:13 +02:00
|
|
|
|
2023-11-26 00:00:53 +01:00
|
|
|
func GenerateUrl() string {
|
2023-10-02 20:33:13 +02:00
|
|
|
listChars := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
|
2023-10-04 01:49:14 +02:00
|
|
|
b := make([]rune, currentConfig.urlLength)
|
2023-10-02 20:33:13 +02:00
|
|
|
for i := range b {
|
2023-10-03 23:20:31 +02:00
|
|
|
b[i] = listChars[mathrand.Intn(len(listChars))]
|
2023-10-02 20:33:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return string(b)
|
|
|
|
}
|
2023-10-03 20:41:03 +02:00
|
|
|
|
2023-11-26 00:00:53 +01:00
|
|
|
func GenerateSecret() string {
|
2023-10-03 23:20:31 +02:00
|
|
|
key := make([]byte, 32)
|
|
|
|
_, err := rand.Read(key)
|
|
|
|
if err != nil {
|
2023-10-04 01:49:14 +02:00
|
|
|
log.Printf("Failed to generate secret")
|
2023-10-03 20:41:03 +02:00
|
|
|
}
|
|
|
|
|
2023-10-04 01:49:14 +02:00
|
|
|
return hex.EncodeToString(key)
|
2023-10-03 20:41:03 +02:00
|
|
|
}
|
|
|
|
|
2023-11-26 00:00:53 +01:00
|
|
|
func UrlExist(url string) bool {
|
|
|
|
return db.Exists(ctx, url).Val() == 1
|
2023-10-03 20:41:03 +02:00
|
|
|
}
|
2023-10-20 03:30:19 +02:00
|
|
|
|
2023-11-26 00:00:53 +01:00
|
|
|
func VerifySecret(url string, secret string) bool {
|
|
|
|
return secret == db.HGet(ctx, url, "secret").Val()
|
2023-10-20 03:30:19 +02:00
|
|
|
}
|
2023-12-28 00:02:50 +01:00
|
|
|
|
|
|
|
func parseIntBeforeSeparator(source *string, sep string) (int, error) { // return -1 if error, only accept positive number
|
|
|
|
var value int
|
|
|
|
var err error
|
|
|
|
if strings.Contains(*source, sep) {
|
|
|
|
value, err = strconv.Atoi(strings.Split(*source, sep)[0])
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return -1, fmt.Errorf("parseIntBeforeSeparator : \"%s\" : cannot parse value as int", *source)
|
|
|
|
}
|
|
|
|
if value < 0 { // Only positive value is correct
|
|
|
|
return -1, fmt.Errorf("parseIntBeforeSeparator : \"%s\" : format only take positive value", *source)
|
|
|
|
}
|
|
|
|
*source = strings.Join(strings.Split(*source, sep)[1:], "")
|
|
|
|
}
|
|
|
|
return value, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParseExpiration(source string) (int, error) { // return -1 if error
|
|
|
|
var expiration int
|
|
|
|
var tempOutput int
|
|
|
|
var err error
|
|
|
|
errMessage := "ParseExpiration : \"%s\" : invalid syntax"
|
|
|
|
if source == "0" {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
tempOutput, err = parseIntBeforeSeparator(&source, "d")
|
|
|
|
expiration = tempOutput * 86400
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return -1, fmt.Errorf(errMessage, source)
|
|
|
|
}
|
|
|
|
tempOutput, err = parseIntBeforeSeparator(&source, "h")
|
|
|
|
expiration += tempOutput * 3600
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return -1, fmt.Errorf(errMessage, source)
|
|
|
|
}
|
|
|
|
tempOutput, err = parseIntBeforeSeparator(&source, "m")
|
|
|
|
expiration += tempOutput * 60
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return -1, fmt.Errorf(errMessage, source)
|
|
|
|
}
|
|
|
|
tempOutput, err = parseIntBeforeSeparator(&source, "s")
|
|
|
|
expiration += tempOutput * 1
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return -1, fmt.Errorf(errMessage, source)
|
|
|
|
}
|
|
|
|
|
|
|
|
return expiration, nil
|
|
|
|
}
|