ITeye博客

最小的ruby飞信客户端

鹤惊昆仑 · 2009-08-09 14:38 · ruby · 编程语言 · Markdown

最小的飞信的ruby客户端,功能很简单,目前只能给自己和飞信好友发短信。使用TCP连接,没有走HTTP通道。

源码:http://code.google.com/p/fetion-ruby/

2012-06-13 更新,又可以工作了 登录时rsa加密部分参考了pyftion(http://code.google.com/p/pytool/source/browse/#svn%2Ftrunk%2FPyFetion 可以正常登录)和rfetion(https://github.com/flyerhzm/rfetion/blob/master/lib/rfetion/fetion.rb#L530 已经不能正常登录)的实现,外加自己的推测。

使用:


fetion = Fetion.new "mobile_num","password"
fetion.login
fetion.send_sms "mobile_num","any sms"
fetion.send_sms_to_self "test-中文-ruby-fetion"
fetion.logout

经过几个不同ip测试,均没有遇到验证码问题。

评论(18)

  1. #18 楼 · qxt · 2010-08-02 15:24

    鹤惊昆仑 writes:

    今天测试了一下,每次都要图形验证码,绕不过去了。正式宣布放弃维护这段代码。

    我怎么没有遇到图形验证码呢? 是不是如那位兄弟说的,密码设置的太过简单? 别这么轻易放弃哪

  2. #17 楼 · 鹤惊昆仑 · 2010-08-01 19:45

    如果只是做天气预报、服务器监控等应用,可以利用移动139邮箱的邮件到达通知来做(绝对免费的!)http://methodmissing.iteye.com/blog/726588

  3. #16 楼 · webdi · 2010-07-31 10:31

    楼上的飞信密码太简单?改为字母+数字试试?

  4. #15 楼 · 鹤惊昆仑 · 2010-07-30 20:05

    今天测试了一下,每次都要图形验证码,绕不过去了。正式宣布放弃维护这段代码。

  5. #14 楼 · kaka2008 · 2010-07-30 08:04

    我照着 http://code.google.com/p/maplefetion/ 这个例子改了下,目前在我机器上(windows 2003)可用

    
      require 'uri'  
    require 'net/http'  
    require 'net/https'  
    require "socket"  
    require 'rexml/document'  
    require 'digest/md5'    
    require 'digest/sha1'   
    require "iconv"     
      
    class Fetion  
        def initialize(phone_num , password)  
            @phone_num = phone_num;  
            @password = password;  
            @domain = "fetion.com.cn";  
    	#@domain = "feixin.10086.cn"
            @login_xml = '<args><device type="PC" version="0" client-version="4.0.3390" /><caps value="simple-im;im-session;temp-group;personal-group" /><events value="contact;permission;system-message;personal-group" /><user-info attributes="all" /><presence><basic value="400" desc="" /></presence></args>';  
              
            self.init  
        end  
          
        def init  
            doc = REXML::Document.new(self.get_system_config()) 
            sipc_proxy = ""  
            doc.elements.each("//sipc-proxy") do |element|  # using regexp should be faster  
                sipc_proxy = element.text  
            end    
            @SIPC = SIPC.new(sipc_proxy);  
              
            sipc_url = ""   
            doc.elements.each("//ssi-app-sign-in") do |element|    
                sipc_url = element.text  
            end    
            @fetion_num = self.get_fetion_num(self.SSIAppSignIn(sipc_url));  
              
            doc.elements.each("//http-tunnel") do |element|    
                @http_tunnel = element.text  
            end    
        end  
          
        def login()  
            request1 = sprintf("R %s SIP-C/2.0\r\nF: %s\r\nI: 1\r\nQ: 1 R\r\nL: %s\r\n\r\n",@domain, @fetion_num, @login_xml.length);  
            request1 = request1 + @login_xml;  
            server_response = @SIPC.request(request1);  
            @nonce = server_response.scan(/nonce="(.*)"/)[0][0]  
              
            request2 = sprintf("R %s SIP-C/2.0\r\nF: %s\r\nI: 1\r\nQ: 2 R\r\nA: Digest response=\"%s\",cnonce=\"%s\"\r\nL: %s\r\n\r\n", @domain, @fetion_num, self.get_response(), @cnonce, @login_xml.length);  
            request2 = request2 + @login_xml;  
            @SIPC.request(request2);  
        end  
          
        def send_sms(phone, sms_text)  
            sms_text = Iconv.iconv("UTF-8","GB2312",sms_text)[0]   
            request = sprintf("M %s SIP-C/2.0\r\nF: %s\r\nI: 2\r\nQ: 1 M\r\nT: tel:%s\r\nN: SendSMS\r\nL: %s\r\n\r\n",@domain, @fetion_num, phone, sms_text.length);  
            request = request + sms_text;  
            @SIPC.request(request);  
        end  
          
        def send_sms_to_self(sms_text)  
            sms_text = Iconv.iconv("UTF-8","GB2312",sms_text)[0]    
            request = sprintf("M %s SIP-C/2.0\r\nF: %s\r\nI: 2\r\nQ: 1 M\r\nT: %s\r\nN: SendCatSMS\r\nL: %s\r\n\r\n",@domain, @fetion_num, @uri, sms_text.length);  
            request = request + sms_text  
            @SIPC.request(request);  
        end  
      
        def logout()  
            logout_request = sprintf("R %s SIP-C/2.0\r\nF: %s\r\nI: 1 \r\nQ: 3 R\r\nX: 0\r\n\r\n", @domain, @fetion_num);  
            @SIPC.request(logout_request);  
        end  
          
        def get_response()  
            @cnonce = Digest::MD5.hexdigest(rand.to_s)  
            puts "conce: #{@cnonce} , fetion_num: #{@fetion_num} , domain : #{@domain} ,password:#{@password}"  
            key = Digest::MD5.digest(@fetion_num + ":" + @domain + ":" + @password)  
            h1 = Digest::MD5.hexdigest(key + ":" + @nonce + ":" + @cnonce).upcase   
            h2 = Digest::MD5.hexdigest("REGISTER:" + @fetion_num).upcase  
            return Digest::MD5.hexdigest(h1+":" + @nonce + ":" + h2).upcase  
        end  
          
        def get_system_config()  
            uri = URI.parse("http://nav.fetion.com.cn/nav/getsystemconfig.aspx")  
            http = Net::HTTP.new(uri.host, uri.port)  
            params = sprintf('<config><user mobile-no="%s" /><client type="PC" version="4.0.3390" platform="W5.1" /><servers version="0" /><service-no version="0" /><parameters version="0" /><hints version="0" /><http-applications version="0" /><client-config version="0" /></config>',@phone_num)  
            headers = {  
              'Content-Type' => 'application/x-www-form-urlencoded'  
            }  
            resp = http.post(uri.path, params, headers)  
            return resp.body  
        end  
          
        def SSIAppSignIn(url)     
            uri = URI.parse(url);  
            #path = uri.path + "?mobileno=" + @phone_num + "&pwd=" + @password  
    	path = "https://uid.fetion.com.cn/ssiportal/SSIAppSignInV2.aspx?mobileno="+ @phone_num + "&pwd=" + @password  
            http = Net::HTTP.new(uri.host,uri.port)  
            http.use_ssl = true  
            http.verify_mode = OpenSSL::SSL::VERIFY_NONE # turn off SSL warning  
    
            resp, xml = http.get(path, nil)
    	count = 0
    	while resp.response['set-cookie'].nil?
    	   break if count > 3
    	   sleep(1)
    	   resp,xml=http.get(path,nil)
    	   count = count+1
    	end
            @cookie = resp.response['set-cookie'].split(';')[0]  
            puts "cookie---------- #{@cookie}"  
            @ssic = @cookie.split("=")[1]  
            return xml  
        end  
      
        def get_fetion_num(xml)  
            @uri = ""  
            doc = REXML::Document.new(xml)  
            doc.elements.each("//results/user") do |element|  
              @uri = element.attribute("uri").value  
            end   
            return @uri.scan(/sip:([0-9]+)@/)[0][0]  
        end  
    end  
      
    class SIPC  
        def initialize(sipc_addr)  
            puts sipc_addr #uri = "221.176.31.33:8080"  
            uri = sipc_addr.split(":")  
            @socket = TCPSocket.new(uri[0], uri[1].to_i)  
        end  
      
        # send SIP request  
        def request(sip_request)  
            puts sip_request  
            @socket.write_nonblock(sip_request)  
            #@socket.write(sip_request)  
              
            #select,read_nonblock and rescue is the key  
            #another choice may be: http://rev.rubyforge.org/  
            IO.select [@socket]  
            res = ""  
            begin  
                while chunk = @socket.read_nonblock(4096)  
                    res = res + chunk  
                end  
            rescue  
            end  
            puts res   
            return res   
        end  
    end  
    
    
    
    

    麻烦大家也测试一下

  6. #13 楼 · 鹤惊昆仑 · 2010-07-29 22:28

    更新部分代码,ubuntu下测试通过。http://gist.github.com/498262

  7. #12 楼 · kaka2008 · 2010-07-29 13:57

    lz,搞定了。

  8. #11 楼 · qxt · 2010-07-27 10:24

    呵呵,我的天气预报也没有了。 是移动停止低版本号客户端登陆,那很简单吧,修改下版本号不就成了?

  9. #10 楼 · 鹤惊昆仑 · 2010-07-27 09:54

    移动停止了低版本电脑客户端的登陆。有空再改造下吧。难怪定时天气预报也没有了

  10. #9 楼 · qxt · 2010-07-26 08:57

    lz,今早运行程序时报错了。

    
      ./fetion.rb:103:in `SSIAppSignIn': private method `split' called for nil:NilClas
    s (NoMethodError)
            from ./fetion.rb:32:in `init'
            from ./fetion.rb:17:in `initialize'
    
    

    看了下程序,SSIAppSignIn方法中的

    
      resp, xml = http.get(path, nil) 
    
    

    有异常了 Net:HTTPNotFound 不知是网络问题呢还是飞信进行了调整

  11. #8 楼 · cana_xu · 2010-06-29 06:13

    写PHP的路过,rails的语法竟如此奇怪!

  12. #7 楼 · yydriver · 2010-06-25 14:27

    那位大大能给小弟讲讲如何开始分析一款软件?

  13. #6 楼 · 鹤惊昆仑 · 2010-02-25 19:40

    我放到google code上了(http://fetion-ruby.googlecode.com/svn/trunk/Fetion.rb)

  14. #5 楼 · googya · 2010-02-25 10:15

    感觉ruby写出来的东西就是比python写的,看起来要顺眼一点! 最近我也想写一个练习一下呢,没想到就看到你写的了!

  15. #4 楼 · shaobin0604 · 2009-09-21 01:14

    鹤惊昆仑 writes:

    大部分直接翻译自可可熊的PyFetion,协议分析使用fiddle(需要先强迫飞信走HTTP代理)简单看了看。ruby代码写的少,难入法眼,见笑,嘿嘿。

    协议分析可以使用 nathan 提供的工具 http://hi.baidu.com/nathan2007/blog/item/095a7d66088d2825aa184c20.html

  16. #3 楼 · loushizan · 2009-09-02 17:09

    这段代码还要求gbk编码,utf8编码的时候, sms_text = Iconv.iconv("UTF-8","GB2312",sms_text), 这行代码要注释掉。

  17. #2 楼 · 鹤惊昆仑 · 2009-09-02 14:29

    大部分直接翻译自可可熊的PyFetion,协议分析使用fiddle(需要先强迫飞信走HTTP代理)简单看了看。ruby代码写的少,难入法眼,见笑,嘿嘿。

  18. #1 楼 · lanjingyu · 2009-08-31 16:39

    呵呵,浏览这么多咋没人说句话呢?

评论功能维护中,当前仅展示历史评论。