OPNsense Forum

English Forums => Development and Code Review => Topic started by: vupibi on May 12, 2016, 06:46:48 am

Title: Find api URLs
Post by: vupibi on May 12, 2016, 06:46:48 am
Hello,

For a project I need to control OPNsense with command line and for that the API with curl is the thing I am looking for! (https://wiki.opnsense.org/index.php/Howto_use_the_API)

But, I am not able to find the API URLs...
What I found out is that the APIs are there:
/usr/local/opnsense/mvc/app/controllers/OPNsense/

In core there is a file called "FirmwareController.php", and a couple of functions called anythingAction (for example: statusAction, infoAction, etc.). So i found out, that I can access there with:
core -> FirmwareController.php" -> statusAction -> https://ipOfOPNsense/api/core/firmware/status or
core -> FirmwareController.php" -> infoAction -> https://ipOfOPNsense/api/core/firmware/info

Another file is called "MenuController.php" and again there are functions with "Action" for example: treeAction and I can access it with:
core -> MenuController.php" -> treeAction -> https://ipOfOPNsense/api/core/menu/tree

But this can not be the way to the goal, right? So how can I figure out these API URLs?

For example I would like to get the data of "status_interfaces.php", means a list of all interfaces with whose settings (analog to the PHP function: get_configured_interface_with_descr), how would the API URL be?

Thanks!
Title: Re: Find api URLs
Post by: AdSchellevis on May 12, 2016, 08:38:09 am
Hi vupibi,

Only new modules like IDS/Proxy/Firmware/.. are "api" aware, the legacy pages aren't.
In case you want to develop new modules yourself, you might want to look at https://docs.opnsense.org/development/examples/helloworld.html (https://docs.opnsense.org/development/examples/helloworld.html)

Adding simple start/stop/status actions for your own application isn't very hard to do, using the configd system, some easy examples can be found in the Diagnostics/Interfaces api module (src/opnsense/mvc/app/controllers/OPNsense/Diagnostics/Api/InterfaceController.php -> look at getArpAction).

Best regards,

Ad
Title: Re: Find api URLs
Post by: vupibi on May 12, 2016, 09:23:16 pm
Hi Ad,

thank you very much for your answer! I already thought about writing a custom module, but for my scenario I need to control the whole PFSense system without using the frontend - so I found another solution.

A small nodejs server will read and edit the config.xml, because in this single file I can read and write all the Informations I am looking for (interfaces, DynDNS, and so on).

Best regards!
Title: Re: Find api URLs
Post by: AdSchellevis on May 12, 2016, 10:26:32 pm
Hi vupibi,

There is a script available to restart most of the legacy services (/usr/local/etc/rc.reload_all), but that won't restart all, you probably better create your own script if you want to use the software this way.
Good inspirations can be found in the bootup script (/usr/local/etc/rc.bootup), depending on your needs you can find most of the configure commands in there.

For new code, we try to follow the rc system more closely (regular service commands) and detach user input / validation from config generation using templates.

Using the data without the interface is a bit tricky, you always have to follow the developments closely to keep your code compatible with original product and be very careful when upgrading.

Best regards,

Ad

(p.s. pfSense is that other project.... your using OPNsense  :) )

Title: Re: Find api URLs
Post by: fabian on May 12, 2016, 10:48:34 pm
Hi vupibi,

you can write the things you need for example in python, perl (installed by default) or ruby (you can install it via pkg).

Regards

Fabian