clean up
This commit is contained in:
parent
fb32f3532e
commit
c594c93559
4 changed files with 183 additions and 173 deletions
34
common.go
34
common.go
|
@ -1,7 +1,11 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"html/template"
|
||||
"log"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -15,3 +19,33 @@ var (
|
|||
},
|
||||
}
|
||||
)
|
||||
|
||||
func generateStrongRandomString(length int) string {
|
||||
bytes := make([]byte, length)
|
||||
_, err := rand.Read(bytes)
|
||||
if err != nil {
|
||||
log.Fatalf("Error generating random string: %v", err)
|
||||
}
|
||||
return base64.URLEncoding.EncodeToString(bytes)[:length]
|
||||
}
|
||||
|
||||
// Checks if the URL already includes a protocol
|
||||
func hasProtocol(url string) bool {
|
||||
return strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "https://")
|
||||
}
|
||||
|
||||
// Checks if the domain is a local address
|
||||
func isLocalAddress(domain string) bool {
|
||||
return domain == "localhost" || strings.HasPrefix(domain, "127.") || strings.HasPrefix(domain, "192.168.") || strings.HasPrefix(domain, "10.")
|
||||
}
|
||||
|
||||
// Ensures that HTTP or HTTPS is befor the adress if needed
|
||||
func addProtocol(domain string) string {
|
||||
if hasProtocol(domain) {
|
||||
return domain
|
||||
}
|
||||
if isLocalAddress(domain) {
|
||||
return "http://" + domain
|
||||
}
|
||||
return "https://" + domain
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue