-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
49 lines (40 loc) · 982 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main
import (
"errors"
"log"
"net/http"
"net/http/httputil"
"github.com/projectdiscovery/networkpolicy"
)
func main() {
var npOptions networkpolicy.Options
// deny connections to localhost
npOptions.DenyList = append(npOptions.DenyList, "127.0.0.0/8")
np, err := networkpolicy.New(npOptions)
if err != nil {
log.Fatal(err)
}
customRedirectHandler := func(req *http.Request, via []*http.Request) error {
// if at least one address is valid we follow the redirect
if _, ok := np.ValidateHost(req.Host); ok {
return nil
}
return errors.New("redirected to a forbidden target")
}
client := &http.Client{
CheckRedirect: customRedirectHandler,
}
req, err := http.NewRequest(http.MethodGet, "https://projectdiscovery.io", nil)
if err != nil {
log.Fatal(err)
}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
bin, err := httputil.DumpResponse(resp, true)
if err != nil {
log.Fatal(err)
}
log.Println(string(bin))
}