Skip to content

Instantly share code, notes, and snippets.

View nanmu42's full-sized avatar

Zhennan LI nanmu42

View GitHub Profile
@nanmu42
nanmu42 / isPrivateIP.go
Created January 19, 2021 02:19
Golang: check if IP address is private
// source: https://stackoverflow.com/questions/41240761/check-if-ip-address-is-in-private-network-space
var privateIPBlocks []*net.IPNet
func init() {
for _, cidr := range []string{
"127.0.0.0/8", // IPv4 loopback
"10.0.0.0/8", // RFC1918
"172.16.0.0/12", // RFC1918
"192.168.0.0/16", // RFC1918
"169.254.0.0/16", // RFC3927 link-local
@nanmu42
nanmu42 / README.md
Created October 15, 2023 08:47
Example Dockerfile for Python Poetry Projects
@nanmu42
nanmu42 / pkcs7padding.go
Last active October 3, 2023 06:55
Golang PKCS7 Padding/Unpadding
// pkcs7strip remove pkcs7 padding
func pkcs7strip(data []byte, blockSize int) ([]byte, error) {
length := len(data)
if length == 0 {
return nil, errors.New("pkcs7: Data is empty")
}
if length%blockSize != 0 {
return nil, errors.New("pkcs7: Data is not block-aligned")
}
padLen := int(data[length-1])
@nanmu42
nanmu42 / Dockerfile
Last active July 27, 2023 10:18
Minimal-sized Golang Docker Image
FROM golang:1-alpine as golang
RUN apk --no-cache add git zip tzdata ca-certificates
# avoid go path, use go mod
WORKDIR /app
COPY . .
RUN CGO_ENABLED=0 go build -ldflags "-s -w -extldflags "-static"" ./cmd/appnamehere
WORKDIR /usr/share/zoneinfo
# -0 means no compression. Needed because go's
# tz loader doesn't handle compressed data.
RUN zip -r -0 /zoneinfo.zip .
@nanmu42
nanmu42 / update_tmux.sh
Last active July 10, 2023 12:01
Update tmux to latest version
#!/usr/bin/env bash
set -e
# dependecies
apt update
apt install -y git automake build-essential pkg-config libevent-dev libncurses5-dev byacc
# where our temp file locates
rm -rf /tmp/tmux
{
"123":"application/vnd.lotus-1-2-3",
"ez":"application/andrew-inset",
"aw":"application/applixware",
"atom":"application/atom+xml",
"atomcat":"application/atomcat+xml",
"atomsvc":"application/atomsvc+xml",
"ccxml":"application/ccxml+xml",
"cdmia":"application/cdmi-capability",
"cdmic":"application/cdmi-container",
@nanmu42
nanmu42 / openinbrowser.go
Created May 20, 2019 09:43
Golang: Open URL in Browser
func openBrowser(url string) {
var err error
switch runtime.GOOS {
case "linux":
err = exec.Command("xdg-open", url).Start()
case "windows":
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
case "darwin":
err = exec.Command("open", url).Start()
@nanmu42
nanmu42 / Flink: throttle messages to walkaround deadlock under heavy iteration feedback load.md
Last active October 11, 2021 11:15
Flink: throttle messages to walkaround deadlock under heavy iteration feedback load
@nanmu42
nanmu42 / Dockerfile
Created March 22, 2021 09:44
Golang: Dockerfile based on Debian
FROM golang:buster as golang
RUN apt-get update && \
DEBIAN_FRONTEND=noninteractive apt-get install -y \
ca-certificates \
tzdata \
build-essential \
tar \
bash
WORKDIR /app
COPY . .
@nanmu42
nanmu42 / firstn.go
Created March 8, 2021 08:10
Golang: returns first n UTF-8 character at Most
func firstNUTF8AtMost(s string, n int) string {
i := 0
for j := range s {
if i >= n {
return s[:j]
}
i++
}
return s
}