network

package
v0.0.14 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 8, 2023 License: GPL-3.0 Imports: 16 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const DefaultFileContentType = "application/octet-stream"

Variables

View Source
var DefaultHttpClient = &http.Client{Timeout: 5 * time.Second,
	Transport: &http.Transport{
		TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
	},
	CheckRedirect: nil}

DefaultHttpClient default http client use http.Client timeout 5 Second ignore certificate warnings

View Source
var DefaultResolver = &Dns{
	NewMsg:  NewDefaultMsg,
	Ns:      "223.6.6.6:53",
	Timeout: 3 * time.Second,
}
View Source
var NoneCheckRedirect = func(req *http.Request, via []*http.Request) error {
	return http.ErrUseLastResponse
}
View Source
var PRIVATE_IPV4 = [3]string{
	"10.0.0.0/8",
	"172.16.0.0/12",
	"192.168.0.0/16",
}
View Source
var USER_AGENTS = [8]string{
	"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:108.0) Gecko/20100101 Firefox/108.0",
	"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36",
	"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.89 Safari/537.36",
	"Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.130 Safari/537.36",
	"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/7046A194A",
	"Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25",
	"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.13+ (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2",
	"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/534.55.3 (KHTML, like Gecko) Version/5.1.3 Safari/534.53.10",
}

USER_AGENT http header User-Agent's

Functions

func IsDomainName added in v0.0.7

func IsDomainName(s string) bool

IsDomainName checks if a string is a presentation-format domain name (currently restricted to hostname-compatible "preferred name" LDH labels and SRV-like "underscore labels"; see golang.org/issue/12421).

Example
package main

import (
	"fmt"
	"github.com/sechelper/seclib/network"
)

func main() {
	fmt.Println(network.IsDomainName("secself.com"))        // true
	fmt.Println(network.IsDomainName("www.secself"))        // true
	fmt.Println(network.IsDomainName("secself.com/"))       // false
	fmt.Println(network.IsDomainName("http://secself.com")) // false
}
Output:

func ParseCIDR

func ParseCIDR(s string) (net.IP, []net.IP, error)

ParseCIDR parses s as a CIDR notation IP address and prefix length, like "192.0.2.0/24" or "2001:db8::/32", as defined in RFC 4632 and RFC 4291.

It returns the IP address and the network implied by the IP and prefix length. For example, ParseCIDR("192.0.2.1/24") returns the IP address 192.0.2.1 and the network segment. TODO support ipv6

Example
package main

import (
	"fmt"
	"github.com/sechelper/seclib/network"
	"log"
)

func main() {
	ipv4Addr, ipv4NetSegment, err := network.ParseCIDR("192.0.2.1/24")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(ipv4Addr)
	for i := range ipv4NetSegment {
		fmt.Println(ipv4NetSegment[i])
	}
}
Output:

func RandomUserAgent added in v0.0.9

func RandomUserAgent() string

RandomUserAgent generate random User-Agent

Example
package main

import (
	"fmt"
	"github.com/sechelper/seclib/network"
)

func main() {
	for i := 0; i < 10; i++ {
		fmt.Println(network.RandomUserAgent())
	}
}
Output:

Types

type Addr added in v0.0.7

type Addr struct {
	IP   net.IP
	Port int
}

func (Addr) String added in v0.0.7

func (addr Addr) String() string
Example
package main

import (
	"fmt"
	"github.com/sechelper/seclib/network"
	"net"
)

func main() {
	addr := network.Addr{
		IP:   net.ParseIP("114.114.114.114"),
		Port: 53,
	}

	fmt.Println(addr)
}
Output:

type Dns added in v0.0.7

type Dns struct {
	Ns      string
	NewMsg  func() *Msg
	Timeout time.Duration
}

func (*Dns) Exchange added in v0.0.7

func (_dns *Dns) Exchange(dnsType uint16, domain string) (r *dns.Msg, rtt time.Duration, err error)
Example
package main

import (
	"fmt"
	"github.com/miekg/dns"
	"github.com/sechelper/seclib/network"
	"log"
	"net"
)

func main() {
	var ips []net.IP = nil
	in, _, err := network.DefaultResolver.Exchange(dns.TypeA, "go-hacker-code.lab.secself.com")

	if err != nil || in.Answer == nil {
		log.Fatal(err)
	}

	ips = make([]net.IP, 0)
	for i := range in.Answer {
		if rr, ok := in.Answer[i].(*dns.A); ok {
			ips = append(ips, rr.A)
		}
	}
	fmt.Println(ips)
}
Output:

func (*Dns) LookupCNAME added in v0.0.7

func (_dns *Dns) LookupCNAME(host string) (cname string, err error)
Example
package main

import (
	"fmt"
	"github.com/sechelper/seclib/network"
	"log"
)

func main() {
	cname, err := network.DefaultResolver.LookupCNAME("go-hacker-code.lab.secself.com")
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(cname)
}
Output:

func (*Dns) LookupIP added in v0.0.7

func (_dns *Dns) LookupIP(host string) (ips []net.IP, err error)

LookupIP looks up host using the local resolver. It returns a slice of that host's IPv4 and IPv6 addresses.

Example
package main

import (
	"fmt"
	"github.com/sechelper/seclib/network"
	"log"
)

func main() {
	ip, err := network.DefaultResolver.LookupIP("go-hacker-code.lab.secself.com")
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(ip)
}
Output:

type HttpClient added in v0.0.14

type HttpClient struct {
	http.Client
	// contains filtered or unexported fields
}

func (*HttpClient) Get added in v0.0.14

func (c *HttpClient) Get(url string) (*http.Response, error)
Example
package main

import (
	"fmt"
	"github.com/sechelper/seclib/network"
)

func main() {
	c := network.HttpClient{}
	if reps, err := c.Get("https://go-hacker-code.lab.secself.com/"); err == nil {
		fmt.Println(reps.StatusCode)
	}
}
Output:

func (*HttpClient) Headers added in v0.0.14

func (c *HttpClient) Headers(k string, v string)

func (*HttpClient) Post added in v0.0.14

func (c *HttpClient) Post(url, contentType string, body io.Reader) (resp *http.Response, err error)
Example
package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"github.com/sechelper/seclib/network"
)

func main() {
	c := network.HttpClient{}
	data := map[string]string{"username": "password"}
	body, _ := json.Marshal(data)
	if reps, err := c.Post("https://go-hacker-code.lab.secself.com/", "application/json", bytes.NewReader(body)); err == nil {
		fmt.Println(reps.StatusCode)
	}
}
Output:

func (*HttpClient) PostForm added in v0.0.14

func (c *HttpClient) PostForm(url string, data url.Values) (resp *http.Response, err error)

func (*HttpClient) PreDo added in v0.0.14

func (c *HttpClient) PreDo(method string, url string, body io.Reader) (*http.Response, error)

func (*HttpClient) UploadFile added in v0.0.14

func (c *HttpClient) UploadFile(url string, files []UPFile) (resp *http.Response, err error)

UploadFile ref https://gist.github.com/andrewmilson/19185aab2347f6ad29f5

Example
package main

import (
	"fmt"
	"github.com/sechelper/seclib/network"
)

func main() {
	c := network.HttpClient{}
	file1 := network.UPFile{Path: "file1.txt",
		Field: "file"}

	file2 := network.UPFile{Path: "file2.txt",
		Field: "file1"}

	file3 := network.UPFile{Path: "file3.txt",
		Field: "file2"}

	_, err := c.UploadFile("http://127.0.0.1:8080/upladMore", []network.UPFile{file1, file2, file3})
	if err != nil {
		fmt.Println(err)
		return
	}
}
Output:

type Msg added in v0.0.7

type Msg dns.Msg

func NewDefaultMsg added in v0.0.7

func NewDefaultMsg() *Msg

type UPFile added in v0.0.14

type UPFile struct {
	Path        string
	Field       string
	Filename    string
	ContentType string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL