Class: Net::Openvpn::ClientConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/net/openvpn/client_config.rb

Instance Method Summary collapse

Constructor Details

#initialize(hostname) ⇒ ClientConfig

Returns a new instance of ClientConfig.



7
8
9
10
# File 'lib/net/openvpn/client_config.rb', line 7

def initialize(hostname)
  @hostname = hostname
  load if exists?
end

Instance Method Details

#exists?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/net/openvpn/client_config.rb', line 23

def exists?
  File.exists? path
end

#ip=(ip) ⇒ Object



27
28
29
# File 'lib/net/openvpn/client_config.rb', line 27

def ip=(ip)
  @ip = ip
end

#loadObject



12
13
14
15
16
17
# File 'lib/net/openvpn/client_config.rb', line 12

def load
  ccd = File.read(path)
  matches = ccd.match /ifconfig-push ([0-9\.]+) ([0-9\.]+)/
  @ip = matches[1]
  @network = matches[2]
end

#network=(network) ⇒ Object



31
32
33
# File 'lib/net/openvpn/client_config.rb', line 31

def network=(network)
  @network = network
end

#pathObject



19
20
21
# File 'lib/net/openvpn/client_config.rb', line 19

def path
  Net::Openvpn.ccdpath @hostname
end

#removeObject



40
41
42
43
# File 'lib/net/openvpn/client_config.rb', line 40

def remove
  return true if !File.exist? path
  FileUtils.rm path
end

#saveObject



45
46
47
48
49
50
51
# File 'lib/net/openvpn/client_config.rb', line 45

def save
  validate!
  
  File.open(path, "w") do |f|
    f.puts "ifconfig-push #{@ip} #{@network}"
  end
end

#validate!Object

Raises:

  • (ArgumentError)


35
36
37
38
# File 'lib/net/openvpn/client_config.rb', line 35

def validate!
  raise ArgumentError, "No IP set!" if @ip.nil? or @ip.empty?
  raise ArgumentError, "No network set!" if @network.nil? or @network.empty?
end