Documentation
¶
Overview ¶
Package routeros implements a client for the MikroTik RouterOS REST API.
The entry point is a Client, created from a Config (loadable from environment variables with NewClientConfigFromEnv) that carries the base URL, credentials, request timeout, and an optional custom *http.Client. The client groups typed services covering IP firewall address lists, IP addresses, IP routes, and the ping tool.
Every request passes through a rate.Limiter before hitting the device; pacing and burst size are configured via the Config fields PauseBetweenRequests and BurstRequestCount. HTTP-level failures surface as the typed ResponseError, and invalid configuration returns the exported ErrMissBaseURL or ErrMissUserOrPass sentinels.
The upstream API is documented at https://help.mikrotik.com/docs/display/ROS/REST+API. API_COVERAGE.md in the repository tracks which endpoints are implemented and which are planned.
Index ¶
- Variables
- type Client
- type Config
- type IPFirewallAddressListService
- type IPRouteService
- func (s *IPRouteService) AddRoute(ctx context.Context, item types.IPRouteAdd) (types.IPRoute, error)
- func (s *IPRouteService) GetRouteByID(ctx context.Context, id string) (types.IPRoute, error)
- func (s *IPRouteService) GetRoutes(ctx context.Context) (types.IPRouteList, error)
- func (s *IPRouteService) RemoveRoute(ctx context.Context, id string) error
- func (s *IPRouteService) UpdateRoute(ctx context.Context, id string, item types.IPRouteAdd) (types.IPRoute, error)
- type IPService
- func (ips *IPService) AddAddress(ctx context.Context, item types.IPAddressAdd) (types.IPAddress, error)
- func (ips *IPService) GetAddressByID(ctx context.Context, id string) (types.IPAddress, error)
- func (ips *IPService) GetAddresses(ctx context.Context) (types.IPAddressList, error)
- func (ips *IPService) RemoveAddress(ctx context.Context, id string) error
- func (ips *IPService) UpdateAddress(ctx context.Context, id string, item types.IPAddressAdd) (types.IPAddress, error)
- type ResponseError
- type ToolService
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrMissBaseURL is returned by NewClient when Config.BaseURL is empty. ErrMissBaseURL = errors.New("miss baseURL") // ErrMissUserOrPass is returned by NewClient when Config.User // or Config.Password is empty. ErrMissUserOrPass = errors.New("miss user/password") )
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
IPFirewallAddressListService *IPFirewallAddressListService
ToolService *ToolService
IPService *IPService
IPRouteService *IPRouteService
// contains filtered or unexported fields
}
func NewClient ¶
Example ¶
ExampleNewClient builds a client from an explicitly constructed Config.
RequestTimeout is set explicitly because the zero value would expire every request context immediately at runtime.
package main
import (
"fmt"
"time"
"github.com/art-frela/routeros"
)
func main() {
cfg := routeros.Config{
BaseURL: "http://192.168.88.1",
RequestTimeout: 10 * time.Second,
User: "admin",
Password: "master",
}
client, err := routeros.NewClient(cfg)
if err != nil {
fmt.Println("client error:", err)
return
}
fmt.Println("client base URL:", client.BaseURL())
}
Output:
type Config ¶
type Config struct {
BaseURL string `envconfig:"base_url" validate:"required"`
RequestTimeout time.Duration `envconfig:"request_timeout" default:"10s"`
PauseBetweenRequests time.Duration `envconfig:"pause_between_requests" default:"100ms"`
BurstRequestCount int `envconfig:"burst_req_count" default:"10"`
User string `envconfig:"user" default:"root"`
Password string `envconfig:"password" default:"master"`
// HTTPClient is an optional custom *http.Client; envconfig:"-" is required
// because envconfig cannot derive an env key for *http.Client.
HTTPClient *http.Client `envconfig:"-"`
}
func NewClientConfigFromEnv ¶
Example ¶
ExampleNewClientConfigFromEnv loads the client configuration from environment variables prefixed with ROS_ (ROS_BASE_URL, ROS_USER and ROS_PASSWORD — see the Config documentation for the full list).
package main
import (
"fmt"
"github.com/art-frela/routeros"
)
func main() {
cfg, err := routeros.NewClientConfigFromEnv("ROS")
if err != nil {
fmt.Println("config error:", err)
return
}
client, err := routeros.NewClient(*cfg)
if err != nil {
fmt.Println("client error:", err)
return
}
fmt.Println("client base URL:", client.BaseURL())
}
Output:
type IPFirewallAddressListService ¶
type IPFirewallAddressListService struct {
// contains filtered or unexported fields
}
IPFirewallAddressListService handles communication with the IP firewall address-list methods of the RouterOS REST API.
RouterOS API docs: https://manual.mikrotik.com/docs/Developer+Guides/rest-api
func (*IPFirewallAddressListService) Add ¶
func (ipfwls *IPFirewallAddressListService) Add(ctx context.Context, item types.FirewallAddressListNewItem) (*types.FirewallAddressListItem, error)
Add adds a new entry to a firewall address list.
RouterOS API docs: https://manual.mikrotik.com/docs/Developer+Guides/rest-api
Example ¶
ExampleIPFirewallAddressListService_Add adds a new entry to a firewall address list and prints the stored item with its assigned ID.
package main
import (
"context"
"fmt"
"time"
"github.com/art-frela/routeros"
"github.com/art-frela/routeros/types"
)
func main() {
cfg := routeros.Config{
BaseURL: "http://192.168.88.1",
RequestTimeout: 10 * time.Second,
User: "admin",
Password: "master",
}
client, err := routeros.NewClient(cfg)
if err != nil {
fmt.Println("client error:", err)
return
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
item := types.FirewallAddressListNewItem{
Address: "192.168.1.100",
List: "blacklist",
Comment: "blocked by example",
}
added, err := client.IPFirewallAddressListService.Add(ctx, item)
if err != nil {
fmt.Println("request error:", err)
return
}
fmt.Printf("added %s to list %s with id %s\n", added.Address, added.List, added.ID)
}
Output:
func (*IPFirewallAddressListService) Find ¶
func (ipfwls *IPFirewallAddressListService) Find(ctx context.Context, list string, ips ...string) (types.FirewallAddressList, error)
Find retrieves firewall address list entries optionally filtered by list name and/or address.
RouterOS API docs: https://manual.mikrotik.com/docs/Developer+Guides/rest-api
Example ¶
ExampleIPFirewallAddressListService_Find retrieves firewall address list entries filtered by list name and address.
package main
import (
"context"
"fmt"
"time"
"github.com/art-frela/routeros"
)
func main() {
cfg := routeros.Config{
BaseURL: "http://192.168.88.1",
RequestTimeout: 10 * time.Second,
User: "admin",
Password: "master",
}
client, err := routeros.NewClient(cfg)
if err != nil {
fmt.Println("client error:", err)
return
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
entries, err := client.IPFirewallAddressListService.Find(ctx, "blacklist", "192.168.1.100")
if err != nil {
fmt.Println("request error:", err)
return
}
for _, entry := range entries {
fmt.Printf("%s list=%s disabled=%s\n", entry.Address, entry.List, entry.Disabled)
}
}
Output:
type IPRouteService ¶ added in v0.0.4
type IPRouteService struct {
// contains filtered or unexported fields
}
IPRouteService handles communication with the IP route methods of the RouterOS REST API.
RouterOS REST API: https://manual.mikrotik.com/docs/Developer+Guides/rest-api
func (*IPRouteService) AddRoute ¶ added in v0.0.4
func (s *IPRouteService) AddRoute(ctx context.Context, item types.IPRouteAdd) (types.IPRoute, error)
AddRoute adds a new static route.
RouterOS REST API: https://manual.mikrotik.com/docs/Developer+Guides/rest-api
func (*IPRouteService) GetRouteByID ¶ added in v0.0.4
GetRouteByID returns a specific IP route by its ID.
RouterOS API docs: https://manual.mikrotik.com/docs/Developer+Guides/rest-api
func (*IPRouteService) GetRoutes ¶ added in v0.0.4
func (s *IPRouteService) GetRoutes(ctx context.Context) (types.IPRouteList, error)
GetRoutes returns a list of all IP routes configured on the device.
RouterOS REST API: https://manual.mikrotik.com/docs/Developer+Guides/rest-api
func (*IPRouteService) RemoveRoute ¶ added in v0.0.4
func (s *IPRouteService) RemoveRoute(ctx context.Context, id string) error
RemoveRoute removes an IP route by its ID.
RouterOS REST API: https://manual.mikrotik.com/docs/Developer+Guides/rest-api
func (*IPRouteService) UpdateRoute ¶ added in v0.0.4
func (s *IPRouteService) UpdateRoute(ctx context.Context, id string, item types.IPRouteAdd) (types.IPRoute, error)
UpdateRoute updates an existing IP route identified by its ID.
RouterOS REST API: https://manual.mikrotik.com/docs/Developer+Guides/rest-api
type IPService ¶ added in v0.0.4
type IPService struct {
// contains filtered or unexported fields
}
IPService handles communication with the IP address methods of the RouterOS REST API.
RouterOS API docs: https://manual.mikrotik.com/docs/Developer+Guides/rest-api
func (*IPService) AddAddress ¶ added in v0.0.4
func (ips *IPService) AddAddress(ctx context.Context, item types.IPAddressAdd) (types.IPAddress, error)
AddAddress adds a new IP address.
RouterOS API docs: https://manual.mikrotik.com/docs/Developer+Guides/rest-api
func (*IPService) GetAddressByID ¶ added in v0.0.4
GetAddressByID returns a specific IP address by its ID.
RouterOS API docs: https://manual.mikrotik.com/docs/Developer+Guides/rest-api
func (*IPService) GetAddresses ¶ added in v0.0.4
GetAddresses returns a list of all IP addresses configured on the device.
RouterOS API docs: https://manual.mikrotik.com/docs/Developer+Guides/rest-api
Example ¶
ExampleIPService_GetAddresses lists all IP addresses configured on the device and prints each address with its interface.
package main
import (
"context"
"fmt"
"time"
"github.com/art-frela/routeros"
)
func main() {
cfg := routeros.Config{
BaseURL: "http://192.168.88.1",
RequestTimeout: 10 * time.Second,
User: "admin",
Password: "master",
}
client, err := routeros.NewClient(cfg)
if err != nil {
fmt.Println("client error:", err)
return
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
addresses, err := client.IPService.GetAddresses(ctx)
if err != nil {
fmt.Println("request error:", err)
return
}
for _, addr := range addresses {
fmt.Printf("%s on %s\n", addr.Address, addr.Interface)
}
}
Output:
func (*IPService) RemoveAddress ¶ added in v0.0.4
RemoveAddress removes an IP address by its ID.
RouterOS API docs: https://manual.mikrotik.com/docs/Developer+Guides/rest-api
func (*IPService) UpdateAddress ¶ added in v0.0.4
func (ips *IPService) UpdateAddress(ctx context.Context, id string, item types.IPAddressAdd) (types.IPAddress, error)
UpdateAddress updates an existing IP address identified by its ID.
RouterOS API docs: https://manual.mikrotik.com/docs/Developer+Guides/rest-api
type ResponseError ¶ added in v0.0.4
ResponseError represents a non-successful HTTP response (status >= 400) from the RouterOS REST API. Body holds the raw response payload.
func (*ResponseError) Error ¶ added in v0.0.4
func (e *ResponseError) Error() string
Error implements the error interface, preserving the legacy "status_code: %d, response: %s" format byte-identical.
type ToolService ¶ added in v0.0.2
type ToolService struct {
// contains filtered or unexported fields
}
ToolService handles communication with the tool methods of the RouterOS REST API.
RouterOS REST API: https://manual.mikrotik.com/docs/Developer+Guides/rest-api
func (*ToolService) Ping ¶ added in v0.0.2
func (ts *ToolService) Ping(ctx context.Context, req types.EchoRequest) (types.EchoResponse, error)
Ping performs a ping test to a specified host address.
RouterOS REST API: https://manual.mikrotik.com/docs/Developer+Guides/rest-api
Example ¶
ExampleToolService_Ping pings a host from the device and prints one line per reply; timed-out replies carry a status instead of RTT fields.
package main
import (
"context"
"fmt"
"time"
"github.com/art-frela/routeros"
"github.com/art-frela/routeros/types"
)
func main() {
cfg := routeros.Config{
BaseURL: "http://192.168.88.1",
RequestTimeout: 10 * time.Second,
User: "admin",
Password: "master",
}
client, err := routeros.NewClient(cfg)
if err != nil {
fmt.Println("client error:", err)
return
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
req := types.EchoRequest{
Address: "8.8.8.8",
Count: 3,
Interval: 1,
}
replies, err := client.ToolService.Ping(ctx, req)
if err != nil {
fmt.Println("request error:", err)
return
}
for _, echo := range replies {
if echo.Time != nil {
fmt.Printf("reply from %s: seq=%s time=%s\n", echo.Host, echo.Seq, *echo.Time)
continue
}
fmt.Printf("reply from %s: seq=%s status=%s\n", echo.Host, echo.Seq, *echo.Status)
}
}
Output: