Skip to content

Instantly share code, notes, and snippets.

View stokito's full-sized avatar
Self-hosting become easier 🤗

Sergey Ponomarev stokito

Self-hosting become easier 🤗
View GitHub Profile
@stokito
stokito / addr_is_ip.go
Created October 16, 2021 07:15
Check that string is an IP address
// IsIp checks that the given string is IPv4 or IPv6 address.
func IsIp(addr string) bool {
// IPv6
if addr[0] == '[' {
return true
}
// domain can have digits, but it can't have ONLY digits
// So if all symbols are digits then this is an IPv4 address. We can check only first octet before dot .
package bytesmap
var m = make(map[string]*string)
// this works faster
func GetOrDefaultInline(host []byte) *string {
hc, found := m[string(host)]
if !found {
m[string(host)] = nil
}
@stokito
stokito / json_encode_text.go
Created November 17, 2021 14:48
Fast json text value encode: replace
// EncodeToJsonAsciiText Fast json text value encode: escape " with \" and \ with \\
// Any non printable or Unicode chars will be removed.
// Please note that value is not enclosed into double quotes.
func EncodeToJsonAsciiText(plain string) string {
plainLen := len(plain)
newSize := plainLen
for i := 0; i < plainLen; i++ {
c := plain[i]
if c < 32 || c > 127 { // skip non printable and unicode
newSize--
@stokito
stokito / grpc_error_handling.go
Created November 29, 2021 21:29
GRPC catch error
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"reflect"
"strconv"
)
// GrpcError The interface the same as internal type Error from grpc: internal/status/status.go
@stokito
stokito / ipv4_parse.go
Created December 1, 2021 18:48
Golang parse IPv4 to bytes array without unnecessary allocation
package main
// parseIPv4 fast path clone of net.ParseIP that don't wrap IPv4 into IPv6 array
func parseIPv4(s string) []byte {
var p [4]byte
for i := 0; i < 4; i++ {
octet, pos := dtoi(s)
p[i] = octet
if i < 3 {
s = s[pos+1:]
package main
// UInt16Slice is a sorted slice
type UInt16Slice []uint16
// BinSearch binary search over the sorted sclice
func (a UInt16Slice) BinSearch(key uint16) int {
low := 0
high := len(a) - 1
for low <= high {
@stokito
stokito / b64.md
Last active February 12, 2022 09:46
Base64 in shell scripts (ash,bash)

Base64 URL safe in shell scripts

The Base64 url safe already supported by basenc command. But it may be not pre-installed on many platfroms.

You can call base64 command but there is a pitfalls:

  • On most Linux distros the base64 command comes from coreutils package. And it already have a basenc command
  • On OpenWrt the base64 is not installed by default and comes from coreutils-base64 package and the baseenc command from coreutils-baseenc
  • You can enable the base64 in BusyBox i.e. if you building the OpenWrt image yourself.
  • You may use openssl base64 or openssl enc -base64 see docs but it will read by lines so you must also specify -A param.
  • On MacOS the -d decode flag must be in upper case base64 -D so most users use --decode flag. Previously MacOS used the -d for debug but it's not clear who ever used it. Then Applce just removed the -d param but again it's not clear why they don't changed it to be compatible with coreutil
@stokito
stokito / lighttpd_letsencrypt.md
Last active January 31, 2022 01:26
Lighttpd How to Configure Let’s Encrypt TLS

Firts you need to enable mod_openssl

/usr/sbin/lighttpd-enable-mod ssl

Then edit /etc/lighttpd/conf-enabled/10-ssl.conf :

vi /etc/lighttpd/conf-enabled/10-ssl.conf

Adjust to: