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"
|
|
|
|
mathrand "math/rand"
|
2023-10-03 20:41:03 +02:00
|
|
|
)
|
2023-10-02 20:33:13 +02:00
|
|
|
|
|
|
|
func generateUrl() string {
|
|
|
|
length := currentConfig.urlLength
|
|
|
|
listChars := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
|
|
|
|
b := make([]rune, length)
|
|
|
|
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 {
|
|
|
|
// handle error here
|
2023-10-03 20:41:03 +02:00
|
|
|
}
|
|
|
|
|
2023-10-03 23:20:31 +02:00
|
|
|
secret := hex.EncodeToString(key)
|
|
|
|
return secret
|
2023-10-03 20:41:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func urlExist(url string) bool {
|
|
|
|
exist := connectDB().Exists(ctx, url).Val()
|
|
|
|
if exist == 1 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|