From 702ca7af634f00bc16a4a5cce5d7b31369da5f82 Mon Sep 17 00:00:00 2001 From: Philippe PITTOLI Date: Tue, 22 Oct 2019 13:19:16 +0200 Subject: [PATCH] meh, stuff around wireless configuration --- shard.yml | 2 ++ src/configuration.cr | 67 ++++++++++++++++++++++++++++++++++------- src/context.cr | 2 ++ src/network_commands.cr | 24 +++++++++++++++ 4 files changed, 84 insertions(+), 11 deletions(-) diff --git a/shard.yml b/shard.yml index fb8b5ea..86cad2a 100644 --- a/shard.yml +++ b/shard.yml @@ -15,6 +15,8 @@ description: | targets: networkctl: main: src/main.cr + tests: + main: src/tests.cr dependencies: ipaddress: diff --git a/src/configuration.cr b/src/configuration.cr index eb6660c..50a9ab6 100644 --- a/src/configuration.cr +++ b/src/configuration.cr @@ -38,6 +38,10 @@ class WirelessAPSetup property key : String def initialize(@key) end + + def to_s(io : IO) + io << "WPA access point, key #{key}" + end end def initialize(@ssid, @security) @@ -84,6 +88,11 @@ class WirelessAPSetup end end + def store_access_point_keys + puts "TODO: store_access_point_keys" + puts "security for #{ssid} = #{security}" + end + def execute puts "TODO: wireless configuration" @@ -191,23 +200,50 @@ class InterfaceConfiguration end end + 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| + # k = ssid + puts "#{CGREEN}configuring #{ssid}#{CRESET}" + wireless_configuration.store_access_point_keys + end + end + # configure the interface def execute unless NetworkCommands.interface_exists(@name) raise "The interface #{@name} doesn't exists, yet." end - # TODO: treat differently wireless and non-wireless interfaces - if @wireless - puts "interface #{name} is wireless" - puts "TODO:" - puts "1. scan for SSID" - puts "2. select configured SSID then try to connect" - puts "3. connectivity check with the gateway" - else - puts "interface #{name} is not wireless" - end - if up NetworkCommands.up name else @@ -223,6 +259,15 @@ class InterfaceConfiguration NetworkCommands.description name, description.not_nil! end + # TODO: treat differently wireless and non-wireless interfaces + if @wireless + puts "interface #{name} is wireless: connection to the access point" + store_access_point_keys + access_point_connection + else + puts "interface #{name} is not wireless" + end + # ipv4 configuration main_ip_v4.tap do |ip| case ip diff --git a/src/context.cr b/src/context.cr index 3dfb2de..2facf26 100644 --- a/src/context.cr +++ b/src/context.cr @@ -2,6 +2,8 @@ class Context class_property root : String = "/" + class_property keydir : String = "/etc/network/keydir" + class_property simulation = false class_property verbosity = 1 diff --git a/src/network_commands.cr b/src/network_commands.cr index d4bcf4a..00685d8 100644 --- a/src/network_commands.cr +++ b/src/network_commands.cr @@ -70,6 +70,30 @@ class NetworkCommands end end + class WPASupplicant + # verify if the passphrase already is stored for wpa_supplicant + def self.passphrase?(ssid : String) + puts "TODO: is the wpa_supplicant passphrase for ssid #{ssid} stored?" + end + + # store the AP passphrase in the wpa_supplicant way + def self.passphrase(ssid : String, passphrase : String) + puts "TODO: storing wpa_supplicant passphrase #{passphrase} for ssid #{ssid}" + File.open("#{Context.root}/#{Context.keydir}/#{ssid}.conf") do |file| + Do.run("wpa_passphrase", [ ssid, passphrase ]) do |content| + file.puts content + end + end + end + + def self.connection(ssid : String, ifname : String) + unless Do.run("wpa_supplicant", + [ "-B", "-c", "#{Context.root}/#{Context.keydir}/#{ssid}.conf", "-i", ifname ]).success? + raise "(wpa_supplicant) cannot connect to the wireless AP #{ssid} via the interface #{ifname}" + end + end + end + class UDHCPCCommand def self.run(ifname : String) unless Do.run("udhcpc", [ ifname ]).success?