goubus

package module
v0.0.0-...-fb56fa5 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2024 License: GPL-3.0 Imports: 9 Imported by: 1

README

GoUbus

That library was developed for communication with Ubus (OpenWrt micro bus architecture) with http requests. The library is in alpha-development and NOT RECOMMENDED FOR PRODUCTION until the moment.

The project was created to solve a problem at UNIVERSIDADE DO ESTADO DO RIO GRANDE DO NORTE - UERN with network management with low-cost routers.

Please check our License

TO-DO

  • Structs to represent the Ubus interaction following JSON-RPC 2.0
  • Login Function
  • Wireless Info Get
  • Interfaces Info Get
  • DHCP Info Get
  • File Management
  • Log System
  • UCI
  • Check if the necessary plugins are installed on OpenWRT Router

Conventions

The ID 1 from JSON-RPC 2.0 Requests is reserved for Login procedure

Example

package main

import (
	"fmt"
	"log"
	"github.com/cdavid14/goubus"
)

func main() {
	ubus := goubus.Ubus{
	  Username: "root",
	  Password: "admin",
	  URL:      "http://192.168.1.1/ubus",
	}
	result, err := ubus.Login()
	if err != nil {
	  log.Fatal(err)
	}
	fmt.Println(result)
}

and it will return something like

{8b93715dbb85378d87daf0b1cc64a83b 300 299 2019-03-25 01:36:28.248837063 -0300 -03 m=+299.016411013 {map[uci-access:[read write] unauthenticated:[read]] map[session:[access login]] map[*:[read write]]} map[username:root]}

Final Notes

Please contribute to make this library most usually as possible and improve more functions!

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DHCPIPv4Lease

type DHCPIPv4Lease struct {
	Hostname  string
	IPv4      string
	MAC       string
	Leasetime time.Time
	ClientID  string
}

type DHCPIPv4Leases

type DHCPIPv4Leases []DHCPIPv4Lease

type DHCPIPv6Address

type DHCPIPv6Address struct {
	Address           string
	PreferredLifetime int `json:"preferred-lifetime"`
	ValidLifetime     int `json:"valid-lifetime"`
}

type DHCPIPv6Device

type DHCPIPv6Device struct {
	Device string
	Leases []DHCPIPv6Lease
}

type DHCPIPv6Lease

type DHCPIPv6Lease struct {
	DUID         string
	IAID         int
	Hostname     string
	AcceptReconf bool `json:"accept-reconf"`
	Assigned     int
	Flags        []string
	IPv6Addr     []DHCPIPv6Address `json:"ipv6-addr"`
	Valid        int
}

type DHCPIPv6Leases

type DHCPIPv6Leases struct {
	Devices []DHCPIPv6Device
}

type Ubus

type Ubus struct {
	Username string
	Password string
	URL      string
	AuthData UbusAuthData
}

Ubus represents information to JSON-RPC Interaction with router

func (*Ubus) AuthLogin

func (u *Ubus) AuthLogin() (UbusResponse, error)

AuthLogin Call JSON-RPC method to Router Authentication

func (*Ubus) Call

func (u *Ubus) Call(jsonStr []byte) (UbusResponse, error)

Call do a call to Json-RPC to get/set information

func (*Ubus) DHCPIPv4Leases

func (u *Ubus) DHCPIPv4Leases(id int) (DHCPIPv4Leases, error)

func (*Ubus) DHCPIPv6Leases

func (u *Ubus) DHCPIPv6Leases(id int) (DHCPIPv6Leases, error)

func (*Ubus) FileExec

func (u *Ubus) FileExec(id int, command string, params []string) (UbusExec, error)

func (*Ubus) FileList

func (u *Ubus) FileList(id int, path string) (UbusFileList, error)

func (*Ubus) FileRead

func (u *Ubus) FileRead(id int, path string) (UbusFile, error)

func (*Ubus) FileStat

func (u *Ubus) FileStat(id int, path string) (UbusFileStat, error)

func (*Ubus) FileWrite

func (u *Ubus) FileWrite(id int, path string, data string, append bool, mode int, base64 bool) error

func (*Ubus) LogRead

func (u *Ubus) LogRead(id int, lines int, stream bool, oneshot bool) (UbusLog, error)

func (*Ubus) LogWrite

func (u *Ubus) LogWrite(id int, event string) error

func (*Ubus) LoginCheck

func (u *Ubus) LoginCheck() error

LoginCheck check if login RPC Session id has expired

func (*Ubus) NetworkDeviceStatus

func (u *Ubus) NetworkDeviceStatus(id int, name string) (UbusNetworkDevice, error)

func (*Ubus) UciChanges

func (u *Ubus) UciChanges(id int) (map[string]map[string][][]string, error)

func (*Ubus) UciCommit

func (u *Ubus) UciCommit(id int, config string) error

func (*Ubus) UciGetConfig

func (u *Ubus) UciGetConfig(id int, request UbusUciRequest) (UbusUciResponse, error)

func (*Ubus) UciGetConfigs

func (u *Ubus) UciGetConfigs(id int) (UbusUciConfigs, error)

func (*Ubus) UciReloadConfig

func (u *Ubus) UciReloadConfig(id int) error

func (*Ubus) UciSetConfig

func (u *Ubus) UciSetConfig(id int, request interface{}) error

func (*Ubus) WirelessAssocList

func (u *Ubus) WirelessAssocList(id int, device string, mac string) (UbusWirelessAssocList, error)

func (*Ubus) WirelessCountryList

func (u *Ubus) WirelessCountryList(id int, device string) (UbusCountryList, error)

func (*Ubus) WirelessDevices

func (u *Ubus) WirelessDevices(id int) (UbusWirelessDevice, error)

func (*Ubus) WirelessFreqList

func (u *Ubus) WirelessFreqList(id int, device string) (UbusWirelessFreqList, error)

func (*Ubus) WirelessInfo

func (u *Ubus) WirelessInfo(id int, device string) (UbusWirelessInfoData, error)

func (*Ubus) WirelessScanner

func (u *Ubus) WirelessScanner(id int, device string) (UbusWirelessScanner, error)

func (*Ubus) WirelessTxPowerList

func (u *Ubus) WirelessTxPowerList(id int, device string) (UbusTxPowerList, error)

type UbusAuthACLS

type UbusAuthACLS struct {
	AccessGroup map[string][]string `json:"access-group"`
	Ubus        map[string][]string
	Uci         map[string][]string
}

UbusAuthACLS represents the ACL from user on Authentication

type UbusAuthData

type UbusAuthData struct {
	UbusRPCSession string `json:"ubus_rpc_session"`
	Timeout        int
	Expires        int
	ExpireTime     time.Time
	ACLs           UbusAuthACLS `json:"acls"`
	Data           map[string]string
}

UbusAuthData represents the Data response from auth module

type UbusCountryList

type UbusCountryList struct {
	Results []UbusCountryListData
}

type UbusCountryListData

type UbusCountryListData struct {
	Code    string
	Country string
	ISO3166 string
	Active  bool
}

type UbusExec

type UbusExec struct {
	Code   int
	Stdout string
}

type UbusFile

type UbusFile struct {
	Data string
}

type UbusFileList

type UbusFileList struct {
	Entries []UbusFileListData
}

type UbusFileListData

type UbusFileListData struct {
	Name string
	Type string
}

type UbusFileStat

type UbusFileStat struct {
	Path  string
	Type  string
	Size  int
	Mode  int
	Atime int
	Mtime int
	Ctime int
	Inode int
	Uid   int
	Gid   int
}

type UbusLog

type UbusLog struct {
	Log []UbusLogData
}

type UbusLogData

type UbusLogData struct {
	Msg      string
	ID       int
	Priority int
	Source   int
	Time     int
}

type UbusNetworkDevice

type UbusNetworkDevice struct {
	Acceptlocal         bool                       `json:"acceptlocal"`
	Carrier             bool                       `json:"carrier"`
	Dadtransmits        int                        `json:"dadtransmits"`
	External            bool                       `json:"external"`
	Igmpversion         int                        `json:"igmpversion"`
	Ipv6                bool                       `json:"ipv6"`
	Macaddr             string                     `json:"macaddr"`
	Mldversion          int                        `json:"mldversion"`
	Mtu                 int                        `json:"mtu"`
	Mtu6                int                        `json:"mtu6"`
	Multicast           bool                       `json:"multicast"`
	Neigh4gcstaletime   int                        `json:"neigh4gcstaletime"`
	Neigh4locktime      int                        `json:"neigh4locktime"`
	Neigh4reachabletime int                        `json:"neigh4reachabletime"`
	Neigh6gcstaletime   int                        `json:"neigh6gcstabletime"`
	Neigh6reachabletime int                        `json:"neigh6reachabletime"`
	Present             bool                       `json:"present"`
	Promisc             bool                       `json:"promisc"`
	Rpfilter            int                        `json:"rpfilter"`
	Sendredirects       bool                       `json:"sendredirects"`
	Statistics          UbusNetworkDeviceStatistic `json:"statistics"`
	Txqueuelen          int                        `json:"txqueuelen"`
	Type                string                     `json:"type"`
	Up                  bool                       `json:"up"`
}

type UbusNetworkDeviceStatistic

type UbusNetworkDeviceStatistic struct {
	Collisions       int `json:"collisions"`
	RxFrameErrors    int `json:"rx_frame_errors"`
	TxCompressed     int `json:"tx_compressed"`
	Multicast        int `json:"multicast"`
	RxLengthErrors   int `json:"rx_length_errors"`
	TxDropped        int `json:"tx_dropped"`
	RxBytes          int `json:"rx_bytes"`
	RxMissedErrors   int `json:"rx_missed_errors"`
	TxErrors         int `json:"tx_errors"`
	RxCompressed     int `json:"rx_compressed"`
	RxOverErrors     int `json:"rx_over_errors"`
	TxFifoErrors     int `json:"tx_fifo_errors"`
	RxCrcErrors      int `json:"rx_crc_errors"`
	RxPackets        int `json:"rx_packets"`
	TxHeatbeatErrors int `json:"tx_heatbeat_errors"`
	RxDropped        int `json:"rx_dropped"`
	TxAbortedErrors  int `json:"tx_aborted_errors"`
	TxPackets        int `json:"tx_packets"`
	RxErrors         int `json:"rx_errors"`
	TxBytes          int `json:"tx_bytes"`
	TxWindowErrors   int `json:"tx_window_errors"`
	RxFifoErrors     int `json:"rx_fifo_errors"`
	TxCarrierErrors  int `json:"tx_carrier_errors"`
}

type UbusResponse

type UbusResponse struct {
	JSONRPC          string
	ID               int
	Error            UbusResponseError
	Result           interface{}
	UbusResponseCode UbusResponseCode
}

UbusResponse represents a response from JSON-RPC

type UbusResponseCode

type UbusResponseCode int

UbusResponseCode represent the status code from JSON-RPC Call

const (
	UbusStatusOK               UbusResponseCode = 0
	UbusStatusInvalidCommand   UbusResponseCode = 1
	UbusStatusInvalidArgument  UbusResponseCode = 2
	UbusStatusMethodNotFound   UbusResponseCode = 3
	UbusStatusNotFound         UbusResponseCode = 4
	UbusStatusNoData           UbusResponseCode = 5
	UbusStatusPermissionDenied UbusResponseCode = 6
	UbusStatusTimeout          UbusResponseCode = 7
	UbusStatusNotSupported     UbusResponseCode = 8
	UbusStatusUnknownError     UbusResponseCode = 9
	UbusStatusConnectionFailed UbusResponseCode = 10
	UbusStatusLast             UbusResponseCode = 11
)

Represents enum ubus_msg_status from https://git.openwrt.org/?p=project/ubus.git;a=blob;f=ubusmsg.h;h=398b126b6dc01833937749a110181ea0debb1476;hb=HEAD

func (UbusResponseCode) String

func (i UbusResponseCode) String() string

type UbusResponseError

type UbusResponseError struct {
	Code    int
	Message string
}

type UbusTxPowerList

type UbusTxPowerList struct {
	Results []UbusTxPowerListData
}

type UbusTxPowerListData

type UbusTxPowerListData struct {
	Dbm    int
	Mw     int
	Active bool
}

type UbusUciConfigs

type UbusUciConfigs struct {
	Configs []string
}

type UbusUciRequest

type UbusUciRequest struct {
	UbusUciRequestGeneric
	Values map[string]string `json:"values,omitempty"`
}

type UbusUciRequestGeneric

type UbusUciRequestGeneric struct {
	Config  string `json:"config"`
	Section string `json:"section,omitempty"`
	Option  string `json:"option,omitempty"`
	Type    string `json:"type,omitempty"`
	Match   string `json:"match,omitempty"`
}

type UbusUciRequestList

type UbusUciRequestList struct {
	UbusUciRequestGeneric
	Values map[string][]string `json:"values,omitempty"`
}

type UbusUciResponse

type UbusUciResponse struct {
	Value  interface{}
	Values interface{}
}

type UbusWirelessAssocList

type UbusWirelessAssocList struct {
	Results []UbusWirelessAssocListData
}

type UbusWirelessAssocListData

type UbusWirelessAssocListData struct {
	Mac      string
	Signal   int
	Noise    int
	Inactive int
	Rx       UbusWirelessAssocListRate
	Tx       UbusWirelessAssocListRate
}

type UbusWirelessAssocListRate

type UbusWirelessAssocListRate struct {
	Rate    int
	Mcs     int
	Is40Mhz bool `json:"40mhz"`
	ShortGi bool
}

type UbusWirelessDevice

type UbusWirelessDevice struct {
	Devices []string
}

type UbusWirelessEncryption

type UbusWirelessEncryption struct {
	Enabled        bool
	Wpa            []int
	Authentication []string
	Ciphers        []string
}

type UbusWirelessFreqList

type UbusWirelessFreqList struct {
	Results []UbusWirelessFreqListData
}

type UbusWirelessFreqListData

type UbusWirelessFreqListData struct {
	Channel    int
	Mhz        int
	Restricted bool
	Active     bool
}

type UbusWirelessInfoData

type UbusWirelessInfoData struct {
	Phy        string
	SSID       string
	BSSID      string
	Country    string
	Mode       string
	Channel    int
	Frequency  int
	TXPower    int
	Quality    int
	Qualitymax int
	Signal     int
	Noise      int
	Bitrate    int
	Encryption UbusWirelessEncryption
	Hwmodes    []string
	Hardware   UbusWirelessInfoHardware
}

type UbusWirelessInfoHardware

type UbusWirelessInfoHardware struct {
	Name string
}

type UbusWirelessScanner

type UbusWirelessScanner struct {
	Results []UbusWirelessScannerData
}

type UbusWirelessScannerData

type UbusWirelessScannerData struct {
	SSID       string
	BSSID      string
	Mode       string
	Channel    int
	Signal     int
	Quality    int
	QualityMax int
	Encryption UbusWirelessEncryption
}

Jump to

Keyboard shortcuts

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