From 81dbdf2457b8506b211f22e18bc884267ac1df07 Mon Sep 17 00:00:00 2001 From: Tarun Koyalwar Date: Mon, 21 Nov 2022 16:27:23 +0530 Subject: [PATCH 1/3] adds example (closes #23) --- .github/workflows/build-test.yml | 5 ++-- README.md | 9 ++++++ examples/main.go | 49 ++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 examples/main.go diff --git a/.github/workflows/build-test.yml b/.github/workflows/build-test.yml index 2ee7a85..1bd12f8 100644 --- a/.github/workflows/build-test.yml +++ b/.github/workflows/build-test.yml @@ -21,6 +21,5 @@ jobs: - name: Test run: go test ./... - # Todo: create example folder - # - name: Build - # run: go build . \ No newline at end of file + - name: Build + run: go build examples/main.go \ No newline at end of file diff --git a/README.md b/README.md index 597ed92..1c44b64 100644 --- a/README.md +++ b/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 diff --git a/examples/main.go b/examples/main.go new file mode 100644 index 0000000..8d704a4 --- /dev/null +++ b/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)) +} From 8ef73f5f63bd6e00a35cddd676095b2260982d91 Mon Sep 17 00:00:00 2001 From: mzack Date: Mon, 21 Nov 2022 19:17:33 +0100 Subject: [PATCH 2/3] small refactor --- .github/workflows/build-test.yml | 3 ++- examples/main.go | 8 +++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build-test.yml b/.github/workflows/build-test.yml index 1bd12f8..b8470e1 100644 --- a/.github/workflows/build-test.yml +++ b/.github/workflows/build-test.yml @@ -22,4 +22,5 @@ jobs: run: go test ./... - name: Build - run: go build examples/main.go \ No newline at end of file + run: go run main.go + working-directory: examples/ \ No newline at end of file diff --git a/examples/main.go b/examples/main.go index 8d704a4..21016c8 100644 --- a/examples/main.go +++ b/examples/main.go @@ -1,6 +1,7 @@ package main import ( + "crypto/tls" "errors" "log" "net/http" @@ -29,8 +30,13 @@ func main() { client := &http.Client{ CheckRedirect: customRedirectHandler, + Transport: &http.Transport{ + TLSClientConfig: &tls.Config{ + InsecureSkipVerify: true, + }, + }, } - req, err := http.NewRequest(http.MethodGet, "https://projectdiscovery.io", nil) + req, err := http.NewRequest(http.MethodGet, "https://scanme.sh", nil) if err != nil { log.Fatal(err) } From ab2a28810f8634a945ce74655e2915ea8006f9a4 Mon Sep 17 00:00:00 2001 From: mzack Date: Mon, 21 Nov 2022 19:18:12 +0100 Subject: [PATCH 3/3] removing redundant check --- .github/workflows/build-test.yml | 1 - .github/workflows/lint-test.yml | 1 - 2 files changed, 2 deletions(-) diff --git a/.github/workflows/build-test.yml b/.github/workflows/build-test.yml index b8470e1..da43cde 100644 --- a/.github/workflows/build-test.yml +++ b/.github/workflows/build-test.yml @@ -1,6 +1,5 @@ name: 🔨 Build Test on: - push: pull_request: workflow_dispatch: diff --git a/.github/workflows/lint-test.yml b/.github/workflows/lint-test.yml index 9d45d98..fd17be2 100644 --- a/.github/workflows/lint-test.yml +++ b/.github/workflows/lint-test.yml @@ -1,6 +1,5 @@ name: 🙏🏻 Lint Test on: - push: pull_request: workflow_dispatch: