2022-04-04 09:32:41 +02:00
|
|
|
package http
|
|
|
|
|
|
|
|
|
|
import (
|
2022-04-13 17:41:02 +02:00
|
|
|
"net"
|
2022-04-04 09:32:41 +02:00
|
|
|
"net/http"
|
|
|
|
|
"regexp"
|
|
|
|
|
"strings"
|
2022-04-13 17:41:02 +02:00
|
|
|
|
|
|
|
|
"github.com/projectdiscovery/iputil"
|
|
|
|
|
"github.com/projectdiscovery/stringsutil"
|
|
|
|
|
"github.com/projectdiscovery/urlutil"
|
2022-04-04 09:32:41 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// @Host:target overrides the input target with the annotated one (similar to self-contained requests)
|
2022-04-13 17:41:02 +02:00
|
|
|
var reHostAnnotation = regexp.MustCompile(`(?m)^@Host:\s*(.+)\s*$`)
|
2022-04-04 09:32:41 +02:00
|
|
|
|
|
|
|
|
// parseAnnotations and override requests settings
|
|
|
|
|
func parseAnnotations(rawRequest string, request *http.Request) {
|
|
|
|
|
// parse request for known ovverride annotations
|
|
|
|
|
if hosts := reHostAnnotation.FindStringSubmatch(rawRequest); len(hosts) > 0 {
|
2022-04-13 17:41:02 +02:00
|
|
|
value := strings.TrimSpace(hosts[1])
|
|
|
|
|
// handle scheme
|
|
|
|
|
switch {
|
|
|
|
|
case stringsutil.HasPrefixI(value, "http://"):
|
|
|
|
|
request.URL.Scheme = urlutil.HTTP
|
|
|
|
|
case stringsutil.HasPrefixI(value, "https://"):
|
|
|
|
|
request.URL.Scheme = urlutil.HTTPS
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
value = stringsutil.TrimPrefixAny(value, "http://", "https://")
|
|
|
|
|
|
|
|
|
|
if isHostPort(value) {
|
|
|
|
|
request.URL.Host = value
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func isHostPort(value string) bool {
|
|
|
|
|
_, port, err := net.SplitHostPort(value)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
if !iputil.IsPort(port) {
|
|
|
|
|
return false
|
2022-04-04 09:32:41 +02:00
|
|
|
}
|
2022-04-13 17:41:02 +02:00
|
|
|
return true
|
2022-04-04 09:32:41 +02:00
|
|
|
}
|