Class: Kytoon::Vpn::VpnNetworkManager

Inherits:
VpnConnection show all
Defined in:
lib/kytoon/vpn/vpn_network_manager.rb

Constant Summary

Constants inherited from VpnConnection

Kytoon::Vpn::VpnConnection::CERT_DIR

Instance Method Summary collapse

Methods inherited from VpnConnection

#create_certs, #delete_certs, #get_cfile

Constructor Details

#initialize(group, client = nil) ⇒ VpnNetworkManager

Returns a new instance of VpnNetworkManager.



15
16
17
# File 'lib/kytoon/vpn/vpn_network_manager.rb', line 15

def initialize(group, client = nil)
  super(group, client)
end

Instance Method Details

#cleanObject



33
34
35
36
# File 'lib/kytoon/vpn/vpn_network_manager.rb', line 33

def clean
  unset_gconf_config
  delete_certs
end

#configure_gconfObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/kytoon/vpn/vpn_network_manager.rb', line 38

def configure_gconf

  xml = Builder::XmlMarkup.new
  xml.gconfentryfile do |file|
    file.entrylist({ "base" => "/system/networking/connections/vpc_#{@group.id}"}) do |entrylist|

      entrylist.entry do |entry|
        entry.key("connection/autoconnect")
        entry.value do |value|
          value.bool("false")
        end
      end
      entrylist.entry do |entry|
        entry.key("connection/id")
        entry.value do |value|
          value.string("VPC Group: #{@group.id}")
        end
      end
      entrylist.entry do |entry|
        entry.key("connection/name")
        entry.value do |value|
          value.string("connection")
        end
      end
      entrylist.entry do |entry|
        entry.key("connection/timestamp")
        entry.value do |value|
          value.string(Time.now.to_i.to_s)
        end
      end
      entrylist.entry do |entry|
        entry.key("connection/type")
        entry.value do |value|
          value.string("vpn")
        end
      end
      entrylist.entry do |entry|
        entry.key("connection/uuid")
        entry.value do |value|
          value.string(UUIDTools::UUID.random_create)
        end
      end
      entrylist.entry do |entry|
        entry.key("ipv4/addresses")
        entry.value do |value|
          value.list("type" => "int") do |list|
          end
        end
      end
      entrylist.entry do |entry|
        entry.key("ipv4/dns")
        entry.value do |value|
          value.list("type" => "int") do |list|
            ip=IPAddr.new(@group.vpn_network.chomp("0")+"1")
            list.value do |lv|
              lv.int(ip_to_integer(ip.to_s))
            end
          end
        end
      end
      entrylist.entry do |entry|
        entry.key("ipv4/dns-search")
        entry.value do |value|
          value.list("type" => "string") do |list|
            list.value do |lv|
              lv.string(@group.domain_name)
            end
          end
        end
      end
      entrylist.entry do |entry|
        entry.key("ipv4/ignore-auto-dns")
        entry.value do |value|
          value.bool("true")
        end
      end
      entrylist.entry do |entry|
        entry.key("ipv4/method")
        entry.value do |value|
          value.string("auto")
        end
      end
      entrylist.entry do |entry|
        entry.key("ipv4/name")
        entry.value do |value|
          value.string("ipv4")
        end
      end
      entrylist.entry do |entry|
        entry.key("ipv4/never-default")
        entry.value do |value|
          value.bool("true")
        end
      end
      entrylist.entry do |entry|
        entry.key("ipv4/routes")
        entry.value do |value|
          value.list("type" => "int") do |list|
          end
        end
      end
      entrylist.entry do |entry|
        entry.key("vpn/ca")
        entry.value do |value|
          value.string(@ca_cert)
        end
      end
      entrylist.entry do |entry|
        entry.key("vpn/cert")
        entry.value do |value|
          value.string(@client_cert)
        end
      end
      entrylist.entry do |entry|
        entry.key("vpn/comp-lzo")
        entry.value do |value|
          value.string("yes")
        end
      end
      entrylist.entry do |entry|
        entry.key("vpn/connection-type")
        entry.value do |value|
          value.string("tls")
        end
      end
      entrylist.entry do |entry|
        entry.key("vpn/key")
        entry.value do |value|
          value.string(@client_key)
        end
      end
      if @group.vpn_proto == "tcp"
        entrylist.entry do |entry|
          entry.key("vpn/proto-tcp")
          entry.value do |value|
            value.string("yes")
          end
        end
      else
        entrylist.entry do |entry|
          entry.key("vpn/proto-udp")
          entry.value do |value|
            value.string("yes")
          end
        end
      end
      if @group.vpn_device == "tap"
        entrylist.entry do |entry|
          entry.key("vpn/tap-dev")
          entry.value do |value|
            value.string("yes")
          end
        end
      end
      entrylist.entry do |entry|
        entry.key("vpn/remote")
        entry.value do |value|
          value.string(@group.gateway_ip)
        end
      end
      entrylist.entry do |entry|
        entry.key("vpn/service-type")
        entry.value do |value|
          value.string("org.freedesktop.NetworkManager.openvpn")
        end
      end
    end

  end

  Tempfile.open('w') do |f|
    f.write(xml.target!)
    f.flush
    puts %x{gconftool-2 --load #{f.path}}
  end

  return true

end

#connectObject



19
20
21
22
23
# File 'lib/kytoon/vpn/vpn_network_manager.rb', line 19

def connect
  create_certs
  configure_gconf
  puts %x{#{sudo_display} nmcli con up id "VPC Group: #{@group.id}"}
end

#connected?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/kytoon/vpn/vpn_network_manager.rb', line 29

def connected?
  return system("#{sudo_display} nmcli con status | grep -c 'VPC Group: #{@group.id}' &> /dev/null")
end

#disconnectObject



25
26
27
# File 'lib/kytoon/vpn/vpn_network_manager.rb', line 25

def disconnect
  puts %x{#{sudo_display} nmcli con down id "VPC Group: #{@group.id}"}
end

#ip_to_integer(ip_string) ⇒ Object



222
223
224
225
226
# File 'lib/kytoon/vpn/vpn_network_manager.rb', line 222

def ip_to_integer(ip_string)
  return 0 if ip_string.nil?
  ip_arr=ip_string.split(".").collect{ |s| s.to_i }
  return ip_arr[0] + ip_arr[1]*2**8 + ip_arr[2]*2**16 + ip_arr[3]*2**24
end

#sudo_displayObject



228
229
230
231
232
233
234
# File 'lib/kytoon/vpn/vpn_network_manager.rb', line 228

def sudo_display
  if ENV['DISPLAY'].nil? or ENV['DISPLAY'] != ":0.0" then
    "sudo"
  else
    ""
  end
end

#unset_gconf_configObject



218
219
220
# File 'lib/kytoon/vpn/vpn_network_manager.rb', line 218

def unset_gconf_config
  puts %x{gconftool-2 --recursive-unset /system/networking/connections/vpc_#{@group.id}}
end