Class: Pcapr

Inherits:
Object
  • Object
show all
Defined in:
lib/pcapr/pcapr.rb,
lib/pcapr/version.rb

Constant Summary collapse

VERSION =
"0.0.7"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, pass, logger = Logger.new(STDOUT)) ⇒ Pcapr

Returns a new instance of Pcapr.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/pcapr/pcapr.rb', line 11

def initialize( user,pass, logger = Logger.new(STDOUT) )
  @user = user
  @pass = pass
  
  @logger = logger
  
  #驱动浏览器底层的接口, patron对象
  @driver = Patron::Session.new
  @driver.timeout = 60 * 60 # 1 hour
  @driver.connect_timeout = 10000
  @driver.base_url = "http://www.pcapr.net"
  @driver.handle_cookies
  
  @protos = nil
end

Instance Attribute Details

#driverObject (readonly)

for spec test



28
29
30
# File 'lib/pcapr/pcapr.rb', line 28

def driver
  @driver
end

#loggerObject

Returns the value of attribute logger.



30
31
32
# File 'lib/pcapr/pcapr.rb', line 30

def logger
  @logger
end

Instance Method Details

#loginObject



32
33
34
35
36
37
38
39
# File 'lib/pcapr/pcapr.rb', line 32

def 
  #获取uuid
   = @driver.get("/account/login")
  uuid = Nokogiri::HTML(.body).css('#uuid')[0]['value']
  
   = @driver.post("/account/login", {:_user=>@user ,:pass=>@pass, :uuid=>"#{uuid}", :_auth=> auth_md5(@user,@pass,uuid)})
  raise "login fail" if .url.include?("/account/login")
end

#pcap_file(pcap_url, file) ⇒ Object

获取该数据包文件



67
68
69
70
71
72
73
74
# File 'lib/pcapr/pcapr.rb', line 67

def pcap_file(pcap_url, file)
  # set cookie
  @driver.get(pcap_url)
  res = @driver.get("/view/download")
  File.open(file,"wb") do |f|
    f.write(res.body)
  end
end

#pcap_urls(proto) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/pcapr/pcapr.rb', line 52

def pcap_urls(proto)
  ret = []
  proto_url = URI.encode("/browse?proto=#{proto}")
  url = @driver.get(proto_url).body
  nokogiri_parser = Nokogiri::HTML(url)
  ret += nokogiri_parser.css("ul#p-main div.p-body>a").collect { |link| link['href'] }
  if nokogiri_parser.css('li.p-overflow a').size > 0
    href = nokogiri_parser.css('li.p-overflow a').attr('href').value
    url = @driver.get( "/browse" + href.gsub(" ","%20") ).body
    ret += Nokogiri::HTML(url).css('li.l0 div.p-body>a').collect { |link| link['href'] }
  end
  ret
end

#protosObject

获取协议内容



42
43
44
45
46
47
48
49
50
# File 'lib/pcapr/pcapr.rb', line 42

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



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/pcapr/pcapr.rb', line 76

def run(dir)
  base_dir = dir
  
  protos.each do |proto|
    proto_dir = proto2dir_and_create(base_dir, proto)
    logger.info "proto: #{proto}, downloading...(pcap save as: #{proto_dir}"
    pcap_urls(proto).each do |pcap_url|
      file = pcap2file(proto_dir, pcap_url)
      logger.info "  pcap file: #{pcap_url} save at '#{file}'"
      begin
        pcap_file(pcap_url, file)
        logger.debug "  save ok"
      rescue Patron::TimeoutError
        logger.error "  save fail: timeout after #{@driver.timeout} seconds"
      rescue =>e
        logger.error "  save fail: #{$!}"
      end
    end
  end
end