Ajout du service NAT et nouvelle architecture de l'API

This commit is contained in:
alphayax 2016-05-14 13:40:37 +02:00
parent c204ce623f
commit 6668eec620
20 changed files with 856 additions and 44 deletions

7
.gitignore vendored
View File

@ -4,7 +4,10 @@
# Composer
/composer.phar
/composer.lock
/vendor/*
# Project files
/exemple/dl_rss/app_token
/app_token
/exemple/config/app_token
/exemple/download/app_token
/app_token

View File

@ -17,6 +17,10 @@ Jusqu'a présent, les fonctionalités suivantes ont été implémentées :
- DHCP
- FTP
- System
- NAT
- Dmz
- Port Forwarding
- Incoming Port
## Utilisation
@ -53,7 +57,13 @@ $SystemConfig = $System->getConfiguration();
## Exemples
Les exemples sont disponibles dans le repertoire `exemple`:
- `dhcp_config` : Un script pour récuperer la configuration courrante du DHCP
- `dl_rss` : Un script qui parse les flux RSS et qui rajoute en téléchagement les items correspondant a une expression réguliere
Les exemples sont disponibles dans le repertoire `exemple`. Ils sont classés par services :
- `config`
- `check_dns` : Un script pour récuperer la configuration courrante du DHCP
- `DMZ` : Récupération de la confiugration de votre zone démilitarisée
- `IncomingPort` : Retourne la configuration actuelle du port d'entrée HTTP
- `PortForwarding` : Exemple d'ajout d'une redirection de port
- `download`
- `dl_rss` : Un script qui parse les flux RSS et qui rajoute en téléchagement les items correspondant a une expression réguliere

View File

@ -8,6 +8,7 @@ $App = new \alphayax\freebox\utils\Application( 'com.alphayax.freebox.example',
$App->authorize();
$App->openSession();
$toto = new \alphayax\freebox\api\v3\services\config\DHCP( $App);
var_dump( $toto->get_current_configuration());
// DMZ
$toto = new \alphayax\freebox\api\v3\services\config\NAT\DMZ( $App);
$a = $toto->getConfiguration();
var_dump( $a);

View File

@ -0,0 +1,14 @@
<?php
/// Require Composer AutoLoader
require_once '../../vendor/autoload.php';
/// Define our application
$App = new \alphayax\freebox\utils\Application( 'com.alphayax.freebox.example', 'Freebox PHP API Example', '0.0.2');
$App->authorize();
$App->openSession();
// PortForwarding
$toto = new \alphayax\freebox\api\v3\services\config\NAT\IncomingPort( $App);
$a = $toto->getById('http');
var_dump( $a);

View File

@ -0,0 +1,24 @@
.<?php
/// Require Composer AutoLoader
require_once '../../vendor/autoload.php';
/// Define our application
$App = new \alphayax\freebox\utils\Application( 'com.alphayax.freebox.example', 'Freebox PHP API Example', '0.0.2');
$App->authorize();
$App->openSession();
$toto = new \alphayax\freebox\api\v3\services\config\NAT\PortForwarding( $App);
$a = new \alphayax\freebox\api\v3\models\PortForwardingConfig();
$a->setEnabled( false);
$a->setComment('test');
$a->setLanPort( 4242);
$a->setWanPortStart( 4242);
$a->setWanPortEnd( 4242);
$a->setLanIp('192.168.0.42');
$a->setIpProto( 'tcp');
$a->setSrcIp('0.0.0.0');
$b = $toto->add($a);
var_dump( $b);

View File

@ -0,0 +1,16 @@
<?php
/// Require Composer AutoLoader
require_once '../../vendor/autoload.php';
/// Define our application
$App = new \alphayax\freebox\utils\Application( 'com.alphayax.freebox.example', 'Freebox PHP API Example', '0.0.2');
$App->authorize();
$App->openSession();
/// DNS
$toto = new \alphayax\freebox\api\v3\services\config\DHCP( $App);
$DHCPConfig = $toto->getConfiguration();
echo 'gateway : ' . $DHCPConfig->getGateway() . PHP_EOL;
echo 'netmask : ' . $DHCPConfig->getNetmask() . PHP_EOL;
echo 'DHCP IP range : ' . $DHCPConfig->getIpRangeStart() . ' => '. $DHCPConfig->getIpRangeEnd() . PHP_EOL;

View File

@ -1,5 +1,5 @@
{
"rss": "https:\/\/www.nyaa.se\/?page=rss&user=175467",
"pattern": "\/One_Piece.*HD.*mp4\/",
"last_date": 1457259493
"last_date": 1462698936
}

View File

@ -20,30 +20,11 @@ abstract class Model {
}
/**
* Magic getter
* @param $name
* @return null
* Return an array representation of the model properties
* @return array
*/
function __get( $name){
if( property_exists( static::class, $name)){
return $this->$name;
}
return null; // TODO : maybe throw exception ?
}
/**
* Magic setter
* @param $name
* @param $value
*/
public function __set( $name, $value){
if( property_exists( static::class, $name)){
$this->$name = $value;
}
}
public function toArray(){
return get_object_vars( $this);
}
}

View File

@ -32,6 +32,90 @@ class DhcpConfig extends Model {
/** @var array of string List of dns servers to include in DHCP reply */
protected $dns = [];
/**
* @return boolean
*/
public function isEnabled(){
return $this->enabled;
}
/**
* @param boolean $enabled
*/
public function setEnabled( $enabled){
$this->enabled = $enabled;
}
/**
* @return boolean
*/
public function isStickyAssign(){
return $this->sticky_assign;
}
/**
* @param boolean $sticky_assign
*/
public function setStickyAssign( $sticky_assign){
$this->sticky_assign = $sticky_assign;
}
/**
* @return string
*/
public function getGateway(){
return $this->gateway;
}
/**
* @return string
*/
public function getNetmask(){
return $this->netmask;
}
/**
* @return string
*/
public function getIpRangeStart(){
return $this->ip_range_start;
}
/**
* @param string $ip_range_start
*/
public function setIpRangeStart( $ip_range_start){
$this->ip_range_start = $ip_range_start;
}
/**
* @return string
*/
public function getIpRangeEnd(){
return $this->ip_range_end;
}
/**
* @param string $ip_range_end
*/
public function setIpRangeEnd( $ip_range_end){
$this->ip_range_end = $ip_range_end;
}
/**
* @return boolean
*/
public function isAlwaysBroadcast(){
return $this->always_broadcast;
}
/**
* @param boolean $always_broadcast
*/
public function setAlwaysBroadcast( $always_broadcast){
$this->always_broadcast = $always_broadcast;
}
/**
* @return array
*/
@ -39,4 +123,11 @@ class DhcpConfig extends Model {
return $this->dns;
}
/**
* @param array $dns
*/
public function setDNS( $dns){
$this->dns = $dns;
}
}

View File

@ -0,0 +1,45 @@
<?php
namespace alphayax\freebox\api\v3\models;
use alphayax\freebox\api\v3\Model;
/**
* Class DmzConfig
* @package alphayax\freebox\api\v3\models
*/
class DmzConfig extends Model {
/** @var string Dmz host IP */
protected $ip;
/** @var bool Enable/Disable the DMZ */
protected $enabled;
/**
* @return boolean
*/
public function isEnabled(){
return $this->enabled;
}
/**
* @param boolean $enabled
*/
public function setEnabled( $enabled){
$this->enabled = $enabled;
}
/**
* @return string
*/
public function getIp(){
return $this->ip;
}
/**
* @param string $ip
*/
public function setIp( $ip){
$this->ip = $ip;
}
}

View File

@ -0,0 +1,72 @@
<?php
namespace alphayax\freebox\api\v3\models;
use alphayax\freebox\api\v3\Model;
/**
* Class FtpConfig
* @package alphayax\freebox\api\v3\models
*/
class FtpConfig extends Model {
/** @var bool : is the FTP server enabled */
protected $enabled;
/** @var bool : can anonymous user log in */
protected $allow_anonymous;
/** @var bool : can anonymous user write data */
protected $allow_anonymous_write;
/** @var string (Write-only) : user password */
protected $password;
/**
* @return boolean
*/
public function isEnabled(){
return $this->enabled;
}
/**
* @param boolean $enabled
*/
public function setEnabled( $enabled){
$this->enabled = $enabled;
}
/**
* @return boolean
*/
public function isAllowAnonymous(){
return $this->allow_anonymous;
}
/**
* @param boolean $allow_anonymous
*/
public function setAllowAnonymous( $allow_anonymous){
$this->allow_anonymous = $allow_anonymous;
}
/**
* @return boolean
*/
public function isAllowAnonymousWrite(){
return $this->allow_anonymous_write;
}
/**
* @param boolean $allow_anonymous_write
*/
public function setAllowAnonymousWrite( $allow_anonymous_write){
$this->allow_anonymous_write = $allow_anonymous_write;
}
/**
* @param string $password
*/
public function setPassword( $password){
$this->password = $password;
}
}

View File

@ -0,0 +1,93 @@
<?php
namespace alphayax\freebox\api\v3\models;
use alphayax\freebox\api\v3\Model;
/**
* Class IncomingPortConfig
* @package alphayax\freebox\api\v3\models
*/
class IncomingPortConfig extends Model {
/**
* @var string (Read-only) : incoming port id
* http : http port for remote access to Freebox OS
* bittorrent-main : main bittorrent port for Freebox downloader
* bittorrent-dht : bittorrent port for DHT
*/
protected $id;
/** @var bool : is the port binding allowed */
protected $enabled;
/** @var bool (Read-only) : is the port binding currently active */
protected $active;
/** @var string (Read-only) : Enum("tcp", "udp", "tcp_udp") */
protected $type;
/** @var int binding port */
protected $in_port;
/**
* @var string (Read-only) : network namespace.
* The service may be running on a different namespace (for instance if the service uses the vpn client).
*/
protected $netns;
/**
* @return string
*/
public function getId(){
return $this->id;
}
/**
* @return boolean
*/
public function isEnabled(){
return $this->enabled;
}
/**
* @param boolean $enabled
*/
public function setEnabled( $enabled){
$this->enabled = $enabled;
}
/**
* @return boolean
*/
public function isActive(){
return $this->active;
}
/**
* @return string
*/
public function getType(){
return $this->type;
}
/**
* @return int
*/
public function getInPort(){
return $this->in_port;
}
/**
* @param int $in_port
*/
public function setInPort( $in_port){
$this->in_port = $in_port;
}
/**
* @return string
*/
public function getNetns(){
return $this->netns;
}
}

View File

@ -0,0 +1,188 @@
<?php
namespace alphayax\freebox\api\v3\models;
use alphayax\freebox\api\v3\Model;
/**
* Class PortForwardingConfig
* @package alphayax\freebox\api\v3\models
*/
class PortForwardingConfig extends Model {
/** @var int : forwarding id */
protected $id;
/** @var bool : is forwarding enabled */
protected $enabled;
/** @var string (enum: tcp, udp) */
protected $ip_proto;
/** @var string : forwarding range start */
protected $wan_port_start;
/** @var int : forwarding range end */
protected $wan_port_end;
/** @var string : forwarding target on LAN */
protected $lan_ip;
/** @var int : forwarding target start port on LAN, (last port is lan_port + wan_port_end - wan_port_start) */
protected $lan_port;
/** @var string (Read-only) : Forwarding target host name */
protected $hostname;
/** @var LanHost (Read-only) : forwarding target host information (@see LanHost) */
protected $host;
/**
* @var string
* if src_ip == 0.0.0.0 this rule will apply to any src ip
* otherwise it will only apply to the specified ip address
*/
protected $src_ip = '0.0.0.0';
/** @var string comment */
protected $comment;
/**
* @return int
*/
public function getId(){
return $this->id;
}
/**
* @param int $id
*/
public function setId( $id){
$this->id = $id;
}
/**
* @return boolean
*/
public function isEnabled(){
return $this->enabled;
}
/**
* @param boolean $enabled
*/
public function setEnabled( $enabled){
$this->enabled = $enabled;
}
/**
* @return string
*/
public function getIpProto(){
return $this->ip_proto;
}
/**
* @param string $ip_proto
*/
public function setIpProto( $ip_proto = 'tcp'){
$this->ip_proto = $ip_proto;
}
/**
* @return string
*/
public function getWanPortStart(){
return $this->wan_port_start;
}
/**
* @param string $wan_port_start
*/
public function setWanPortStart( $wan_port_start){
$this->wan_port_start = $wan_port_start;
}
/**
* @return int
*/
public function getWanPortEnd(){
return $this->wan_port_end;
}
/**
* @param int $wan_port_end
*/
public function setWanPortEnd( $wan_port_end){
$this->wan_port_end = $wan_port_end;
}
/**
* @return string
*/
public function getLanIp(){
return $this->lan_ip;
}
/**
* @param string $lan_ip
*/
public function setLanIp( $lan_ip){
$this->lan_ip = $lan_ip;
}
/**
* @return int
*/
public function getLanPort(){
return $this->lan_port;
}
/**
* @param int $lan_port
*/
public function setLanPort( $lan_port){
$this->lan_port = $lan_port;
}
/**
* @return string
*/
public function getHostname(){
return $this->hostname;
}
/**
* @return LanHost
*/
public function getHost(){
return $this->host;
}
/**
* @return string
*/
public function getSrcIp(){
return $this->src_ip;
}
/**
* @param string $src_ip
*/
public function setSrcIp( $src_ip){
$this->src_ip = $src_ip;
}
/**
* @return string
*/
public function getComment(){
return $this->comment;
}
/**
* @param string $comment
*/
public function setComment( $comment){
$this->comment = $comment;
}
}

View File

@ -41,4 +41,81 @@ class SystemConfig extends Model {
/** @var bool (Read-only) is the box authenticated (“étape 6”) */
protected $box_authenticated;
/**
* @return string
*/
public function getFirmwareVersion(){
return $this->firmware_version;
}
/**
* @return string
*/
public function getMac(){
return $this->mac;
}
/**
* @return string
*/
public function getSerial(){
return $this->serial;
}
/**
* @return string
*/
public function getUptime(){
return $this->uptime;
}
/**
* @return int
*/
public function getUptimeVal(){
return $this->uptime_val;
}
/**
* @return string
*/
public function getBoardName(){
return $this->board_name;
}
/**
* @return int
*/
public function getTempCpum(){
return $this->temp_cpum;
}
/**
* @return int
*/
public function getTempSw(){
return $this->temp_sw;
}
/**
* @return int
*/
public function getTempCpub(){
return $this->temp_cpub;
}
/**
* @return int
*/
public function getFanRpm(){
return $this->fan_rpm;
}
/**
* @return boolean
*/
public function isBoxAuthenticated(){
return $this->box_authenticated;
}
}

View File

@ -15,8 +15,9 @@ class DHCP extends Service {
/**
* @throws \Exception
* @return DhcpConfig
*/
public function get_current_configuration(){
public function getConfiguration(){
$rest = $this->getAuthService( self::API_DHCP_CONFIG);
$rest->GET();
@ -24,15 +25,15 @@ class DHCP extends Service {
}
/**
* @param array $new_config_x
* @return array
* @param DhcpConfig $new_DhcpConfig
* @return DhcpConfig
* @throws \Exception
*/
public function set_attribute_configuration( $new_config_x = []){
public function setConfiguration( DhcpConfig $new_DhcpConfig){
$rest = $this->getAuthService( self::API_DHCP_CONFIG);
$rest->PUT( $new_config_x);
$rest->PUT( $new_DhcpConfig->toArray());
return $rest->getCurlResponse();
return new DhcpConfig( $rest->getCurlResponse()['result']);
}
}

View File

@ -1,5 +1,6 @@
<?php
namespace alphayax\freebox\api\v3\services\config;
use alphayax\freebox\api\v3\models\FtpConfig;
use alphayax\freebox\api\v3\Service;
@ -14,24 +15,25 @@ class FTP extends Service {
/**
* @throws \Exception
* @return FtpConfig
*/
public function get_current_configuration(){
public function getConfiguration(){
$rest = $this->getAuthService( self::API_FTP_CONFIG);
$rest->GET();
return $rest->getCurlResponse();
return new FtpConfig( $rest->getCurlResponse());
}
/**
* @param array $new_config_x
* @return array
* @param FtpConfig $new_configConfig
* @return FtpConfig
* @throws \Exception
*/
public function set_attribute_configuration( $new_config_x = []){
public function setConfiguration( FtpConfig $new_configConfig){
$rest = $this->getAuthService( self::API_FTP_CONFIG);
$rest->PUT( $new_config_x);
$rest->PUT( $new_configConfig);
return $rest->getCurlResponse();
return new FtpConfig( $rest->getCurlResponse());
}
}

View File

@ -0,0 +1,40 @@
<?php
namespace alphayax\freebox\api\v3\services\config\NAT;
use alphayax\freebox\api\v3\models\DmzConfig;
use alphayax\freebox\api\v3\Service;
/**
* Class DMZ
* @package alphayax\freebox\api\v3\services\config\NAT
* @author <alphayax@gmail.com>
*/
class DMZ extends Service {
const API_NAT_DMZ = '/api/v3/fw/dmz/';
/**
* Get the current Dmz configuration
* @throws \Exception
*/
public function getConfiguration(){
$rest = $this->getAuthService( self::API_NAT_DMZ);
$rest->GET();
return new DmzConfig( $rest->getCurlResponse()['result']);
}
/**
* Update the current Dmz configuration
* @param DmzConfig $new_DmzConfig
* @return DmzConfig
* @throws \Exception
*/
public function setConfiguration( DmzConfig $new_DmzConfig){
$rest = $this->getAuthService( self::API_NAT_DMZ);
$rest->PUT( $new_DmzConfig->toArray());
return new DmzConfig( $rest->getCurlResponse()['result']);
}
}

View File

@ -0,0 +1,59 @@
<?php
namespace alphayax\freebox\api\v3\services\config\NAT;
use alphayax\freebox\api\v3\models\DmzConfig;
use alphayax\freebox\api\v3\models\IncomingPortConfig;
use alphayax\freebox\api\v3\models\PortForwardingConfig;
use alphayax\freebox\api\v3\Service;
/**
* Class IncomingPort
* @package alphayax\freebox\api\v3\services\config\NAT
*/
class IncomingPort extends Service {
const API_NAT_INCOMING = '/api/v3/fw/incoming/';
/**
* Getting the list of incoming ports
* @throws \Exception
* @return IncomingPortConfig[]
*/
public function getAll(){
$rest = $this->getAuthService( self::API_NAT_INCOMING);
$rest->GET();
$result = $rest->getCurlResponse()['result'];
$PortForwardingList = [];
foreach( $result as $PortForwardingElement){
$PortForwardingList[] = new IncomingPortConfig( $PortForwardingElement);
}
return $PortForwardingList;
}
/**
* Getting a specific incoming port
* @param string $IncomingPortId
* @return IncomingPortConfig
*/
public function getById( $IncomingPortId){
$rest = $this->getAuthService( self::API_NAT_INCOMING . $IncomingPortId);
$rest->GET();
return new IncomingPortConfig( $rest->getCurlResponse()['result']);
}
/**
* Updating an incoming port
* @param IncomingPortConfig $new_IncomingPortConfig
* @return IncomingPortConfig
* @throws \Exception
*/
public function update( IncomingPortConfig $new_IncomingPortConfig){
$rest = $this->getAuthService( self::API_NAT_INCOMING . $new_IncomingPortConfig->getId());
$rest->PUT( $new_IncomingPortConfig->toArray());
return new IncomingPortConfig( $rest->getCurlResponse()['result']);
}
}

View File

@ -0,0 +1,95 @@
<?php
namespace alphayax\freebox\api\v3\services\config\NAT;
use alphayax\freebox\api\v3\models\DmzConfig;
use alphayax\freebox\api\v3\models\PortForwardingConfig;
use alphayax\freebox\api\v3\Service;
/**
* Class PortForwarding
* @package alphayax\freebox\api\v3\services\config\NAT
* @author <alphayax@gmail.com>
*/
class PortForwarding extends Service {
const API_NAT_REDIR = '/api/v3/fw/redir/';
/**
* Getting the list of port forwarding
* @throws \Exception
* @return PortForwardingConfig[]
*/
public function getAll(){
$rest = $this->getAuthService( self::API_NAT_REDIR);
$rest->GET();
$result = $rest->getCurlResponse()['result'];
$PortForwardingList = [];
foreach( $result as $PortForwardingElement){
$PortForwardingList[] = new PortForwardingConfig( $PortForwardingElement);
}
return $PortForwardingList;
}
/**
* Getting a specific port forwarding
* @param $PortForwarding_id
* @return PortForwardingConfig
*/
public function getById( $PortForwarding_id){
$rest = $this->getAuthService( self::API_NAT_REDIR . $PortForwarding_id);
$rest->GET();
return new PortForwardingConfig( $rest->getCurlResponse()['result']);
}
/**
* Update a specific port forwarding
* @param PortForwardingConfig $new_PortForwardingConfig
* @return PortForwardingConfig
* @throws \Exception
*/
public function update( PortForwardingConfig $new_PortForwardingConfig){
$rest = $this->getAuthService( self::API_NAT_REDIR . $new_PortForwardingConfig->getId());
$rest->PUT( $new_PortForwardingConfig->toArray());
return new PortForwardingConfig( $rest->getCurlResponse()['result']);
}
/**
* Add a port forwarding
* @param PortForwardingConfig $new_PortForwardingConfig
* @return PortForwardingConfig
* @throws \Exception
*/
public function add( PortForwardingConfig $new_PortForwardingConfig){
$rest = $this->getAuthService( self::API_NAT_REDIR);
$rest->POST( $new_PortForwardingConfig->toArray());
return new PortForwardingConfig( $rest->getCurlResponse()['result']);
}
/**
* Delete a port forwarding
* @param PortForwardingConfig $PortForwardingConfig
* @return PortForwardingConfig
* @throws \Exception
*/
public function delete( PortForwardingConfig $PortForwardingConfig){
return $this->deleteById( $PortForwardingConfig->getId());
}
/**
* Delete a port forwarding
* @param int $PortForwardingId
* @return boolean success
* @throws \Exception
*/
public function deleteById( $PortForwardingId){
$rest = $this->getAuthService( self::API_NAT_REDIR . $PortForwardingId);
$rest->DELETE();
return $rest->getCurlResponse()['success'];
}
}