Change image API port, added TLS support, fixed content-type header

This commit is contained in:
Essem 2021-12-02 18:01:33 -06:00
parent 871979105c
commit 38e5a1d9f2
No known key found for this signature in database
GPG key ID: 7D497397CC3A2A8C
6 changed files with 12 additions and 10 deletions

View file

@ -55,6 +55,6 @@ RUN npm run build
USER esmBot USER esmBot
EXPOSE 8080 8081 EXPOSE 3762
ENTRYPOINT ["node", "api/index.js"] ENTRYPOINT ["node", "api/index.js"]

View file

@ -1,5 +1,5 @@
# esmBot Image API # esmBot Image API
The esmBot image API is a combined HTTP and WebSocket API running on port 8080. The API supports very basic authentication, which is defined on the server via the PASS environment variable and is sent from the client via the Authentication header in both HTTP and WS requests. The esmBot image API is a combined HTTP and WebSocket API running on port 3762. The API supports very basic authentication, which is defined on the server via the PASS environment variable and is sent from the client via the Authentication header in both HTTP and WS requests.
## HTTP ## HTTP

View file

@ -196,7 +196,8 @@ httpServer.on("request", async (req, res) => {
contentType = "image/webp"; contentType = "image/webp";
break; break;
} }
res.setHeader("Content-Type", contentType); if (contentType) res.setHeader("Content-Type", contentType);
else res.setHeader("Content-Type", ext);
const data = jobs.get(id).data; const data = jobs.get(id).data;
jobs.delete(id); jobs.delete(id);
return res.end(data, (err) => { return res.end(data, (err) => {
@ -230,8 +231,8 @@ httpServer.on("error", (e) => {
console.error("An HTTP error occurred: ", e); console.error("An HTTP error occurred: ", e);
}); });
httpServer.listen(8080, () => { httpServer.listen(3762, () => {
log("HTTP and WS listening on port 8080"); log("HTTP and WS listening on port 3762");
}); });
const runJob = (job, ws) => { const runJob = (job, ws) => {

View file

@ -3,7 +3,7 @@
{ "id": "1", "host": "localhost", "port": 2333, "password": "youshallnotpass", "local": true } { "id": "1", "host": "localhost", "port": 2333, "password": "youshallnotpass", "local": true }
], ],
"image": [ "image": [
{ "server": "localhost", "auth": "verycoolpass100" } { "server": "localhost", "auth": "verycoolpass100", "tls": false }
], ],
"searx": [ "searx": [
"https://searx.xyz" "https://searx.xyz"

View file

@ -26,6 +26,7 @@ class ImageConnection {
constructor(host, auth, tls = false) { constructor(host, auth, tls = false) {
this.requests = new Map(); this.requests = new Map();
this.host = host; this.host = host;
this.port = 3762;
this.auth = auth; this.auth = auth;
this.tag = null; this.tag = null;
this.disconnected = false; this.disconnected = false;
@ -38,7 +39,7 @@ class ImageConnection {
} else { } else {
this.wsproto = "ws"; this.wsproto = "ws";
} }
this.sockurl = `${this.wsproto}://${host}/sock`; this.sockurl = `${this.wsproto}://${host}:${this.port}/sock`;
this.conn = new WebSocket(this.sockurl, { this.conn = new WebSocket(this.sockurl, {
headers: { headers: {
"Authentication": auth && auth !== "" ? auth : undefined "Authentication": auth && auth !== "" ? auth : undefined
@ -50,7 +51,7 @@ class ImageConnection {
} else { } else {
httpproto = "http"; httpproto = "http";
} }
this.httpurl = `${httpproto}://${host}/image`; this.httpurl = `${httpproto}://${host}:${this.port}/image`;
this.conn.on("message", (msg) => this.onMessage(msg)); this.conn.on("message", (msg) => this.onMessage(msg));
this.conn.once("error", (err) => this.onError(err)); this.conn.once("error", (err) => this.onError(err));
this.conn.once("close", () => this.onClose()); this.conn.once("close", () => this.onClose());
@ -86,7 +87,7 @@ class ImageConnection {
} }
this.requests.clear(); this.requests.clear();
if (!this.disconnected) { if (!this.disconnected) {
logger.warn(`Lost connection to ${this.host}, attempting to reconnect in 5 seconds...`); logger.warn(`Lost connection to ${this.host}:${this.port}, attempting to reconnect in 5 seconds...`);
await setTimeout(5000); await setTimeout(5000);
this.conn = new WebSocket(this.sockurl, { this.conn = new WebSocket(this.sockurl, {
headers: { headers: {

View file

@ -82,7 +82,7 @@ class ImageWorker extends BaseServiceWorker {
} }
async connect(server, auth) { async connect(server, auth) {
const connection = new ImageConnection(`${server}:8080`, auth); const connection = new ImageConnection(server, auth);
this.connections.set(server, connection); this.connections.set(server, connection);
} }