gnosus - Meetup
Transcription
gnosus - Meetup
XMPP Virtual Hosting with ejabberd and Nitrogen by Troy Stribling Wednesday, June 15, 2011 Outline • Overview • Architecture • Implementation • Demo Wednesday, June 15, 2011 What is XMPP Virtual Hosting? • Hosting multiple XMPP domains on a single XMPP server. • Much like HTTP virtual hosting. Wednesday, June 15, 2011 Goal of Project • Self serve on demand XMPP virtual hosting web service. • Virtual Host Administration Console • Web based XMPP Client • Prototype running at: http://gnos.us • Source: https://github.com/troystribling/ gnosus Wednesday, June 15, 2011 Architecture Wednesday, June 15, 2011 Technologies • Erlang OTP: Mnesia • Mociweb: App Server • Nginx: Web Server • ejabberd: XMPP Server • Nitrogen: Web Framework,Virtual Host Administration Console • Strophe.js: BOSH Client, Web Based XMPP Wednesday, June 15, 2011 ejabberd • Open Source XMPP Server written in Erlang • Process One: http://www.process-one.net/ en/ejabberd/ • Source: https://github.com/processone/ ejabberd • My fork: https://github.com/troystribling/ ejabberd Wednesday, June 15, 2011 Nitrogen • Open source Erlang web framework, http:// nitrogenproject.com/ • Developed by Rusty Klophause: http:// rustyklophaus.com/ Wednesday, June 15, 2011 Strophe.js • Open source javascript XMPP BOSH Client, http://strophe.im/strophejs/ • Developed by Jack Moffitt: http:// metajack.im/ Wednesday, June 15, 2011 Architecture Web Client BOSH web browser Erlang RPC Admin Console Mnesia Mnesia Db Db beam password Wednesday, June 15, 2011 ejabberd Mirrored beam Mnesia Db Implementation Wednesday, June 15, 2011 Tasks • Modify ejabberd to not require reboot when virtual host added • Administration Console implemented as Nitrogen web application. • Web Client • Administration Console to ejabberd interfaces for virtual host provisioning and authentication Wednesday, June 15, 2011 ejabberd Customization The open source version of ejabberd loads virtual domains from ejabberd.cfg. To add or delete a domains a reboot is required to reread the config file. Changes were made so that virtual domains could be added or deleted without a reboot. Less than100 total lines of code needed to be added to only four files Wednesday, June 15, 2011 Files Changed File Change Removed code that purged host database on ejabberd_config.erl start. Added methods to add and remove host from database. ejabberd_local.erl Added methods to create and delete servers that managed C2S and S2S connections and message routing for virtual host. Added ,methods to register and unregister callbacks used for host message processing. acl.erl Added method to delete ACL from database. mod_caps.erl Fixed bug where CAPS message processing serever was not completely cleaned up when parent server was deleted. Wednesday, June 15, 2011 Administration Console • Nitrogen web application • Create/Delete virtual hosts • Create/Delete virtual host users Wednesday, June 15, 2011 Nitrogen Overview • No Controllers • Uses layouts but not view templates instead view is written in Erlang and referred to a pages by Nitrogen • Pages use Erlang record based DSL with some method calls to generate HTML and javascript. • Routes hardcoded in module name Wednesday, June 15, 2011 Template Example Callback to body() method on page. Here page module is part of Nitrogen framework. <div id="body-wrapper"> <div id="body"> [[[page:body()]]] </div> </div> Wednesday, June 15, 2011 Page Module Example Nitrogen hooks in page module -module (web_index). -include_lib("nitrogen/include/wf.inc"). main() -> #template{file="./wwwroot/template.html"}. Wednesday, June 15, 2011 Page Module Example Page Module body() method body() -> Body = [ #p{body=[#label{text="uid"}, #textbox{id=userTextBox,next=loginButton}, #link{id=loginButton, text="login", postback=login, class="button"}], class="form login"}, wf:wire(loginButton, userTextBox, #validate{validators=[ #is_required{text="uid required"}]}), wf:render(Body). Wednesday, June 15, 2011 Page Module Example Page module login postback event(login) -> [Uid] = wf:html_encode(wf:q(userTextBox)), case user_model:authorize(Uid) of true -> wf:flash("authorized") false -> wf:flash("unauthorized") end; event(_) -> ok. Wednesday, June 15, 2011 Administration Console ejabberd Interface • Erlang RPCs for virtual host provisioning • Mirror of ejabberd password database for authentication Wednesday, June 15, 2011 Erlang Cookies • Adminstration Console and ejabberd run in seperate Erlang VMs. • Erlang cookies must match for RPCs and database mirror to work. • Erlang cookie can be set using the .erlang_cookie file or the --set-cookie erl command line option. Here the .erlang_cookie file is used. Wednesday, June 15, 2011 Erlang VM Names • Erlang VMs must have unique names to communicate • VM names are set using the -sname option of the erl command • Names are of the form name@host, where host is the network address. Wednesday, June 15, 2011 Erlang RPC Syntax General Syntax rpc:call(name@host, module_name, method_name, [arg1, arg2, ..., agrn]) Example: Register User for Virtual Host from Administration Console rpc:call(ejabberd@localhost, ejabberd_admin, register, [Uid, Host, Password]) Wednesday, June 15, 2011 Database Mirror Add remote node database using extra_db_nodes option for erl command erl -mnesia extra_db_nodes "['ejabberd@$HOST']" Create local RAM copy of database table mnesia:add_table_copy(table, node(), ram_copies). Wednesday, June 15, 2011 Demo https://github.com/troystribling/ejabberd https://github.com/troystribling/gnosus https://gnos.us Wednesday, June 15, 2011