2020-04-04 15:59:05 +05:30
|
|
|
package runner
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
2020-04-22 22:45:02 +02:00
|
|
|
"net/url"
|
2020-04-04 15:59:05 +05:30
|
|
|
"strings"
|
|
|
|
|
"unsafe"
|
2020-04-22 22:45:02 +02:00
|
|
|
|
|
|
|
|
"github.com/asaskevich/govalidator"
|
2020-04-04 15:59:05 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// unsafeToString converts byte slice to string with zero allocations
|
|
|
|
|
func unsafeToString(bs []byte) string {
|
|
|
|
|
return *(*string)(unsafe.Pointer(&bs))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// headersToString converts http headers to string
|
|
|
|
|
func headersToString(headers http.Header) string {
|
|
|
|
|
builder := &strings.Builder{}
|
|
|
|
|
|
|
|
|
|
for header, values := range headers {
|
|
|
|
|
builder.WriteString(header)
|
|
|
|
|
builder.WriteString(": ")
|
|
|
|
|
|
|
|
|
|
for i, value := range values {
|
|
|
|
|
builder.WriteString(value)
|
|
|
|
|
if i != len(values)-1 {
|
|
|
|
|
builder.WriteRune(',')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
builder.WriteRune('\n')
|
|
|
|
|
}
|
|
|
|
|
return builder.String()
|
|
|
|
|
}
|
2020-04-22 22:45:02 +02:00
|
|
|
|
|
|
|
|
// isURL tests a string to determine if it is a well-structured url or not.
|
|
|
|
|
func isURL(toTest string) bool {
|
|
|
|
|
_, err := url.ParseRequestURI(toTest)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
u, err := url.Parse(toTest)
|
|
|
|
|
if err != nil || u.Scheme == "" || u.Host == "" {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-24 15:19:43 +02:00
|
|
|
func extractDomain(URL string) string {
|
|
|
|
|
u, err := url.Parse(URL)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return u.Hostname()
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-22 22:45:02 +02:00
|
|
|
// isDNS tests a string to determine if it is a well-structured dns or not
|
|
|
|
|
// even if it's oneliner, we leave it wrapped in a function call for
|
|
|
|
|
// future improvements
|
|
|
|
|
func isDNS(toTest string) bool {
|
|
|
|
|
return govalidator.IsDNSName(toTest)
|
|
|
|
|
}
|