Module: RemoteSh::PortForwarder

Defined in:
lib/remote_sh/port_forwarder.rb

Class Method Summary collapse

Class Method Details

.current_ports(local_blacklisted_ports) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/remote_sh/port_forwarder.rb', line 61

def current_ports(local_blacklisted_ports)
  `lsof -PiTCP -sTCP:LISTEN`
    .split("\n")
    .map(&:split)[1..]
    .map { _1[8].split(":").last }
    .uniq - local_blacklisted_ports
end

.start_or_skip(host_config) ⇒ Object



7
8
9
10
11
12
13
14
15
16
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/remote_sh/port_forwarder.rb', line 7

def start_or_skip(host_config)
  host_name = host_config["name"]
  host_host = host_config["host"]

  remote_blacklist_ports = host_config["blacklist_ports"]
  local_blacklisted_ports = HostsConfiguration.config["blacklisted_ports"]

  pid_filename = "#{PID_FOLDER}/#{host_name}_portforwarding"

  return if File.exist?(pid_filename)

  # puts "started"

  pid_folder = "#{PID_FOLDER}/#{host_name}"

  local_opened_ports = current_ports(local_blacklisted_ports)
  local_opened_ports.each { |port| SshHelper.forward_local_port(host_host, port) }

  remote_opened_ports = SshHelper.current_ports(host_host, remote_blacklist_ports + local_opened_ports)
  remote_opened_ports.each { |port| SshHelper.forward_port(host_host, port) }

  Process.fork do
    File.open(pid_filename, "w") { |f| f.write("busy") }

    work_loop(
      host_host,
      pid_folder,
      remote_blacklist_ports,
      remote_opened_ports,
      local_blacklisted_ports,
      local_opened_ports
    )
    SshHelper.current_ports(host_host, remote_blacklist_ports + local_opened_ports).each { |port| SshHelper.close_port(host_host, port) }
    current_ports(local_blacklisted_ports + remote_opened_ports).each { |port| SshHelper.close_local_port(host_host, port) }
  rescue
    start_or_skip(host_config)
  ensure
    SshHelper
      .current_ports(host_host, remote_blacklist_ports)
      .each do |port|
        SshHelper.close_port(host_host, port)
      rescue
      end

    current_ports(local_blacklisted_ports)
      .each do |port|
        SshHelper.close_local_port(host_host, port)
      rescue
      end

    File.delete(pid_filename)
  end
end

.work_loop(host, pid_folder, remote_blacklisted_ports, remote_opened_ports, local_blacklisted_ports, local_opened_ports) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/remote_sh/port_forwarder.rb', line 69

def work_loop(
  host,
  pid_folder,
  remote_blacklisted_ports,
  remote_opened_ports,
  local_blacklisted_ports,
  local_opened_ports
)
  until Dir["#{pid_folder}/*"].empty?
    # remote ports
    remote_opened_ports_was = remote_opened_ports
    remote_opened_ports = SshHelper.current_ports(host, remote_blacklisted_ports + local_opened_ports)

    remote_ports_to_close = remote_opened_ports_was - remote_opened_ports
    remote_ports_to_open = remote_opened_ports - remote_opened_ports_was

    remote_ports_to_open.each { |port| SshHelper.forward_port(host, port) }
    remote_ports_to_close.each { |port| SshHelper.close_port(host, port) }

    #local ports

    local_opened_ports_was = local_opened_ports
    local_opened_ports = current_ports(local_blacklisted_ports + remote_opened_ports)

    local_ports_to_close = local_opened_ports_was - local_opened_ports
    local_ports_to_open = local_opened_ports - local_opened_ports_was

    # puts 'local_ports_to_open' + local_ports_to_open.inspect
    local_ports_to_open.each { |port| SshHelper.forward_local_port(host, port) }
    # puts 'local_ports_to_close' + local_ports_to_close.inspect
    local_ports_to_close.each { |port| SshHelper.close_local_port(host, port) }

    sleep(1)
  end
end