class Proproxy::Server

Public Class Methods

new(os_name, ip, port, options={}) click to toggle source
# File lib/proproxy.rb, line 17
def initialize(os_name, ip, port, options={})
  # TODO: enable to choose even if the server is not ubuntu
  # unless AVAILABLE_OS_NAME.include? os_name
  #   raise InvalidServerNameError.new 'invalid os name'
  # end

  ssh_path = if options[:ssh_path].nil?
    '~/.ssh/id_rsa'
  else
    options[:ssh_path]
  end

  username = if options[:username].nil?
    'root'
  else
    options[:username]
  end

  SSHKit.config.output_verbosity = Logger::DEBUG

  @remote_host = SSHKit::Host.new(ip)
  @remote_host.user = username
  @remote_host.ssh_options = {
    keys: [ ssh_path ],
    auth_methods: %w(publickey)
  }
end

Public Instance Methods

clear_squid_cache() click to toggle source
# File lib/proproxy.rb, line 108
def clear_squid_cache
  on @remote_host do
    execute 'squid -z'
  end
end
configure_ip_table() click to toggle source
# File lib/proproxy.rb, line 90
def configure_ip_table
  on @remote_host do
    execute 'iptables-restore < /etc/sysconfig/iptables'
  end
end
provision() click to toggle source
# File lib/proproxy.rb, line 45
def provision
  on @remote_host do
    execute 'sudo apt-get update -y'
    execute 'sudo apt-get install squid -y'
    execute 'mkdir /etc/sysconfig/'
    execute 'touch /etc/sysconfig/iptables'
  end
  copy_template
end
restart_squid() click to toggle source
# File lib/proproxy.rb, line 55
def restart_squid
  stop_squid
  start_squid
end
start_squid() click to toggle source
# File lib/proproxy.rb, line 102
def start_squid
  on @remote_host do
    execute 'service squid start'
  end
end
stop_squid() click to toggle source
# File lib/proproxy.rb, line 96
def stop_squid
  on @remote_host do
    execute 'service squid stop'
  end
end
update_ip_table(ip_v4, port, with_ssh_port: true) click to toggle source
# File lib/proproxy.rb, line 60
def update_ip_table(ip_v4, port, with_ssh_port: true)
  new_tonnel = "-A FWINPUT -p tcp -m tcp --dport #{port} -s #{ip_v4} -j ACCEPT"
  new_port = "http_port #{port}"
  new_src = "acl myacl src #{ip_v4}/255.255.255.255"

  remove_last_2_line
  on @remote_host do
    execute "echo #{new_tonnel} >> /etc/sysconfig/iptables"
    execute "echo #{new_port} >> /etc/squid/squid.conf"
    execute "echo #{new_src} >> /etc/squid/squid.conf"
  end
  add_icmp_host_prohibited_line
  add_last_commit_line_command

  if with_ssh_port
    ssh_tonnel = "-A FWINPUT -p tcp -m tcp --dport 22 -s #{ip_v4} -j ACCEPT"
    ssh_port = "http_port 22"
    remove_last_2_line
    on @remote_host do
      execute "echo #{ssh_tonnel} >> /etc/sysconfig/iptables"
      execute "echo #{ssh_port} >> /etc/squid/squid.conf"
    end
    add_icmp_host_prohibited_line
    add_last_commit_line_command
  end
  allow_specified_src
  configure_ip_table
  restart_squid
end

Private Instance Methods

add_icmp_host_prohibited_line() click to toggle source
# File lib/proproxy.rb, line 128
def add_icmp_host_prohibited_line
  on @remote_host do
    execute 'echo -A FWINPUT -j REJECT --reject-with icmp-host-prohibited >> /etc/sysconfig/iptables'
  end
end
add_last_commit_line_command() click to toggle source
# File lib/proproxy.rb, line 122
def add_last_commit_line_command
  on @remote_host do
    execute 'echo COMMIT >> /etc/sysconfig/iptables'
  end
end
allow_specified_src() click to toggle source
# File lib/proproxy.rb, line 134
def allow_specified_src
  on @remote_host do
    execute 'echo http_access allow myacl >> /etc/squid/squid.conf'
    execute 'echo http_access deny all >> /etc/squid/squid.conf'
  end
end
copy_template() click to toggle source
# File lib/proproxy.rb, line 141
def copy_template
  file_path = File.dirname(__FILE__).gsub('/lib', '')
  on @remote_host do
    upload! "#{file_path}/templates/iptables", '/etc/sysconfig/iptables'
    upload! "#{file_path}/templates/squid.conf", '/etc/squid/squid.conf'
  end
end
remove_last_2_line() click to toggle source
# File lib/proproxy.rb, line 116
def remove_last_2_line
  on @remote_host do
    execute 'head -n -2 /etc/sysconfig/iptables > /etc/sysconfig/tmp_iptables ; mv /etc/sysconfig/tmp_iptables /etc/sysconfig/iptables'
  end
end