utils

package
v0.0.0-...-8291241 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2023 License: MIT Imports: 49 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var GoogleResolver = &net.Resolver{
	PreferGo: true,
	Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
		d := net.Dialer{
			Timeout: time.Millisecond * time.Duration(10000),
		}
		return d.DialContext(ctx, network, "8.8.8.8:53")
	},
}
View Source
var LoopbackIPUint uint32
View Source
var PrivateCIDRUint = []uint32{167772160, 184549375, 2886729728, 2887778303, 3232235520, 3232301055} // 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16
View Source
var R interface {
	LookupAddr(ctx context.Context, addr string) ([]string, error)
	LookupCNAME(ctx context.Context, host string) (string, error)
	LookupHost(ctx context.Context, host string) (addrs []string, err error)
	// LookupIP(ctx context.Context, network, host string) ([]net.IP, error)
	LookupIPAddr(ctx context.Context, host string) ([]net.IPAddr, error)
	LookupMX(ctx context.Context, name string) ([]*net.MX, error)
	LookupNS(ctx context.Context, name string) ([]*net.NS, error)
	LookupPort(ctx context.Context, network, service string) (port int, err error)
	LookupSRV(ctx context.Context, service, proto, name string) (string, []*net.SRV, error)
	LookupTXT(ctx context.Context, name string) ([]string, error)
} = GoogleResolver

Functions

func BigEndianUint64

func BigEndianUint64(n uint64) []byte

func BreakBaseURL

func BreakBaseURL(rawurl string) (scheme, ipaddr, port string, err error)

func ByteSlice2String

func ByteSlice2String(bs []byte) string

Do not apply it after a String2ByteSlice was applied

func CheckPasswordHash

func CheckPasswordHash(password, hash string) bool

func CombineMonthYear

func CombineMonthYear(month, year int) int

func ContainsDotDot

func ContainsDotDot(v string) bool

func Copy

func Copy(src string, dst string) error

CopyFile copies both files and directories

func Debug

func Debug(keyvals ...interface{})

func DecrIP

func DecrIP(ip net.IP, inc uint) net.IP

func DirEmpty

func DirEmpty(dirname string) (b bool)

DirEmpty returns true for non existant directories

func DirExists

func DirExists(dirname string) bool

func DiskUsage

func DiskUsage(path string) (all, free, used uint64, err error)

func DumpCookie

func DumpCookie(cks [][]*http.Cookie) []string

func EqualDomain

func EqualDomain(d1, d2 string) bool

func Error

func Error(keyvals ...interface{})

func ExtractRootDomain

func ExtractRootDomain(domain string) (string, error)

ExtractRootDomain extracts the EffectiveTLDPlusOne, see https://godoc.org/golang.org/x/net/publicsuffix#EffectiveTLDPlusOne for more explanations

func Fatal

func Fatal(keyvals ...interface{})

func FileExists

func FileExists(filename string) bool

func ForceIPHTTPClient

func ForceIPHTTPClient(ip, port string) *http.Client

func ForceIPWebsocketDialer

func ForceIPWebsocketDialer(ip, port string) *websocket.Dialer

func FormatHelloServerName

func FormatHelloServerName(servername string) (string, error)

func GetLocalPrivateIP

func GetLocalPrivateIP() (net.IP, error)

func GetLocalPublicIP

func GetLocalPublicIP() (net.IP, error)

func GetMonthYear

func GetMonthYear(n time.Time) int

func GuessImageFormat

func GuessImageFormat(r io.Reader) (format string, err error)

Guess image format from gif/jpeg/png/webp

func HashPassword

func HashPassword(password string) (string, error)

func IPUint

func IPUint(ip net.IP) uint32

func IPUintSl

func IPUintSl(ip []byte) uint32

func IncrIP

func IncrIP(ip net.IP, inc uint) net.IP

func Info

func Info(keyvals ...interface{})

func InitLogging

func InitLogging(level string)

func IsAlphaNumHyphen

func IsAlphaNumHyphen(s string) bool

func IsIPPrivateRFC1918

func IsIPPrivateRFC1918(ipuint uint32) bool

IsIPPrivateRFC1918 -> See https://tools.ietf.org/html/rfc1918

func IsMultipart

func IsMultipart(r *http.Request) bool

IsMultipart returns true if the given request is multipart forrm

func IsPowerOf2

func IsPowerOf2(x int) bool

func IsSlashRune

func IsSlashRune(r rune) bool

func IsValidEmail

func IsValidEmail(email string) error

func IsValidPassword

func IsValidPassword(pass string) error

func IsWebSocketRequest

func IsWebSocketRequest(req *http.Request) bool

func JSONMarshal

func JSONMarshal(t interface{}) ([]byte, error)

JSONMarshal does not escape HTML

func Max

func Max[T constraints.Ordered](x, y T) T

func MimetypeFromFileExtension

func MimetypeFromFileExtension(path string) (mt string)

func Min

func Min[T constraints.Ordered](x, y T) T

func NextHighestPowerOf2

func NextHighestPowerOf2(x uint32) uint32

func PathExists

func PathExists(path string) (bool, os.FileInfo)

func ReadCookies

func ReadCookies(h http.Header) [][]*http.Cookie

from request header Cookie

func ReadSetCookies

func ReadSetCookies(h http.Header) [][]*http.Cookie

from response header Set-Cookie

func Redirect2HTTPS

func Redirect2HTTPS(w http.ResponseWriter, req *http.Request)

func SecureToken32

func SecureToken32() (string, error)

func SendError

func SendError(w http.ResponseWriter, message, code string, statusCode int)

func SendErrorAndLog

func SendErrorAndLog(w http.ResponseWriter, message, code string, statusCode int, origin string, err error)

func SendInternalError

func SendInternalError(w http.ResponseWriter, origin string, err error)

func SendSuccess

func SendSuccess(w http.ResponseWriter, resp interface{})

func SetKeepaliveParameters

func SetKeepaliveParameters(conn *net.TCPConn) error

Sets additional keepalive parameters. Uses new interfaces introduced in Go1.11, which let us get connection's file descriptor, without blocking, and therefore without uncontrolled spawning of threads (not goroutines, actual threads).

func SplitHostPort

func SplitHostPort(hostport string) (string, string)

func SplitSlash

func SplitSlash(url string) []string

func String2ByteSlice

func String2ByteSlice(str string) []byte

Do not apply it after a ByteSlice2String was applied

func StripPort

func StripPort(hostport string) string

StripPort also removes IPV6 addresses square brackets

func UintIP

func UintIP(v uint32) net.IP

func UniqueSecureID120

func UniqueSecureID120() (string, error)

func UniqueSecureID36

func UniqueSecureID36() (string, error)

func UniqueSecureID60

func UniqueSecureID60() (string, error)

func UploadEmbedFolderMultipart

func UploadEmbedFolderMultipart(client *http.Client, url, method string, relfilepaths []string, files []fs.File, fields ...string) (*http.Response, error)

func UploadFileMultipart

func UploadFileMultipart(client *http.Client, url string, path string) (*http.Response, error)

func UploadFolderMultipart

func UploadFolderMultipart(client *http.Client, url, method string, folder string, fields ...string) (*http.Response, error)

func Varint

func Varint(n int64) []byte

func VerifyTXT

func VerifyTXT(domain, token string) (bool, error)

func Warn

func Warn(keyvals ...interface{})

Types

type CertificateAuthority

type CertificateAuthority struct {
	// contains filtered or unexported fields
}

func NewCertificateAuthority

func NewCertificateAuthority(organization pkix.Name) (*CertificateAuthority, error)

func UnmarshalCertificateAuthority

func UnmarshalCertificateAuthority(b []byte) (*CertificateAuthority, error)

func (*CertificateAuthority) CertificateBytes

func (ca *CertificateAuthority) CertificateBytes() []byte

func (*CertificateAuthority) CreateDNSRSACertificate

func (ca *CertificateAuthority) CreateDNSRSACertificate(dnsNames []string) (tls.Certificate, error)

func (*CertificateAuthority) Marshal

func (ca *CertificateAuthority) Marshal() ([]byte, error)

type JSONErr

type JSONErr struct {
	Success bool   `json:"success"`
	Message string `json:"message,omitempty"`
	Code    string `json:"code,omitempty"`
}

func UnmarshalJSONErr

func UnmarshalJSONErr(b []byte) *JSONErr

type LimitedBuffer

type LimitedBuffer struct {
	sync.Mutex
	// contains filtered or unexported fields
}

LimitedBuffer is like a bytes.Buffer but dumps everything that exceeds maxLen

func NewLimitedBuffer

func NewLimitedBuffer(maxLen int) *LimitedBuffer

NewLimitedBuffer returns a new Limitedbuffer with a defined maxLen

func (*LimitedBuffer) Bytes

func (b *LimitedBuffer) Bytes() []byte

func (*LimitedBuffer) CopyBytes

func (b *LimitedBuffer) CopyBytes() []byte

func (*LimitedBuffer) String

func (b *LimitedBuffer) String() string

func (*LimitedBuffer) Write

func (b *LimitedBuffer) Write(in []byte) (int, error)

type ProxyReader

type ProxyReader struct {
	// contains filtered or unexported fields
}

func NewProxyReader

func NewProxyReader(underlying io.Reader, progress chan int) *ProxyReader

func (*ProxyReader) Read

func (pr *ProxyReader) Read(b []byte) (int, error)

type TeeWriter

type TeeWriter struct {
	// contains filtered or unexported fields
}

func NewTeeWriter

func NewTeeWriter(wrs ...io.Writer) *TeeWriter

TeeWriter returns the first writer results

func (*TeeWriter) Write

func (t *TeeWriter) Write(b []byte) (int, error)

Jump to

Keyboard shortcuts

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