Skip to content

Instantly share code, notes, and snippets.

@stigmergik
Created June 27, 2018 00:35
Show Gist options
  • Save stigmergik/0d236e4b2807c85ed69f747f229f2b67 to your computer and use it in GitHub Desktop.
Save stigmergik/0d236e4b2807c85ed69f747f229f2b67 to your computer and use it in GitHub Desktop.
Moves `./my_binary` to `./my_binary.original` and creates a bash script at `./my_binary` that calls the original with `torsocks`, forwarding arguments
#!/usr/bin/env ruby
require 'fileutils'
files = ARGV
files
.map { |file| [File.dirname(file), File.basename(file)] }
.each do |relative_path, original_bin_arg|
# `path` means absolute path
path = File.expand_path(relative_path)
if original_bin_arg =~ /\.original$/
wrapped_bin_name = original_bin_arg.split('.').first
original_bin_name = original_bin_arg
move = false
else
wrapped_bin_name = original_bin_arg
original_bin_name = wrapped_bin_name + '.original'
move = true
end
wrapped_bin_path = File.join(path, wrapped_bin_name)
original_bin_path = File.join(path, original_bin_name)
if move
FileUtils.mv(wrapped_bin_path, original_bin_path)
puts "Moved", wrapped_bin_path, "to", original_bin_path
end
File.write(File.join(path, wrapped_bin_name),
<<BASH
#!/bin/bash
torsocks #{original_bin_path} \"$@\" &
BASH
)
FileUtils.chmod('+x', File.join(path, wrapped_bin_name))
puts "Wrapper script created at", wrapped_bin_path
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment