2019-10-14 00:42:15 +02:00
|
|
|
|
|
|
|
class NotSetup
|
|
|
|
def to_s(io : IO)
|
|
|
|
io << "not setup"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Autoconfiguration
|
|
|
|
def to_s(io : IO)
|
|
|
|
io << "autoconfiguration"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class DHCP
|
|
|
|
def to_s(io : IO)
|
|
|
|
io << "dhcp"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
class WirelessAPSetup
|
2019-10-25 14:26:46 +02:00
|
|
|
property ifname : String
|
2019-10-14 00:42:15 +02:00
|
|
|
property ssid : String
|
|
|
|
|
|
|
|
# This is a list of parameters that should be unique to each AP
|
|
|
|
property up : Bool
|
|
|
|
property description : String?
|
|
|
|
property mtu : Int32?
|
|
|
|
property main_ip_v4 : IPAddress | DHCP | NotSetup
|
|
|
|
property main_ip_v6 : IPAddress | DHCP | Autoconfiguration | NotSetup
|
|
|
|
property aliasses_v4 : Array(IPAddress)
|
|
|
|
property aliasses_v6 : Array(IPAddress)
|
2019-10-17 18:29:26 +02:00
|
|
|
property dns : NetworkCommands::DNS
|
2019-10-14 00:42:15 +02:00
|
|
|
|
|
|
|
# we currently only support WPA2-PSK wireless security mechanism
|
|
|
|
property security : WPA
|
|
|
|
|
|
|
|
class WPA
|
|
|
|
property key : String
|
|
|
|
def initialize(@key)
|
|
|
|
end
|
2019-10-22 13:19:16 +02:00
|
|
|
|
|
|
|
def to_s(io : IO)
|
|
|
|
io << "WPA access point, key #{key}"
|
|
|
|
end
|
2019-10-14 00:42:15 +02:00
|
|
|
end
|
|
|
|
|
2019-10-25 14:26:46 +02:00
|
|
|
def initialize(@ifname, @ssid, @security)
|
2019-10-14 00:42:15 +02:00
|
|
|
@main_ip_v4 = NotSetup.new
|
|
|
|
@main_ip_v6 = NotSetup.new
|
|
|
|
@aliasses_v4 = Array(IPAddress).new
|
|
|
|
@aliasses_v6 = Array(IPAddress).new
|
|
|
|
@up = true
|
2019-10-17 18:29:26 +02:00
|
|
|
@dns = NetworkCommands::DNS.new
|
2019-10-14 00:42:15 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def to_s(io : IO)
|
2019-10-18 04:36:05 +02:00
|
|
|
io << indent(1, to_string)
|
2019-10-14 00:42:15 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_string
|
|
|
|
String.build do |str|
|
2019-10-18 04:36:05 +02:00
|
|
|
str << "#{CBLUE}#{ssid}#{CRESET}\n"
|
2019-10-14 00:42:15 +02:00
|
|
|
|
2019-10-18 04:36:05 +02:00
|
|
|
str << "\t#description #{description.not_nil!}\n" unless description.nil?
|
|
|
|
str << "\t#{@up? "up" : "down"}\n"
|
|
|
|
str << "\tmtu #{mtu}\n" unless mtu.nil?
|
2019-10-14 00:42:15 +02:00
|
|
|
|
|
|
|
# ipv4
|
|
|
|
unless main_ip_v4.is_a?(NotSetup)
|
2019-10-18 04:36:05 +02:00
|
|
|
str << "\tinet #{main_ip_v4}\n"
|
2019-10-14 00:42:15 +02:00
|
|
|
|
|
|
|
aliasses_v4.each do |a|
|
2019-10-18 04:36:05 +02:00
|
|
|
str << "\talias #{a}\n"
|
2019-10-14 00:42:15 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# ipv6
|
|
|
|
unless main_ip_v6.is_a?(NotSetup)
|
2019-10-18 04:36:05 +02:00
|
|
|
str << "\tinet6 #{main_ip_v6}\n"
|
2019-10-14 00:42:15 +02:00
|
|
|
|
|
|
|
@aliasses_v6.each do |a|
|
2019-10-18 04:36:05 +02:00
|
|
|
str << "\talias6 #{a}\n"
|
2019-10-14 00:42:15 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-18 04:36:05 +02:00
|
|
|
str << indent(1, dns.to_s) unless dns.addresses.empty?
|
2019-10-14 00:42:15 +02:00
|
|
|
end
|
|
|
|
end
|
2019-10-17 18:29:26 +02:00
|
|
|
|
2019-10-22 13:19:16 +02:00
|
|
|
def store_access_point_keys
|
2019-10-23 00:25:54 +02:00
|
|
|
NetworkCommands.store_access_point_keys ssid, security
|
2019-10-22 13:19:16 +02:00
|
|
|
end
|
|
|
|
|
2019-10-17 18:29:26 +02:00
|
|
|
def execute
|
|
|
|
puts "TODO: wireless configuration"
|
|
|
|
|
2019-10-25 14:26:46 +02:00
|
|
|
NetworkCommands.wireless_access_point_connection ifname, ssid
|
|
|
|
|
2019-10-17 18:29:26 +02:00
|
|
|
# DNS configuration
|
|
|
|
dns.execute
|
|
|
|
end
|
2019-10-14 00:42:15 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# interface configuration
|
|
|
|
#
|
|
|
|
|
|
|
|
class InterfaceConfiguration
|
|
|
|
|
|
|
|
property name : String
|
|
|
|
property up : Bool
|
|
|
|
property description : String?
|
|
|
|
property mtu : Int32?
|
|
|
|
property main_ip_v4 : IPAddress | DHCP | NotSetup
|
|
|
|
property main_ip_v6 : IPAddress | Autoconfiguration | DHCP | NotSetup
|
|
|
|
property aliasses_v4 : Array(IPAddress)
|
|
|
|
property aliasses_v6 : Array(IPAddress)
|
|
|
|
property wireless_networks : Hash(String, WirelessAPSetup)
|
2019-10-17 18:29:26 +02:00
|
|
|
property dns : NetworkCommands::DNS
|
2019-10-14 00:42:15 +02:00
|
|
|
|
|
|
|
def initialize (@name, @up,
|
|
|
|
@description,
|
|
|
|
@mtu,
|
|
|
|
@main_ip_v4, @main_ip_v6, aliasses,
|
2019-10-25 14:26:46 +02:00
|
|
|
@wireless_networks,
|
2019-10-17 18:29:26 +02:00
|
|
|
@dns
|
2019-10-14 00:42:15 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
@aliasses_v4 = Array(IPAddress).new
|
|
|
|
@aliasses_v6 = Array(IPAddress).new
|
|
|
|
|
|
|
|
aliasses.each do |ip|
|
|
|
|
if ip.ipv4?
|
|
|
|
aliasses_v4 << ip
|
|
|
|
else
|
|
|
|
aliasses_v6 << ip
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_s(io : IO)
|
|
|
|
io << to_string
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_string
|
|
|
|
String.build do |str|
|
|
|
|
if NetworkCommands.interface_exists(@name)
|
|
|
|
str << "#{CGREEN}#{@name}#{CRESET}\n"
|
|
|
|
else
|
|
|
|
str << "#{CRED}#{@name}#{CRESET}\n"
|
|
|
|
end
|
|
|
|
|
|
|
|
str << "\tdescription: '#{description.not_nil!}'\n" unless description.nil?
|
|
|
|
|
|
|
|
str << "\t#{@up? "up" : "down"}\n"
|
|
|
|
|
|
|
|
unless mtu.nil?
|
|
|
|
str << "\tmtu #{mtu}\n"
|
|
|
|
end
|
|
|
|
|
|
|
|
# ipv4
|
|
|
|
unless main_ip_v4.is_a?(NotSetup)
|
|
|
|
str << "\tinet #{main_ip_v4}\n"
|
|
|
|
|
|
|
|
aliasses_v4.each do |a|
|
|
|
|
str << "\talias #{a}\n"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# warning: alias but no main ip address
|
|
|
|
if main_ip_v4.is_a?(NotSetup) && ! aliasses_v4.empty?
|
|
|
|
str << "\t#{CRED}alias configured but no main ipv4 configuration.#{CRESET}\n"
|
|
|
|
str << "\t#{CRED}Should main ipv4 be obtained from DHCP? Static configuration?#{CRESET}\n"
|
|
|
|
end
|
|
|
|
|
|
|
|
# ipv6
|
|
|
|
unless main_ip_v6.is_a?(NotSetup)
|
|
|
|
str << "\tinet6 #{main_ip_v6}\n"
|
|
|
|
|
|
|
|
aliasses_v6.each do |a|
|
|
|
|
str << "\talias6 #{a}\n"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# warning: alias but no main ip address
|
|
|
|
if main_ip_v6.is_a?(NotSetup) && ! aliasses_v6.empty?
|
|
|
|
str << "\t#{CRED}alias6 configured but no main ipv6 configuration.#{CRESET}\n"
|
|
|
|
str << "\t#{CRED}Should main ipv6 be obtained from autoconfiguration? DHCP? Static configuration?#{CRESET}\n"
|
|
|
|
end
|
|
|
|
|
2019-10-18 04:36:05 +02:00
|
|
|
str << indent(1, dns.to_s) unless dns.addresses.empty?
|
2019-10-14 00:42:15 +02:00
|
|
|
|
|
|
|
unless wireless_networks.empty?
|
|
|
|
wireless_networks.each do |k,v|
|
|
|
|
str << v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-22 13:19:16 +02:00
|
|
|
def access_point_connection
|
|
|
|
ssid_list = NetworkCommands.scan name
|
|
|
|
|
|
|
|
if ssid_list.nil?
|
|
|
|
raise "no ssid scanned"
|
|
|
|
end
|
|
|
|
|
|
|
|
if wireless_networks.empty?
|
|
|
|
raise "no configured access point for interface #{name}, cannot connect"
|
|
|
|
end
|
|
|
|
|
|
|
|
wireless_networks.each do |k,v|
|
|
|
|
ssid_list.each do |ssid|
|
|
|
|
if k == ssid
|
|
|
|
puts "#{CGREEN}#{k} == #{ssid}#{CRESET}"
|
|
|
|
v.execute
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# TODO: sleep for a second before testing the gateway?
|
|
|
|
# TODO: configuring the interface
|
|
|
|
|
|
|
|
puts "TODO: connectivity check with the gateway"
|
|
|
|
end
|
|
|
|
|
|
|
|
def store_access_point_keys
|
|
|
|
if wireless_networks.empty?
|
|
|
|
raise "no configured access point for interface #{name}"
|
|
|
|
end
|
|
|
|
|
|
|
|
wireless_networks.each do |ssid, wireless_configuration|
|
2019-10-23 00:25:54 +02:00
|
|
|
puts "#{CGREEN}storing access point key for #{ssid}#{CRESET}"
|
2019-10-22 13:19:16 +02:00
|
|
|
wireless_configuration.store_access_point_keys
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-14 00:42:15 +02:00
|
|
|
# configure the interface
|
|
|
|
def execute
|
|
|
|
unless NetworkCommands.interface_exists(@name)
|
|
|
|
raise "The interface #{@name} doesn't exists, yet."
|
|
|
|
end
|
|
|
|
|
|
|
|
if up
|
|
|
|
NetworkCommands.up name
|
|
|
|
else
|
|
|
|
puts "not marked as 'up' -- ending here"
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
unless mtu.nil?
|
|
|
|
NetworkCommands.mtu name, mtu
|
|
|
|
end
|
|
|
|
|
|
|
|
unless description.nil?
|
|
|
|
NetworkCommands.description name, description.not_nil!
|
|
|
|
end
|
|
|
|
|
2019-10-22 13:19:16 +02:00
|
|
|
# TODO: treat differently wireless and non-wireless interfaces
|
2019-10-25 14:26:46 +02:00
|
|
|
if @wireless_networks.empty?
|
2019-10-26 00:23:02 +02:00
|
|
|
puts "interface #{name} is not wireless"
|
|
|
|
else
|
2019-10-22 13:19:16 +02:00
|
|
|
puts "interface #{name} is wireless: connection to the access point"
|
|
|
|
store_access_point_keys
|
|
|
|
access_point_connection
|
|
|
|
end
|
|
|
|
|
2019-10-14 00:42:15 +02:00
|
|
|
# ipv4 configuration
|
|
|
|
main_ip_v4.tap do |ip|
|
|
|
|
case ip
|
|
|
|
when IPAddress
|
|
|
|
NetworkCommands.set_ip name, ip
|
|
|
|
when DHCP
|
|
|
|
NetworkCommands.dhcp name
|
|
|
|
when NotSetup
|
|
|
|
# do nothing
|
|
|
|
else
|
|
|
|
raise "ipv4 configuration: neither static nor dynamic"
|
|
|
|
end
|
|
|
|
|
|
|
|
# We wont setup aliasses unless there is an actual IP address
|
|
|
|
if ip != NotSetup
|
|
|
|
aliasses_v4.each do |ip_alias|
|
|
|
|
NetworkCommands.set_alias name, ip_alias
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# ipv6 configuration
|
|
|
|
main_ip_v6.tap do |ip|
|
|
|
|
case ip
|
|
|
|
when IPAddress
|
|
|
|
NetworkCommands.set_ip name, ip
|
|
|
|
when Autoconfiguration
|
|
|
|
NetworkCommands.autoconfiguration name
|
|
|
|
when DHCP
|
|
|
|
NetworkCommands.dhcp6 name
|
|
|
|
when NotSetup
|
|
|
|
# do nothing
|
|
|
|
else
|
|
|
|
raise "ipv6 configuration: neither static nor dynamic"
|
|
|
|
end
|
|
|
|
|
|
|
|
# We wont setup aliasses unless there is an actual IP address
|
|
|
|
if ip != NotSetup
|
|
|
|
aliasses_v6.each do |ip_alias|
|
|
|
|
NetworkCommands.set_alias name, ip_alias
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-10-17 18:29:26 +02:00
|
|
|
|
|
|
|
# DNS configuration
|
|
|
|
dns.execute
|
2019-10-14 00:42:15 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def down
|
|
|
|
unless NetworkCommands.interface_exists(@name)
|
|
|
|
raise "The interface #{@name} doesn't exists or is already down."
|
|
|
|
end
|
|
|
|
|
|
|
|
NetworkCommands.flush name
|
|
|
|
NetworkCommands.down name
|
|
|
|
end
|
|
|
|
|
|
|
|
def scan
|
|
|
|
unless NetworkCommands.interface_exists(@name)
|
|
|
|
raise "The interface #{@name} doesn't exists or is already down."
|
|
|
|
end
|
|
|
|
|
|
|
|
ssid_list = NetworkCommands.scan name
|
|
|
|
pp! ssid_list
|
|
|
|
end
|
|
|
|
end
|