utils

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2019 License: MIT Imports: 17 Imported by: 36

Documentation

Index

Constants

View Source
const (
	SecretTokenLength = 26
)

Variables

View Source
var (
	// slingamn's own private b32 alphabet, removing 1, l, o, and 0
	B32Encoder = base32.NewEncoding("abcdefghijkmnpqrstuvwxyz23456789").WithPadding(base32.NoPadding)
)
View Source
var (
	ErrInvalidParams = errors.New("Invalid parameters")
)
View Source
var (
	IPv4LoopbackAddress = net.ParseIP("127.0.0.1").To16()
)

Functions

func AddrIsLocal added in v0.11.0

func AddrIsLocal(addr net.Addr) bool

AddrIsLocal returns whether the address is from a trusted local connection (loopback or unix).

func AddrIsUnix added in v0.11.0

func AddrIsUnix(addr net.Addr) bool

AddrIsUnix returns whether the address is a unix domain socket.

func AddrToIP added in v0.11.0

func AddrToIP(addr net.Addr) net.IP

AddrToIP returns the IP address for a net.Addr; unix domain sockets are treated as IPv4 loopback

func ArgsToStrings

func ArgsToStrings(maxLength int, arguments []string, delim string) []string

ArgsToStrings takes the arguments and splits them into a series of strings, each argument separated by delim and each string bounded by maxLength.

func BitsetCopy added in v1.0.0

func BitsetCopy(set []uint32, other []uint32)

BitsetCopy copies the contents of `other` over `set`. Similar caveats about race conditions as with `BitsetUnion` apply.

func BitsetEmpty added in v0.12.0

func BitsetEmpty(set []uint32) (empty bool)

BitsetEmpty returns whether the bitset is empty. This has false positives under concurrent modification (i.e., it can return true even though w.r.t. the sequence of atomic modifications, there was no point at which the bitset was completely empty), but that's not how we're using this method.

func BitsetGet added in v0.12.0

func BitsetGet(set []uint32, position uint) bool

BitsetGet returns whether a given bit of the bitset is set.

func BitsetSet added in v0.12.0

func BitsetSet(set []uint32, position uint, on bool) (changed bool)

BitsetSet sets a given bit of the bitset to 0 or 1, returning whether it changed.

func BitsetSubtract added in v1.0.0

func BitsetSubtract(set []uint32, other []uint32)

BitsetSubtract modifies `set` to subtract the contents of `other`. Similar caveats about race conditions as with `BitsetUnion` apply.

func BitsetUnion added in v0.12.0

func BitsetUnion(set []uint32, other []uint32)

BitsetUnion modifies `set` to be the union of `set` and `other`. This has race conditions in that we don't necessarily get a single consistent view of `other` across word boundaries.

func CopyFile added in v0.12.0

func CopyFile(src string, dst string) (err error)

implementation of `cp` (go should really provide this...)

func FieldsFuncN added in v1.0.0

func FieldsFuncN(s string, f func(rune) bool, n int) []string

FieldsFuncN is like strings.FieldsFunc, but returns at most n fields, and the nth field includes any runes at the end of the string normally excluded by f.

func FieldsN added in v1.0.0

func FieldsN(s string, n int) []string

FieldsN is like strings.Fields, but returns at most n fields, and the nth field includes any whitespace at the end of the string.

func GenerateSecretKey added in v1.1.0

func GenerateSecretKey() string

generate a 256-bit secret key that can be written into a config file

func GenerateSecretToken added in v1.0.0

func GenerateSecretToken() string

generate a secret token that cannot be brute-forced via online attacks

func IPInNets added in v1.0.0

func IPInNets(ip net.IP, nets []net.IPNet) bool

Convenience to test whether `ip` is contained in any of `nets`.

func IsHostname

func IsHostname(name string) bool

IsHostname returns whether we consider `name` a valid hostname.

func LookupHostname

func LookupHostname(addr string) string

LookupHostname returns the hostname for `addr` if it has one. Otherwise, just returns `addr`.

func MungeSecretToken added in v1.1.0

func MungeSecretToken(token string) (result string)

"munge" a secret token to a new value. requirements: 1. MUST be roughly as unlikely to collide with `GenerateSecretToken` outputs as those outputs are with each other 2. SHOULD be deterministic (motivation: if a JOIN line has msgid x, create a deterministic msgid y for the fake HistServ PRIVMSG that "replays" it) 3. SHOULD be in the same "namespace" as `GenerateSecretToken` outputs (same length and character set)

func NetToNormalizedString added in v1.0.0

func NetToNormalizedString(network net.IPNet) string

Given a network, produce a human-readable string (i.e., CIDR if it's actually a network, IPv6 address if it's a v6 /128, dotted quad if it's a v4 /32).

func NormalizeIPToNet added in v1.0.0

func NormalizeIPToNet(addr net.IP) (network net.IPNet)

NormalizeIPToNet represents an address (v4 or v6) as the v6 /128 CIDR containing only it.

func NormalizeNet added in v1.0.0

func NormalizeNet(network net.IPNet) (result net.IPNet)

NormalizeNet normalizes an IPNet to a v6 CIDR, using the 4-in-6 prefix. (this is like IP.To16(), but for IPNet instead of IP)

func NormalizedNetFromString added in v1.0.0

func NormalizedNetFromString(str string) (result net.IPNet, err error)

Parse a human-readable description (an address or CIDR, either v4 or v6) into a normalized v6 net.IPNet.

func ParseNetList added in v1.0.0

func ParseNetList(netList []string) (nets []net.IPNet, err error)

Parse a list of IPs and nets as they would appear in one of our config files, e.g., proxy-allowed-from or a throttling exemption list.

func SecretTokensMatch added in v1.0.0

func SecretTokensMatch(storedToken string, suppliedToken string) bool

securely check if a supplied token matches a stored token

func StringToBool added in v1.1.0

func StringToBool(str string) (result bool, err error)

func WordWrap added in v1.0.0

func WordWrap(text string, lineWidth int) []string

WordWrap wraps the given text into a series of lines that don't exceed lineWidth characters.

Types

type MessagePair added in v1.1.0

type MessagePair struct {
	Message string
	Msgid   string
}

type Once added in v1.1.0

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

Once is a fork of sync.Once to expose a Done() method.

func (*Once) Do added in v1.1.0

func (o *Once) Do(f func())

func (*Once) Done added in v1.1.0

func (o *Once) Done() bool

type Semaphore added in v1.1.0

type Semaphore (chan e)

Semaphore is a counting semaphore. A semaphore of capacity 1 can be used as a trylock.

func (*Semaphore) Acquire added in v1.1.0

func (semaphore *Semaphore) Acquire()

Acquire acquires a semaphore, blocking if necessary.

func (*Semaphore) AcquireWithContext added in v1.2.0

func (semaphore *Semaphore) AcquireWithContext(ctx context.Context) (acquired bool)

AcquireWithContext tries to acquire a semaphore, blocking at most until the context expires. It returns whether the acquire was successful. Note that if the context is already expired, the acquire may succeed anyway.

func (*Semaphore) AcquireWithTimeout added in v1.1.0

func (semaphore *Semaphore) AcquireWithTimeout(timeout time.Duration) (acquired bool)

AcquireWithTimeout tries to acquire a semaphore, blocking for a maximum of approximately `d` while waiting for it. It returns whether the acquire was successful.

func (*Semaphore) Initialize added in v1.1.0

func (semaphore *Semaphore) Initialize(capacity int)

Initialize initializes a semaphore to a given capacity.

func (*Semaphore) Release added in v1.1.0

func (semaphore *Semaphore) Release()

Release releases a semaphore. It never blocks. (This is not a license to program spurious releases.)

func (*Semaphore) TryAcquire added in v1.1.0

func (semaphore *Semaphore) TryAcquire() (acquired bool)

TryAcquire tries to acquire a semaphore, returning whether the acquire was successful. It never blocks.

type SplitMessage added in v1.0.0

type SplitMessage struct {
	MessagePair
	Wrapped []MessagePair // if this is nil, `Message` didn't need wrapping and can be sent to anyone
	Time    time.Time
}

SplitMessage represents a message that's been split for sending.

func MakeSplitMessage added in v1.0.0

func MakeSplitMessage(original string, origIs512 bool) (result SplitMessage)

type TokenLineBuilder added in v1.2.0

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

TokenLineBuilder is a helper for building IRC lines composed of delimited tokens, with a maximum line length.

func (*TokenLineBuilder) Add added in v1.2.0

func (t *TokenLineBuilder) Add(token string)

Add adds a token to the line, creating a new line if necessary.

func (*TokenLineBuilder) Initialize added in v1.2.0

func (t *TokenLineBuilder) Initialize(lineLen int, delim string)

func (*TokenLineBuilder) Lines added in v1.2.0

func (t *TokenLineBuilder) Lines() (result []string)

Lines terminates the line-building and returns all the lines.

Jump to

Keyboard shortcuts

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