Skip to content

Instantly share code, notes, and snippets.

View flavienbwk's full-sized avatar

Flavien Berwick flavienbwk

  • Montréal, Canada
  • 14:55 (UTC -04:00)
View GitHub Profile
@stevedoyle
stevedoyle / cpu_freq
Last active February 12, 2024 01:01
Get CPU frequency (Linux platforms - /proc/cpuinfo)
#include <iostream>
#include <fstream>
#include <string>
#include <stdint.h>
#include <pcre.h>
using namespace std;
uint32_t cpufreq()
{
@asabaylus
asabaylus / gist:3071099
Created July 8, 2012 14:12
Github Markdown Heading Anchors

Anchors in Markdown

To create an anchor to a heading in github flavored markdown. Add - characters between each word in the heading and wrap the value in parens (#some-markdown-heading) so your link should look like so:

[create an anchor](#anchors-in-markdown)

@raveren
raveren / cryptographically_secure_random_strings.php
Last active November 21, 2023 12:35
Generate cryptographically secure random strings. Based on Kohana's Text::random() method and this answer: http://stackoverflow.com/a/13733588/179104
function random_text( $type = 'alnum', $length = 8 )
{
switch ( $type ) {
case 'alnum':
$pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
break;
case 'alpha':
$pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
break;
case 'hexdec':
@yarl
yarl / geoportal-tms.php
Created January 2, 2014 18:32
Geoportal WMS to TMS conversion.
<?php
$zoom = htmlspecialchars($_GET["zoom"]);
$x = htmlspecialchars($_GET["x"]);
$y = htmlspecialchars($_GET["y"]);
//explanation: https://gist.github.com/tmcw/4954720
$y = pow(2, $zoom)-$y-1;
//source: https://github.com/timwaters/whoots/blob/master/whoots.rb
@hest
hest / gist:8798884
Created February 4, 2014 06:08
Fast SQLAlchemy counting (avoid query.count() subquery)
def get_count(q):
count_q = q.statement.with_only_columns([func.count()]).order_by(None)
count = q.session.execute(count_q).scalar()
return count
q = session.query(TestModel).filter(...).order_by(...)
# Slow: SELECT COUNT(*) FROM (SELECT ... FROM TestModel WHERE ...) ...
print q.count()
@vdel26
vdel26 / nginx_basicauth.lua
Last active November 13, 2022 18:22
Extracting Basic Auth token
--[[
Authorization logic
]]--
function get_auth_params(where, method)
local params = {}
if where == "headers" then
params = ngx.req.get_headers()
elseif where == "basicauth" then
@kimus
kimus / ufw.md
Created March 2, 2014 22:46
NAT and FORWARD with Ubuntu’s ufw firewall

UFW

I use Ubuntu’s Uncomplicated firewall because it is available on Ubuntu and it's very simple.

Install UFW

if ufw is not installed by default be sure to install it first.

@thomasfr
thomasfr / iptables.sh
Last active April 13, 2024 01:59
iptable rules to allow outgoing DNS lookups, outgoing icmp (ping) requests, outgoing connections to configured package servers, outgoing connections to all ips on port 22, all incoming connections to port 22, 80 and 443 and everything on localhost
#!/bin/bash
IPT="/sbin/iptables"
# Server IP
SERVER_IP="$(ip addr show eth0 | grep 'inet ' | cut -f2 | awk '{ print $2}')"
# Your DNS servers you use: cat /etc/resolv.conf
DNS_SERVER="8.8.4.4 8.8.8.8"
# Allow connections to this package servers
@prashanthrajagopal
prashanthrajagopal / mirror_sync_cron.sh
Last active December 8, 2019 01:38
Sync Ubuntu Mirror v1
### Admin email address
admin_email=admin@domain.com
### Setup the server to mirror
mirror=rsync://archive.ubuntu.com/ubuntu
### Log file path
log=/var/log/ubuntu
### Setup the local directory
@adrianorsouza
adrianorsouza / mkselfssl.sh
Last active September 1, 2023 10:34
Script to create a new self-signed SSL Certificate for Nginx
#!/bin/bash
# @author Adriano Rosa (http://adrianorosa.com)
# @date: 2014-05-13 09:43
#
# Bash Script to create a new self-signed SSL Certificate
# At the end of creating a new Certificate this script will output a few lines
# to be copied and placed into NGINX site conf
#
# USAGE: this command will ask for the certificate name and number in days it will expire