-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
executable file
·153 lines (142 loc) · 3.85 KB
/
server.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
#!/usr/bin/env node
/* global process, global */
'use strict'
var verifyIdToken = require('google-id-token-verifier').verify
var Y = require('yjs')
var minimist = require('minimist')
var atob = require('atob')
var md5 = require('js-md5')
require('y-memory')(Y)
try {
require('y-leveldb')(Y)
} catch (err) {}
try {
// try to require local y-websockets-server
require('./y-websockets-server.js')(Y)
} catch (err) {
// otherwise require global y-websockets-server
require('y-websockets-server')(Y)
}
var options = minimist(process.argv.slice(2), {
string: ['port', 'debug', 'db'],
default: {
port: process.env.PORT || '1234',
debug: false,
db: 'memory'
}
})
var port = Number.parseInt(options.port, 10)
var io = require('socket.io')(port)
console.log('Running y-websockets-server on port ' + port)
// set of y instances created with type : 'room'
global.yInstances = {}
// set of y instances created with type : 'overview' (using idToken)
global.yInstancesOverview = {}
function checkAuthRoom (authInfo, y) {
return new Promise(function (resolve, reject) {
var roomId = JSON.parse(atob(y.connector.options.room)).roomId
if (authInfo != null && md5(authInfo) === roomId) {
resolve('write')
} else {
resolve('read')
}
})
}
function checkAuthOverview (authInfo, y) {
return new Promise(function (resolve, reject) {
verifyIdToken(authInfo, "863221146052-4h694v1mmn93991iq8vm9ub8tap3h7ve.apps.googleusercontent.com", function (err, data) {
var roomId = JSON.parse(atob(y.connector.options.room)).roomId
if (err == null && data.sub === roomId) {
resolve('write')
} else {
reject('Unauthorized')
}
})
})
}
function createYInstance (roomName) {
var checkAuth
try {
var conf = JSON.parse(atob(roomName))
if (conf.type === 'overview') {
checkAuth = checkAuthOverview
} else if (conf.type === 'room') {
checkAuth = checkAuthRoom
} else {
return
}
return Y({
db: {
name: options.db,
dir: 'y-leveldb-databases',
namespace: roomName
},
connector: {
name: 'websockets-server',
room: roomName,
io: io,
debug: !!options.debug,
checkAuth: checkAuth
},
share: {}
})
} catch (error) {
return Promise.reject(error)
}
}
function getInstanceOfY (roomName) {
if (yInstances[roomName] == null) {
yInstances[roomName] = createYInstance(roomName)
}
return yInstances[roomName]
}
io.on('connection', function (socket) {
var rooms = {} // maps room (binary encoded str) to { instance, canWrite }
socket.on('joinRoom', function (room) {
if (rooms[room] != null) {
return
}
rooms[room] = getInstanceOfY(room).then(function (y) {
console.log('User', socket.id, 'joins room:', room)
y.connector.userJoined(socket.id, 'slave')
function authListener (event) {
if (event.action === 'userAuthenticated' && event.user === socket.id) {
// socket is allowed to subscribe to room activity
socket.join(y.connector.options.room)
y.connector.removeUserEventListener(authListener)
}
}
y.connector.onUserEvent(authListener)
return y
}, function (err) {
console.warn(err)
})
})
socket.on('yjsEvent', function (msg) {
var p = rooms[msg.room]
if (p != null) {
p.then(function (y) {
y.connector.receiveMessage(socket.id, msg)
})
}
})
socket.on('disconnect', function () {
for (var n in rooms) {
rooms[n].then(function (y) {
y.connector.userLeft(socket.id)
})
delete rooms[n]
}
})
socket.on('leaveRoom', function (room) {
var r = rooms[room]
if (r != null) {
r.then(function (y) {
y.connector.userLeft(socket.id)
})
delete rooms[room]
} else {
debugger
}
})
})