Skip to content

Commit

Permalink
Merge pull request #14 from bramanda48/develop
Browse files Browse the repository at this point in the history
Fix Error 500 - unable to open database file and some issue
  • Loading branch information
bramanda48 authored May 13, 2024
2 parents 6a80163 + 8b7e28d commit 00e79bc
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 16 deletions.
6 changes: 3 additions & 3 deletions src/controllers/V1/relay/relay.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const addAuth: Handler = async (c) => {
const responseFormat = new ResponseFormat(c);
const relayService = new RelayService();

const auth = relayService.addAuth(domain, username, password);
const auth = await relayService.addAuth(domain, username, password);
return responseFormat
.withRequestData({
timestamp: new Date(),
Expand All @@ -28,7 +28,7 @@ const addDomain: Handler = async (c) => {
const responseFormat = new ResponseFormat(c);
const relayService = new RelayService();

const domains = relayService.addDomain(domain, host, port);
const domains = await relayService.addDomain(domain, host, port);
return responseFormat
.withRequestData({
timestamp: new Date(),
Expand All @@ -45,7 +45,7 @@ const excludeDomain: Handler = async (c) => {
const responseFormat = new ResponseFormat(c);
const relayService = new RelayService();

const exclude = relayService.excludeDomain(domain);
const exclude = await relayService.excludeDomain(domain);
return responseFormat
.withRequestData({
timestamp: new Date(),
Expand Down
8 changes: 6 additions & 2 deletions src/exceptions/service-unavailable.exception.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { StatusCodes } from "status-code";
import { BaseException } from "./base.exception.ts";

export class ServiceUnavailableException extends BaseException {
constructor(message: string) {
super("SERVICE_UNAVAILABLE", message, StatusCodes.SERVICE_UNAVAILABLE);
constructor(message: string, name?: string) {
super(
name ?? "SERVICE_UNAVAILABLE",
message,
StatusCodes.SERVICE_UNAVAILABLE
);
}
}
4 changes: 2 additions & 2 deletions src/routes/v1.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ route.post("/relay/add-domain", RelayController.addDomain);
route.post("/relay/exclude-domain", RelayController.excludeDomain);

route.get("/fail2ban", Fail2banController.getJail);
route.post("/fail2ban/ban/{ip}", Fail2banController.banIpAddress);
route.post("/fail2ban/unban/{ip}", Fail2banController.unbanIpAddress);
route.post("/fail2ban/ban/:ip", Fail2banController.banIpAddress);
route.post("/fail2ban/unban/:ip", Fail2banController.unbanIpAddress);

route.get("/logs/fail2ban", LogsController.getFail2banLogs);
export const V1Route = route;
31 changes: 25 additions & 6 deletions src/services/base.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { path, sqlite3 } from "../../deps.ts";
import { fs, path, sqlite3 } from "../../deps.ts";
import { ServiceUnavailableException } from "../exceptions/service-unavailable.exception.ts";
import { DatabaseName } from "./database.service.ts";
import { DatabaseService } from "./database.service.ts";
import { EnvService } from "./env.service.ts";
Expand All @@ -13,7 +14,6 @@ export class BaseService {
protected dbVirtual: DatabaseService;
protected dbPasswd: DatabaseService;
protected dbRelay: DatabaseService;
protected dbFail2ban: sqlite3.Database;

constructor() {
this.env = new EnvService();
Expand All @@ -25,9 +25,28 @@ export class BaseService {
this.dbVirtual = new DatabaseService(DatabaseName.VIRTUAL);
this.dbPasswd = new DatabaseService(DatabaseName.PASSWD);
this.dbRelay = new DatabaseService(DatabaseName.RELAY);
this.dbFail2ban = new sqlite3.Database(
path.resolve(this.env.get("WEB_API_FAIL2BAN_SQLITE_PATH")),
{ readonly: true }
);
}

protected get dbFail2ban(): sqlite3.Database {
if (this.env.get<boolean>("ENABLE_FAIL2BAN") == false) {
// Reference : https://docker-mailserver.github.io/docker-mailserver/latest/config/environment/#enable_fail2ban
throw new ServiceUnavailableException(
"Fail2ban is not running. Ensure you've included 'ENABLE_FAIL2BAN' in your compose file",
"SERVICE_FAIL2BAN_UNAVAILABLE"
);
}

const dbPath = path.resolve(this.env.get("WEB_API_FAIL2BAN_SQLITE_PATH"));
if (!fs.existsSync(dbPath)) {
throw new ServiceUnavailableException(
`Unable to open fail2ban database in ${dbPath}`,
"SERVICE_FAIL2BAN_UNAVAILABLE"
);
}

// open database
return new sqlite3.Database(dbPath, {
readonly: true,
});
}
}
6 changes: 3 additions & 3 deletions src/services/relay/relay.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ export class RelayService extends BaseService {
): Promise<RelayAuth> {
const sender: string = `@${domain}`;
const relayHostEntry: string = `${username}:${password}`;
const getPasswd: string[] = await this.dbPasswd.findText(sender);
if (getPasswd.length > 0) {
this.dbRelay.replace(getPasswd[0], sender, relayHostEntry);
const getRelay: string[] = await this.dbRelay.findText(sender);
if (getRelay.length > 0) {
this.dbRelay.replace(getRelay[0], sender, relayHostEntry);
} else {
this.dbRelay.addOrAppend(sender, relayHostEntry);
}
Expand Down

0 comments on commit 00e79bc

Please sign in to comment.