Class: StickySession

Inherits:
Object
  • Object
show all
Defined in:
lib/sticky_session.rb

Class Method Summary collapse

Class Method Details

.change_rule(offline_isps = [], online_isps = []) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/sticky_session.rb', line 18

def change_rule(offline_isps=[], online_isps=[])
  #online_isps consist of online isps
  online_isps.sort! { |a,b| a.priority  <=> b.priority }

 # Remove rule of failed(offline) isp
  offline_isps.each do |isp|
    if isp.skip_sticky_session
      command = "ip rule del fwmark #{isp.port} lookup #{isp.name}"
    end
  end

  # Add new rule to online isp with high priority
  offline_isps.each do |isp|
    if isp.skip_sticky_session
      command = "ip rule add fwmark #{isp.port} lookup #{online_isps[0].name}"
    end
  end

end

.execute(isps = []) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/sticky_session.rb', line 5

def execute(isps=[])
  isps.each do |isp|
    if isp.skip_sticky_session
      # Mark the outgoing packet with port value
      command = "iptables -t mangle -A PREROUTING -s #{isp.network_ip} -p #{isp.protocol} --dport #{isp.dport} -j MARK --set-mark #{isp.dport}"
      SystemCall.execute(command)
      # Lookup the ISP depends on marked value 
      command = "ip rule add fwmark #{isp.port} lookup #{isp.name}"
      SystemCall.execute(command)
    end
  end
end