用 Punjab 做 XMPP + BOSH Proxy,與 chat.facebook.com 介接
  • 6,761 views,
  • 2013-09-18,
  • 上傳者: Kuann Hung,
  •  0
5b579083a7f80181310b550c505b5ecb.jpg
 
這篇其實是花了不少時間研究出來的,台灣好像不太多人在作這方面的工作,查到的資料都是國外的,但是舊的、錯誤的居多!
需要前情提要一下,如果你需要實作一個 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 以上喔!
* 以下所使用的函式庫,建議直接上官網尋找最新版。
步驟如下
1.
安裝 Python 2.7
安裝時要注意一下,如果你跟我一樣使用 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
2.
安裝 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
3.
安裝 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
4.
安裝 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
5.
產生 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>
 
6.
安裝 Punjab

請到此處下載:https://github.com/twonds/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
7.
啟動服務
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。等作出來再跟大家分享! tongue
另外,目前查到資料最正確,最詳盡的就是 jack moffitt 了。如果遇到問題,或是想要了解細節都可以去那邊查喔!
 
圖片引用來源:
Facebook 討論區載入中...
資料夾 :
發表時間 :
2013-09-18 20:52:07
觀看數 :
6,761
發表人 :
Kuann Hung
部門 :
老洪的 IT 學習系統
QR Code :