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 / MemoryEater.java
Created June 19, 2018 22:34
Recover from OutOfmemoryException
package name.stokito.memeater;
import org.apache.derby.iapi.services.memory.LowMemory;
import java.lang.Thread.UncaughtExceptionHandler;
import java.util.ArrayList;
import java.util.List;
public class MemoryEater {
private static final int MB = 1048576;
/**This example illustrates how to extract the sign (the leftmost bit), exponent (the 8 following bits) and mantissa (the 23 rightmost bits) from a float in Java.*/
void floatDestuct() {
int bits = Float.floatToIntBits(-0.005f);
int sign = bits >>> 31;
int exp = (bits >>> 23 & ((1 << 8) - 1)) - ((1 << 7) - 1);
int mantissa = bits & ((1 << 23) - 1);
System.out.println(sign + " " + exp + " " + mantissa + " " +
Float.intBitsToFloat((sign << 31) | (exp + ((1 << 7) - 1)) << 23 | mantissa));
}
@stokito
stokito / binary_search.asm
Created March 17, 2019 14:40
Binary Search on x8085 ASM
; Just for test purposes it would be nice to automatically generate test array (haystack) in which to find a needle.
; Fill memory with 8 elements: 0, 1.. 7 and value of element corresponds to it's addresses.
mvi b, 7 ; last index to fill untill
mvi h, 0 ; set up HL as memory pointer
mvi l, 0 ; set up HL as memory pointer
loop: inr l ; increase l by one
mov m, l
mov a, l
cmp b
jm loop
Motorola Pulse Plus Headphones Buttons Operation
Music
Play/Pause: Press [Call 📞]
Volume Down/Up: Press [ -< ] or [ >+ ]
Previous / Next Track: Long press [ -< ] or [ >+ ]
Change Equalizer: Long press and [ -< ] AND [ >+ ]
@stokito
stokito / firewall.user
Last active January 7, 2024 22:06 — forked from Manouchehri/cloudflare.sh
OpenWrt: Allow only CloudFlare to access HTTP 80 and HTTPS 443 ports. Use if your uhttpd is hidden behind CF. Put this file to /etc/firewall.user. NOTE: It uses HTTP to get the list of IPs because to wget via https we need to install ca-certs. This makes you vulnerable to MiTM attacks but that's ok to be protected from internet's hackers
# https://www.cloudflare.com/ips replace the ips-v4 with ips-v6 if needed
# https://blog.cloudflare.com/cloudflare-now-supporting-more-ports/
for ip in `wget -qO- http://www.cloudflare.com/ips-v4`; do
iptables -I INPUT -p tcp -m multiport --dports 80,443,8080,8443,2052,2053,2082,2083,2086,2087,2095,2096,8880 -s $ip -j ACCEPT
done
@stokito
stokito / logback.xml
Created August 14, 2019 22:40
Get rid off Janino condition tags for the pattern and simplify it with variable reference The <if> tag requires the Janino library with full java compiler while we just need a simple pattern matching by env var. The solution is to declare two <property> constants with the pattern and then chose one of the constants as a pattern. If SINGLE_LINE_P…
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<property scope="context" name="pattern.singleLine.true" value="%d{yyyy-MM-dd'T'HH:mm:ss.SSS'Z', UTC} %5p %logger [%t] \(%X{trace}\) %X{loggingMessageExtender}: %replace(%m%n%ex){'\n(?=[^$])','\\n'}%nopex" />
<property scope="context" name="pattern.singleLine.false" value="%d{yyyy-MM-dd'T'HH:mm:ss.SSS'Z', UTC} -%5p %logger [%t] \(%X{trace}\) %X{loggingMessageExtender}: %m%n" />
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<Pattern>${pattern.singleLine.${SINGLE_LINE_PATTERN:-false}}"</Pattern>
</encoder>
</appender>
package com.github.stokito.experiments;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.openjdk.jmh.annotations.Mode.Throughput;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Locale;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Level;
@stokito
stokito / create_patch.sh
Last active February 16, 2024 07:44
git: create a single patch file with multiple commits
# last three commits
git format-patch -3 --stdout > multi_commit.patch
# all commits that are in your branch and not in master into a single patch file multi_commit.patch
git format-patch --signoff master --stdout > multi_commit.patch
# create patches in the folder ~/output/directory/ for all commits that are in your branch and not in master
git format-patch -o ~/output/directory/ --signoff master
@stokito
stokito / edit_file.sh
Created September 12, 2019 17:08
The one-liner to open a file in editor (pluma). It will show a file chose dialog and if the file is owned by root it will ask for a sudo password
FILE=$(zenity --file-selection); echo $FILE; if [ -w "$FILE" ]; then pluma "$FILE"; else pkexec pluma "$FILE"; fi;
@stokito
stokito / txt
Created October 28, 2019 00:02
replace single line comments // with multiline comments /*
Search pattern: (.*)(//)(.*)(\n)
Replace with: $1/\*$3 \*/$4