diff --git a/node/README.md b/node/README.md index 0f6ff45..422101e 100644 --- a/node/README.md +++ b/node/README.md @@ -1,4 +1,4 @@ ### Listen config -If `type` is `unix`, `path` is the path where to create the socket. +If `type` is `unix`, `path` is the path where to create the socket, and `force` is whether to overwrite the socket file if it already exists. If `type` is `tcp`, `port` and `host` are the host/port where to listen. diff --git a/node/example-config.json b/node/example-config.json index e81822c..5b07153 100644 --- a/node/example-config.json +++ b/node/example-config.json @@ -1,6 +1,7 @@ { "listen": { "type": "unix", - "path": "/var/run/matrix-appservice-kakaotalk/rpc.sock" + "path": "/var/run/matrix-appservice-kakaotalk/rpc.sock", + "force": false } } diff --git a/node/src/clientmanager.js b/node/src/clientmanager.js index 9305790..b89dcc0 100644 --- a/node/src/clientmanager.js +++ b/node/src/clientmanager.js @@ -46,12 +46,17 @@ export default class ClientManager { } } - async startUnix(socketPath) { + async startUnix(socketPath, force) { try { await fs.promises.access(path.dirname(socketPath)) } catch (err) { await fs.promises.mkdir(path.dirname(socketPath), 0o700) } + if (force) { + try { + await fs.promises.unlink(socketPath) + } catch (err) {} + } await promisify(cb => this.server.listen(socketPath, cb)) await fs.promises.chmod(socketPath, 0o700) this.log("Now listening at", socketPath) @@ -66,7 +71,7 @@ export default class ClientManager { this.log("Starting server") if (this.listenConfig.type === "unix") { - await this.startUnix(this.listenConfig.path) + await this.startUnix(this.listenConfig.path, this.listenConfig.force) } else if (this.listenConfig.type === "tcp") { await this.startTCP(this.listenConfig.port, this.listenConfig.host) }