XMPP 檔案傳輸協定(轉)
2012-05-12 14:52:09| 分類: 手機開發 | 标簽: |字号大中小 訂閱
http://wkkyo.iteye.com/blog/1403465
項目中需要用到openfire的檔案傳輸,但是用戶端使用flex,官方提供的xiff包中并沒有封裝檔案傳輸的功能,沒辦法,研究了幾天,在google和官方smock源碼的幫助下終于實作了xiff下的檔案傳輸,在這裡做個總結。
openfire伺服器是基于xmpp協定的,XMPP支援兩種檔案流傳輸協定,SOCKS5 Bytestreams和 In-Band Bytestreams,SOCKS5是直接發送二進制流,而IBB是将檔案轉成base64碼進行然後用message的形式進行傳輸,我這裡僅實作了 SOCKS5的檔案代理傳輸。
SOCKS5檔案傳輸需要用到兩個協定,XEP-0065和XEP-0096
XEP-0096定義檔案傳輸協定,提供了一個子產品化架構使能交換被傳輸檔案的資訊以及參數的協商,也就是在傳輸檔案之前協商将要傳輸的檔案資訊。
XEP-0065定義SOCKS5流傳輸标準協定,提供用于在任意兩個XMPP使用者之間建立位元組流并進行檔案傳輸。
根據我的了解,檔案傳輸的過程分為協商,建立socks5連接配接,二進制傳輸這三個階段
協商的過程最複雜,然後是建立連接配接,傳輸就比較簡單,下面一個一個來講
協商包括初始方、目标方、代理方,初始方就是發送檔案方,目标方即檔案接收方,代理方是socks5代理伺服器,
協商過程就是三方互相發送xml來交換資訊的過程,通俗點就是三個人溝通一下傳什麼檔案和怎麼傳檔案。
首先遵循XMP-0096協定,初始方給目标方發送包含檔案資訊的xml
Xml代碼
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicmbw5ichR3cf52bjl2LcNXZnFWbp9CXt92YuUWelRXau8Wert2dvw1LcpDc0RHaiojIsJye.png)
- <iq to="andro[email protected]/Spark 2.6.3" type="set" id="iq_13" from="[email protected]/xiff">
- <si profile="http://jabber.org/protocol/si/profile/file-transfer" mime-type="text/plain" id="82B0C697-C1DE-93F9-103E-481C8E7A3BD8" xmlns="http://jabber.org/protocol/si">
- <feature xmlns="http://jabber.org/protocol/feature-neg">
- <x xmlns="jabber:x:data" type="form">
- <field var="stream-method" type="list-single">
- <option><value>http://jabber.org/protocol/bytestreams</value></option>
- <option><value>http://jabber.org/protocol/ibb</value></option>
- </field>
- </x>
- </feature>
- <file xmlns="http://jabber.org/protocol/si/profile/file-transfer" name="img0545.png" size="152443"><desc>send</desc></file>
- </si>
- </iq>
目标方接收到資訊後發送回執,表示同意接收檔案
Xml代碼
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicmbw5ichR3cf52bjl2LcNXZnFWbp9CXt92YuUWelRXau8Wert2dvw1LcpDc0RHaiojIsJye.png)
- <iq id="iq_13" to="[email protected]/xiff" from="[email protected]/Spark 2.6.3" type="result">
- <si xmlns="http://jabber.org/protocol/si">
- <feature xmlns="http://jabber.org/protocol/feature-neg">
- <x xmlns="jabber:x:data" type="submit">
- <field var="stream-method">
- <value>http://jabber.org/protocol/bytestreams</value>
- <value>http://jabber.org/protocol/ibb</value>
- </field>
- </x>
- </feature>
- </si>
- </iq>
這時進入XEP-0065協定階段
初始方給伺服器發送資訊,請求提供代理伺服器
Xml代碼
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicmbw5ichR3cf52bjl2LcNXZnFWbp9CXt92YuUWelRXau8Wert2dvw1LcpDc0RHaiojIsJye.png)
- <iq id="iq_15" type="get"><query xmlns="http://jabber.org/protocol/disco#items" /></iq>
伺服器回複資訊,告知可用的代理
Xml代碼
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicmbw5ichR3cf52bjl2LcNXZnFWbp9CXt92YuUWelRXau8Wert2dvw1LcpDc0RHaiojIsJye.png)
- <iq type="result" id="iq_15" to="[email protected]/xiff">
- <query xmlns="http://jabber.org/protocol/disco#items">
- <item jid="proxy.192.168.1.113" name="Socks 5 Bytestreams Proxy"/>
- <item jid="pubsub.192.168.1.113" name="Publish-Subscribe service"/>
- <item jid="conference.192.168.1.113" name="公共房間"/>
- <item jid="search.192.168.1.113" name="User Search"/>
- </query>
- </iq>
這裡選擇name=“Socks 5 Bytestreams Proxy”的代理,初始方給這個代理發送資訊擷取代理連接配接資訊
Xml代碼
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicmbw5ichR3cf52bjl2LcNXZnFWbp9CXt92YuUWelRXau8Wert2dvw1LcpDc0RHaiojIsJye.png)
- <iq id="iq_17" to="proxy.192.168.1.113" type="get"><query xmlns="http://jabber.org/protocol/bytestreams" /></iq>
代理方回複資訊,告知初始方代理的jid、IP、端口等資訊
Xml代碼
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicmbw5ichR3cf52bjl2LcNXZnFWbp9CXt92YuUWelRXau8Wert2dvw1LcpDc0RHaiojIsJye.png)
- <iq type="result" id="iq_17" from="proxy.192.168.1.113" to="[email protected]/xiff">
- <query xmlns="http://jabber.org/protocol/bytestreams">
- <streamhost jid="proxy.192.168.1.113" host="192.168.1.113" port="7777"/>
- </query>
- </iq>
初始方收到代理資訊後将代理的資訊發送給目标方
Xml代碼
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicmbw5ichR3cf52bjl2LcNXZnFWbp9CXt92YuUWelRXau8Wert2dvw1LcpDc0RHaiojIsJye.png)
- <iq to="[email protected]/Spark 2.6.3" type="set" id="iq_19" from="[email protected]/xiff">
- <query xmlns="http://jabber.org/protocol/bytestreams" mode="tcp" sid="82B0C697-C1DE-93F9-103E-481C8E7A3BD8">
- <streamhost port="7777" host="192.168.1.113" jid="proxy.192.168.1.113" />
- </query>
- </iq>
然後就進入連接配接階段,也就是初始方和目标方分别和代理建立socks5連接配接的過程。(關于SOCKS5協定連接配接,我之後會補充)。
目标方收到代理資訊後和代理建立socket連接配接(使用SOCKS5協定連接配接),連接配接成功後通知初始方使用的代理jid
Xml代碼
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicmbw5ichR3cf52bjl2LcNXZnFWbp9CXt92YuUWelRXau8Wert2dvw1LcpDc0RHaiojIsJye.png)
- <iq id="iq_19" to="[email protected]/xiff" type="result" from="[email protected]/Spark 2.6.3">
- <query xmlns="http://jabber.org/protocol/bytestreams">
- <streamhost-used jid="proxy.192.168.1.113"/>
- </query>
- </iq>
初始方開始與代理建立socket連接配接(也使用SOCKS5協定),連接配接成功後給代理發送請求,要求激活檔案流
Xml代碼
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicmbw5ichR3cf52bjl2LcNXZnFWbp9CXt92YuUWelRXau8Wert2dvw1LcpDc0RHaiojIsJye.png)
- <iq to="proxy.192.168.1.113" type="set" id="iq_21" from="[email protected]/xiff">
- <query xmlns="http://jabber.org/protocol/bytestreams" sid="82B0C697-C1DE-93F9-103E-481C8E7A3BD8">
- <activate>[email protected]/Spark 2.6.3</activate>
- </query>
- </iq>
代理回複激活成功資訊
Xml代碼
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicmbw5ichR3cf52bjl2LcNXZnFWbp9CXt92YuUWelRXau8Wert2dvw1LcpDc0RHaiojIsJye.png)
- <iq type="result" id="iq_21" from="proxy.192.168.1.113" to="[email protected]/xiff"/>
初始方收到回複資訊後就進入二進制流傳輸階段,這時就可以開始發送二進制流了
等初始方将流發送完畢後把socket流關閉傳輸就完成了檔案的傳輸。
注意:type為result的回複資訊使用的id一定要和請求的資訊id一樣。