Module: RCS::Common::WinFirewall

Extended by:
WinFirewall, Tracer
Included in:
WinFirewall
Defined in:
lib/rcs-common/winfirewall.rb

Defined Under Namespace

Classes: Advfirewall, AdvfirewallResponse, Rule

Constant Summary

Constants included from Tracer

Tracer::TRACE_YAML_NAME

Instance Method Summary collapse

Methods included from Tracer

thread_name, trace, trace_ensure_log_folders, trace_init, trace_named_put, trace_named_remove, trace_nested_pop, trace_nested_push, trace_setup

Instance Method Details

#add_rule(attributes) ⇒ Object



216
217
218
# File 'lib/rcs-common/winfirewall.rb', line 216

def add_rule(attributes)
  Rule.new(attributes).save
end

#block_inbound?Boolean

Returns true if the default firewall policy is to block all inbound connections

Returns:

  • (Boolean)


206
207
208
209
# File 'lib/rcs-common/winfirewall.rb', line 206

def block_inbound?
  line = Advfirewall.call("show currentprofile firewallpolicy", read: true).first_line
  line.to_s.downcase.include?('blockinbound')
end

#del_rule(name) ⇒ Object



220
221
222
# File 'lib/rcs-common/winfirewall.rb', line 220

def del_rule(name)
  Rule.new(name: name.to_s).del
end

#exists?Boolean

Delegate

Returns:

  • (Boolean)


212
213
214
# File 'lib/rcs-common/winfirewall.rb', line 212

def exists?
  Advfirewall.exists?
end

#has_rule?(name) ⇒ Boolean

Returns:

  • (Boolean)


224
225
226
# File 'lib/rcs-common/winfirewall.rb', line 224

def has_rule?(name)
  Advfirewall.call("firewall show rule name=\"#{name}\"").ok?
end

#raw_rulesObject



228
229
230
# File 'lib/rcs-common/winfirewall.rb', line 228

def raw_rules
  Advfirewall.call("firewall show rule name=all", read: true)
end

#statusObject

Return :on or :off depending of the firewall state

Note that the files test/fixtures/advfirewall/show_currentprofile_state_on and test/fixtures/advfirewall/show_currentprofile_state_off contains an example of the command output



184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/rcs-common/winfirewall.rb', line 184

def status
  return status_from_registry if @use_registry_for_status

  first_line = Advfirewall.call("show currentprofile state", read: true).first_line

  if first_line =~ /ON\z/
    :on
  elsif first_line =~ /OFF\z/
    :off
  else
    @use_registry_for_status = true
    status_from_registry
  end
end

#status_from_registryObject



199
200
201
202
203
# File 'lib/rcs-common/winfirewall.rb', line 199

def status_from_registry
  command = 'reg query HKLM\SYSTEM\CurrentControlSet\Services\SharedAccess\Parameters\FirewallPolicy\StandardProfile /v EnableFirewall'
  trace(:debug, "[Advfirewall] #{command}")
  `#{command}`.include?('0x1') ? :on : :off
end