forked from thom4parisot/crx
-
Notifications
You must be signed in to change notification settings - Fork 1
/
crx.js
189 lines (145 loc) · 4.59 KB
/
crx.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
var fs = require("fs")
, join = require("path").join
, crypto = require("crypto")
, child = require("child_process")
, spawn = child.spawn
, exec = child.exec
module.exports = new function() {
function ChromeExtension(attrs) {
for (var name in attrs) this[name] = attrs.name
this.path = join("/tmp", "crx-" + (Math.random() * 1e17).toString(36))
}
ChromeExtension.prototype = this
this.destroy = function() {
spawn("rm", ["-rf", this.path])
}
this.pack = function(cb) {
this.loadManifest(function(err) {
if (err) return cb(err)
this.generatePublicKey(function(err) {
if (err) return cb(err)
this.loadContents(function(err) {
if (err) return cb(err)
this.generateSignature()
this.generatePackage()
cb.call(this, null, this.package)
})
})
})
}
this.load = function(path, cb) {
fs.stat(path, function(err, stat) {
if (stat.isDirectory()) this.loadFromDir(path, cb)
else if (stat.isFile()) this.loadFromFile(path, cb)
}.bind(this))
}
this.loadFromDir = function(path, cb) {
spawn("cp", ["-R", path, this.path]).on("exit", cb.bind(this))
}
this.readFile = function(name, cb) {
var path = join(this.path, name)
fs.readFile(path, "binary", function(err, data) {
if (err) return cb.call(this, err)
cb.call(this, null, this[name] = data)
}.bind(this))
}
this.writeFile = function(path, data, cb) {
path = join(this.path, path)
fs.writeFile(path, data, function(err, data) {
if (err) return cb.call(this, err)
cb.call(this)
}.bind(this))
}
this.loadFromFile = function(path, cb) {
fs.readFile(path, function(err, data) {
if (err) return cb.call(this, err)
path = this.path + ".zip"
data = data.slice(16 + crx[8] + crx[12])
fs.writeFile(path, data, function(err) {
if (err) return cb.call(this, err)
spawn("unzip", [path], {dir: this.path}, function() {
fs.unlink(path)
cb.call(this)
})
}.bind(this))
}.bind(this))
}
this.loadManifest = function(cb) {
this.readFile("manifest.json", function(err, data) {
if (!err) {
try { this.manifest = JSON.parse(data.toString()) }
catch (e) { err = e }
}
err
? cb.call(this, err)
: cb.call(this, null, this.manifest)
})
}
this.generatePublicKey = function(cb) {
var rsa = spawn("openssl", ["rsa", "-pubout", "-outform", "DER"])
rsa.stdout.on("data", function(data) {
this.publicKey = data
cb && cb.call(this, null, this)
}.bind(this))
rsa.stdin.end(this.privateKey)
}
this.generateSignature = function() {
return this.signature = new Buffer(
crypto
.createSign("sha1")
.update(this.contents)
.sign(this.privateKey),
"binary"
)
}
this.loadContents = function(cb) {
var command = "zip -qr -9 -X - . -x key.pem"
, options = {cwd: this.path, encoding: "binary"}
exec(command, options, function(err, data) {
if (err) return cb.call(this, err)
this.contents = new Buffer(data, "binary")
cb.call(this)
}.bind(this))
}
this.generatePackage = function() {
var signature = this.signature
, publicKey = this.publicKey
, contents = this.contents
, keyLength = publicKey.length
, sigLength = signature.length
, zipLength = contents.length
, length = 16 + keyLength + sigLength + zipLength
, crx = new Buffer(length)
crx.write("Cr24" + Array(13).join("\x00"), "binary")
crx[4] = 2
crx[8] = keyLength
crx[12] = sigLength
publicKey.copy(crx, 16)
signature.copy(crx, 16 + keyLength)
contents.copy(crx, 16 + keyLength + sigLength)
return this.package = crx
}
this.generateAppId = function() {
return this.appId = crypto
.createHash("sha256")
.update(this.publicKey)
.digest("hex")
.slice(0, 32)
.replace(/./g, function(x) {
return (parseInt(x, 16) + 10).toString(26)
})
}
this.generateUpdateXML = function() {
if (!this.codebase) throw new Error("No URL provided for update.xml.")
return this.updateXML =
Buffer(
"<?xml version='1.0' encoding='UTF-8'?>\n" +
"<gupdate xmlns='http://www.google.com/update2/response' protocol='2.0'>\n" +
" <app appid='" + this.generateAppId() + "'>\n" +
" <updatecheck codebase='" + this.codebase + "' version='" + this.manifest.version + "' />\n" +
" </app>\n" +
"</gupdate>"
)
}
return ChromeExtension
}