Skip to content

Instantly share code, notes, and snippets.

View ik5's full-sized avatar
🎯
Focusing

ik5 ik5

🎯
Focusing
View GitHub Profile
@ik5
ik5 / index.js
Created October 26, 2018 21:11
Get OSM details on long and lat in go and javascript
fetch('https://nominatim.openstreetmap.org/reverse?format=json&lat=32.0959358&lon=34.8215234&addressdetails=1&accept-language=en')
.then((data) => { return data.json() } )
.then((data) => { console.table(JSON.stringify(data)) } )
.catch((err) => { console.error(err) } )
@ik5
ik5 / scan.rb
Last active August 27, 2018 04:18
simple example for naive TCP port scanner in Ruby
#!/usr/bin/env ruby
require 'socket'
def tcp_connect(address, port, timeout: 20)
# making sure we are talking with IP
connected = false
addr = Socket.getaddrinfo(address, nil)
sock_addr = Socket.pack_sockaddr_in(port, addr[0][3])
Socket.new(Socket.const_get(addr[0][0]), Socket::SOCK_STREAM, 0).tap do |socket|
2018-04-25T09:22:15 [I|app|] Started POST "/api/v2/discovered_hosts/facts" for ::1 at 2018-04-25 09:22:15 +0300
2018-04-25T09:22:15 [I|app|13a50] Processing by Api::V2::DiscoveredHostsController#facts as JSON
2018-04-25T09:22:15 [I|app|13a50] Parameters: {"facts"=>"[FILTERED]", "apiv"=>"v2", "discovered_host"=>{"facts"=>"[FILTERED]"}}
2018-04-25T09:22:15 [I|app|13a50] Current user: foreman_admin (administrator)
2018-04-25T09:22:15 [D|app|13a50] Setting current user thread-local variable to foreman_admin
2018-04-25T09:22:15 [I|aud|13a50] create event for Nic::Managed with id 44
2018-04-25T09:22:15 [D|app|13a50] Setting current user thread-local variable to nil
2018-04-25T09:22:15 [W|app|13a50] Host discovery failed, facts: {"physicalprocessorcount"=>"3", "memorysize_mb"=>"900", "blockdevice.sda_size"=>"1234567890"
require 'benchmark'
N = 1_000_000
REGEX = /^abc/
STR = 'abc are letters'.freeze
Benchmark.bm(13) do |x|
x.report('start_with?') do
N.times do
STR.start_with?('abc')
@ik5
ik5 / regexp_vs_start_with.rb
Created March 18, 2018 10:02
Benchmark that calculate the difference between regexp and start_with?
require 'benchmark'
require 'securerandom'
N = 1_000_000
REGEX = /^(abc|Abc|ABC)/
def gen_randstr(len = 24)
SecureRandom.base64(len)
end
@ik5
ik5 / documented_code.c
Last active March 14, 2018 08:38
example of self documenting code
/* ... */
const char * get_message(void * payload) {
payload_t *data = payload;
if (!data->len > MIN_PAYLOAD_LEN) {
return null;
}
return sprintf("%s%s", data->name, data->content);
}
@ik5
ik5 / func.sh
Last active March 3, 2018 08:23
Bash Example for short var names methodology
#!/bin/sh
func() {
for p in $(echo /tmp/*); do
f=$(basename -a "$path")
echo $f
done
}
@ik5
ik5 / sanitize.rb
Last active November 23, 2017 08:29
Example of how to sanitize file names (base, without path) to avoid any malicious actions
# help to avoid path traversal, and execution of anything on a machine
# due to file name
def escape_file_name(name)
# regex is a s follows:
# if it's the begining of the string, or there is no escape char
# for the following chars,
# add an escape for that char
name.gsub(/(^|[^\\])([\s\!\'\"#$&\^\*\`\/\(\)\[\]\?\{\}\|\~])/) do |match|
"\\#{match[1]}"
end
@ik5
ik5 / pause_resume.go
Created July 30, 2017 16:59
Example on pause and resume threads in golang
package main
import (
"fmt"
"runtime"
"time"
)
// Thread stages
const (
@ik5
ik5 / scryp.go
Created March 2, 2017 20:35
example on using scrypt
package main
import (
"crypto/rand"
"fmt"
"golang.org/x/crypto/scrypt"
)
func main() {