43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
# File 'lib/bnicovideo/user_session/mac_os_x.rb', line 43
def self.init_from_safari
cookie_base_path = File.join(ENV['HOME'], 'Library', 'Cookies')
bcpath = File.join(cookie_base_path, 'Cookies.binarycookies')
if File.exist?(bcpath)
cookie_path = bcpath
bin = nil
File.open(cookie_path, 'rb') do |file|
bin = file.read
end
raise 'Invalid Cookie file' unless bin[0..3] == 'cook'
page_num = bin[4..7].unpack('N')[0]
pages_length = []
page_num.times do |i|
page_length = bin[(8 + i * 4)..(11 + i * 4)].unpack('N')[0]
pages_length.push(page_length)
end
read_ptr = 8 + page_num * 4
pages_length.each do |pgl|
page = bin[read_ptr..(read_ptr + pgl - 1)]
cookies_num = page[4..7].unpack('V')[0]
nread_ptr = read_ptr
cookies_offset = []
cookies_num.times do |i|
cookie_offset = page[(8 + i * 4)..(11 + i * 4)].unpack('V')[0]
cookies_offset.push(cookie_offset)
end
nread_ptr += 12 + cookies_num * 4
read_ptr += pgl
cookies_offset.each do |cof|
cookie_length = page[cof..(cof + 3)].unpack('V')[0]
cookie_bin = page[cof..(cof + cookie_length - 1)]
offset_url = page[(cof + 16)..(cof + 19)].unpack('V')[0]
offset_name = page[(cof + 20)..(cof + 23)].unpack('V')[0]
offset_path = page[(cof + 24)..(cof + 27)].unpack('V')[0]
offset_value = page[(cof + 28)..(cof + 31)].unpack('V')[0]
url_end = (/\x00/ =~ cookie_bin[offset_url..-1])
url = cookie_bin[offset_url, url_end]
name_end = (/\x00/ =~ cookie_bin[offset_name..-1])
name = cookie_bin[offset_name, name_end]
path_end = (/\x00/ =~ cookie_bin[offset_path..-1])
path = cookie_bin[offset_path, path_end]
value_end = (/\x00/ =~ cookie_bin[offset_value..-1])
value = cookie_bin[offset_value, value_end]
return value if (/nicovideo\.jp$/ =~ url) && (name == 'user_session')
end
end
return nil
else
xml = REXML::Document.new(File.read(File.join(cookie_base_path, 'Cookies.plist')))
xml.root.elements.each('array/dict') do |elem|
if elem.elements['key[text()="Domain"]'] &&
elem.elements['key[text()="Domain"]'].next_element &&
elem.elements['key[text()="Domain"]'].next_element.text == '.nicovideo.jp' &&
elem.elements['key[text()="Name"]'] &&
elem.elements['key[text()="Name"]'].next_element &&
elem.elements['key[text()="Name"]'].next_element.text == 'user_session'
return elem.elements['key[text()="Value"]'].next_element.text
end
end
return nil
end
end
|