Skip to content

Instantly share code, notes, and snippets.

View troyk's full-sized avatar

Troy Kruthoff troyk

View GitHub Profile
# BruteForceKilla
#
# A Rack middleware to limit requests by ip address, coded for fun as my first
# middleware, thanks http://coderack.org for giving me a reason :)
#
# For production use, one would want to make a memcache or redis tracker.
#
# options:
#
# :tracker => Class name of the tracker to use (default Memory (all there is for now!))
Cursor.prototype.streamRecords = function(callback) {
var
self = this,
stream = new process.EventEmitter(),
recordLimitValue = this.limitValue || 0,
emittedRecordCount = 0,
queryCommand = this.generateQueryCommand();
// see http://www.mongodb.org/display/DOCS/Mongo+Wire+Protocol
queryCommand.numberToReturn = 500;
Last login: Fri Aug 13 17:17:35 on ttys003
troy:~ $ history | awk '{print $2}' | sort | uniq -c | sort -rn | head
173 node
89 git
36 cd
25 ls
20 ./script/server
15 sshebara
14 sc
12 ssh
@troyk
troyk / PostgreSQLAdapter.rb
Created November 3, 2010 07:10
go go multi-tenant via schema
class ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
def ensure_search_path!
return true if existing_path = Thread.current[:search_path] and @schema_search_path == Thread.current[:search_path]
if existing_path
@schema_search_path = Thread.current[:search_path] = existing_path
else
puts "WARNING::SETTING SEARCH_PATH TO SUB1,PUBLIC CUZ Thread.current[:search_path] is nil"
@schema_search_path = Thread.current[:search_path]='sub1,public'
end
@troyk
troyk / pgrecon.js
Created November 17, 2010 19:01
postgres log outputter
var fs = require("fs"),
spawn = require('child_process').spawn;
var filename = process.ARGV[2];
if (!filename)
return console.log("Usage: node <pgrecon.js> <filename>");
var tail = spawn("tail", ["-f", filename]);
console.log("start tailing");
@troyk
troyk / iptables-block-apache-out
Created February 15, 2011 02:00
Apache user should not be allowed to talk to people
iptables --append OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# create a new chain
iptables --new-chain chk_apache_user
# use new chain to process packets generated by apache (replace apache with uid)
iptables -A OUTPUT -m owner --uid-owner apache -j chk_apache_user
# Allow 143 (IMAP) and 25 so that webmail works :)
iptables -A chk_apache_user -p tcp --syn -d 127.0.0.1 --dport 143 -j RETURN
@troyk
troyk / date.rb
Created April 15, 2011 17:07
Date.parse() with Ruby 1.9 is now defaulting to the European date style, John Wayne would be sad, and our apps don't work right, so this fixes it
# Date.parse() with Ruby 1.9 is now defaulting to the European date style where the format is DD/MM/YYYY, not MM/DD/YYYY
# patch it to use US format by default
class Date
class << self
alias :euro_parse :_parse
def _parse(str,comp=false)
str = str.to_s.strip
if str == ''
{}
elsif str =~ /^(\d{1,2})[-\/](\d{1,2})[-\/](\d{2,4})/
@troyk
troyk / whois.rb
Created May 4, 2011 15:37
Ruby script to find available domain names
#!/usr/bin/env ruby
require "whois"
require "hirb"
$results = []
$alphebet = (97..122).map {|x| x.chr}
$base = "fit"
$alphebet.each do |letter|
["#{letter}#{$base}.com","#{$base}#{letter}.com"].each do |domain|
@troyk
troyk / ruby1.9.2ubuntu
Created June 29, 2011 12:12
Installing Ruby 1.9.2 on ubuntu
apt-get install gcc g++ build-essential libssl-dev libreadline5-dev zlib1g-dev linux-headers-generic libsqlite3-dev
wget http://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.2-p180.tar.gz
./configure && make && make install
@troyk
troyk / gist:1098836
Created July 22, 2011 03:34
How to install RMagick on OS X
# download ImageMagick source from http://www.imagemagick.org/script/advanced-unix-installation.php
# get and install libjpeg
curl -O http://www.ijg.org/files/jpegsrc.v8c.tar.gz
cd jpeg-8c
./configure --enable-shared --prefix=/usr/local
make
sudo make install
# install imagemagick