Giter Club home page Giter Club logo

wpa-connect's Introduction

wpa-connect

Package provides API for connection Linux device to Wi-Fi Network.

wpa-connect communicates with WPA supplicant over D-Bus (linux message bus system).

This package was developed as part of IoT project in order to add Wi-Fi connectivity to headless Raspberry Pi like devices. No need for connman or Network Manager be installed.

Setup

On Linux:

wpa_supplicant service should run with -u flag in order to enable DBus interface. Run it as Linux service before first call to wpa_supplicant. Otherwise system will start it automatically without -u flag.

Systemd service configuration file - /etc/systemd/system/[email protected]

[Unit]
Description=WPA supplicant for %i

[Service]
ExecStart=/usr/sbin/wpa_supplicant -u -i%i -c/etc/wpa_supplicant.conf -Dwext

[Install]
WantedBy=multi-user.target

On Raspberry PI OS (Debian Buster):

Raspbery PI OS (formerely known as Raspbian) uses dhcpd-run-hooks to setup and invoke the wpa_supplicant daemon.

  1. Disable the systemd managed wpa_supplicant located under /etc/systemd/dbus-fi.w1.wpa_supplicant1.service by running sudo systemctl disable wpa_supplicant
  2. Modify the existing wpa_supplicant dhcpd-run-hook available under /lib/dhcpcd/dhcpcd-hooks/10-wpa_supplicant by adding the -u flag to the invocation of the wpa_supplicant daemon in the wpa_supplicant_start() function.
  3. Alternatively run sudo sed -i 's/wpa_supplicant -B/wpa_supplicant -u -B/g' /lib/dhcpcd/dhcpcd-hooks/10-wpa_supplicant to modify the hook in place.

On Project:

go get github.com/mark2b/wpa-connect

Usage

Please see godoc.org for documentation. (Not ready yet)

Examples

Connect to Wi-Fi network

import wifi "wpa-connect"

if conn, err := wifi.ConnectManager.Connect(ssid, password, time.Second * 60); err == nil {
	fmt.Println("Connected", conn.NetInterface, conn.SSID, conn.IP4.String(), conn.IP6.String())
} else {
	fmt.Println(err)
}

Scan for Wi-Fi networks

import wifi "wpa-connect"

if bssList, err := wifi.ScanManager.Scan(); err == nil {
	for _, bss := range bssList {
		print(bss.SSID, bss.Signal, bss.KeyMgmt)
	}
}

Package release under a MIT license.

wpa-connect's People

Contributors

andy-shi88 avatar buildscientist avatar mark2b avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

wpa-connect's Issues

Error - "address_not_allocated"

Hello.
My code.

package main

import (
	"fmt"
	"time"

	wifi "github.com/mark2b/wpa-connect"
)

func main() {
	ssid := "My_name_wifi"
	password := "My_password_wifi"
	if conn, err := wifi.ConnectManager.Connect(ssid, password, time.Second*60); err == nil {
		fmt.Println("Connected", conn.NetInterface, conn.SSID, conn.IP4.String(), conn.IP6.String())
		// fmt.Println("Connected", conn)
	} else {
		fmt.Println("Err connect: ", err)
	}
}

An error appears - "address_not_allocated".
Wifi is working properly. The password is correct.
Can you tell me what the problem may be?

I understand that the problem is related to the code -

func (self *connectContext) hasIP() bool {
	return self.ip4 != nil && self.ip6 != nil
}

Advice

Trying to figure out how to use this, it seems like a good way to go with headless config on a raspberry pi. I'm calling it like this:

bssList, err := wifi.ScanManager.Scan()
	if err != nil {
		return err
	}
	for _, bss := range bssList {
		print(bss.SSID, bss.Signal, bss.KeyMgmt)
	}

	conn, err := wifi.ConnectManager.Connect(wificreds.SSID, wificreds.Password, time.Second*60)
	if err != nil {
		return err
	}

But all I'm getting is:
wpa_supplicant knows nothing about this interface.

When I ps wpa_supplicant the table looks like this:

root       377     1  0 04:24 ?        00:00:00 /sbin/wpa_supplicant -u -s -O /run/wpa_supplicant
root       421     1  0 04:24 ?        00:00:00 wpa_supplicant -B -c/etc/wpa_supplicant/wpa_supplicant.conf -iwlan0 -Dnl80211,wext

I see the -u option in there and when I run dbus-send --system --dest=org.freedesktop.DBus --type=method_call --print-reply /org/freedesktop/DBus org.freedesktop.DBus.ListNames I get:

method return time=1609130134.013166 sender=org.freedesktop.DBus -> destination=:1.16 serial=3 reply_serial=2
   array [
      string "org.freedesktop.DBus"
      string "org.freedesktop.login1"
      string "org.freedesktop.timesync1"
      string "org.freedesktop.systemd1"
      string "org.freedesktop.Avahi"
      string "org.bluez"
      string ":1.12"
      string ":1.0"
      string ":1.1"
      string ":1.2"
      string ":1.16"
      string ":1.3"
      string ":1.4"
      string "fi.epitest.hostap.WPASupplicant"
      string "fi.w1.wpa_supplicant1"
    

Running the latest RaspberryPi OS armhf as well.

WPA_Supplicant DBUS Interface Permissions

@mark2b Opening this up as an issue more for documentation purposes in case anyone else runs into this. I ran into this issue while developing a Go service using your library on Ubuntu. Likely not an issue on Raspberry PI OS since it doesn't use Polkit.

When using DBUS to manage wpa_supplicant some Linux distributions utilize Polkit (formerly known as PolicyKit) to manage permissions for processes that can interface directly with the bus.

This blog provides a good treatise on the subject matter. Developers running on Debian based distro's like Ubuntu will need to modify the /etc/dbus-1/system.d/wpa_supplicant.conf to allow your user (non-root) access to interface with dbus.

Add another <policy_user> block in addition to the existing one.

<busconfig>
        <policy user="yourUserName">
                <allow own="fi.w1.wpa_supplicant1"/>

                <allow send_destination="fi.w1.wpa_supplicant1"/>
                <allow send_interface="fi.w1.wpa_supplicant1"/>
                <allow receive_sender="fi.w1.wpa_supplicant1" receive_type="signal"/>
        </policy>
</busconfig>

Note the policy user you set should be identical to the owner of the process running the Go binary using the wpa-connect library.

Scan/Connect Managers hardcode wifi dev name

The scan & connect managers both hardcode the wifi dev name to wlan0 unless the developer overrides the device name prior to calling the Scan() or Connect() methods.

Following your code example one would need to do the following:

wifi.ScanManager.NetInterface  = "customUDev0"
if bssList, err := wifi.ScanManager.Scan(); err == nil {
	for _, bss := range bssList {
		print(bss.SSID, bss.Signal, bss.KeyMgmt)
	}
}

Is there any reason why the Scan/Connect manager define global vars with a pre-instantiated struct setting the wifi uDev name? I think it would make more sense to require the developer to create a reference to their own Scan/Connect managers. Consider a case where the system using this library has multiple wifi devices.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.