utils

package
v0.5.3 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2026 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const Authorization string = "Authorization"

Authorization for header key

View Source
const CacheControl string = "Cache-Control"

CacheControl for header key

View Source
const CacheControlMustRevalidate string = "must-revalidate, post-check=0, pre-check=0"

CacheControlMustRevalidate for header key

View Source
const Connection string = "Connection"

Connection for header key

View Source
const ContentDescription string = "Content-Description"

ContentDescription for header key

View Source
const ContentDisposition string = "Content-Disposition"

ContentDisposition for header key

View Source
const ContentLength string = "Content-Length"

ContentLength for header key

View Source
const ContentTransferEncoding string = "Content-Transfer-Encoding"

ContentTransferEncoding for header key

View Source
const ContentType string = "Content-Type"

ContentType for header key

View Source
const Expires string = "Expires"

Expires for header key

View Source
const JSONApplication string = "application/json"

JSONApplication for Content-Type headers

View Source
const JSONApplicationUTF8 string = JSONApplication + "; charset=UTF-8"

JSONApplicationUTF8 for Content-Type headers, UTF charset

View Source
const KeepAlive string = "Keep-Alive"

KeepAlive for Connection headers

View Source
const OctetStream string = "application/octet-stream"

OctetStream for Content-Type headers

View Source
const OsctrlUserAgent string = "osctrl-http-client/1.1"

OsctrlUserAgent for customized User-Agent

View Source
const Pragma string = "Pragma"

Pragma for header key

View Source
const PragmaPublic string = "public"

PragmaPublic for header key

View Source
const TextPlain string = "text/plain"

TextPlain for Content-Type headers

View Source
const TextPlainUTF8 string = TextPlain + "; charset=UTF-8"

TextPlainUTF8 for Content-Type headers, UTF charset

View Source
const TransferEncodingBinary string = "binary"

TransferEncodingBinary for header key

View Source
const UserAgent string = "User-Agent"

UserAgent for header key

View Source
const XForwardedFor string = "X-Forwarded-For"
View Source
const XRealIP string = "X-Real-IP"

XRealIP for header key

Variables

View Source
var (
	// OneMinute - 60 seconds
	OneMinute = 60 * time.Second
	// OneHour - 3600 seconds
	OneHour = 60 * time.Minute
	// SixHours - 21600 seconds
	SixHours = 6 * time.Hour
	// OneDay - 86400 seconds
	OneDay = 24 * time.Hour
	// FifteenDays - 1296000 seconds
	FifteenDays = 15 * OneDay
)

Functions

func AcceptsJSON added in v0.5.2

func AcceptsJSON(r *http.Request) bool

AcceptsJSON reports whether the request's Accept header signals JSON. Used by the auth middleware to choose between 401 JSON (for SPA/XHR clients) and 302 redirect (for browser navigation).

func BytesReceivedConversion

func BytesReceivedConversion(b int) string

BytesReceivedConversion - Helper to format bytes received into KB, MB, TB... Binary format

func CheckUUID

func CheckUUID(s string) bool

CheckUUID - Helper to check if a string is a valid UUID

func Contains added in v0.4.7

func Contains(all []string, target string) bool

Contains checks if string is in the slice

func DebugHTTP

func DebugHTTP(r *http.Request, showBody bool) string

DebugHTTP - Helper for debugging purposes and dump a full HTTP request

func DebugHTTPDump

func DebugHTTPDump(l *zerolog.Logger, r *http.Request, showBody bool)

DebugHTTPDump - Helper for debugging purposes and dump a full HTTP request

func DurationSeconds

func DurationSeconds(duration time.Duration) int

DurationSeconds - Helper to get the seconds value fom a Duration

func GenKSUID

func GenKSUID() string

GenKSUID - Helper to generate a KSUID See https://github.com/segmentio/ksuid for more info about KSUIDs

func GenRandomString

func GenRandomString(n int) string

GenRandomString - Helper to generate a random string of n characters

func GenUUID

func GenUUID() string

GenUUID - Helper to generate a UUID See https://github.com/google/uuid for more info about UUIDs

func GetIP

func GetIP(r *http.Request) string

GetIP returns the client IP for r. When trusted-proxies are configured AND r.RemoteAddr's IP is inside one of them, the right-most untrusted hop from X-Forwarded-For (or X-Real-IP) is used (per RFC 7239 §5.2 the right-most-untrusted is the IP the trusted edge actually saw connect). Otherwise the forwarding headers are ignored and the connection's RemoteAddr IP is returned.

func HTTPDownload

func HTTPDownload(w http.ResponseWriter, description, filename string, filesize int64)

HTTPDownload - Helper to send HTTP response with a file

func HTTPResponse

func HTTPResponse(w http.ResponseWriter, cType string, code int, data interface{})

HTTPResponse - Helper to send HTTP response

func InFutureTime

func InFutureTime(t time.Time) string

InFutureTime - Helper to format future times only returning one value (minute, hour, day)

func Intersect

func Intersect(slice1, slice2 []uint) []uint

Intersect returns the intersection of two slices of uints

func PastFutureTimes

func PastFutureTimes(t time.Time) string

PastFutureTimes - Helper to format past or future times

func PastFutureTimesEpoch

func PastFutureTimesEpoch(ts int64) string

PastFutureTimesEpoch - Helper to format past or future times

func PastTimeAgo

func PastTimeAgo(t time.Time) string

PastTimeAgo - Helper to format past times only returning one value (minute, hour, day)

func RandomForNames

func RandomForNames() string

Helper to generate a random MD5 to be used with queries/carves

func RemoteIP added in v0.5.3

func RemoteIP(r *http.Request) string

RemoteIP returns the direct connection peer's IP, ignoring every forwarding header. Unlike GetIP, this never trusts X-Forwarded-For or X-Real-IP even when --trusted-proxies is configured.

Use RemoteIP for any key that an attacker could weaponize by rotating header values — chiefly rate-limit bucket keys. A typical edge proxy (nginx, ELB, Cloudflare with default settings) appends to X-Forwarded-For rather than replacing it, so when GetIP walks right-to-left looking for the first untrusted hop it lands on an attacker-controlled value. RemoteIP defeats that bypass by keying on the TCP peer the trusted proxy itself terminates against, which the attacker cannot influence.

Use GetIP for audit-log fields and anything where the operator wants to see the "real" client IP even though it transits a proxy. The trade-off is documented: audit accuracy vs. rate-limit authenticity.

func SendRequest

func SendRequest(reqType, reqURL string, params io.Reader, headers map[string]string) (int, []byte, error)

SendRequest - Helper function to send HTTP requests

func SetTrustedProxies added in v0.5.2

func SetTrustedProxies(cidrs []string)

SetTrustedProxies configures the CIDR allowlist for forwarding-header trust. Pass an empty slice (or call with no args) to revert to the safe-by-default "ignore forwarding headers" posture. Each CIDR string must parse via net.ParseCIDR; invalid entries are logged and skipped.

func StringToBoolean

func StringToBoolean(s string) bool

StringToBoolean - Helper to convert a string into boolean

func StringToInteger

func StringToInteger(s string) int64

StringToInteger - Helper to convert a string into integer

func StringifyTime

func StringifyTime(seconds int) string

StringifyTime - Helper to get a string based on the difference of two times

func TimeTimestamp

func TimeTimestamp(t time.Time) string

TimeTimestamp - Helper to format times in timestamp format

Types

This section is empty.

Jump to

Keyboard shortcuts

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