Skip to content

Commit

Permalink
Migrate to socat for portfarwarding
Browse files Browse the repository at this point in the history
  • Loading branch information
gnufied committed Jan 16, 2016
1 parent ea3674f commit 2468b9c
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 36 deletions.
1 change: 0 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
AllCops:
RunRailsCops: false
Excludes:
- db/**

Expand Down
5 changes: 1 addition & 4 deletions lib/invoker/power/setup/distro/arch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@ module Power
module Distro
class Arch < Base
def install_required_software
system("pacman -S --needed --noconfirm dnsmasq")
system("pacman -S --needed --noconfirm dnsmasq socat")
system("mkdir -p /etc/dnsmasq.d")
unless File.open("/etc/dnsmasq.conf").each_line.any? { |line| line.chomp == "conf-dir=/etc/dnsmasq.d" }
File.open("/etc/dnsmasq.conf", "a") {|f| f.write("conf-dir=/etc/dnsmasq.d") }
end
unless system("ls /usr/bin/rinetd > /dev/null 2>&1")
fail "You'll need to install rinetd from the AUR in order to continue"
end
end
end
end
Expand Down
25 changes: 15 additions & 10 deletions lib/invoker/power/setup/distro/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ module Power
module Distro
class Base
RESOLVER_FILE = "/etc/dnsmasq.d/dev-tld"
RINETD_FILE = "/etc/rinetd.conf"
SOCAT_SHELLSCRIPT = "files/invoker_forwarder.sh"
SOCAT_SYSTEMD = "files/socat_invoker.service"

def self.distro_installer
case Facter[:operatingsystem].value
Expand All @@ -22,6 +23,9 @@ def self.distro_installer
when "LinuxMint"
require "invoker/power/setup/distro/mint"
Mint.new
when "OpenSuSE"
require "invoker/power/setup/distro/opensuse"
Opensuse.new
else
raise "Your selected distro is not supported by Invoker"
end
Expand All @@ -31,8 +35,12 @@ def resolver_file
RESOLVER_FILE
end

def rinetd_file
RINETD_FILE
def socat_script
SOCAT_SHELLSCRIPT
end

def socat_systemd
SOCAT_SYSTEMD
end

# Install required software
Expand All @@ -41,13 +49,10 @@ def install_required_software
end

def restart_services
if Facter[:systemctl] == "true"
system("systemctl restart rinetd")
system("systemctl restart dnsmasq")
else
system("service rinetd restart")
system("service dnsmasq restart")
end
system("systemctl enable socat_invoker.service")
system("systemctl enable dnsmasq")
system("systemctl start socat_invoker.service")
system("systemctl restart dnsmasq")
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/invoker/power/setup/distro/debian.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Power
module Distro
class Debian < Base
def install_required_software
system("apt-get --assume-yes install dnsmasq rinetd")
system("apt-get --assume-yes install dnsmasq socat")
end
end
end
Expand Down
11 changes: 11 additions & 0 deletions lib/invoker/power/setup/distro/opensuse.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Invoker
module Power
module Distro
class Opensuse < Base
def install_required_software
system("zypper install -l dnsmasq socat")
end
end
end
end
end
8 changes: 1 addition & 7 deletions lib/invoker/power/setup/distro/redhat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,7 @@ module Power
module Distro
class Redhat < Base
def install_required_software
system("yum --assumeyes install dnsmasq rinetd")
end

def restart_services
system("systemctl enable rinetd")
system("service rinetd restart")
system("service dnsmasq restart")
system("yum --assumeyes install dnsmasq socat")
end
end
end
Expand Down
17 changes: 17 additions & 0 deletions lib/invoker/power/setup/files/invoker_forwarder.sh.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash
set -e
KillJobs() {
for job in $(jobs -p); do
kill -s SIGTERM $job > /dev/null 2>&1 || (sleep 10 && kill -9 $job > /dev/null 2>&1 &)
done
}

# Whatever you need to clean here
trap KillJobs SIGINT SIGTERM EXIT

/usr/bin/socat TCP-LISTEN:80,reuseaddr,fork TCP:0.0.0.0:<%= http_port %>&
pid1=$!
/usr/bin/socat TCP-LISTEN:443,reuseaddr,fork TCP:0.0.0.0:<%= https_port %>&
pid2=$!
wait $pid1 $pid2
wait $pid1 $pid2
12 changes: 12 additions & 0 deletions lib/invoker/power/setup/files/socat_invoker.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[Unit]
Description=Socat port forwarding service
After=network.target
Documentation=man:socat(1)

[Service]
ExecStart=/usr/bin/invoker_forwarder.sh
Restart=on-success


[Install]
WantedBy=multi-user.target
35 changes: 22 additions & 13 deletions lib/invoker/power/setup/linux_setup.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require "invoker/power/setup/distro/base"
require "facter"
require 'erb'
require 'fileutils'

module Invoker
module Power
Expand Down Expand Up @@ -32,7 +34,7 @@ def create_config_file

def uninstall_invoker
Invoker::Logger.puts("Uninstall is not yet supported on Linux."\
" You can remove invoker changes by uninstalling dnsmasq and rinetd")
" You can remove invoker changes by uninstalling dnsmasq and socat")
end

private
Expand All @@ -54,10 +56,8 @@ def install_resolver
end

def install_port_forwarder
File.open(distro_installer.rinetd_file, "a") do |fl|
fl << "\n"
fl << rinetd_setup(port_finder.http_port, port_finder.https_port)
end
install_forwarder_script()
install_systemd_unit()
end

def tld_setup
Expand All @@ -68,17 +68,26 @@ def tld_setup
tld_string
end

def rinetd_setup(http_port, https_port)
rinetd_string =<<-EOD
0.0.0.0 80 0.0.0.0 #{http_port}
0.0.0.0 443 0.0.0.0 #{https_port}
EOD
rinetd_string
def install_forwarder_script(http_port, https_port)
script_file = File.join(File.dirname(__FILE__), "files/invoker_forwarder.sh.erb")
script_template = File.read(script_file)
renderer = ERB.new(script_template)
script_output = renderer.result()
File.open("/usr/bin/invoker_forwarder.sh", "w") do |fl|
fl.write(script_output)
end
system("chmod +x /usr/bin/invoker_forwarder.sh")
end

def install_systemd_unit
unit_file = File.join(File.dirname(__FILE__), "files/socat_invoker.service")
FileUtils.cp(unit_file, "/etc/systemd/system/")
system("chmod 644 /etc/systemd/system/socat_invoker.service")
end

def get_user_confirmation?
Invoker::Logger.puts("Invoker is going to install dnsmasq and rinetd on this machine."\
" It is also going to install a local resolver for .dev domain and a rinetd rule"\
Invoker::Logger.puts("Invoker is going to install dnsmasq and socat on this machine."\
" It is also going to install a local resolver for .dev domain and a socat service"\
" which will forward all local requests on port 80 and 443 to another port")
Invoker::Logger.puts("If you still want to proceed with installation, press y.")
Invoker::CLI::Question.agree("Proceed with installation (y/n) : ")
Expand Down

0 comments on commit 2468b9c

Please sign in to comment.