pcapr 0.0.4 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,6 +3,7 @@ require 'nokogiri'
3
3
  require 'patron'
4
4
  require 'digest'
5
5
  require 'timeout'
6
+ require 'uri'
6
7
 
7
8
  require 'fileutils'
8
9
 
@@ -15,13 +16,17 @@ class Pcapr
15
16
 
16
17
  #驱动浏览器底层的接口, patron对象
17
18
  @driver = Patron::Session.new
18
- @driver.timeout = 10000
19
+ @driver.timeout = 60 * 60 # 1 hour
20
+ @driver.connect_timeout = 10000
19
21
  @driver.base_url = "http://www.pcapr.net"
20
22
  @driver.handle_cookies
21
23
 
22
24
  @protos = nil
23
25
  end
24
26
 
27
+ # for spec test
28
+ attr_reader :driver
29
+
25
30
  attr_accessor :logger
26
31
 
27
32
  def login
@@ -45,9 +50,17 @@ class Pcapr
45
50
  end
46
51
 
47
52
  def pcap_urls(proto)
48
- #TODO
49
- url = @driver.get("/browse?proto=#{proto}").body
50
- Nokogiri::HTML(url).css("ul#p-main div.p-body>a").collect { |link| link['href'] }
53
+ ret = []
54
+ proto_url = URI.encode("/browse?proto=#{proto}")
55
+ url = @driver.get(proto_url).body
56
+ nokogiri_parser = Nokogiri::HTML(url)
57
+ ret += nokogiri_parser.css("ul#p-main div.p-body>a").collect { |link| link['href'] }
58
+ if nokogiri_parser.css('li.p-overflow a').size > 0
59
+ href = nokogiri_parser.css('li.p-overflow a').attr('href').value
60
+ url = @driver.get( "/browse" + href.gsub(" ","%20") ).body
61
+ ret += Nokogiri::HTML(url).css('li.l0 div.p-body>a').collect { |link| link['href'] }
62
+ end
63
+ ret
51
64
  end
52
65
 
53
66
  #获取该数据包文件
@@ -69,16 +82,14 @@ class Pcapr
69
82
  logger.info "proto: #{proto}, downloading...(pcap save as: #{proto_dir}"
70
83
  pcap_urls(proto).each do |pcap_url|
71
84
  file = File.join( proto_dir, File.basename(pcap_url).gsub(/\.html$/,"").tr("\\/:*?\"<>|"," ") )
72
- logger.info " pcap file: #{file} save at '#{file}'"
85
+ logger.info " pcap file: #{pcap_url} save at '#{file}'"
73
86
  begin
74
- Timeout.timeout(60 * 60 * 2) do
75
- pcap_file(pcap_url, file)
76
- end
87
+ pcap_file(pcap_url, file)
77
88
  logger.debug " save ok"
89
+ rescue Patron::TimeoutError
90
+ logger.error " save fail: timeout after #{@driver.timeout} seconds"
78
91
  rescue =>e
79
92
  logger.error " save fail: #{$!}"
80
- rescue Timeout::Error
81
- logger.error " save fail: timeout in 2 hours"
82
93
  end
83
94
  end
84
95
  end
@@ -1,3 +1,3 @@
1
1
  class Pcapr
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -8,8 +8,8 @@ Gem::Specification.new do |s|
8
8
  s.authors = ["yafei LI"]
9
9
  s.email = ["lyfi2003@gmail.com"]
10
10
  s.homepage = ""
11
- s.summary = %q{a libarily for downloading all pcap files from pcapr.net}
12
- s.description = %q{a libarily for downloading all pcap files from pcapr.net}
11
+ s.summary = %q{a library for downloading all pcap files from pcapr.net}
12
+ s.description = %q{a library for downloading all pcap files from pcapr.net}
13
13
 
14
14
  s.rubyforge_project = "pcapr"
15
15
 
@@ -35,11 +35,55 @@ describe Pcapr do
35
35
  @o.protos.should be_include("m2pa (id 12)")
36
36
  end
37
37
 
38
+ it "should get pcapfile urls when proto include quote" do
39
+ @o.login
40
+ urls = @o.pcap_urls("afs (rx)")
41
+ urls.size.should >= 7
42
+ urls.should be_include("/view/tyson.key/2009/10/0/6/LiquidWar_Lobby_1_00002_20091101140004.pcap.html")
43
+ urls = @o.pcap_urls("fc els")
44
+ urls.size.should > 0
45
+ urls = @o.pcap_urls("gtp <ftp>")
46
+ urls.size.should > 0
47
+ end
48
+
49
+ it "should support more pcap urls" do
50
+ @o.login
51
+ urls = @o.pcap_urls("dns")
52
+ urls.size.should == 34
53
+ end
54
+
38
55
  it "all_proto should get auth.cap" do
39
56
  @o.login
40
57
  @o.pcap_urls("dns").should be_include("/view/siim/2011/11/3/7/capture.pcap.html")
41
58
  end
42
59
 
60
+ #~ it "should have pcap files more than 3000(excute slow)" do
61
+ #~ @o.login
62
+ #~ files = []
63
+ #~ @o.protos.each_with_index do |proto,i|
64
+ #~ @o.logger.info("proto: #{proto}, index=#{i}")
65
+ #~ files += @o.pcap_urls(proto)
66
+ #~ end
67
+ #~ files.should >= 3085
68
+ #~ end
69
+
70
+ it "file get it but timeout" do
71
+ begin
72
+ @o.login
73
+ pcap_url = "/view/sudhakar_gajjala/2010/6/1/21/6462.pcap.html"
74
+ file = File.join($helper_dir, 'timeout.pcap')
75
+ @o.driver.timeout = 1
76
+ lambda { @o.pcap_file(pcap_url,file) }.should raise_error(Patron::TimeoutError)
77
+ ensure
78
+ @o.driver.timeout = 60*60
79
+ File.delete(file) if File.exist?(file)
80
+ end
81
+ end
82
+
83
+ it "driver timeout default is 1 hour" do
84
+ @o.driver.timeout.should == 60*60
85
+ end
86
+
43
87
  it "file get it " do
44
88
  @o.login
45
89
  pcap_url = "/view/siim/2011/11/3/7/capture.pcap.html"
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 4
9
- version: 0.0.4
8
+ - 6
9
+ version: 0.0.6
10
10
  platform: ruby
11
11
  authors:
12
12
  - yafei LI
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2012-03-12 00:00:00 +08:00
17
+ date: 2012-03-14 00:00:00 +08:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: "0"
44
44
  type: :runtime
45
45
  version_requirements: *id002
46
- description: a libarily for downloading all pcap files from pcapr.net
46
+ description: a library for downloading all pcap files from pcapr.net
47
47
  email:
48
48
  - lyfi2003@gmail.com
49
49
  executables:
@@ -96,7 +96,7 @@ rubyforge_project: pcapr
96
96
  rubygems_version: 1.3.7
97
97
  signing_key:
98
98
  specification_version: 3
99
- summary: a libarily for downloading all pcap files from pcapr.net
99
+ summary: a library for downloading all pcap files from pcapr.net
100
100
  test_files:
101
101
  - spec/pcapr_spec.rb
102
102
  - spec/spec_helper.rb