Skip to content

Instantly share code, notes, and snippets.

@skanga
skanga / URLTools.java
Last active June 19, 2024 08:31
Make an HTTPS connection in Java using a certificate file
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
# Download latest release of a specific file from a Github repo
REPO_OWNER='PowerShell'
REPO_NAME='Win32-OpenSSH'
ARTIFACT_NAME=OpenSSH-Win64.zip
ARTIFACT_URL=$(curl -sL https://api.github.com/repos/${REPO_OWNER}/${REPO_NAME}/releases/latest | jq --raw-output '.assets[] | .browser_download_url' | grep ${ARTIFACT_NAME})
curl -sLO $ARTIFACT_URL
@skanga
skanga / genhaar.bat
Created July 24, 2017 21:32
Train HAAR cascades with OpenCV 3.2 on windows
@echo off
REM SETUP
REM Download opencv-3.2.0-vc14.exe from https://github.com/opencv/opencv/releases
REM Extract it and then copy all executables from ..\opencv-3.2.0-vc14\build\x64\vc14\bin\ into .\bin
REM Place BMP positive images into .\positives and JPG negative images into .\negatives and then run this script
REM REFERENCES:
REM https://www.academia.edu/9149928/A_complete_guide_to_train_a_cascade_classifier_filter
REM http://johnallen.github.io/opencv-object-detection-tutorial/
/*
"%JAVA_HOME%\bin\javac" -cp .;lib\*; JettyProxyServer.java && "%JAVA_HOME%\bin\java" -cp .;lib\*; JettyProxyServer
*/
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.handler.ConnectHandler;
import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
@skanga
skanga / SingleFileHTTPServer.java
Created December 8, 2017 19:53
Multi threaded HTTP server in Java that will only serve 1 single file for ANY http request
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URLConnection;
@skanga
skanga / runpipe.c
Last active April 1, 2018 02:06
Here's an example of how to pipe an arbitrary number of commands together using C including input and output redirection. The demo example in main() executes the command "grep nologin < /etc/passwd | awk "{print $1}" | sort > zzz". In case you do not need the file redirection then either one or both can be set to NULL.
/**
* gcc -o runpipe runpipe.c && ./runpipe
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
@skanga
skanga / StringTemplate.java
Last active February 21, 2019 17:43
Simple string templates for java. The template can be in the code or in an external file, and any type of delimiters may be used for the variables. The data is provided in a HashMap.
import java.io.IOException;
import java.lang.StringBuilder;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
public class StringTemplate
@skanga
skanga / HttpServer.java
Last active June 1, 2018 01:22
Feature rich web server in around 500 lines using pure JDK classes only. Features include file server, dir listing, basic auth, https (self-signed or let's encrypt), file upload, multithreading, access logging, etc.
import com.sun.net.httpserver.*;
import javax.net.ssl.*;
import java.io.*;
import java.net.*;
import java.nio.file.*;
import java.security.*;
import java.security.cert.*;
import java.text.*;
import java.util.*;
@skanga
skanga / HttpsNoVerify.java
Created December 21, 2018 18:25
Fetch an HTTPS URL with NO verification and BLINDLY trusting all hosts
// NOTE: It is IRRESPONSIBLE to use this in any production type environment or with even remotely valuable data
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.security.cert.X509Certificate;
import java.security.SecureRandom;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
@skanga
skanga / gist:39353a02142cbc26cc8cd339ffcfe2dc
Created December 27, 2018 11:06
Upgrade TLS on Centos 5 by building WGET, CURL and OPENSSL from scratch
# Download, compile and install openssl
mkdir ~/src/
cd ~/src/
wget https://www.openssl.org/source/openssl-1.0.2a.tar.gz
tar -zxvf openssl-*.tar.gz
cd openssl-*
./config -fpic shared
make
sudo make install