2021-03-15 01:40:56 -04:00
|
|
|
// matrix-puppeteer-line - A very hacky Matrix-LINE bridge based on running LINE's Chrome extension in Puppeteer
|
2021-02-26 01:28:54 -05:00
|
|
|
// Copyright (C) 2020-2021 Tulir Asokan, Andrew Ferrazzutti
|
2020-08-18 09:47:06 -04:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
import MessagesPuppeteer from "./puppet.js"
|
|
|
|
import { emitLines, promisify } from "./util.js"
|
|
|
|
|
|
|
|
export default class Client {
|
|
|
|
/**
|
|
|
|
* @param {PuppetAPI} manager
|
|
|
|
* @param {import("net").Socket} socket
|
|
|
|
* @param {number} connID
|
|
|
|
* @param {?string} [userID]
|
|
|
|
* @param {?MessagesPuppeteer} [puppet]
|
|
|
|
*/
|
|
|
|
constructor(manager, socket, connID, userID = null, puppet = null) {
|
|
|
|
this.manager = manager
|
|
|
|
this.socket = socket
|
|
|
|
this.connID = connID
|
|
|
|
this.userID = userID
|
|
|
|
this.puppet = puppet
|
|
|
|
this.stopped = false
|
|
|
|
this.notificationID = 0
|
|
|
|
this.maxCommandID = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
log(...text) {
|
|
|
|
if (this.userID) {
|
|
|
|
console.log(`[API/${this.userID}/${this.connID}]`, ...text)
|
|
|
|
} else {
|
|
|
|
console.log(`[API/${this.connID}]`, ...text)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-24 07:55:44 -04:00
|
|
|
error(...text) {
|
|
|
|
if (this.userID) {
|
|
|
|
console.error(`[API/${this.userID}/${this.connID}]`, ...text)
|
|
|
|
} else {
|
|
|
|
console.error(`[API/${this.connID}]`, ...text)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-18 09:47:06 -04:00
|
|
|
start() {
|
|
|
|
this.log("Received connection", this.connID)
|
|
|
|
emitLines(this.socket)
|
|
|
|
this.socket.on("line", line => this.handleLine(line)
|
|
|
|
.catch(err => this.log("Error handling line:", err)))
|
|
|
|
this.socket.on("end", this.handleEnd)
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
if (!this.userID && !this.stopped) {
|
|
|
|
this.log("Didn't receive register request within 3 seconds, terminating")
|
2020-08-24 07:55:44 -04:00
|
|
|
this.stop("Register request timeout")
|
2020-08-18 09:47:06 -04:00
|
|
|
}
|
|
|
|
}, 3000)
|
|
|
|
}
|
|
|
|
|
2020-08-24 07:55:44 -04:00
|
|
|
async stop(error = null) {
|
2020-08-18 09:47:06 -04:00
|
|
|
if (this.stopped) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
this.stopped = true
|
2020-08-24 07:55:44 -04:00
|
|
|
try {
|
|
|
|
await this._write({ id: --this.notificationID, command: "quit", error })
|
|
|
|
await promisify(cb => this.socket.end(cb))
|
|
|
|
} catch (err) {
|
|
|
|
this.error("Failed to end connection:", err)
|
|
|
|
this.socket.destroy(err)
|
|
|
|
}
|
2020-08-18 09:47:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
handleEnd = () => {
|
|
|
|
this.stopped = true
|
|
|
|
if (this.userID && this.manager.clients.get(this.userID) === this) {
|
|
|
|
this.manager.clients.delete(this.userID)
|
|
|
|
}
|
|
|
|
this.log(`Connection closed (user: ${this.userID})`)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write JSON data to the socket.
|
|
|
|
*
|
|
|
|
* @param {object} data - The data to write.
|
|
|
|
* @return {Promise<void>}
|
|
|
|
*/
|
|
|
|
_write(data) {
|
|
|
|
return promisify(cb => this.socket.write(JSON.stringify(data) + "\n", cb))
|
|
|
|
}
|
|
|
|
|
|
|
|
sendMessage(message) {
|
2021-06-10 02:10:18 -04:00
|
|
|
this.log(`Sending message ${message.id || "with no ID"} to client`)
|
2020-08-18 09:47:06 -04:00
|
|
|
return this._write({
|
|
|
|
id: --this.notificationID,
|
|
|
|
command: "message",
|
2021-04-21 02:38:10 -04:00
|
|
|
is_sequential: true,
|
2020-08-18 09:47:06 -04:00
|
|
|
message,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-04-20 20:01:50 -04:00
|
|
|
sendReceipt(receipt) {
|
|
|
|
this.log(`Sending read receipt (${receipt.count || "DM"}) of msg ${receipt.id} for chat ${receipt.chat_id}`)
|
|
|
|
return this._write({
|
|
|
|
id: --this.notificationID,
|
|
|
|
command: "receipt",
|
|
|
|
receipt
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-08-18 09:47:06 -04:00
|
|
|
sendQRCode(url) {
|
2021-02-20 03:48:08 -05:00
|
|
|
this.log(`Sending QR ${url} to client`)
|
2020-08-18 09:47:06 -04:00
|
|
|
return this._write({
|
|
|
|
id: --this.notificationID,
|
|
|
|
command: "qr",
|
|
|
|
url,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-02-10 02:34:19 -05:00
|
|
|
sendPIN(pin) {
|
|
|
|
this.log(`Sending PIN ${pin} to client`)
|
|
|
|
return this._write({
|
|
|
|
id: --this.notificationID,
|
|
|
|
command: "pin",
|
|
|
|
pin,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-06-08 00:24:00 -04:00
|
|
|
sendLoginSuccess() {
|
|
|
|
this.log("Sending login success to client")
|
2021-02-10 02:34:19 -05:00
|
|
|
return this._write({
|
|
|
|
id: --this.notificationID,
|
2021-06-08 00:24:00 -04:00
|
|
|
command: "login_success",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
sendLoginFailure(reason) {
|
|
|
|
this.log(`Sending login failure to client${reason ? `: "${reason}"` : ""}`)
|
|
|
|
return this._write({
|
|
|
|
id: --this.notificationID,
|
|
|
|
command: "login_failure",
|
2021-02-10 02:34:19 -05:00
|
|
|
reason,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-05-30 17:41:28 -04:00
|
|
|
sendLoggedOut() {
|
|
|
|
this.log("Sending logout notice to client")
|
|
|
|
return this._write({
|
|
|
|
id: --this.notificationID,
|
|
|
|
command: "logged_out",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-08-18 09:47:06 -04:00
|
|
|
handleStart = async (req) => {
|
2020-08-24 16:29:09 -04:00
|
|
|
let started = false
|
|
|
|
if (this.puppet === null) {
|
|
|
|
this.log("Opening new puppeteer for", this.userID)
|
2021-06-17 00:42:06 -04:00
|
|
|
this.puppet = new MessagesPuppeteer(this.userID, this.ownID, this.sendPlaceholders, this)
|
2020-08-24 16:29:09 -04:00
|
|
|
this.manager.puppets.set(this.userID, this.puppet)
|
|
|
|
await this.puppet.start(!!req.debug)
|
|
|
|
started = true
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
started,
|
|
|
|
is_logged_in: await this.puppet.isLoggedIn(),
|
|
|
|
is_connected: !await this.puppet.isDisconnected(),
|
|
|
|
is_permanently_disconnected: await this.puppet.isPermanentlyDisconnected(),
|
|
|
|
}
|
2020-08-18 09:47:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
handleStop = async () => {
|
|
|
|
if (this.puppet === null) {
|
|
|
|
return { stopped: false }
|
|
|
|
}
|
|
|
|
this.log("Closing puppeteer for", this.userID)
|
|
|
|
this.manager.puppets.delete(this.userID)
|
|
|
|
await this.puppet.stop()
|
|
|
|
this.puppet = null
|
|
|
|
return { stopped: true }
|
|
|
|
}
|
|
|
|
|
|
|
|
handleUnknownCommand = () => {
|
|
|
|
throw new Error("Unknown command")
|
|
|
|
}
|
|
|
|
|
|
|
|
handleRegister = async (req) => {
|
|
|
|
this.userID = req.user_id
|
2021-06-15 02:48:29 -04:00
|
|
|
this.ownID = req.own_id
|
2021-06-17 00:42:06 -04:00
|
|
|
this.sendPlaceholders = req.ephemeral_events
|
|
|
|
this.log(`Registered socket ${this.connID} -> ${this.userID}${!this.sendPlaceholders ? "" : " (with placeholder message support)"}`)
|
2020-08-18 09:47:06 -04:00
|
|
|
if (this.manager.clients.has(this.userID)) {
|
|
|
|
const oldClient = this.manager.clients.get(this.userID)
|
|
|
|
this.manager.clients.set(this.userID, this)
|
2021-06-15 02:48:29 -04:00
|
|
|
this.log(`Terminating previous socket ${oldClient.connID} for ${this.userID}`)
|
2020-08-24 07:55:44 -04:00
|
|
|
await oldClient.stop("Socket replaced by new connection")
|
2020-08-18 09:47:06 -04:00
|
|
|
} else {
|
|
|
|
this.manager.clients.set(this.userID, this)
|
|
|
|
}
|
|
|
|
this.puppet = this.manager.puppets.get(this.userID) || null
|
|
|
|
if (this.puppet) {
|
|
|
|
this.puppet.client = this
|
|
|
|
}
|
|
|
|
return { client_exists: this.puppet !== null }
|
|
|
|
}
|
|
|
|
|
|
|
|
async handleLine(line) {
|
|
|
|
if (this.stopped) {
|
|
|
|
this.log("Ignoring line, client is stopped")
|
|
|
|
return
|
|
|
|
}
|
2020-08-18 10:05:53 -04:00
|
|
|
let req
|
|
|
|
try {
|
|
|
|
req = JSON.parse(line)
|
|
|
|
} catch (err) {
|
|
|
|
this.log("Non-JSON request:", line)
|
|
|
|
return
|
|
|
|
}
|
2020-08-18 09:47:06 -04:00
|
|
|
if (!req.command || !req.id) {
|
2020-08-18 10:05:53 -04:00
|
|
|
this.log("Invalid request:", line)
|
2020-08-18 09:47:06 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if (req.id <= this.maxCommandID) {
|
|
|
|
this.log("Ignoring old request", req.id)
|
|
|
|
return
|
|
|
|
}
|
2021-02-25 22:21:11 -05:00
|
|
|
if (req.command != "is_connected") {
|
|
|
|
this.log("Received request", req.id, "with command", req.command)
|
|
|
|
}
|
2020-08-18 09:47:06 -04:00
|
|
|
this.maxCommandID = req.id
|
|
|
|
let handler
|
|
|
|
if (!this.userID) {
|
|
|
|
if (req.command !== "register") {
|
|
|
|
this.log("First request wasn't a register request, terminating")
|
2020-08-24 07:55:44 -04:00
|
|
|
await this.stop("Invalid first request")
|
2020-08-18 09:47:06 -04:00
|
|
|
return
|
|
|
|
} else if (!req.user_id) {
|
|
|
|
this.log("Register request didn't contain user ID, terminating")
|
2020-08-24 07:55:44 -04:00
|
|
|
await this.stop("Invalid register request")
|
2020-08-18 09:47:06 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
handler = this.handleRegister
|
|
|
|
} else {
|
|
|
|
handler = {
|
|
|
|
start: this.handleStart,
|
|
|
|
stop: this.handleStop,
|
2020-08-24 09:45:44 -04:00
|
|
|
disconnect: () => this.stop(),
|
2021-02-10 02:34:19 -05:00
|
|
|
login: req => this.puppet.waitForLogin(req.login_type, req.login_data),
|
|
|
|
cancel_login: () => this.puppet.cancelLogin(),
|
2020-08-18 09:47:06 -04:00
|
|
|
send: req => this.puppet.sendMessage(req.chat_id, req.text),
|
2021-03-27 03:37:41 -04:00
|
|
|
send_file: req => this.puppet.sendFile(req.chat_id, req.file_path),
|
2021-06-30 03:04:25 -04:00
|
|
|
set_last_message_ids: req => this.puppet.setLastMessageIDs(req.msg_ids, req.own_msg_ids, req.rct_ids),
|
2021-04-21 02:38:10 -04:00
|
|
|
pause: () => this.puppet.stopObserving(),
|
|
|
|
resume: () => this.puppet.startObserving(),
|
2021-06-15 02:48:29 -04:00
|
|
|
get_own_profile: () => this.puppet.getOwnProfile(),
|
2020-08-18 09:47:06 -04:00
|
|
|
get_chats: () => this.puppet.getRecentChats(),
|
2021-06-11 02:53:30 -04:00
|
|
|
get_chat: req => this.puppet.getChatInfo(req.chat_id, req.force_view),
|
2020-08-18 09:47:06 -04:00
|
|
|
get_messages: req => this.puppet.getMessages(req.chat_id),
|
2021-03-26 02:27:21 -04:00
|
|
|
read_image: req => this.puppet.readImage(req.image_url),
|
2020-11-03 10:57:51 -05:00
|
|
|
is_connected: async () => ({ is_connected: !await this.puppet.isDisconnected() }),
|
2020-08-18 09:47:06 -04:00
|
|
|
}[req.command] || this.handleUnknownCommand
|
|
|
|
}
|
|
|
|
const resp = { id: req.id }
|
|
|
|
try {
|
|
|
|
resp.command = "response"
|
|
|
|
resp.response = await handler(req)
|
|
|
|
} catch (err) {
|
|
|
|
resp.command = "error"
|
|
|
|
resp.error = err.toString()
|
|
|
|
this.log("Error handling request", req.id, err)
|
|
|
|
}
|
|
|
|
await this._write(resp)
|
|
|
|
}
|
|
|
|
}
|