require 'rest_client' require 'json' module FreeboxApi class Freebox attr_reader :app_token, :app_id, :app_name, :app_version, :device_name, :base_url def initialize(app_token, app_id, app_name, app_version, device_name, base_url = 'http://mafreebox.free.fr') @app_id = app_id @app_name = app_name @app_version = app_version @device_name = device_name @app_token = app_token @base_url = base_url end def self.app_token(app_id, app_name, app_version, device_name) JSON.parse(RestClient.post( "#{@base_url}/api/v1/login/authorize", { :app_id => app_id, :app_name => app_name, :app_version => app_version, :device_name => device_name, }.to_json, :content_type => :json, :accept => :json ))['result']['app_token'] end def challenge JSON.parse(RestClient.get("#{@base_url}/api/v1/login/"))['result']['challenge'] end def password Digest::HMAC.hexdigest(challenge, @app_token, Digest::SHA1) end def session_token JSON.parse( RestClient.post( "#{@base_url}/api/v1/login/session/", { :app_id => @app_id, :password => password, }.to_json, :content_type => :json, :accept => :json ) )['result']['session_token'] end def interfaces JSON.parse( RestClient.get( "#{@base_url}/api/v1/lan/browser/interfaces", :'X_Fbx_App_Auth' => session_token ) )['result'].collect { |interface| FreeboxApi::Resources::Interface.new(interface['name'], interface['host_count'], self) } end def lan_hosts interfaces.collect { |interface| interface.lan_hosts }.flatten end def static_leases JSON.parse( RestClient.get( "#{@base_url}/api/v1/dhcp/static_lease/", :'X_Fbx_App_Auth' => session_token ) )['result'].collect { |static_lease| FreeboxApi::Resources::StaticLease.new( static_lease['id'], static_lease['mac'], static_lease['comment'], static_lease['hostname'], static_lease['ip'], lan_hosts.select { |lan_host| lan_host.id == static_lease['host']['id'] }, self ) } end def redirs JSON.parse( RestClient.get( "#{@base_url}/api/v1/fw/redir/", :'X_Fbx_App_Auth' => session_token ) )['result'].collect { |redir| FreeboxApi::Resources::Redir.new( redir['id'], redir['enabled'], redir['ip_proto'], redir['wan_port_start'], redir['wan_port_end'], redir['lan_ip'], redir['lan_port'], redir['hostname'], lan_hosts.select { |lan_host| lan_host.id == redir['host']['id'] }, redir['comment'], self ) } end end end require 'freebox_api/version' resource_files = Dir[File.expand_path("#{File.dirname(__FILE__)}/freebox_api/resources/*.rb", __FILE__)] resource_files.each { |f| require f }