Pwning Your Phone with Adhearsion and Asterisk


Published on

This presentation was given by Jay Phillips, Adhearsion's creator, at the Gotham Ruby Conference on April 21st, 2007. Adhearsion is a framework for developing collaboration applications with Asterisk (VoIP), Rails, instant messaging, email, and many other technologies.

Published in: Economy & Finance, Technology
  • VOIP skills are so complex. Thanks for sharing this slide. I hope you won't mind if I embed it at my site at :

    Thanks. Hope to see more slides from you.
    Anita Gomez.
    Are you sure you want to  Yes  No
    Your message goes here
  • Jay, Thanks for creating this wonderful code! I heard you talk last year @ astricon and was very interested what you had to say..... Life kept me tooo obusy to delve into this .... recently I have had to solve some problems on our asterisk core that were becoming really a PITA to deal with in perl and i remember 'adhearsion'.... after getting it up and running I nearly fell out of my chair!.. writing dialplan login in ruby is fun!
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Pwning Your Phone with Adhearsion and Asterisk

  1. 1. Pwning Your Phone <ul><li>with Adhearsion and Asterisk </li></ul>
  2. 2. Past & Today <ul><li>Started January of 2005 at an ITSP </li></ul><ul><li>I got into Rails. Started rewrite last summer </li></ul><ul><li>Launched last Christmas </li></ul><ul><li>Spoke at O’Reilly Emerging Telephony 2007 </li></ul><ul><li>4 page article in Linux Journal last March </li></ul><ul><li>Section in next O’Reilly Asterisk book </li></ul>
  3. 3. Adhearsion in a Nutshell <ul><li>Build very complex phone systems with Ruby </li></ul><ul><li>Open source (LGPL) framework </li></ul><ul><li>Builds atop Asterisk without losing anything </li></ul><ul><li>More of a “collaboration framework” than an “Asterisk development framework” </li></ul><ul><li>Ad hear sion = “adhesion” you can “hear” </li></ul><ul><li>Not Rails. For the VoIP domain specifically! </li></ul>
  4. 4. Why VoIP Rocks <ul><li>VoIP skills are awesome for hackers </li></ul><ul><li>VoIP can dramatically cut a business’ expenses </li></ul><ul><li>VoIP skills look good on a resume </li></ul><ul><li>Builds skills in CS, SE, EE, UX, BA, and all kinds of other acronyms </li></ul><ul><li>Plus, now you can use Ruby! </li></ul>(why the lucky stiff???)
  5. 5. Fun VoIP Projects <ul><li>Unlock apartment door with your cell phone </li></ul><ul><li>Control your hacked Xbox Media Center </li></ul><ul><li>Rarely ever pay for a phone call </li></ul><ul><li>Make your own crazy reminder system </li></ul><ul><li>Control anything with voice commands </li></ul><ul><li>Phone controlled Roomba </li></ul>
  6. 6. Dave Troy’s Asterisk-Powered Roomba Dave’s also writing “Telephony with Ruby”. Will be published by The Prags in November
  7. 7. What is ? What is ? <ul><li>Open source phone call manager (PBX) </li></ul><ul><li>Sponsored and developed by Digium, Inc. </li></ul><ul><li>Revolutionized the telecom world forever </li></ul><ul><li>Makes hackers really happy </li></ul><ul><li>Makes cash flow statements really happy </li></ul><ul><li>Does everything you could want with a phone </li></ul>
  8. 8. Existing Control Grammars Suck At... <ul><li>Conditional looping & branching </li></ul><ul><li>Variables </li></ul><ul><li>Complex data structures </li></ul><ul><li>Database/LDAP integration </li></ul><ul><li>Use of third-party libraries </li></ul><ul><li>Extending the language </li></ul><ul><li>Error handling </li></ul><ul><li>Date & time handling </li></ul><ul><li>Pattern matching </li></ul> name just a few things!
  9. 9. Old Dialplan Example [macro-dial-iax] exten => s,1,SetGlobalVar(FOUNDME=ANSWER) exten => s,2,GotoIf($[${LEN(${ARG1})} = 10]?2:4) exten => s,3,SetVar(NumToDial=1${ARG1}) exten => s,4,Goto(s,6) exten => s,5,SetVar(NumToDial=${ARG1}) ... From extensions.conf
  10. 10. Why Use Adhearsion? <ul><li>Asterisk rocks but its barrier to entry sucks </li></ul><ul><li>Grammar feels like assembler or Excel </li></ul><ul><li>Adds many new features to your PBX </li></ul><ul><li>Adhearsion does dramatically improve Asterisk </li></ul><ul><li>Database integration with ActiveRecord </li></ul><ul><li>Last but not least, integrate VoIP and Rails! </li></ul>
  11. 11. Before Adhearsion.
  12. 12. Before Adhearsion. After Adhearsion.
  13. 13. Other Aspects of Adhearsion <ul><li>Helper system for extending the framework </li></ul><ul><li>One of the first times VoIP code can be traded </li></ul><ul><li>Integrates with on-phone micro-browsers </li></ul><ul><li>Use other collaboration technologies together </li></ul><ul><li>Instant messaging, growl, weather, reverse number lookup, etc... </li></ul>
  14. 14. Getting Started
  15. 15. Asterisk?! Isn’t that hard to install? <ul><li>No! Use Digium’s AsteriskNOW distro </li></ul><ul><li>Available as install or live CD, VMWare & Xen </li></ul><ul><li>Asterisk installed and running when booted </li></ul><ul><li>Asterisk v1.4 now has a web GUI </li></ul><ul><li>Many good resources exist to get you started </li></ul><ul><li>Not to mention it now has ADHEARSION </li></ul>
  16. 16. Installing Adhearsion gem install adhearsion [asterisk-context] exten => _.,1,AGI(agi://
  17. 17. Writing Adhearsion Dialplans internal { }
  18. 18. Writing Adhearsion Dialplans internal { play ‘hello-world’ }
  19. 19. Writing Adhearsion Dialplans internal { loop { play ‘tt-monkeys’ } }
  20. 20. Writing Adhearsion Dialplans internal { +foo } foo { record { dial SIP/:out_trunk/1234 } }
  21. 21. Writing Adhearsion Dialplans internal { play %w”a-connect-charge-of 16 cents-per-minute will-apply” }
  22. 22. Writing Adhearsion Dialplans internal { case extension when 10 ... 100 dial SIP /extension when 6000 ... 6020 , 7000 .. 7030 join extension when _ ' 21XX ' if Time .now.hour.between? 2 , 10 dial SIP / &quot; berlin-office &quot; /extension[ 2 .. 4 ] else speak &quot; It is not business hours now in Germany. &quot; end ...
  23. 23. Writing Adhearsion Dialplans ... when US_NUMBER dial SIP / ' us-trunk-out ' /extension when / ^ d {11,}$ / dial IAX / ' intl-trunk-out ' /extension else play %w' sorry invalid extension please-try-again ' end }
  24. 24. Database-Driven Dialplans Dialplans service { customer = Account.find_by_phone_number callerid usage = customer.usage_this_month if usage >= 100.hours then +beyond_limit else customer.usage_this_month += time do dial IAX2/'main-trunk'/extension end end } beyond_limit { play %w&quot;sorry your-limit-of 100 hours has-been-reached&quot; }
  25. 25. How Does Adhearsion Talk to Asterisk? <ul><li>Asterisk receives a call normally </li></ul><ul><li>Asterisk establishes a socket to Adhearsion, sending all call-related information </li></ul><ul><li>Adhearsion evaluates its own dial plan and sends commands over one at a time using AGI </li></ul><ul><li>Asterisk evals commands one at a time, sending a result after each </li></ul><ul><li>Dialog continues until call ends </li></ul>
  26. 26. Things you can do from Rails <ul><li>Invoke virtually any PBX event with Adhearsion’s sexy syntaxes </li></ul><ul><ul><li>Start calls, view live channels, record channel </li></ul></ul><ul><li>Share Adhearsion’s ActiveRecord models </li></ul><ul><ul><li>Manage users, groups, anything... </li></ul></ul><ul><ul><li>Manage a DB-driven dial plan </li></ul></ul><ul><li>Use your imagination! </li></ul>
  27. 27. Helpers
  28. 28. What’s a Helper? <ul><li>Code loaded when Adhearsion boots </li></ul><ul><li>Introduces a technology to the framework </li></ul><ul><li>Can be virtually anything </li></ul><ul><li>Can be written in Ruby, C, Java (JRuby) </li></ul>
  29. 29. Helpers require &quot;hpricot&quot; require &quot;open-uri&quot; def lookup number hash = {} url = &quot;{number} &quot; doc = Hpricot open(url) # This div contains all the information we need, unless it's an unlisted number if (results = &quot;#results_single_listing&quot;) then # This div's h3 contains the name of the caller hash[:first_name], hash[:last_name] ='h3').inner_html.split(/,s*/).reverse # Now we just need the rest of the information contained in p's. meta = results/'p' meta.pop # Discard the useless p element hash[:number] = meta.pop.inner_html city_info = meta.pop.inner_html city_info = city_info.match /(.+), ([A-Za-z]{2}) (d{5})/ hash[:city] = city_info[1] hash[:state] = city_info[2] hash[:zip] = city_info[3] hash[:address] = * &quot; &quot; elsif (results = &quot;#results_single_phone_info&quot;) then meta = results/'span' hash[:location] = (meta.pop.inner_html.match /Location: (.*)/)[1] end if hash[:first_name] or hash[:last_name] then hash[:composite] = &quot;#{hash[:first_name]} #{hash[:last_name]}&quot; else hash[:composite] = hash[:location] end hash end
  30. 30. Helpers (in C) (in C) int fast_factorial(int input) { int prod = 1, count = 1; while(count <= input) { prod *= count++; } return sum; }
  31. 31. Micromenus
  32. 32. Micromenus <ul><li>Many modern IP phones have micro-browsers </li></ul><ul><li>Use custom XML schema over HTTP </li></ul><ul><li>Uses a Ruby DSL to generate </li></ul><ul><li>Mini-web app framework </li></ul><ul><li>Also viewable in a web browser! </li></ul>
  33. 33. Micromenus item 'Adhearsion Server Statistics' do item 'View Registered SIP Users' do PBX.sip_users.each do |u| item &quot;SIP user '#{u.username}' on IP #{u.ip}&quot; end end item 'View System Uptime' do item `uptime` end item 'Network' do heading 'Network Interface Info' `ifconfig eth1`.each_line do |line| item line end end end
  34. 34. Micromenus item 'Call an Employee' do item 'Select an employee below.' User.find(:all).each do |user| call user.extension, end end call 'Join main conference' do play 'welcome' join 5555 end
  35. 35. Where Adhearsion is Headed is Headed <ul><li>Rails GUI to Adhearsion </li></ul><ul><li>Built-in TFTP server serving dynamic configs </li></ul><ul><li>Adhearsion CLI </li></ul><ul><li>High-level Queue and Agent abstractions </li></ul><ul><li>ORM for Asterisk configs (ActiveAsterisk) </li></ul><ul><li>RSpec testing... for a PBX </li></ul>
  36. 36. Get Involved! <ul><li>I’m always looking for Adhearsion hackers! </li></ul><ul><li>Share your ideas on our mailing list! </li></ul><ul><li>Build your own awesome PBX on a weekend </li></ul><ul><li>For corporate needs, I offer Adhearsion consulting </li></ul><ul><li>Contact me at [email_address] </li></ul><ul><li>Adhearsion’s website: </li></ul>