Class: Pcapr
- Inherits:
-
Object
- Object
- Pcapr
- Defined in:
- lib/pcapr/pcapr.rb,
lib/pcapr/version.rb
Constant Summary collapse
- VERSION =
"0.0.3"
Instance Attribute Summary collapse
-
#logger ⇒ Object
Returns the value of attribute logger.
Instance Method Summary collapse
-
#initialize(user, pass, logger = Logger.new(STDOUT)) ⇒ Pcapr
constructor
A new instance of Pcapr.
- #login ⇒ Object
-
#pcap_file(pcap_url, file) ⇒ Object
获取该数据包文件.
- #pcap_urls(proto) ⇒ Object
-
#protos ⇒ Object
获取协议内容.
- #run(dir) ⇒ Object
Constructor Details
#initialize(user, pass, logger = Logger.new(STDOUT)) ⇒ Pcapr
Returns a new instance of Pcapr.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/pcapr/pcapr.rb', line 9 def initialize( user,pass, logger = Logger.new(STDOUT) ) @user = user @pass = pass @logger = logger #驱动浏览器底层的接口, patron对象 @driver = Patron::Session.new @driver.timeout = 10000 @driver.base_url = "http://www.pcapr.net" @driver. @protos = nil end |
Instance Attribute Details
#logger ⇒ Object
Returns the value of attribute logger.
24 25 26 |
# File 'lib/pcapr/pcapr.rb', line 24 def logger @logger end |
Instance Method Details
#login ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/pcapr/pcapr.rb', line 26 def login #获取uuid login_html = @driver.get("/account/login") uuid = Nokogiri::HTML(login_html.body).css('#uuid')[0]['value'] login_result = @driver.post("/account/login", {:_user=>@user ,:pass=>@pass, :uuid=>"#{uuid}", :_auth=> auth_md5(@user,@pass,uuid)}) raise "login fail" if login_result.url.include?("/account/login") end |
#pcap_file(pcap_url, file) ⇒ Object
获取该数据包文件
53 54 55 56 57 |
# File 'lib/pcapr/pcapr.rb', line 53 def pcap_file(pcap_url, file) @driver.get(pcap_url) #~ file = @driver.get_file("/view/download", "d:/ok.pcap", "Referer"=>@driver.base_url + pcap_url) file = @driver.get_file("/view/download", file) end |
#pcap_urls(proto) ⇒ Object
46 47 48 49 50 |
# File 'lib/pcapr/pcapr.rb', line 46 def pcap_urls(proto) #TODO url = @driver.get("/browse?proto=#{proto}").body Nokogiri::HTML(url).css("ul#p-main div.p-body>a").collect { |link| link['href'] } end |
#protos ⇒ Object
获取协议内容
36 37 38 39 40 41 42 43 44 |
# File 'lib/pcapr/pcapr.rb', line 36 def protos return @protos if @protos protos_html = @driver.get("/browse/protos").body #获取协议内容 raise "get protos fail,maybe this code is out of update" unless protos_html.match(/var raw = \(\{(.*)\}\)/) #格式为xx:1,xxx:2 protos_str = $1 @protos = str2protos(protos_str) end |
#run(dir) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/pcapr/pcapr.rb', line 59 def run(dir) base_dir = dir login protos.each do |proto| proto_dir = File.join(base_dir, proto.tr("\\/:*?\"<>|"," ")) FileUtils.mkdir_p(proto_dir) unless File.directory?(proto_dir) logger.info "proto: #{proto}, downloading...(pcap save as: #{proto_dir}" pcap_urls(proto).each do |pcap_url| file = File.join( proto_dir, File.basename(pcap_url).gsub(/\.html$/,"").tr("\\/:*?\"<>|"," ") ) logger.info " pcap file: #{file} save at '#{file}'" begin pcap_file(pcap_url, file) logger.debug " save ok" rescue =>e logger.error " save fail: #{$!}" end end end end |