Skip to content

Commit

Permalink
adds example (closes #23)
Browse files Browse the repository at this point in the history
  • Loading branch information
tarunKoyalwar committed Nov 21, 2022
1 parent 08460ad commit 81dbdf2
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/build-test.yml
Expand Up @@ -21,6 +21,5 @@ jobs:
- name: Test
run: go test ./...

# Todo: create example folder
# - name: Build
# run: go build .
- name: Build
run: go build examples/main.go
9 changes: 9 additions & 0 deletions README.md
@@ -1,4 +1,13 @@
# networkpolicy

[![License](https://img.shields.io/github/license/projectdiscovery/networkpolicy)](LICENSE.md)
![Go version](https://img.shields.io/github/go-mod/go-version/projectdiscovery/networkpolicy?filename=go.mod)
[![Release](https://img.shields.io/github/release/projectdiscovery/networkpolicy)](https://github.com/projectdiscovery/networkpolicy/releases/)
[![Checks](https://github.com/projectdiscovery/networkpolicy/actions/workflows/build-test.yml/badge.svg)](https://github.com/projectdiscovery/networkpolicy/actions/workflows/build-test.yml)
[![GoDoc](https://pkg.go.dev/badge/projectdiscovery/networkpolicy)](https://pkg.go.dev/github.com/projectdiscovery/networkpolicy)



The package acts as an embeddable configurable container handling allow/deny verdicts over a series of conditions including
- IPs
- CIDRs
Expand Down
49 changes: 49 additions & 0 deletions examples/main.go
@@ -0,0 +1,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))
}

0 comments on commit 81dbdf2

Please sign in to comment.