Skip to content

Instantly share code, notes, and snippets.

View nanmu42's full-sized avatar

Zhennan LI nanmu42

View GitHub Profile
@nanmu42
nanmu42 / GetMyIP.go
Created June 6, 2018 10:22
Get My IP - Golang
package main
import (
"errors"
"fmt"
"net"
)
func main() {
@nanmu42
nanmu42 / airdroper.sol
Created August 2, 2018 04:36
Airdropper for any ERC20 contract
pragma solidity ^0.4.24;
interface Token {
function transfer(address _to, uint256 _value) external returns (bool);
}
contract onlyOwner {
address public owner;
/**
* @dev The Ownable constructor sets the original `owner` of the contract to the sender
@nanmu42
nanmu42 / random_string.js
Created March 7, 2019 07:52
Javascript Ramdom String With Specific Length
// change 10 to the length desired
[...Array(10)].map(i=>(~~(Math.random()*36)).toString(36)).join('')
@nanmu42
nanmu42 / randomstring.go
Created March 7, 2019 10:25
Superfast Golang Random String
// credits: https://stackoverflow.com/questions/22892120/how-to-generate-a-random-string-of-a-fixed-length-in-go
import (
"math/rand"
"time"
)
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
const (
letterIdxBits = 6 // 6 bits to represent a letter index
@nanmu42
nanmu42 / Dockerfile
Last active May 15, 2019 08:01
Docker loves Vue.js
FROM node:10.15 as builder
WORKDIR /project
COPY . .
RUN npm i && npm run lint && npm run build && rm -f dist/**/*.map
FROM nginx:stable
# COPY copies folder content, not folder itself
COPY --from=builder /project/dist /var/www
COPY ./nginx.conf /etc/nginx/conf.d/default.conf
@nanmu42
nanmu42 / compress.sh
Last active May 16, 2019 05:00
ffmpeg compressing video for web browser
#!/usr/bin/env bash
# preset veryslow: more time for more compressing rate
# -movflags +faststart optimizes for web browser so video can be played sooner.
# crf: small = better quality + bigger size; sane option between 17–28
SRC="$1"
CRF="$2"
ffmpeg -i "$SRC" -vcodec libx264 -crf "$CRF" -movflags +faststart -preset veryslow -profile:v high -level 4.1 web_libx264_crf_${CRF}_faststart_veryslow.mp4
@nanmu42
nanmu42 / optimize_img.sh
Created August 5, 2019 03:48
Opitimize JPEG and PNG to a both smaller file size and resolution
#!/usr/bin/env bash
FOLDER=$1
MAX_WIDTH=1400
MAX_HEIGHT=1000
# resize, strip metadata and save in a lower quality
# aspect ratio will be kept.
find ${FOLDER} \( -iname '*.jpg' -o -iname '*.jpeg' -o -iname '*.png' \) -exec convert \{} -verbose -strip -quality 75 -resize "${MAX_WIDTH}x${MAX_HEIGHT}>" \{} \;
@nanmu42
nanmu42 / geoOffset.go
Created August 21, 2019 08:28
计算从 (lon0, lat0) 经纬度(单位 °)移动 dx, dy 米后的新的经纬度
// geoOffset 计算从 (lon0, lat0) 经纬度(单位 °)移动 dx, dy 米后的新的经纬度
//
// 这个算法在100km以内有良好精度。
//
// source: https://stackoverflow.com/questions/2839533/adding-distance-to-a-gps-coordinate
func geoOffset(lon0, lat0, dx, dy float64) (lon, lat float64) {
lon = lon0 + (180/math.Pi)*(dx/6378137)/math.Cos(lat0/180*math.Pi)
lat = lat0 + (180/math.Pi)*(dy/6378137)
return
@nanmu42
nanmu42 / check_cert_valid_date.sh
Created August 30, 2019 05:58
Check valid date of certificate
echo | openssl s_client -servername www.github.com -connect www.github.com:443 2>/dev/null | openssl x509 -noout -dates
@nanmu42
nanmu42 / scanner_valuer.go
Created October 23, 2019 07:29
Golang SQL scanner and valuer example
// QueryParams every key is a query param
type QueryParams map[string]QueryParam
// Scan satisfies SQL scanner interface
func (q *QueryParams) Scan(src interface{}) error {
b, ok := src.([]byte)
if !ok {
return errors.New("type assertion to []byte failed")
}