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-10-04 01:49:14 +02:00
|
|
|
"log"
|
2023-10-03 23:20:31 +02:00
|
|
|
mathrand "math/rand"
|
2023-10-03 20:41:03 +02:00
|
|
|
)
|
2023-10-02 20:33:13 +02:00
|
|
|
|
|
|
|
func generateUrl() string {
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func urlExist(url string) bool {
|
2023-10-20 03:30:19 +02:00
|
|
|
exist := db.Exists(ctx, url).Val()
|
2023-10-04 01:49:14 +02:00
|
|
|
return exist == 1
|
2023-10-03 20:41:03 +02:00
|
|
|
}
|
2023-10-20 03:30:19 +02:00
|
|
|
|
|
|
|
func verifySecret(url string, secret string) bool {
|
|
|
|
if secret == db.HGet(ctx, url, "secret").Val() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|