forked from tateisu/PushToFCM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
376 lines (323 loc) · 9.14 KB
/
index.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
'use strict'
const util = require('util')
const crypto = require('crypto')
const npmlog = require('npmlog')
const Koa = require('koa')
const KoaBody = require('koa-body')
const getRawBody = require('raw-body')
const Sequelize = require('sequelize')
const axios = require('axios')
const base64us = require('urlsafe-base64')
const asn = require('asn1.js')
const jwt = require('jsonwebtoken')
const fcmServerKey = process.env.FCM_SERVER_KEY
if(! fcmServerKey) throw new Error("missing FCM_SERVER_KEY in .env")
process.on('unhandledRejection', console.dir);
npmlog.info(`database: ${process.env.DB_DIALECT} ${process.env.DB_HOST} ${process.env.DB_PORT} ${process.env.DB_NAME} ${process.env.DB_USER}`)
const sequelize = new Sequelize(
process.env.DB_NAME,
process.env.DB_USER,
process.env.DB_PASS,
{
dialect: process.env.DB_DIALECT,
host: process.env.DB_HOST,
port: process.env.DB_PORT,
operatorsAliases: false,
logging: (a,b,c,file,dir)=>{
//const args = Array.from(arguments)
//npmlog.info(`logging ${dir} ${file}`)
//npmlog.info(`SQL Log: ${a}` )
}
}
)
const WebPushTokenCheck = sequelize.define('webpush_token_check', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
},
tokenDigest: {
type: Sequelize.STRING,
allowNull: false,
},
installId: {
type: Sequelize.STRING,
allowNull: false,
},
createdAt: {
type: Sequelize.DATE,
defaultValue: Sequelize.NOW,
allowNull: false,
},
updatedAt: {
type: Sequelize.DATE,
defaultValue: Sequelize.NOW,
allowNull: false,
},
}, {
indexes: [
{
name: 'webpush_token_check_token',
unique: true,
fields: ['tokenDigest']
}
]
})
const ServerKey = sequelize.define('webpush_server_key2', {
clientId: {
type: Sequelize.STRING,
allowNull: false,
},
serverKey: {
type: Sequelize.STRING,
allowNull: false,
},
},{
indexes: [
{
name: 'webpush_server_key2_unique',
unique: true,
fields: ['clientId']
}
]
})
const body_normal = KoaBody({
multipart: true
})
const body_raw = async (ctx,next) =>{
ctx.request.body = await getRawBody(ctx.req,{
limit: '40kb',
})
await next()
}
async function serverKeyUpdate(ctx,m){
return await body_normal(ctx,async()=>{
const client_id = ctx.request.body.client_id
const server_key = ctx.request.body.server_key
const user_agent = ctx.get('User-Agent')
npmlog.info(`serverKeyUpdate client_id=${client_id}, server_key=${server_key},user_agent=${user_agent}`)
if( !client_id) ctx.throw(422,`missing parameter 'client_id'`)
if( !server_key ) ctx.throw(422,`missing parameter 'server_key'`)
const created = await ServerKey.upsert({
clientId: client_id,
serverKey: server_key
})
npmlog.info(`created=${created}`)
ctx.status = 200
})
}
async function tokenCheck(ctx,m){
return await body_normal(ctx,async()=>{
const token_digest=ctx.request.body.token_digest
const install_id=ctx.request.body.install_id
npmlog.info(`check token_digest=${token_digest},install_id=${install_id}`)
if( !token_digest ) ctx.throw(422,`missing parameter 'token_digest'`)
if( !install_id ) ctx.throw(422,`missing parameter 'install_id'`)
const rows = await WebPushTokenCheck.findOrCreate({
where: {
tokenDigest: token_digest
},
defaults: {
installId: install_id
}
})
if( rows == null || rows.length == 0 ){
ctx.throw(500,`findOrCreate() returns null or empty.`)
}
let row = rows[0]
npmlog.info(`row tokenDigest=${row.tokenDigest}, installId=${row.installId}, updatedAt=${row.updatedAt}`)
if( install_id != row.installId ){
ctx.status=403
ctx.message=`installId not match.`
}else{
const affected = await WebPushTokenCheck.update({
updatedAt: sequelize.literal('CURRENT_TIMESTAMP')
},{
where:{
id: row.id
}
})
if( affected[0] != 1){
npmlog.info(`row update? affected=${affected[0]}`)
}
ctx.status = 200
}
})
}
function decodeBase64(src){
return new Buffer(src,'base64')
}
// ECDSA public key ASN.1 format
const ECPublicKey = asn.define("PublicKey", function() {
this.seq().obj(
this.key("algorithm").seq().obj(
this.key("id").objid(),
this.key("curve").objid()
),
this.key("pub").bitstr()
);
});
// convert public key from p256ecdsa to PEM
function getPemFromPublicKey(public_key){
return ECPublicKey.encode({
algorithm: {
id: [1, 2, 840, 10045, 2, 1], // :id-ecPublicKey
curve: [1,2,840,10045,3,1,7] // prime256v1
},
pub: {
// このunused により bitstringの先頭に 00 が置かれる。
// 先頭の00 04 が uncompressed を示す
// https://tools.ietf.org/html/rfc5480#section-2.3.2
// http://www.secg.org/sec1-v2.pdf section 2.3.3
unused: 0,
data: public_key,
},
}, "pem", {label: "PUBLIC KEY"})
}
const reAuthorizationWebPush = new RegExp("^WebPush\\s+(\\S+)")
const reCryptoKeySignPublicKey = new RegExp("p256ecdsa=([^;\\s]+)")
function verifyServerKey(ctx, savedServerKey){
const auth_header = ctx.get('Authorization')
const crypto_key = ctx.get('Crypto-Key')
if( auth_header && crypto_key ){
let m = reAuthorizationWebPush.exec( auth_header )
if( !m ){
console.log("header not match: Authorization")
}else{
const token = m[1]
m = reCryptoKeySignPublicKey.exec(crypto_key)
if( !m ){
console.log("header not match: Crypto-Key")
}else{
const public_key = decodeBase64(m[1])
const saved_key = decodeBase64(savedServerKey)
if( 0 != Buffer.compare( public_key, saved_key) ){
ctx.throw(400,"server_key not match.")
}
const pem = getPemFromPublicKey(public_key)
try{
jwt.verify(token, Buffer.from(pem), { algorithms: ['ES256'] })
return true
}catch(err){
ctx.throw(503,`JWT verify failed. ${err}`)
}
}
}
}
ctx.throw(400,"missing JWT signature.")
return false
}
async function pushCallback(ctx,m){
return await body_raw(ctx,async()=>{
const params = m[1].split('/').map( x => decodeURIComponent(x) )
const device_id = params[0]
const acct = params[1]
const flags = params[2] // may null, not used
const client_id = params[3] // may null
const body = ctx.request.body
npmlog.info(`callback device_id=${device_id},acct=${acct},body=${body.length}bytes`)
if( client_id ){
const row = await ServerKey.findOne({
where:{
clientId: client_id
}
})
if( row == null ){
npmlog.info(`missing serverkey for client_id=${client_id}`)
}else{
if(!verifyServerKey(ctx, row.serverKey)){
npmlog.error("verifyServerKey failed.")
return
}
}
}
try{
const firebaseMessage = {
to: device_id,
priority: 'high',
data: {
acct: acct,
}
}
const response = await axios.post(
'https://fcm.googleapis.com/fcm/send',
JSON.stringify(firebaseMessage),
{
headers: {
'Authorization': `key=${fcmServerKey}`,
'Content-Type': 'application/json'
}
}
)
npmlog.info(`sendToFCM: status=${response.status} ${JSON.stringify(response.data)}`)
if (response.data.failure === 0 && response.data.canonical_ids === 0) {
ctx.status = 201
return
}
response.data.results.forEach(result => {
if (result.message_id && result.registration_id) {
// デバイストークンが更新された
// この購読はキャンセルされるべき
ctx.status = 410
}else if( result.error == 'NotRegistered' ){
ctx.status = 410
}else{
npmlog.error(`sendToFCM error response. ${result.error}`)
ctx.status = 502
}
})
}catch(err){
if( err.response ){
ctx.throw( 503, `sendToFCM failed. status: ${err.response.status}: ${JSON.stringify(err.response.data)}`)
}else{
ctx.throw( 503, `sendToFCM failed. ${err}`)
}
}
})
}
const rePathCheck = new RegExp("^/webpushtokencheck$")
const rePathCallback = new RegExp("^/webpushcallback/([^\\?#]+)")
const rePathServerKey = new RegExp("/webpushserverkey$")
async function handleRequest(ctx,next){
const method = ctx.request.method
const path = ctx.request.path
npmlog.info(`${method} ${path}`)
if( method=='POST' ){
let m = rePathCheck.exec(path)
if( m ) return await tokenCheck(ctx,m)
m = rePathCallback.exec(path)
if( m ) return await pushCallback(ctx,m)
m = rePathServerKey.exec(path)
if( m ) return await serverKeyUpdate(ctx,m)
}
npmlog.info("status=${ctx.status}")
ctx.throw(404,'Not found')
}
function accessLog_sub(ctx,err){
const status = err ? ( err.status || 500) : (ctx.status || 404)
const message = err ? (err.message || '(no error message)') : (ctx.message || '(no message)')
console.log(`${ctx.host} ${ctx.request.method} ${ctx.request.path} => ${status} ${message}`)
}
async function accessLog(ctx,next){
try{
await next()
}catch(err){
accessLog_sub(ctx,err)
throw err
}
accessLog_sub(ctx)
}
async function main(){
npmlog.info(`DB sync...`)
await WebPushTokenCheck.sync()
await ServerKey.sync()
const app = new Koa()
app.use(accessLog)
app.use(handleRequest)
const port = process.env.LISTEN_PORT || 4005
const addr = process.env.LISTEN_ADDR || '127.0.0.1'
app.listen(port,addr,()=>{
npmlog.info(`listening on addr ${addr} port ${port}...`)
})
}
main()