Skip to content

Instantly share code, notes, and snippets.

@elliotwutingfeng
elliotwutingfeng / example.js
Created November 9, 2021 13:18
argMin and argMax in JavaScript
function argMin(arr) {
if (!arr.length) {
throw new Error('Array cannot be empty');
}
return arr.reduce(
(best_idx, curr, curr_idx, array) =>
curr < array[best_idx] ? curr_idx : best_idx,
0
)
}
@elliotwutingfeng
elliotwutingfeng / aws_ec2_unique_ipv4.py
Last active June 3, 2023 22:25
How many unique AWS EC2 IPv4 Addresses are there?
import ipaddress
import json
from collections import defaultdict
from itertools import chain
from urllib.request import urlopen
with urlopen("https://ip-ranges.amazonaws.com/ip-ranges.json") as req:
resp_json = json.loads(req.read())
ip_prefixes_and_regions = (
(x["ip_prefix"], x["region"])
@elliotwutingfeng
elliotwutingfeng / download_via_ssh.sh
Created January 19, 2022 19:28
Downloading file from remote host via ssh with rsync to current working directory
#!/bin/sh
# -a : archive mode
# -v : verbose
# -z : compress file data during the transfer
# -h : output numbers in a human-readable format
# -P : show progress during transfer and keep partially transferred files to make a subsequent transfer of the rest of the file much faster
# -e : Specify protocol to use; in our case it is ssh
rsync -avzhPe ssh <remote_username>@<remote_hostname>:/path/to/file ./
@elliotwutingfeng
elliotwutingfeng / generate_requirements_python.sh
Created January 21, 2022 21:17
Generate requirements.txt in Python
#!/bin/sh
# https://stackoverflow.com/a/69081814/13253866
# Generates requirements.txt for project in current working directory
pip3 install pipreqs
pip3 install pip-tools
pipreqs --savepath=requirements.in && pip-compile && rm requirements.in
@elliotwutingfeng
elliotwutingfeng / how-to-tmux-background-process.md
Created February 2, 2022 01:14 — forked from davydany/how-to-tmux-background-process.md
Using TMUX for running processes after you log off

How to Run a Process in the Background with TMUX

There are times when you need to log off your Linux Desktop, and you want a process to run in the background. TMUX manages this very well.

For this example, let's suppose you're running a long running task like running rspecs on your project and it is 5pm, and you need to go home.

Run Your Process

@elliotwutingfeng
elliotwutingfeng / ssh_keepalive.md
Created February 4, 2022 02:33
Preventing tmux sessions from getting killed

Preventing SSH tmux sessions from getting automatically killed

On client

Set the following in /etc/ssh/ssh_config

Host *
    ServerAliveInterval 300
    ServerAliveCountMax 3

On server

Set the following in /etc/ssh/sshd_config

@elliotwutingfeng
elliotwutingfeng / Zelo72_Pihole_Big.txt
Created February 11, 2022 05:24
Zelo*s 'Big' Blocklist - for a better internet
This file has been truncated, but you can view the full file.
# ----------------------------------------------
# Zelo*s "Big" Blocklist - for a better internet
# ----------------------------------------------
#
# Title: Zelo72 - Big
#
# An all in one blocklist that can be used as a
# stand alone blocklist. For every region.
#
# Blocks:
@elliotwutingfeng
elliotwutingfeng / pycurl_helper.py
Created February 11, 2022 09:46
Make HTTP GET or POST request with retry attempts and backoff delay between attempts, using CURL.
"""
Requires libcurl (https://curl.se/libcurl)
"""
import time
import pycurl # pip3 install pycurl
def backoff_delay(backoff_factor: float,number_of_retries_made: int) -> None:
"""Time delay that exponentially increases with `number_of_retries_made`
Args:
@elliotwutingfeng
elliotwutingfeng / ssb_reverse.c
Created February 22, 2022 14:02 — forked from kcchu/ssb_reverse.c
SSB Reverse
char ____ZN7Backend6Google12SSBUtilities24shouldConsultWithTencentEv_block_invoke_2(void * _block) {
rax = [NSLocale currentLocale];
rax = [rax retain];
r14 = [[rax countryCode] retain];
[rax release];
rbx = [r14 isEqualToString:@"CN"] != 0x0 ? 0x1 : 0x0;
[r14 release];
rax = rbx;
return rax;
}

Setting up multi-node ray

Head node

systemd-run --scope --user tmux # or `tmux`
sudo ufw allow in 6379
sudo ufw allow out 6379
sudo ufw allow in 37347
sudo ufw allow out 37347
sudo ufw allow in 37348