Skip to content

Instantly share code, notes, and snippets.

@alphamarket
alphamarket / jquery.focus.override.coffee
Last active August 9, 2018 14:35
jQuery.focus() and move cursor at the end of input
# jQuery extensions
(($) ->
_super = { }
#
# [..] some other extensions
#
_super.focus = $.fn.focus
$.fn.focus = ->
# call out the original jQuery focus
_super.focus.apply(this, arguments)
@alphamarket
alphamarket / git-proxy.bash
Last active October 5, 2019 17:04
Creating SSH proxy and making Git connect through it
# [step 1] create a ssh-proxy
ssh -D 9999 -qCN user@server.net
# [step 2] make git connect through the ssh-proxy
# [current script only]
export GIT_SSH_COMMAND='ssh -o ProxyCommand="connect -S 127.0.0.1:9999 %h %p"'
# OR [git global setting]
git config --global core.sshCommand 'ssh -o ProxyCommand="connect -S 127.0.0.1:9999 %h %p"'
# OR [one-time only use]
git clone -c=core.sshCommand 'ssh -o ProxyCommand="connect -S 127.0.0.1:9999 %h %p"' git@github.com:user/repo.git
@alphamarket
alphamarket / ssh-proxy.bash
Created October 5, 2019 17:04
creating an SSH proxy over a port
function ssh-proxy() {
echo "Running ssh-proxy at local port: 9999"
ssh -D 9999 -qCN $1
}
@alphamarket
alphamarket / certbox.cert.bash
Created October 11, 2019 10:29
Creating HTTPS certification using certbot & nginx
#!/bin/bash
# adding certbot PPA
sudo apt-get update
sudo apt-get install software-properties-common
sudo add-apt-repository ppa:certbot/certbot
sudo apt-get update
# install the certbot
sudo apt-get install certbot python-certbot-nginx
@alphamarket
alphamarket / friendly_urls.markdown
Created October 16, 2019 08:14 — forked from mhriess/friendly_urls.markdown
Friendly URLs in Rails

Friendly URLs

By default, Rails applications build URLs based on the primary key -- the id column from the database. Imagine we have a Person model and associated controller. We have a person record for Bob Martin that has id number 6. The URL for his show page would be:

/people/6

But, for aesthetic or SEO purposes, we want Bob's name in the URL. The last segment, the 6 here, is called the "slug". Let's look at a few ways to implement better slugs.

@alphamarket
alphamarket / change-date-time.bash
Last active October 26, 2019 09:54
Change/Restore Linux's System Date & Time
# disable NTP sync
sudo timedatectl set-ntp false
# set the date-time
sudo timedatectl set-time "2019-10-29 16:20:00"
# set the timezone
sudo timedatectl set-timezone Europe/London
@alphamarket
alphamarket / avg-download-time.py
Created November 10, 2019 05:49
Compute average download time of webpages
#!/usr/bin/python -u
from os import popen
import commands, sys
times = []
for x in xrange(int(sys.argv[1])):
status, output = commands.getstatusoutput("curl -so /dev/null %s -w %%{time_starttransfer}" %sys.argv[2])
if(status == 0):
@alphamarket
alphamarket / commit-fix.bash
Created December 9, 2019 06:42
How to add a changed file to an older (not last) commit in Git
# To "fix" an old commit with a small change, without changing the commit message
# of the old commit, where OLDCOMMIT is something like 091b73a:
# Copied from `https://stackoverflow.com/a/27721031/1198291`
git add <my fixed files>
git commit --fixup=OLDCOMMIT
git rebase --interactive --autosquash OLDCOMMIT^ # at here do noting and exit from editor, it will do the job
@alphamarket
alphamarket / spider.rb
Created January 2, 2020 05:35
spider script for testing site's broken links
#!/usr/bin/env ruby
ENV['TZ'] = 'UTC'
require 'rubygems'
require 'active_support/core_ext/object/blank'
require 'nokogiri'
require 'net/http'
require 'byebug'
require 'logger'
require 'uri'
@alphamarket
alphamarket / ip2long.cpp
Last active March 8, 2020 06:05
Converts IPv4 and IPv6 to decimal (long value) in C++
#include <iostream>
#include <boost/asio/ip/address.hpp>
#include <boost/multiprecision/cpp_int.hpp>
using big_int = boost::multiprecision::cpp_int;
big_int ip2long(const std::string&);
int main(int, char**) {
const auto
fals = "xxx.xxx.x.x",