pcapr 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ helper/me/*
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "nokogiri"
4
+
5
+ gem "patron"
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
data/bin/pcapr ADDED
@@ -0,0 +1,14 @@
1
+ require 'pcapr'
2
+ if ARGV[0] == "-h" or ARGV[0] == "--help" or ARGV.size < 2
3
+ puts "Usage: #{File.basename($0)} user pass [dir]"
4
+ exit 0
5
+ end
6
+
7
+ user,pass,file = ARGV[0],ARGV[1],ARGV[2]
8
+
9
+ file = Dir.pwd if file.nil?
10
+ s = Time.now
11
+ Pcapr.new(user,pass).run(file)
12
+ b = Time.now
13
+ puts
14
+ puts "Complete! cost time: #{b-s} seconds."
data/helper/base.pcap ADDED
Binary file
data/lib/pcapr.rb ADDED
@@ -0,0 +1,2 @@
1
+ require "pcapr/version"
2
+ require "pcapr/pcapr"
@@ -0,0 +1,96 @@
1
+ require 'logger'
2
+ require 'nokogiri'
3
+ require 'patron'
4
+ require 'digest'
5
+
6
+ require 'fileutils'
7
+
8
+ class Pcapr
9
+ def initialize( user,pass, logger = Logger.new(STDOUT) )
10
+ @user = user
11
+ @pass = pass
12
+
13
+ @logger = logger
14
+
15
+ #驱动浏览器底层的接口, patron对象
16
+ @driver = Patron::Session.new
17
+ @driver.timeout = 10000
18
+ @driver.base_url = "http://www.pcapr.net"
19
+ @driver.handle_cookies
20
+
21
+ @protos = nil
22
+ end
23
+
24
+ attr_accessor :logger
25
+
26
+ def login
27
+ #获取uuid
28
+ login_html = @driver.get("/account/login")
29
+ uuid = Nokogiri::HTML(login_html.body).css('#uuid')[0]['value']
30
+
31
+ login_result = @driver.post("/account/login", {:_user=>@user ,:pass=>@pass, :uuid=>"#{uuid}", :_auth=> auth_md5(@user,@pass,uuid)})
32
+ raise "login fail" if login_result.url.include?("/account/login")
33
+ end
34
+
35
+ #获取协议内容
36
+ def protos
37
+ return @protos if @protos
38
+ protos_html = @driver.get("/browse/protos").body
39
+ #获取协议内容
40
+ raise "get protos fail,maybe this code is out of update" unless protos_html.match(/var raw = \(\{(.*)\}\)/)
41
+ #格式为xx:1,xxx:2
42
+ protos_str = $1
43
+ @protos = str2protos(protos_str)
44
+ end
45
+
46
+ def pcap_urls(proto)
47
+ #TODO
48
+ url = @driver.get("/browse?proto=#{proto}").body
49
+ Nokogiri::HTML(url).css("ul#p-main div.p-body>a").collect { |link| link['href'] }
50
+ end
51
+
52
+ #获取该数据包文件
53
+ def pcap_file(pcap_url, file)
54
+ @driver.get(pcap_url)
55
+ #~ file = @driver.get_file("/view/download", "d:/ok.pcap", "Referer"=>@driver.base_url + pcap_url)
56
+ file = @driver.get_file("/view/download", file)
57
+ end
58
+
59
+ def run(dir)
60
+ base_dir = dir
61
+ login
62
+ protos.each do |proto|
63
+ proto_dir = File.join(base_dir, proto)
64
+ proto_dir.tr!("<>","")
65
+ FileUtils.mkdir_p(proto_dir) unless File.directory?(proto_dir)
66
+ logger.info "proto: #{proto}, downloading...(pcap save as: #{proto_dir}"
67
+ pcap_urls(proto).each do |pcap_url|
68
+ file = File.join(proto_dir, File.basename(pcap_url).gsub(/\.html$/,""))
69
+ logger.info " pcap file: #{file} save at '#{file}'"
70
+ begin
71
+ pcap_file(pcap_url, file)
72
+ logger.debug " save ok"
73
+ rescue Exception
74
+ logger.error " save fail: #{$!}"
75
+ end
76
+ end
77
+ end
78
+ end
79
+
80
+
81
+ private
82
+ def auth_md5(user,pass,uuid)
83
+ str = user + '-' + pass
84
+ md5 = Digest::MD5.hexdigest str
85
+ Digest::MD5.hexdigest(md5 + uuid)
86
+ end
87
+
88
+ def str2protos(str)
89
+ str.split(',').collect do |kv|
90
+ kv.split(':')[0].gsub("'","").strip
91
+ end
92
+ end
93
+
94
+
95
+
96
+ end
@@ -0,0 +1,3 @@
1
+ class Pcapr
2
+ VERSION = "0.0.2"
3
+ end
data/pcapr.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "pcapr/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "pcapr"
7
+ s.version = Pcapr::VERSION
8
+ s.authors = ["yafei LI"]
9
+ s.email = ["lyfi2003@gmail.com"]
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}
13
+
14
+ s.rubyforge_project = "pcapr"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_dependency("nokogiri")
22
+ s.add_dependency("patron")
23
+ end
data/readme.md ADDED
@@ -0,0 +1,20 @@
1
+ Pcapr
2
+ --------
3
+
4
+ Just download all pcap files from www.pcapr.net, it's all!
5
+
6
+ ----
7
+
8
+ **How to use?**
9
+
10
+
11
+ ```
12
+ gem install pcapr
13
+
14
+ pcapr myuser@mail.com password some/where/save/files
15
+ ```
16
+
17
+ ----
18
+
19
+ *Have fun!*
20
+
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+
3
+ describe Pcapr do
4
+ before(:each) do
5
+ user= "bocycn@gmail.com"
6
+ pass="sinfor"
7
+ @o = Pcapr.new(user,pass)
8
+ end
9
+ it "should login success" do
10
+ lambda { @o.login }.should_not raise_error
11
+ end
12
+
13
+ it "should login fail" do
14
+ user= "bocycn1@gmail.com"
15
+ pass="sinfor"
16
+ @o = Pcapr.new(user,pass)
17
+ lambda { @o.login }.should raise_error
18
+ end
19
+
20
+ it "protos should get right" do
21
+ @o.login
22
+ @o.protos.should be_include("dns")
23
+ @o.protos.should be_include("gtp <ftp>")
24
+ @o.protos.should be_include("http/xml")
25
+ @o.protos.should be_include("ieee 802.15.4")
26
+ @o.protos.should be_include("ipx rip")
27
+ @o.protos.should be_include("megaco/sdp/sdp")
28
+ @o.protos.should be_include("m2pa (id 12)")
29
+ end
30
+
31
+ it "all_proto should get auth.cap" do
32
+ @o.login
33
+ @o.pcap_urls("dns").should be_include("/view/siim/2011/11/3/7/capture.pcap.html")
34
+ end
35
+
36
+ it "file get it " do
37
+ @o.login
38
+ pcap_url = "/view/siim/2011/11/3/7/capture.pcap.html"
39
+ file = File.join($helper_dir,'test.pcap')
40
+ base = File.join($helper_dir,'base.pcap')
41
+ File.delete(file) if File.exist?(file)
42
+ @o.pcap_file(pcap_url,file)
43
+ File.should be_exist(file)
44
+ #~ File.size(file).should == File.size(base)
45
+ File.delete(file) if File.exist?(file)
46
+ end
47
+
48
+ it "run try" do
49
+ @o.run( File.join($helper_dir,"me") )
50
+ end
51
+
52
+
53
+ end
@@ -0,0 +1,3 @@
1
+ $LOAD_PATH.unshift( File.dirname(__FILE__), '..', 'lib' )
2
+ $helper_dir = File.join(File.dirname(__FILE__),'..','helper')
3
+ require 'pcapr'
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pcapr
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 2
9
+ version: 0.0.2
10
+ platform: ruby
11
+ authors:
12
+ - yafei LI
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2012-03-12 00:00:00 +08:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: nokogiri
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ type: :runtime
32
+ version_requirements: *id001
33
+ - !ruby/object:Gem::Dependency
34
+ name: patron
35
+ prerelease: false
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 0
43
+ version: "0"
44
+ type: :runtime
45
+ version_requirements: *id002
46
+ description: a libarily for downloading all pcap files from pcapr.net
47
+ email:
48
+ - lyfi2003@gmail.com
49
+ executables:
50
+ - pcapr
51
+ extensions: []
52
+
53
+ extra_rdoc_files: []
54
+
55
+ files:
56
+ - .gitignore
57
+ - Gemfile
58
+ - Rakefile
59
+ - bin/pcapr
60
+ - helper/base.pcap
61
+ - lib/pcapr.rb
62
+ - lib/pcapr/pcapr.rb
63
+ - lib/pcapr/version.rb
64
+ - pcapr.gemspec
65
+ - readme.md
66
+ - spec/pcapr_spec.rb
67
+ - spec/spec_helper.rb
68
+ has_rdoc: true
69
+ homepage: ""
70
+ licenses: []
71
+
72
+ post_install_message:
73
+ rdoc_options: []
74
+
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ segments:
83
+ - 0
84
+ version: "0"
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ segments:
91
+ - 0
92
+ version: "0"
93
+ requirements: []
94
+
95
+ rubyforge_project: pcapr
96
+ rubygems_version: 1.3.7
97
+ signing_key:
98
+ specification_version: 3
99
+ summary: a libarily for downloading all pcap files from pcapr.net
100
+ test_files:
101
+ - spec/pcapr_spec.rb
102
+ - spec/spec_helper.rb