Add VPN server API

This commit is contained in:
alphayax 2016-05-31 22:09:45 +02:00
parent 07e096af6c
commit a904a63039
21 changed files with 845 additions and 7 deletions

View File

@ -2,7 +2,8 @@
# Freebox v6 PHP API v3
![license](https://img.shields.io/packagist/l/alphayax/freebox_api_php.svg)
![tag](https://img.shields.io/github/tag/alphayax/freebox_api_php.svg)
![unstable](https://poser.pugx.org/alphayax/freebox_api_php/v/unstable)
![stable](https://poser.pugx.org/alphayax/freebox_api_php/v/stable)
![pakagist](https://img.shields.io/packagist/v/alphayax/freebox_api_php.svg)
![codacy](https://api.codacy.com/project/badge/Grade/f3569cf671f04b8ab6d699be3fd011e5)
@ -71,6 +72,8 @@ Jusqu'a présent, les fonctionalités suivantes ont été implémentées :
- UPnP
- AV
- IGD
- VPN
- Server
## Utilisation
@ -146,10 +149,11 @@ Les exemples sont disponibles dans le repertoire `exemple`. Ils sont classés pa
- `PortForwarding` : Exemple d'ajout d'une redirection de port
- `System` : Affichage de la configuration système de la freebox
- `UPnP` : Affichage des configuration UPnP
- `VPN` : Affiche la configuration des serveurs VPN, liste les utilisateurs...
- `download`
- `Download` : Listage des téléchargement en cours, liste des fichiers d'un téléchargement et mise a jour de la priorité de téléchargement
- `DlConfig` : Affichage des configurations de téléchargement (bt, nntp...)
- `dl_rss` : Un script qui parse les flux RSS et qui rajoute en téléchagement les items correspondant a une expression réguliere
- `Bittorrent` : Affiche des infos sur des telechargements bittorent

View File

@ -35,7 +35,7 @@
- ~~Switch~~
- Wi-Fi
- ~~System~~
- VPN Server [UNSTABLE]
- ~~VPN Server [UNSTABLE]~~
- VPN Client [UNSTABLE]
- Storage
- Storage API [UNSTABLE]

View File

@ -1,6 +1,7 @@
{
"name": "alphayax/freebox_api_php",
"description": "API PHP pour la Freebox",
"license": "MIT",
"minimum-stability": "dev",
"prefer-stable": true,
"authors": [
@ -10,12 +11,16 @@
}
],
"require" : {
"alphayax/php_utils" : "^0.1.1",
"php": ">=5.5.0",
"alphayax/php_utils" : "^0.1.1",
"monolog/monolog": "^1.9.1"
},
"autoload": {
"psr-4": {"alphayax\\": "./"}
"psr-4": {
"alphayax\\": "./"
}
},
"license": "MIT"
"support": {
"issues": "https://github.com/alphayax/freebox_api_php/issues"
}
}

29
exemple/config/VPN.php Normal file
View File

@ -0,0 +1,29 @@
<?php
/// Require Composer AutoLoader
require_once '../../vendor/autoload.php';
/// Define our application
$App = new \alphayax\freebox\utils\Application( 'com.alphayax.freebox.config', 'PHP API Example (Config)', '1.0.0');
$App->authorize();
$App->openSession();
/*
/// VPN Config
$VPNServerService = new \alphayax\freebox\api\v3\services\config\VPN\Server\Config( $App);
$a = $VPNServerService->getConfigurationFromId( 'openvpn_routed');
print_r( $a);
/// VPN Users
$VPNUserService = new \alphayax\freebox\api\v3\services\config\VPN\Server\User( $App);
//$Users = $VPNUserService->getAll();
//print_r( $Users);
/// IP Pool
$IPPoolService = new \alphayax\freebox\api\v3\services\config\VPN\Server\IpPool( $App);
$resa = $IPPoolService->getReservations();
print_r( $resa);
$CnxService = new \alphayax\freebox\api\v3\services\config\VPN\Server\Connection( $App);
$connections = $CnxService->getAll();
print_r( $connections);
*/

View File

@ -25,7 +25,7 @@ abstract class Model {
* @param string $propertyClass
*/
protected function initProperty( $propertyName, $propertyClass){
if( property_exists( static::class, $propertyName)){
if( property_exists( static::class, $propertyName) && ! empty( $this->$propertyName)){
$this->$propertyName = new $propertyClass( $this->$propertyName);
}
}

View File

@ -0,0 +1,51 @@
<?php
namespace alphayax\freebox\api\v3\models\VPN\Server\Config;
use alphayax\freebox\api\v3\Model;
use alphayax\freebox\api\v3\symbols;
/**
* Class OpenVPNConfig
* @package alphayax\freebox\api\v3\models\VPN\Server\Config
*/
class OpenVPNConfig extends Model{
/**
* @var string
* @see symbols\VPN\OpenVPNConfig\Cipher
*/
protected $cipher;
/** @var bool : disable fragment configuration option */
protected $disable_fragment;
/**
* @return string
* @see symbols\VPN\OpenVPNConfig\Cipher
*/
public function getCipher() {
return $this->cipher;
}
/**
* @param string $cipher
* @see symbols\VPN\OpenVPNConfig\Cipher
*/
public function setCipher( $cipher) {
$this->cipher = $cipher;
}
/**
* @return boolean
*/
public function isDisableFragment() {
return $this->disable_fragment;
}
/**
* @param boolean $disable_fragment
*/
public function setDisableFragment( $disable_fragment) {
$this->disable_fragment = $disable_fragment;
}
}

View File

@ -0,0 +1,53 @@
<?php
namespace alphayax\freebox\api\v3\models\VPN\Server\Config;
use alphayax\freebox\api\v3\Model;
use alphayax\freebox\api\v3\symbols;
/**
* Class PPTPConfig
* @package alphayax\freebox\api\v3\models\VPN\Server\Config
*/
class PPTPConfig extends Model {
/**
* @var string
* @see symbols\VPN\PPTPConfig\Mppe
*/
protected $mppe;
/** @var bool[] : allowed authentication methods */
protected $allowed_auth = [
'pap' => null,
'chap' => null,
'mschapv2' => null,
];
/**
* @return string
*/
public function getMppe() {
return $this->mppe;
}
/**
* @param string $mppe
*/
public function setMppe( $mppe) {
$this->mppe = $mppe;
}
/**
* @return bool[]
*/
public function getAllowedAuth() {
return $this->allowed_auth;
}
/**
* @param bool[] $allowed_auth
*/
public function setAllowedAuth( $allowed_auth) {
$this->allowed_auth = $allowed_auth;
}
}

View File

@ -0,0 +1,135 @@
<?php
namespace alphayax\freebox\api\v3\models\VPN\Server\Config;
use alphayax\freebox\api\v3\Model;
use alphayax\freebox\api\v3\symbols;
/**
* Class ServerConfig
* @package alphayax\freebox\api\v3\models\VPN\Server\Config
*/
class ServerConfig extends Model {
/** @var string (Read-only) : VPN server id */
protected $id;
/**
* @var string (Read-only) : VPN server type
* @see symbols\VPN\ServerConfig\Type
*/
protected $type;
/** @var bool : is the VPN server enabled */
protected $enabled;
/** @var int : the server port */
protected $port;
/** @var PPTPConfig : only available when type is PPTP */
protected $conf_pptp;
/** @var OpenVPNConfig : only available when type is OpenVPN */
protected $conf_openvpn;
/** @var string (Read-only) : start of the IP range that will be used to give clients an IP */
protected $ip_start;
/** @var string (Read-only) : end of the IP range that will be used to give clients an IP */
protected $ip_end;
/**
* ServerConfig constructor.
* @param array $properties_x
*/
public function __construct( array $properties_x){
parent::__construct( $properties_x);
$this->initProperty( 'conf_pptp' , PPTPConfig::class);
$this->initProperty( 'conf_openvpn' , OpenVPNConfig::class);
}
/**
* @return string
*/
public function getId() {
return $this->id;
}
/**
* @return string
* @see symbols\VPN\ServerConfig\Type
*/
public function getType() {
return $this->type;
}
/**
* @return boolean
*/
public function isEnabled() {
return $this->enabled;
}
/**
* @param boolean $enabled
*/
public function setEnabled( $enabled) {
$this->enabled = $enabled;
}
/**
* @return int
* NOTE: you can only edit the server port when type is openvpn
*/
public function getPort() {
return $this->port;
}
/**
* @param int $port
*/
public function setPort( $port) {
$this->port = $port;
}
/**
* @return \alphayax\freebox\api\v3\models\VPN\Server\Config\PPTPConfig
*/
public function getConfPptp() {
return $this->conf_pptp;
}
/**
* @param \alphayax\freebox\api\v3\models\VPN\Server\Config\PPTPConfig $conf_pptp
*/
public function setConfPptp( $conf_pptp) {
$this->conf_pptp = $conf_pptp;
}
/**
* @return \alphayax\freebox\api\v3\models\VPN\Server\Config\OpenVpnConfig
*/
public function getConfOpenvpn() {
return $this->conf_openvpn;
}
/**
* @param \alphayax\freebox\api\v3\models\VPN\Server\Config\OpenVpnConfig $conf_openvpn
*/
public function setConfOpenvpn( $conf_openvpn) {
$this->conf_openvpn = $conf_openvpn;
}
/**
* @return string
*/
public function getIpStart() {
return $this->ip_start;
}
/**
* @return string
*/
public function getIpEnd() {
return $this->ip_end;
}
}

View File

@ -0,0 +1,111 @@
<?php
namespace alphayax\freebox\api\v3\models\VPN\Server;
use alphayax\freebox\api\v3\Model;
/**
* Class Connection
* @package alphayax\freebox\api\v3\models\VPN\Server
*/
class Connection extends Model {
/** @var string (Read-only) : connection id */
protected $id;
/** @var string (Read-only) : related VPN server id */
protected $vpn;
/** @var string (Read-only) : user login */
protected $user;
/** @var bool (Read-only) : is the connection authenticated */
protected $authenticated;
/** @var int (Read-only) : timestamp of the authentication */
protected $auth_time;
/** @var string ipv4 (Read-only) : connection source IP address */
protected $src_ip;
/** @var int (Read-only) : connection source port */
protected $src_port;
/** @var int (Read-only) : attributed IP address from VPN adress pool */
protected $local_ip;
/** @var int (Read-only) : rx bytes */
protected $rx_bytes;
/** @var int (Read-only) : tx bytes */
protected $tx_bytes;
/**
* @return string
*/
public function getId() {
return $this->id;
}
/**
* @return string
*/
public function getVpn() {
return $this->vpn;
}
/**
* @return string
*/
public function getUser() {
return $this->user;
}
/**
* @return boolean
*/
public function isAuthenticated() {
return $this->authenticated;
}
/**
* @return int
*/
public function getAuthTime() {
return $this->auth_time;
}
/**
* @return string
*/
public function getSrcIp() {
return $this->src_ip;
}
/**
* @return int
*/
public function getSrcPort() {
return $this->src_port;
}
/**
* @return int
*/
public function getLocalIp() {
return $this->local_ip;
}
/**
* @return int
*/
public function getRxBytes() {
return $this->rx_bytes;
}
/**
* @return int
*/
public function getTxBytes() {
return $this->tx_bytes;
}
}

View File

@ -0,0 +1,64 @@
<?php
namespace alphayax\freebox\api\v3\models\VPN\Server;
use alphayax\freebox\api\v3\Model;
use alphayax\freebox\api\v3\symbols;
/**
* Class Server
* @package alphayax\freebox\api\v3\models\VPN
*/
class Server extends Model {
/** @var string (Read-only) : VPN server name (id) */
protected $name;
/** @var string (Read-only) : VPN server type */
protected $type;
/** @var string (Read-only) : server state */
protected $state;
/** @var int (Read-only) : number of active connections */
protected $connection_count;
/** @var int (Read-only) : number of active connections that have passed authentication */
protected $auth_connection_count;
/**
* @return string
*/
public function getName() {
return $this->name;
}
/**
* @return string
* @see symbols\VPN\Server\Type
*/
public function getType() {
return $this->type;
}
/**
* @return string
* @see symbols\VPN\Server\State
*/
public function getState() {
return $this->state;
}
/**
* @return int
*/
public function getConnectionCount() {
return $this->connection_count;
}
/**
* @return int
*/
public function getAuthConnectionCount() {
return $this->auth_connection_count;
}
}

View File

@ -0,0 +1,70 @@
<?php
namespace alphayax\freebox\api\v3\models\VPN\Server;
use alphayax\freebox\api\v3\Model;
/**
* Class User
* @package alphayax\freebox\api\v3\models\VPN\Server
*/
class User extends Model {
/** @var string : VPN user login */
protected $login;
/** @var string (Write-only) : VPN user password (length must be between 8 and 32) */
protected $password;
/** @var bool (Read-only) : True if a password was provided for this user */
protected $password_set;
/**
* @var string ipv4
* You can specify the IP you want to assign to this user.
* If you dont want to use a specific IP pass an empty string or omit this property.
* The IP must be in the VPN range (see ip_start, ip_end).
*/
protected $ip_reservation;
/**
* @return string
*/
public function getLogin() {
return $this->login;
}
/**
* @param string $login
*/
public function setLogin( $login) {
$this->login = $login;
}
/**
* @param string $password
*/
public function setPassword( $password) {
$this->password = $password;
}
/**
* @return boolean
*/
public function isPasswordSet() {
return $this->password_set;
}
/**
* @return string
*/
public function getIpReservation() {
return $this->ip_reservation;
}
/**
* @param string $ip_reservation
*/
public function setIpReservation( $ip_reservation) {
$this->ip_reservation = $ip_reservation;
}
}

View File

@ -0,0 +1,40 @@
<?php
namespace alphayax\freebox\api\v3\services\config\VPN\Server;
use alphayax\freebox\api\v3\models;
use alphayax\freebox\api\v3\Service;
/**
* Class VPNServerConfig
* @package alphayax\freebox\api\v3\services\config\VPN\Server
*/
class Config extends Service {
const API_VPN_SERVER_CONFIG = '/api/v3/vpn/%s/config/';
/**
* Get a VPN config
* @param string $vpnId (eg: openvpn_routed)
* @return models\VPN\Server\Config\ServerConfig
*/
public function getConfigurationFromId( $vpnId){
$service = sprintf( self::API_VPN_SERVER_CONFIG, $vpnId);
$rest = $this->getAuthService( $service);
$rest->GET();
return new models\VPN\Server\Config\ServerConfig( $rest->getCurlResponse()['result']);
}
/**
* Update the VPN configuration
* @param models\VPN\Server\Config\ServerConfig $serverConfig
* @return models\VPN\Server\Config\ServerConfig
*/
public function setConfiguration( models\VPN\Server\Config\ServerConfig $serverConfig){
$service = sprintf( self::API_VPN_SERVER_CONFIG, $serverConfig->getId());
$rest = $this->getAuthService( $service);
$rest->PUT( $serverConfig->toArray());
return new models\VPN\Server\Config\ServerConfig( $rest->getCurlResponse()['result']);
}
}

View File

@ -0,0 +1,37 @@
<?php
namespace alphayax\freebox\api\v3\services\config\VPN\Server;
use alphayax\freebox\api\v3\Service;
use alphayax\freebox\api\v3\models;
/**
* Class Connection
* @package alphayax\freebox\api\v3\services\config\VPN\Server
*/
class Connection extends Service {
const API_VPN_CONNECTION = '/api/v3/vpn/connection/';
/**
* Get the list of connections
* @return models\VPN\Server\Connection::class[]
*/
public function getAll(){
$rest = $this->getAuthService( self::API_VPN_CONNECTION);
$rest->GET();
return $rest->getResultAsArray( models\VPN\Server\Connection::class);
}
/**
* Close a given connection
* @param $connectionId
* @return bool
*/
public function closeFromId( $connectionId){
$rest = $this->getAuthService( self::API_VPN_CONNECTION . $connectionId);
$rest->DELETE();
return $rest->getSuccess();
}
}

View File

@ -0,0 +1,25 @@
<?php
namespace alphayax\freebox\api\v3\services\config\VPN\Server;
use alphayax\freebox\api\v3\Service;
use alphayax\freebox\api\v3\models;
/**
* Class IpPool
* @package alphayax\freebox\api\v3\services\config\VPN\Server
*/
class IpPool extends Service {
const API_VPN_IPPOOL = '/api/v3/vpn/ip_pool/';
/**
* Get the VPN server IP pool reservations
* @return array
*/
public function getReservations(){
$rest = $this->getAuthService( self::API_VPN_IPPOOL);
$rest->GET();
return $rest->getResult();
}
}

View File

@ -0,0 +1,96 @@
<?php
namespace alphayax\freebox\api\v3\services\config\VPN\Server;
use alphayax\freebox\api\v3\Service;
use alphayax\freebox\api\v3\models;
/**
* Class User
* @package alphayax\freebox\api\v3\services\config\VPN\Server
*/
class User extends Service {
const API_VPN_USER = '/api/v3/vpn/user/';
const API_VPN_USER_CONFIG = '/api/v3/vpn/download_config/%s/%s';
/**
* Get the list of VPNUser
* @return models\VPN\Server\User[]
*/
public function getAll(){
$rest = $this->getAuthService( self::API_VPN_USER);
$rest->GET();
return $rest->getResultAsArray( models\VPN\Server\User::class);
}
/**
* Gets the VPNUser with the given login
* @param $login
* @return models\VPN\Server\User
*/
public function getFromLogin( $login){
$rest = $this->getAuthService( self::API_VPN_USER . $login);
$rest->GET();
return $rest->getResult( models\VPN\Server\User::class);
}
/**
* Creates a new VPNUser
* @param \alphayax\freebox\api\v3\models\VPN\Server\User $user
* @return models\VPN\Server\User
*/
public function add( models\VPN\Server\User $user){
$rest = $this->getAuthService( self::API_VPN_USER);
$rest->POST( $user->toArray());
return $rest->getResult( models\VPN\Server\User::class);
}
/**
* Deletes the VPNUser
* @param \alphayax\freebox\api\v3\models\VPN\Server\User $user
* @return bool
*/
public function delete( models\VPN\Server\User $user){
return $this->deleteFromLogin( $user->getLogin());
}
/**
* Deletes the VPNUser
* @param string $login
* @return bool
*/
public function deleteFromLogin( $login){
$rest = $this->getAuthService( self::API_VPN_USER . $login);
$rest->DELETE();
return $rest->getSuccess();
}
/**
* Update a VPN Use
* @param \alphayax\freebox\api\v3\models\VPN\Server\User $user
* @return bool
*/
public function update( models\VPN\Server\User $user){
$rest = $this->getAuthService( self::API_VPN_USER . $user->getLogin());
$rest->PUT( $user->toArray());
return $rest->getResult( models\VPN\Server\User::class);
}
/**
* @param string $serverName
* @param string $login
* @return string The content of the configuration file
*/
public function getConfigurationFile( $serverName, $login){
$service = sprintf( self::API_VPN_USER_CONFIG, $serverName, $login);
$rest = $this->getAuthService( $service, false, false);
$rest->GET( null, false);
return $rest->getCurlResponse();
}
}

View File

@ -0,0 +1,14 @@
<?php
namespace alphayax\freebox\api\v3\symbols\VPN\OpenVPNConfig;
/**
* Symbol Cipher
* @package alphayax\freebox\api\v3\symbols\VPN\OpenVPNConfig
* @see alphayax\freebox\api\v3\models\VPN\Server\Config\OpenVPNConfig
*/
interface Cipher {
const BLOWFISH = 'blowfish';
const AES128 = 'aes128';
const AES256 = 'aes256';
}

View File

@ -0,0 +1,19 @@
<?php
namespace alphayax\freebox\api\v3\symbols\VPN\PPTPConfig;
/**
* Symbol Mppe
* @package alphayax\freebox\api\v3\symbols\VPN\PPTPConfig
* @see alphayax\freebox\api\v3\models\VPN\Server\Config\PPTPConfig
*/
interface Mppe {
/** disable mppe */
const DISABLE = 'disable';
/** require mppe */
const REQUIRE_ = 'require';
/** require 128 bits mppe */
const REQUIRE_128 = 'require_128';
}

View File

@ -0,0 +1,15 @@
<?php
namespace alphayax\freebox\api\v3\symbols\VPN\Server;
/**
* Symbol State
* @package alphayax\freebox\api\v3\symbols\VPN\Server
*/
interface State {
const STOPPED = 'stopped';
const STARTING = 'starting';
const STARTED = 'started';
const STOPPING = 'stopping';
const ERROR = 'error';
}

View File

@ -0,0 +1,15 @@
<?php
namespace alphayax\freebox\api\v3\symbols\VPN\Server;
/**
* Symbol Type
* @package alphayax\freebox\api\v3\symbols\VPN\Server
*/
interface Type {
/** PPTP VPN server */
const PPTP = 'pptp';
/** OpenVPN server */
const OPEN_VPN = 'openvpn';
}

View File

@ -0,0 +1,16 @@
<?php
namespace alphayax\freebox\api\v3\symbols\VPN\ServerConfig;
/**
* Symbol Type
* @package alphayax\freebox\api\v3\symbols\VPN\ServerConfig
* @see alphayax\freebox\api\v3\models\VPN\Server\Config\ServerConfig
*/
interface Type {
/** PPTP VPN server */
const PPTP = 'pptp';
/** OpenVPN server */
const OPEN_VPN = 'openvpn';
}

View File

@ -78,4 +78,43 @@ class RestAuth extends alphayax\utils\Rest {
$this->session_token = $session_token;
}
/**
* @param string $className
* @return array
*/
public function getResultAsArray( $className = ''){
$Model_xs = @$this->getCurlResponse()['result'] ?: [];
/// Cast elements
if( ! empty( $className) && ! empty( $Model_xs) && is_subclass_of( $className, alphayax\freebox\api\v3\Model::class)) {
array_walk( $Model_xs, function( &$item, $key, $className){
$item = new $className( $item);
}, $className);
}
return $Model_xs;
}
/**
* @param string $className
* @return array
*/
public function getResult( $className = ''){
$Model = @$this->getCurlResponse()['result'];
/// Cast element
if( ! empty( $className) && ! empty( $Model) && is_subclass_of( $className, alphayax\freebox\api\v3\Model::class) && is_array( $Model)){
return new $className( $Model);
}
return $Model;
}
/**
* @return bool
*/
public function getSuccess(){
return boolval( $this->getCurlResponse()['success']);
}
}