-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #78 from dappnode/diego/fix-empty-domain
Fix empty dappnode domain
- Loading branch information
Showing
8 changed files
with
140 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,87 +1,97 @@ | ||
import express, { ErrorRequestHandler, Request, Response } from "express"; | ||
import express, { | ||
ErrorRequestHandler, | ||
Request, | ||
Response, | ||
Express, | ||
} from "express"; | ||
import morgan from "morgan"; | ||
import { HttpError, BadRequestError, asyncHandler } from "./utils/asyncHandler"; | ||
import { entriesDb } from "./db"; | ||
import { reconfigureNGINX } from "./utils/nginx"; | ||
import { sanitizeExternal, sanitizeFrom, sanitizeTo } from "./utils/sanitize"; | ||
import { config } from "../config"; | ||
|
||
const app = express(); | ||
|
||
app.use(morgan("tiny")); | ||
|
||
app.get( | ||
"/add", | ||
asyncHandler(async (req) => { | ||
const from = await sanitizeFrom(req.query.from as string); | ||
const to = sanitizeTo(req.query.to as string); | ||
const external = sanitizeExternal(req.query.external as string); //true if not set, we should swap this, but it left like this for backwards compatibility | ||
|
||
const entries = entriesDb.read(); | ||
if (entries.some((entry) => entry.from === from)) { | ||
throw new BadRequestError("External endpoint already exists"); | ||
} | ||
|
||
// NGINX will crash in loop if a domain is longer than `server_names_hash_bucket_size` | ||
// Force that from has only ASCII characters to make sure the char length = bytes lenght | ||
// fulldomain = from + "." + dappnodeDomain | ||
const dappnodeDomain = process.env._DAPPNODE_GLOBAL_DOMAIN; | ||
const maxLen = config.maximum_domain_length - dappnodeDomain.length - 1; | ||
if (from.length > maxLen) { | ||
throw new BadRequestError(`'from' ${from} exceeds max length of ${from}`); | ||
} | ||
|
||
entries.push({ from, to, external }); | ||
entriesDb.write(entries); | ||
|
||
const reconfigured = await reconfigureNGINX(); | ||
if (!reconfigured) { | ||
entriesDb.write(entries.filter((e) => e.from !== from)); // rollback | ||
await reconfigureNGINX(); | ||
throw new HttpError("Unable to add mapping", 500); | ||
} | ||
}) | ||
); | ||
|
||
app.get( | ||
"/remove", | ||
asyncHandler(async (req) => { | ||
const from = await sanitizeFrom(req.query.from as string); | ||
|
||
const entries = entriesDb.read(); | ||
entriesDb.write(entries.filter((e) => e.from !== from)); | ||
|
||
await reconfigureNGINX(); | ||
}) | ||
); | ||
|
||
app.get( | ||
"/", | ||
asyncHandler(async () => entriesDb.read()) | ||
); | ||
|
||
app.get( | ||
"/reconfig", | ||
asyncHandler(async () => await reconfigureNGINX()) | ||
); | ||
|
||
app.get( | ||
"/clear", | ||
asyncHandler(async () => { | ||
entriesDb.write([]); | ||
await reconfigureNGINX(); | ||
}) | ||
); | ||
|
||
app.use((_req: Request, res: Response) => { | ||
res.status(404).json({ error: "Not Found" }); | ||
}); | ||
|
||
// Default error handler | ||
app.use(function (err, _req, res, next) { | ||
if (res.headersSent) return next(err); | ||
const code = err instanceof HttpError ? err.code : 500; | ||
res.status(code).json({ error: err.message }); | ||
} as ErrorRequestHandler); | ||
|
||
export { app }; | ||
function getHttpsApi(dappnodeDomain: string): Express { | ||
const app = express(); | ||
|
||
app.use(morgan("tiny")); | ||
|
||
app.get( | ||
"/add", | ||
asyncHandler(async (req) => { | ||
const from = await sanitizeFrom(req.query.from as string); | ||
const to = sanitizeTo(req.query.to as string); | ||
const external = sanitizeExternal(req.query.external as string); //true if not set, we should swap this, but it left like this for backwards compatibility | ||
|
||
const entries = entriesDb.read(); | ||
if (entries.some((entry) => entry.from === from)) { | ||
throw new BadRequestError("External endpoint already exists"); | ||
} | ||
|
||
// NGINX will crash in loop if a domain is longer than `server_names_hash_bucket_size` | ||
// Force that from has only ASCII characters to make sure the char length = bytes lenght | ||
// fulldomain = from + "." + dappnodeDomain | ||
const maxLen = config.maximum_domain_length - dappnodeDomain.length - 1; | ||
if (from.length > maxLen) { | ||
throw new BadRequestError( | ||
`'from' ${from} exceeds max length of ${from}` | ||
); | ||
} | ||
|
||
entries.push({ from, to, external }); | ||
entriesDb.write(entries); | ||
|
||
const reconfigured = await reconfigureNGINX(dappnodeDomain); | ||
if (!reconfigured) { | ||
entriesDb.write(entries.filter((e) => e.from !== from)); // rollback | ||
await reconfigureNGINX(dappnodeDomain); | ||
throw new HttpError("Unable to add mapping", 500); | ||
} | ||
}) | ||
); | ||
|
||
app.get( | ||
"/remove", | ||
asyncHandler(async (req) => { | ||
const from = await sanitizeFrom(req.query.from as string); | ||
|
||
const entries = entriesDb.read(); | ||
entriesDb.write(entries.filter((e) => e.from !== from)); | ||
|
||
await reconfigureNGINX(dappnodeDomain); | ||
}) | ||
); | ||
|
||
app.get( | ||
"/", | ||
asyncHandler(async () => entriesDb.read()) | ||
); | ||
|
||
app.get( | ||
"/reconfig", | ||
asyncHandler(async () => await reconfigureNGINX(dappnodeDomain)) | ||
); | ||
|
||
app.get( | ||
"/clear", | ||
asyncHandler(async () => { | ||
entriesDb.write([]); | ||
await reconfigureNGINX(dappnodeDomain); | ||
}) | ||
); | ||
|
||
app.use((_req: Request, res: Response) => { | ||
res.status(404).json({ error: "Not Found" }); | ||
}); | ||
|
||
// Default error handler | ||
app.use(function (err, _req, res, next) { | ||
if (res.headersSent) return next(err); | ||
const code = err instanceof HttpError ? err.code : 500; | ||
res.status(code).json({ error: err.message }); | ||
} as ErrorRequestHandler); | ||
|
||
return app; | ||
} | ||
|
||
export { getHttpsApi }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters