Class: Proproxy::Server

Inherits:
Object
  • Object
show all
Includes:
SSHKit::DSL
Defined in:
lib/proproxy.rb

Instance Method Summary collapse

Constructor Details

#initialize(os_name, ip, port, options = {}) ⇒ Server

Returns a new instance of Server.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 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

Instance Method Details

#clear_squid_cacheObject



108
109
110
111
112
# File 'lib/proproxy.rb', line 108

def clear_squid_cache
  on @remote_host do
    execute 'squid -z'
  end
end

#configure_ip_tableObject



90
91
92
93
94
# File 'lib/proproxy.rb', line 90

def configure_ip_table
  on @remote_host do
    execute 'iptables-restore < /etc/sysconfig/iptables'
  end
end

#provisionObject



45
46
47
48
49
50
51
52
53
# 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_squidObject



55
56
57
58
# File 'lib/proproxy.rb', line 55

def restart_squid
  stop_squid
  start_squid
end

#start_squidObject



102
103
104
105
106
# File 'lib/proproxy.rb', line 102

def start_squid
  on @remote_host do
    execute 'service squid start'
  end
end

#stop_squidObject



96
97
98
99
100
# 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) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# 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