2020-08-17 15:03:10 -04:00
|
|
|
// mautrix-amp - A very hacky Matrix-SMS bridge based on using Android Messages for Web in Puppeteer
|
|
|
|
// Copyright (C) 2020 Tulir Asokan
|
|
|
|
//
|
|
|
|
// 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/>.
|
2020-08-18 09:47:06 -04:00
|
|
|
import net from "net"
|
|
|
|
import fs from "fs"
|
|
|
|
import path from "path"
|
|
|
|
|
|
|
|
import Client from "./client.js"
|
|
|
|
import { promisify } from "./util.js"
|
|
|
|
|
|
|
|
export default class PuppetAPI {
|
2020-08-24 09:45:44 -04:00
|
|
|
constructor(listenConfig) {
|
|
|
|
this.listenConfig = listenConfig
|
2020-08-24 07:55:44 -04:00
|
|
|
this.server = net.createServer(this.acceptConnection)
|
2020-08-24 09:45:44 -04:00
|
|
|
this.connections = []
|
2020-08-18 09:47:06 -04:00
|
|
|
this.puppets = new Map()
|
|
|
|
this.clients = new Map()
|
|
|
|
this.connIDSequence = 0
|
2020-08-24 07:55:44 -04:00
|
|
|
this.stopped = false
|
2020-08-18 09:47:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
log(...text) {
|
|
|
|
console.log("[API]", ...text)
|
|
|
|
}
|
|
|
|
|
2020-08-24 07:55:44 -04:00
|
|
|
acceptConnection = sock => {
|
|
|
|
if (this.stopped) {
|
|
|
|
sock.end()
|
2020-08-24 09:45:44 -04:00
|
|
|
sock.destroy()
|
2020-08-24 07:55:44 -04:00
|
|
|
} else {
|
2020-08-24 09:45:44 -04:00
|
|
|
const connID = this.connIDSequence++
|
|
|
|
this.connections[connID] = sock
|
|
|
|
new Client(this, sock, connID).start()
|
2020-08-24 07:55:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-24 09:45:44 -04:00
|
|
|
async startUnix(socketPath) {
|
2020-08-18 09:47:06 -04:00
|
|
|
try {
|
2020-08-24 09:45:44 -04:00
|
|
|
await fs.promises.access(path.dirname(socketPath))
|
2020-08-18 09:47:06 -04:00
|
|
|
} catch (err) {
|
2020-08-24 09:45:44 -04:00
|
|
|
await fs.promises.mkdir(path.dirname(socketPath), 0o700)
|
2020-08-18 09:47:06 -04:00
|
|
|
}
|
|
|
|
try {
|
2020-08-24 09:45:44 -04:00
|
|
|
await fs.promises.unlink(socketPath)
|
2020-08-18 09:47:06 -04:00
|
|
|
} catch (err) {}
|
2020-08-24 09:45:44 -04:00
|
|
|
await promisify(cb => this.server.listen(socketPath, cb))
|
|
|
|
await fs.promises.chmod(socketPath, 0o700)
|
|
|
|
this.log("Now listening at", socketPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
async startTCP(port, host) {
|
|
|
|
await promisify(cb => this.server.listen(port, host, cb))
|
|
|
|
this.log(`Now listening at ${host || ""}:${port}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
async start() {
|
|
|
|
this.log("Starting server")
|
|
|
|
|
|
|
|
if (this.listenConfig.type === "unix") {
|
|
|
|
await this.startUnix(this.listenConfig.path)
|
|
|
|
} else if (this.listenConfig.type === "tcp") {
|
|
|
|
await this.startTCP(this.listenConfig.port, this.listenConfig.host)
|
|
|
|
}
|
2020-08-18 09:47:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async stop() {
|
2020-08-24 07:55:44 -04:00
|
|
|
this.stopped = true
|
|
|
|
for (const client of this.clients.values()) {
|
|
|
|
await client.stop("Server is shutting down")
|
|
|
|
}
|
2020-08-24 09:45:44 -04:00
|
|
|
for (const socket of this.connections) {
|
|
|
|
socket.end()
|
|
|
|
socket.destroy()
|
|
|
|
}
|
2020-08-18 09:47:06 -04:00
|
|
|
this.log("Stopping server")
|
|
|
|
await promisify(cb => this.server.close(cb))
|
2020-08-24 09:45:44 -04:00
|
|
|
if (this.listenConfig.type === "unix") {
|
|
|
|
try {
|
|
|
|
await fs.promises.unlink(this.listenConfig.path)
|
|
|
|
} catch (err) {}
|
|
|
|
}
|
|
|
|
this.log("Stopping puppets")
|
2020-08-18 09:47:06 -04:00
|
|
|
for (const puppet of this.puppets.values()) {
|
|
|
|
await puppet.stop()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|