UnifiVoucherGenerator

package module
v1.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 31, 2024 License: MIT Imports: 10 Imported by: 0

README

UniFi Network Application - Go Voucher Generator

Origin Story

After setting up Ubiquiti access points at my place, I aimed to offer straightforward Guest Wi-Fi. I didn't want to use a PSK. However, I also needed to ensure that only people inside my home could use it, avoiding external access. To solve this, I introduced a button that generates a small voucher code upon being pressed.

Overview

The is an open-source Golang library designed to interface with the UniFi Network Application dashboard, facilitating the automated generation and fetching of new vouchers for WiFi hotspot landing pages.

By enabling the programmatic generation of vouchers, this library hopes to broaden the scope for creating better guest Wi-Fi experiences.

Installation

go get -u github.com/jeremyforan/UnifiVoucherGenerator

Usage

A basic example of the library creating a basic voucher.

package main

import (
	"fmt"
	"github.com/jeremyforan/UnifiVoucherGenerator"
	"github.com/jeremyforan/UnifiVoucherGenerator/voucher"
	"log/slog"
	"net/url"
)

func main() {

	// URL of the Unifi controller.
	baseUrl, err := url.Parse("https://127.0.0.1:8443")
	if err != nil {
		panic(err)
	}

	// Create a new client with the username, password, and URL of the Unifi controller.
	client := UnifiVoucherGenerator.NewClient("user@email.com", "p455w0rd", baseUrl)

	// Login to the Unifi controller.
	err = client.Login()
	if err != nil {
		slog.Info("Failed to login to Unifi controller")
		panic(err)
	}

	// Create a new voucher with default settings.
	v := voucher.NewDefaultVoucher()

	// Add the Voucher to the Unifi controller.
	err = client.AddVoucher(v)
	if err != nil {
		slog.Error("unable to add voucher")
		panic(err)
	}

	fmt.Println(v.AccessCode())
}
Expiration

You can set the expiry of a voucher using the SetExpire method.

v := voucher.NewDefaultVoucher()

// Voucher will expire 7 days from the moment a guest enters it into the landing page.
v.SetExpire(7, voucher.Days)

// Voucher will expire 8 hours from the moment a guest enters it into the landing page.
v.SetExpire(8, voucher.Hours)

// Voucher will expire 59 minutes from the moment a guest enters it into the landing page.
v.SetExpire(59, voucher.Minutes)
Network Limits

You can set the network usage limits with the following:

v := voucher.NewDefaultVoucher()

// Set download to 10Mbps and upload to 2Mbps
v.SetDownloadLimitMbps(10)
v.SetUploadLimitMbps(2)

// Set data limit to 100MB
v.SetDataLimitMB(100)

Contributions

Pull requests are welcome. Feel free to...

  • Revise documentation
  • Add new features
  • Fix bugs
  • Suggest improvements

Limitations

Feedback

Pull requests are welcome. Feel free to...

  • Revise documentation
  • Add new features
  • Fix bugs
  • Suggest improvements

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	Credentials credentials.Credentials

	Url *url.URL

	*voucher.Voucher
	// contains filtered or unexported fields
}

Client is the primary struct that interacts with the Unifi controller using http requests. It holds the credentials, http client, url, and token for the Unifi controller Before using the client, the Login method must be called to authenticate with the Unifi controller. If no error is returned, the client is ready to add vouchers.

func NewClient

func NewClient(username string, password string, url *url.URL) *Client

NewClient creates a new Client struct to interact with the Unifi controller

func (*Client) AddVoucher

func (c *Client) AddVoucher(v *voucher.Voucher) error

AddVoucher sends a request to the Unifi controller to add a voucher. If the request is successful, the voucher is stored in the client struct.

func (*Client) FetchVouchers

func (c *Client) FetchVouchers() (UnifiVouchers, error)

func (*Client) Login

func (c *Client) Login() error

Login sends a login request to the Unifi controller. If the login is successful, the csrf_token is stored in the client struct for future requests.

type LoginResponse

type LoginResponse struct {
	Meta Meta          `json:"meta"` // Maps the "meta" field
	Data []interface{} `json:"data"` // Use []interface{} for arbitrary data; adjust as needed
}

type Meta

type Meta struct {
	Rc string `json:"rc"` // Maps the "rc" field to check if it's "ok"
}

type RequestNewVoucherResponse

type RequestNewVoucherResponse struct {
	Meta struct {
		Rc string `json:"rc"`
	} `json:"meta"`
	Data []struct {
		CreateTime int `json:"create_time"`
	} `json:"data"`
}

type UnifiVoucher

type UnifiVoucher struct {
	Duration      int    `json:"duration"`
	QosOverwrite  bool   `json:"qos_overwrite"`
	Note          string `json:"note"`
	Code          string `json:"code"`
	ForHotspot    bool   `json:"for_hotspot"`
	CreateTime    int64  `json:"create_time"`
	Quota         int    `json:"quota"`
	SiteID        string `json:"site_id"`
	ID            string `json:"_id"`
	AdminName     string `json:"admin_name"`
	Used          int    `json:"used"`
	Status        string `json:"status"`
	StatusExpires int    `json:"status_expires"`
}

UnifiVoucher Define struct for each item in the data array

type UnifiVouchers

type UnifiVouchers []UnifiVoucher

type VoucherListResponse

type VoucherListResponse struct {
	Meta Meta          `json:"meta"`
	Data UnifiVouchers `json:"data"`
}

VoucherListResponse Define struct for the top-level JSON object

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL