goubus

package module
v0.0.0-...-46734e8 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2023 License: MIT Imports: 11 Imported by: 0

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

View Source
const (
	EmptySession         = "00000000000000000000000000000000"
	DefaultSocketPath    = "/var/run/ubus/ubus.sock"
	DefaultInvokeTimeout = time.Second * 3
)

Variables

View Source
var (
	SysErrorIDMismatch     = errors.New("response id mismatch")
	SysErrorNotImplemented = errors.New("function not implemented")
)
View Source
var (
	UbusErrorPermissionDenied = ubusError{UbusStatusPermissionDenied, "Permission Denied"}
	UbusErrorInvalidCommand   = ubusError{UbusStatusInvalidCommand, "Invalid Command"}
	UbusErrorInvalidArgument  = ubusError{UbusStatusInvalidArgument, "Invalid Argument"}
	UbusErrorUnknown          = ubusError{UbusStatusUnknown, "Unknown Error"}
	UbusErrorAccessDenied     = ubusError{UbusStatusAccessDenied, "Access denied"}
)

Functions

func SetLogLevel

func SetLogLevel(lvl slog.Level)

func UbusError

func UbusError(code int) error

Types

type UBus

type UBus struct {
	// contains filtered or unexported fields
}

UBus represents information to JSON-RPC Interaction with router

func NewUbus

func NewUbus(endp string) (*UBus, error)

func (*UBus) Call

func (u *UBus) Call(ubusObj, ubusMethod string, args map[string]interface{}) (string, error)

func (*UBus) List

func (u *UBus) List(ubusObj, ubusMethod string, args map[string]interface{}) (map[string]gjson.Result, error)

func (*UBus) LogWrite

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

func (*UBus) Login

func (u *UBus) Login(username, password string) (*authData, error)

Login Call JSON-RPC method to Router Authentication

func (*UBus) Logined

func (u *UBus) Logined() error

Logined check if login RPC Session id has expired

func (*UBus) RPCRequest

func (u *UBus) RPCRequest(method, ubusObj, ubusMethod string, args map[string]interface{}) (string, error)

type UbusLog

type UbusLog struct {
	Log []UbusLogData
}

type UbusLogData

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

type UbusParamMap

type UbusParamMap map[string]interface{}

type UbusResponseCode

type UbusResponseCode = int
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
	UbusStatusUnknown          UbusResponseCode = 9
	UbusStatusConnectionFailed UbusResponseCode = 10
	UbusStatusLast             UbusResponseCode = 11
	UbusStatusAccessDenied                      = -32002
)

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

Jump to

Keyboard shortcuts

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