Skip to content

Commit

Permalink
Merge pull request #26 from projectdiscovery/issue-23-adds-example
Browse files Browse the repository at this point in the history
adds example
  • Loading branch information
Mzack9999 committed Nov 21, 2022
2 parents 08460ad + ab2a288 commit e152d3d
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 5 deletions.
7 changes: 3 additions & 4 deletions .github/workflows/build-test.yml
@@ -1,6 +1,5 @@
name: 🔨 Build Test
on:
push:
pull_request:
workflow_dispatch:

Expand All @@ -21,6 +20,6 @@ jobs:
- name: Test
run: go test ./...

# Todo: create example folder
# - name: Build
# run: go build .
- name: Build
run: go run main.go
working-directory: examples/
1 change: 0 additions & 1 deletion .github/workflows/lint-test.yml
@@ -1,6 +1,5 @@
name: 🙏🏻 Lint Test
on:
push:
pull_request:
workflow_dispatch:

Expand Down
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
55 changes: 55 additions & 0 deletions examples/main.go
@@ -0,0 +1,55 @@
package main

import (
"crypto/tls"
"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,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
}
req, err := http.NewRequest(http.MethodGet, "https://scanme.sh", 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 e152d3d

Please sign in to comment.