Skip to content

Instantly share code, notes, and snippets.

@slavanap
slavanap / HostedMultiService.cs
Created June 28, 2021 15:26
HostedMultiService
public static class HostedServicesExtensions {
private class HostedMultiService<THostedService> : IHostedService where THostedService : class, IHostedService {
readonly THostedService[] _hostedServices;
public HostedMultiService(IEnumerable<THostedService> hostedServices) {
_hostedServices = hostedServices.ToArray();
if (_hostedServices.Any(s => s == null))
throw new ArgumentNullException(nameof(hostedServices));
}
@slavanap
slavanap / ListExtensions.cs
Last active June 27, 2021 15:43
List Extensions
/*
// * Summary *
BenchmarkDotNet=v0.13.0, OS=Windows 10.0.19042.1052 (20H2/October2020Update)
Intel Core i7-6600U CPU 2.60GHz (Skylake), 1 CPU, 4 logical and 2 physical cores
.NET SDK=6.0.100-preview.4.21255.9
[Host] : .NET 6.0.0 (6.0.21.25307), X64 RyuJIT
DefaultJob : .NET 6.0.0 (6.0.21.25307), X64 RyuJIT
@slavanap
slavanap / Bencoder.cs
Created April 6, 2021 16:21
Simple bencoder & decoder
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace DHT {
public class Bencoder {
@slavanap
slavanap / slavanap.subprocess.py
Last active January 11, 2021 20:47
Run function in subprocess in Python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# slavanap@gitlab
def subprocess(func):
def wrapper(v, kv, w):
w.send(func(*v, **kv))
w.close()
import multiprocessing
def realfunc(*vargs, **kvargs):
@slavanap
slavanap / interval_list.cs
Last active August 6, 2020 18:27
Interval list
/*
MIT license
Copyright 2020 Vyacheslav Napadovsky.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
FROM alpine
RUN apk add iptables
CMD /sbin/sysctl -w net.ipv4.conf.all.route_localnet=1 \
&& /sbin/iptables -t nat -A PREROUTING -p tcp -d 169.254.170.2 --dport 80 -j DNAT --to-destination 127.0.0.1:51679 \
&& /sbin/iptables -t nat -A OUTPUT -d 169.254.170.2 -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 51679 \
&& sleep 100000000000000
# docker build -t ecs-init -f ecs-init.dockerfile .
# docker run --name ecs-init -d --restart=always --net=host --privileged ecs-init
# docker run --name ecs-agent -d --restart=always --volume=/var/run:/var/run --volume=/var/log/ecs/:/log --volume=/var/lib/ecs/data:/data --volume=/etc/ecs:/etc/ecs --net=host --env-file=/etc/ecs/ecs.config amazon/amazon-ecs-agent
@slavanap
slavanap / precision_cut.cpp
Created June 28, 2020 13:30
Decimal to precise float precision cut
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <sstream>
#include <limits>
using namespace std;
int main() {
char buf [100];
@slavanap
slavanap / organize_files.py
Last active January 27, 2020 23:02
Organize files while changing folder structure
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import hashlib
import os
import os.path
import pickle
import sys
@slavanap
slavanap / lineage.dockerfile
Created June 29, 2019 13:40
Lineage OS dockerfile (WIP)
FROM ubuntu:bionic
ENV LANG=C.UTF-8 LC_ALL=C.UTF-8 DEBIAN_FRONTEND=noninteractive
RUN apt-get update -qq && apt-get install -qqy --no-install-recommends \
bc bison build-essential ccache curl flex g++-multilib gcc-multilib git gnupg gperf imagemagick lib32ncurses5-dev lib32readline-dev lib32z1-dev \
liblz4-tool libncurses5-dev libsdl1.2-dev libssl-dev libwxgtk3.0-dev libxml2 libxml2-utils lzop pngcrush rsync schedtool squashfs-tools xsltproc \
zip zlib1g-dev \
ca-certificates wget unzip python \
simg2img openjdk-8-jdk
@slavanap
slavanap / mutable-dict-value.cs
Last active September 26, 2020 22:37
Box value to modify it in-place in dictionary
using Microsoft.Extensions.Caching.Memory;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
namespace ConsoleApp3 {
public class Box<T> {
public T Value;
public Box(T value) {
Value = value;