Skip to content

Instantly share code, notes, and snippets.

@hlzhang
hlzhang / Subfolder to git repo.md
Created June 30, 2020 10:28 — forked from korya/Subfolder to git repo.md
Convert subfolder into Git submodule
@hlzhang
hlzhang / osxvpnrouting.markdown
Created April 18, 2020 10:53 — forked from taldanzig/osxvpnrouting.markdown
Routing tips for VPNs on OS X

Routing tips for VPNs on OS X

When VPNs Just Work™, they're a fantastic way of allowing access to a private network from remote locations. When they don't work it can be an experience in frustration. I've had situations where I can connect to a VPN from my Mac, but various networking situations cause routing conflicts. Here are a couple of cases and how I've been able to get around them.

Specific cases

Case 1: conflicting additional routes.

In this example the VPN we are connecting to has a subnet that does not conflict with our local IP, but has additional routes that conflict in some way with our local network's routing. In my example the remote subnet is 10.0.x.0/24, my local subnet is 10.0.y.0/24, and the conflicting route is 10.0.0.0/8. Without the later route, I can't access all hosts on the VPN without manually adding the route after connecting to the VPN:

Build Linaro for Mac

1. Prepare env


Abe requires Bash version 4+.

brew install bash autogen dejagnu coreutils gcc gawk #binutils
#brew install libmpc gmp mpf
@hlzhang
hlzhang / fixContentRoot.sh
Last active September 26, 2019 21:57
Fix IDEA Intellij's issue "Module must not contain source root. The root already belongs to module". Intellij always add root module's src/main/resources into child modules in multi module maven projects and prevent me saving module config unless I remove them all manually. That's really sucks.
set -x
function fix_content_root() {
local in_directory="$1"
local targets=($(grep -E -l --include \*.iml '<content url="file://\$MODULE_DIR\$/(\.\./)+src/main/resources"' -r ${in_directory} | grep -v 'fixContentRoot.sh'))
for target in ${targets[@]}; do
echo ${target};
sed -Ei 's#<content url="file://\$MODULE_DIR\$/(\.\./)+src/main/resources" />##g' ${target};
sed -i '/<content url="file:\/\/$MODULE_DIR$\/..\/src\/main\/resources">/{:a;N;/<\/content>/!ba};/<sourceFolder url="file:\/\/$MODULE_DIR$\/..\/src\/main\/resources" type="java-resource" \/>/d' ${target};
sed -i '/<content url="file:\/\/$MODULE_DIR$\/..\/..\/src\/main\/resources">/{:a;N;/<\/content>/!ba};/<sourceFolder url="file:\/\/$MODULE_DIR$\/..\/..\/src\/main\/resources" type="java-resource" \/>/d' ${target};
@hlzhang
hlzhang / shared_mutex.c
Created August 13, 2019 06:01 — forked from ben-cohen/shared_mutex.c
Mutex in mmap memory shared between processes.
/*
* shared_mutex: Mutex in mmap memory shared between processes.
*
* Ben Cohen, July 2017.
*
* Compile using:
* gcc -o shared_mutex shared_mutex.c -ggdb -Wall -std=c99 -pthread
*
* Two processes are created with an mmap shared memory region
* containing an array of chars and a mutex. The mutex is created with
@hlzhang
hlzhang / run.tpl
Created July 25, 2019 10:37 — forked from efrecon/run.tpl
`docker inspect` template to regenerate the `docker run` command that created a container
docker run \
--name={{.Name}} \
{{range $e := .Config.Env}}--env={{printf "%q" $e}} \
{{end}}{{range $p, $conf := .NetworkSettings.Ports}}{{with $conf}}-p {{(index $conf 0).HostIp}}:{{(index $conf 0).HostPort}}:{{$p}} \
{{end}}{{end}}{{range $n, $conf := .NetworkSettings.Networks}}{{with $conf}}--network {{printf "%q" $n}} \
{{range $conf.Aliases}}--network-alias {{printf "%q" .}} {{end}} \
{{end}}{{end}}{{range $v := .HostConfig.VolumesFrom}}--volumes-from={{printf "%q" .}} \
{{end}}{{range $v := .HostConfig.Binds}}--volume={{printf "%q" .}} \
{{end}}{{range $l, $v := .Config.Labels}}--label {{printf "%q" $l}}={{printf "%q" $v}} \
{{end}}{{range $v := .HostConfig.CapAdd}}--cap-add {{printf "%q" .}} \
@hlzhang
hlzhang / docker-for-mac.md
Created July 19, 2019 10:22 — forked from BretFisher/docker-for-mac.md
Getting a Shell in the Docker for Mac Moby VM

2018 Update: Easiest option is Justin's repo and image

Just run this from your Mac terminal and it'll drop you in a container with full permissions on the Moby VM. This also works for Docker for Windows for getting in Moby Linux VM (doesn't work for Windows Containers).

docker run -it --rm --privileged --pid=host justincormack/nsenter1

more info: https://github.com/justincormack/nsenter1


@hlzhang
hlzhang / http_request.jsh
Created April 13, 2019 14:26
Send http request in java11 jshell
// jshell -v --add-modules=java.net.http http_request.jsh
import java.net.*;
import java.net.http.*;
HttpClient httpClient = HttpClient.newHttpClient();
HttpRequest httpRequest = HttpRequest.newBuilder().uri(new URI("https://graph.facebook.com/v2.9/me?fields=id,email,cover,first_name,gender,last_name,link,locale,name,timezone,updated_time,verified,friends&access_token=change_it&appsecret_proof=change_it&method=get&format=json")).GET().build();
HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
System.out.println(httpResponse.statusCode());
System.out.println(httpResponse.body());
@hlzhang
hlzhang / nginx-tuning.md
Created March 15, 2019 11:51 — forked from denji/nginx-tuning.md
NGINX tuning for best performance

Moved to git repository: https://github.com/denji/nginx-tuning

NGINX Tuning For Best Performance

For this configuration you can use web server you like, i decided, because i work mostly with it to use nginx.

Generally, properly configured nginx can handle up to 400K to 500K requests per second (clustered), most what i saw is 50K to 80K (non-clustered) requests per second and 30% CPU load, course, this was 2 x Intel Xeon with HyperThreading enabled, but it can work without problem on slower machines.

You must understand that this config is used in testing environment and not in production so you will need to find a way to implement most of those features best possible for your servers.

@hlzhang
hlzhang / ssh-d4m
Created June 27, 2018 15:06
SSH into Docker for Mac xhyve VM
1. screen ~/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux/tty
2. when done use CTRL-A CTRL-\ and then y (for yes) to exit
Alternative method:
1. docker run -it --privileged --pid=host debian nsenter -t 1 -m -u -n -i sh