
這篇其實是花了不少時間研究出來的,台灣好像不太多人在作這方面的工作,查到的資料都是國外的,但是舊的、錯誤的居多!
需要前情提要一下,如果你需要實作一個 Facebook AP,並且使用 facebook chat api 的話,那你就需要準備一個 XMPP Server,然後藉由 XMPP over BOSH 的方式讓你的 javascript 可以直接與 facebook 傳送/接收訊息。
以下介紹如何用 punjab 架設 XMPP over BOSH。
我的環境是 CentOS 5.2,算舊的機器了,所以 Python 還在 2.4 版,若需要使用 punjab 則需要升級到 2.5 以上喔!
* 以下所使用的函式庫,建議直接上官網尋找最新版。
安裝 Python 2.7
安裝時要注意一下,如果你跟我一樣使用 CentOS 5.x ,那你的 python 可能是 2.4 的,如果你貿然更換掉原本的 python 到 2.5 以上,那你的 yum 可能會不能用喔!所以我加上了 –prefix=/usr/local,避免蓋掉系統中的。
安裝時要注意一下,如果你跟我一樣使用 CentOS 5.x ,那你的 python 可能是 2.4 的,如果你貿然更換掉原本的 python 到 2.5 以上,那你的 yum 可能會不能用喔!所以我加上了 –prefix=/usr/local,避免蓋掉系統中的。
wget http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tgz ./configure --prefix=/usr/local make make install
安裝 zope.interface
wget http://pypi.python.org/packages/source/z/zope.interface/zope.interface-3.8.0.tar.gz#md5=8ab837320b4532774c9c89f030d2a389 tar -zxvf zope.interface-3.8.0.tar.gz python2 setup.py install
安裝 twisted
twisted 是用來執行 punjab 的。
wget http://twistedmatrix.com/Releases/Twisted/12.0/Twisted-12.0.0.tar.bz2 bunzip2 Twisted-12.0.0.tar.bz2 tar -xvf Twisted-12.0.0.tar.bz2 cd Twisted-12.0.0 python2 setup.py install
安裝 pyOpenSSL
因為跟 chat.facebook.com 做驗證時需要使用 TLS,所以要安裝 OpenSSL 提供相關函式。
wget https://launchpad.net/pyopenssl/main/0.11/+download/pyOpenSSL-0.11.tar.gz tar -zxvf pyOpenSSL-0.11.tar.gz cd pyOpenSSL-0.11 python2 setup.py install
產生 TLS Support
同上,先把需要的檔案先產生好。
openssl genrsa > privkey.pem openssl req -new -x509 -key privkey.pem -out cacert.pem -days 1000
以下請自行輸入囉:
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [GB]:TW
State or Province Name (full name) [Berkshire]:Taiwan
Locality Name (eg, city) [Newbury]: YourCity
Organization Name (eg, company) [My Company Ltd]:YourCompany
Organizational Unit Name (eg, section) []:YourOU
Common Name (eg, your name or your server's hostname) []: YourHostname
Email Address []:yourmail@yourdomain.com<script type="text/javascript">
/* <![CDATA[ */
(function(){try{var s,a,i,j,r,c,l,b=document.getElementsByTagName("script");l=b[b.length-1].previousSibling;a=l.getAttribute('data-cfemail');if(a){s='';r=parseInt(a.substr(0,2),16);for(j=2;a.length-j;j+=2){c=parseInt(a.substr(j,2),16)^r;s+=String.fromCharCode(c);}s=document.createTextNode(s);l.parentNode.replaceChild(s,l);}}catch(e){}})();
/* ]]> */
</script>
安裝 Punjab
tar -zxvf twonds-punjab-223f1cf.tar.gz cd twonds-punjab-223f1cf python setup.py install
以下是重點修改項目,這部分花了我最多時間,還好查 log 就可以很清楚知道缺什麼東西了!
#在前面加上
from twisted.internet import reactor, ssl
#在 bosh = HttpbService(1) 後面加上
bosh.connect_srv = False
#最後面加上
sslContext = ssl.DefaultOpenSSLContextFactory(
'privkey.pem',
'cacert.pem',
)
reactor.listenSSL(
5281,
site,
contextFactory=sslContext,
)
為了避免看不懂,附上完整的設定:
# punjab tac file
# tac documentation is at the following URL:
# http://twistedmatrix.com/documents/current/core/howto/application.html
from twisted.web import server, resource, static
from twisted.application import service, internet
from twisted.internet import reactor, ssl
from punjab.httpb import Httpb, HttpbService
root = static.File("./html")
# uncomment only one of the bosh lines, use_raw does no xml
# parsing/serialization but is potentially less reliable
#bosh = HttpbService(1, use_raw=True)
bosh = HttpbService(1)
bosh.connect_srv = False
# You can limit servers with a whitelist.
# The whitelist is a list of strings to match domain names.
# bosh.white_list = ['jabber.org', 'thetofu.com']
# or a black list
# bosh.block_list = ['jabber.org', '.thetofu.com']
root.putChild('http-bind', resource.IResource(bosh))
site = server.Site(root)
application = service.Application("punjab")
internet.TCPServer(5280, site).setServiceParent(application)
sslContext = ssl.DefaultOpenSSLContextFactory(
'privkey.pem',
'cacert.pem',
)
reactor.listenSSL(
5281,
site,
contextFactory=sslContext,
)
# To run this simply to twistd -y punjab.tac
啟動服務
twistd -y punjab.tac
接下來就是測試了。怎麼撰寫程式請參考:http://km.snippetinfo.net/media/208
其中需要設定你的 XMPP over BOSH 位置,如下:
connectionFB = new Strophe.Connection('http://your-ip:5280/http-bind');
這其中設定有很多細節,如果有問題就留言討論吧。
不過這樣做出的東西因為沒有任何限制,所以很容易就會變成別人也可以使用。所以下一個目標就是做限制了。目前想到一個方法,但是還沒有實作,腦中的關鍵字是 proxy,http_referrer。等作出來再跟大家分享!
不過這樣做出的東西因為沒有任何限制,所以很容易就會變成別人也可以使用。所以下一個目標就是做限制了。目前想到一個方法,但是還沒有實作,腦中的關鍵字是 proxy,http_referrer。等作出來再跟大家分享!
另外,目前查到資料最正確,最詳盡的就是 jack moffitt 了。如果遇到問題,或是想要了解細節都可以去那邊查喔!



