GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - projectdiscovery/networkpolicy: Network Policy Helper

BranchesTags
Open more actions menu

Latest commit

 

History

221 Commits
221 Commits

Folders and files

NameName
Last commit message
Last commit date
.github
.github
 
 
examples
examples
 
 
.gitignore
.gitignore
 
 
LICENSE.md
LICENSE.md
 
 
README.md
README.md
 
 
cidr.go
cidr.go
 
 
go.mod
go.mod
 
 
go.sum
go.sum
 
 
host.go
host.go
 
 
ip.go
ip.go
 
 
networkpolicy.go
networkpolicy.go
 
 
networkpolicy_test.go
networkpolicy_test.go
 
 
port.go
port.go
 
 
scheme.go
scheme.go
 
 

Repository files navigation

networkpolicy

License Go version Release Checks GoDoc

The package acts as an embeddable configurable container handling allow/deny verdicts over a series of conditions including

  • IPs
  • CIDRs
  • Ports
  • Schemes (eg https, http, ftp)

General usage as allow/deny

The following program prevents the http client to follow targets belonging to the deny list:

Example - General allow/deny list

package main

import (
	"errors"
	"log"
	"net/http"

	"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, "http://yourtarget", nil)
	if err != nil {
		log.Fatal(err)
	}
	resp, err := client.Do(req)
	if err != nil {
		log.Fatal(err)
	}
	log.Println(resp)
}

ASN-based allow/deny

networkpolicy intentionally does not perform ASN→CIDR resolution itself — that responsibility belongs to asnmap, where BGP data, caching, and network errors are already handled. To restrict (or allow) traffic by ASN, expand it to CIDRs upstream and feed them into the existing AllowList/DenyList:

package main

import (
	"log"

	asnmap "github.com/projectdiscovery/asnmap/libs"
	"github.com/projectdiscovery/networkpolicy"
)

func main() {
	client, err := asnmap.NewClient()
	if err != nil {
		log.Fatal(err)
	}

	var denyCIDRs []string
	for _, asn := range []string{"AS13335", "AS15169"} {
		resp, err := client.GetData(asn)
		if err != nil {
			log.Fatal(err)
		}
		nets, err := asnmap.GetCIDR(resp)
		if err != nil {
			log.Fatal(err)
		}
		for _, n := range nets {
			denyCIDRs = append(denyCIDRs, n.String())
		}
	}

	np, err := networkpolicy.New(networkpolicy.Options{DenyList: denyCIDRs})
	if err != nil {
		log.Fatal(err)
	}
	_ = np
}

About

Network Policy Helper

Resources

Readme
MIT license

Code of conduct

Code of conduct

Contributing

Contributing

Security policy

Security policy
Activity
Custom properties

Stars

32 stars

Watchers

8 watching

Forks

4 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.