2020-08-05 17:14:10 +00:00
|
|
|
package main
|
|
|
|
|
2020-09-01 16:53:21 +00:00
|
|
|
import (
|
2020-10-06 17:07:48 +00:00
|
|
|
"math/rand"
|
2020-11-01 17:37:21 +00:00
|
|
|
"net/http"
|
2020-09-19 12:56:31 +00:00
|
|
|
"sort"
|
2020-09-01 16:53:21 +00:00
|
|
|
"strings"
|
2020-10-06 17:07:48 +00:00
|
|
|
"time"
|
2020-09-01 16:53:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func urlize(str string) string {
|
|
|
|
newStr := ""
|
|
|
|
for _, c := range strings.Split(strings.ToLower(str), "") {
|
|
|
|
if c >= "a" && c <= "z" || c >= "A" && c <= "Z" || c >= "0" && c <= "9" {
|
|
|
|
newStr += c
|
|
|
|
} else if c == " " {
|
|
|
|
newStr += "-"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return newStr
|
|
|
|
}
|
2020-09-15 15:01:53 +00:00
|
|
|
|
2020-09-19 12:56:31 +00:00
|
|
|
func sortedStrings(s []string) []string {
|
|
|
|
sort.Slice(s, func(i, j int) bool {
|
|
|
|
return strings.ToLower(s[i]) < strings.ToLower(s[j])
|
|
|
|
})
|
|
|
|
return s
|
|
|
|
}
|
2020-10-06 17:07:48 +00:00
|
|
|
|
2020-10-26 16:37:31 +00:00
|
|
|
func generateRandomString(chars int) string {
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
2020-10-06 17:07:48 +00:00
|
|
|
letters := []rune("abcdefghijklmnopqrstuvwxyz")
|
2020-10-26 16:37:31 +00:00
|
|
|
b := make([]rune, chars)
|
2020-10-06 17:07:48 +00:00
|
|
|
for i := range b {
|
|
|
|
b[i] = letters[rand.Intn(len(letters))]
|
|
|
|
}
|
|
|
|
return string(b)
|
|
|
|
}
|
2020-11-01 17:37:21 +00:00
|
|
|
|
2020-11-06 17:45:31 +00:00
|
|
|
func isAllowedHost(r *http.Request, hosts ...string) bool {
|
|
|
|
if r.URL == nil {
|
|
|
|
return false
|
2020-11-01 17:37:21 +00:00
|
|
|
}
|
2020-11-06 17:45:31 +00:00
|
|
|
rh := r.URL.Host
|
|
|
|
switch r.URL.Scheme {
|
|
|
|
case "http":
|
|
|
|
rh = strings.TrimSuffix(rh, ":80")
|
|
|
|
case "https":
|
|
|
|
rh = strings.TrimSuffix(rh, ":443")
|
|
|
|
}
|
|
|
|
for _, host := range hosts {
|
|
|
|
if rh == host {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
2020-11-01 17:37:21 +00:00
|
|
|
}
|