Fix bug, Support thirdparty streaming access
This commit is contained in:
		
							parent
							
								
									fa591e5c9b
								
							
						
					
					
						commit
						2ded8ba858
					
				
					 4 changed files with 31 additions and 18 deletions
				
			
		|  | @ -2,6 +2,7 @@ import * as http from 'http'; | ||||||
| import * as websocket from 'websocket'; | import * as websocket from 'websocket'; | ||||||
| import * as redis from 'redis'; | import * as redis from 'redis'; | ||||||
| import User from './models/user'; | import User from './models/user'; | ||||||
|  | import Userkey from './models/userkey'; | ||||||
| 
 | 
 | ||||||
| import homeStream from './stream/home'; | import homeStream from './stream/home'; | ||||||
| import messagingStream from './stream/messaging'; | import messagingStream from './stream/messaging'; | ||||||
|  | @ -17,7 +18,13 @@ module.exports = (server: http.Server) => { | ||||||
| 	ws.on('request', async (request) => { | 	ws.on('request', async (request) => { | ||||||
| 		const connection = request.accept(); | 		const connection = request.accept(); | ||||||
| 
 | 
 | ||||||
| 		const user = await authenticate(connection); | 		const user = await authenticate(connection, request.resourceURL.query.i); | ||||||
|  | 
 | ||||||
|  | 		if (user == null) { | ||||||
|  | 			connection.send('authentication-failed'); | ||||||
|  | 			connection.close(); | ||||||
|  | 			return; | ||||||
|  | 		} | ||||||
| 
 | 
 | ||||||
| 		// Connect to Redis
 | 		// Connect to Redis
 | ||||||
| 		const subscriber = redis.createClient( | 		const subscriber = redis.createClient( | ||||||
|  | @ -41,29 +48,36 @@ module.exports = (server: http.Server) => { | ||||||
| 	}); | 	}); | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| function authenticate(connection: websocket.connection): Promise<any> { | function authenticate(connection: websocket.connection, token: string): Promise<any> { | ||||||
| 	return new Promise((resolve, reject) => { | 	return new Promise(async (resolve, reject) => { | ||||||
| 		// Listen first message
 | 		if (token[0] == '!') { | ||||||
| 		connection.once('message', async (data) => { |  | ||||||
| 			const msg = JSON.parse(data.utf8Data); |  | ||||||
| 
 |  | ||||||
| 			// Fetch user
 | 			// Fetch user
 | ||||||
| 			// SELECT _id
 | 			// SELECT _id
 | ||||||
| 			const user = await User | 			const user = await User | ||||||
| 				.findOne({ | 				.findOne({ | ||||||
| 					token: msg.i | 					token: token | ||||||
| 				}, { | 				}, { | ||||||
| 					_id: true | 					_id: true | ||||||
| 				}); | 				}); | ||||||
| 
 | 
 | ||||||
| 			if (user === null) { | 			resolve(user); | ||||||
| 				connection.close(); | 		} else { | ||||||
| 				return; | 			const userkey = await Userkey.findOne({ | ||||||
|  | 				key: token | ||||||
|  | 			}); | ||||||
|  | 
 | ||||||
|  | 			if (userkey == null) { | ||||||
|  | 				return reject('invalid userkey'); | ||||||
| 			} | 			} | ||||||
| 
 | 
 | ||||||
| 			connection.send('authenticated'); | 			// Fetch user
 | ||||||
|  | 			// SELECT _id
 | ||||||
|  | 			const user = await User | ||||||
|  | 				.findOne({ _id: userkey.user_id }, { | ||||||
|  | 					_id: true | ||||||
|  | 				}); | ||||||
| 
 | 
 | ||||||
| 			resolve(user); | 			resolve(user); | ||||||
| 		}); | 		} | ||||||
| 	}); | 	}); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -39,7 +39,7 @@ try { | ||||||
| checkForUpdate(); | checkForUpdate(); | ||||||
| 
 | 
 | ||||||
| // Get token from cookie
 | // Get token from cookie
 | ||||||
| const i = (document.cookie.match(/i=(\w+)/) || [null, null])[1]; | const i = (document.cookie.match(/i=(!\w+)/) || [null, null])[1]; | ||||||
| 
 | 
 | ||||||
| // ユーザーをフェッチしてコールバックする
 | // ユーザーをフェッチしてコールバックする
 | ||||||
| module.exports = callback => { | module.exports = callback => { | ||||||
|  |  | ||||||
|  | @ -9,7 +9,7 @@ class Connection | ||||||
| 		@event = riot.observable! | 		@event = riot.observable! | ||||||
| 		@me = me | 		@me = me | ||||||
| 		host = CONFIG.api.url.replace \http \ws | 		host = CONFIG.api.url.replace \http \ws | ||||||
| 		@socket = new ReconnectingWebSocket "#{host}/messaging?otherparty=#{otherparty}" | 		@socket = new ReconnectingWebSocket "#{host}/messaging?i=#{me.token}&otherparty=#{otherparty}" | ||||||
| 
 | 
 | ||||||
| 		@socket.add-event-listener \open @on-open | 		@socket.add-event-listener \open @on-open | ||||||
| 		@socket.add-event-listener \message @on-message | 		@socket.add-event-listener \message @on-message | ||||||
|  |  | ||||||
|  | @ -9,13 +9,12 @@ module.exports = (me) ~> | ||||||
| 	state-ev = riot.observable! | 	state-ev = riot.observable! | ||||||
| 	event = riot.observable! | 	event = riot.observable! | ||||||
| 
 | 
 | ||||||
| 	socket = new ReconnectingWebSocket CONFIG.api.url.replace \http \ws | 	host = CONFIG.api.url.replace \http \ws | ||||||
|  | 	socket = new ReconnectingWebSocket "#{host}?i=#{me.token}" | ||||||
| 
 | 
 | ||||||
| 	socket.onopen = ~> | 	socket.onopen = ~> | ||||||
| 		state := \connected | 		state := \connected | ||||||
| 		state-ev.trigger \connected | 		state-ev.trigger \connected | ||||||
| 		socket.send JSON.stringify do |  | ||||||
| 			i: me.token |  | ||||||
| 
 | 
 | ||||||
| 	socket.onclose = ~> | 	socket.onclose = ~> | ||||||
| 		state := \reconnecting | 		state := \reconnecting | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue