Skip to content

Commit

Permalink
Merge pull request #7 from bramanda48/develop
Browse files Browse the repository at this point in the history
Add logs endpoints, authenticaton, etc
  • Loading branch information
bramanda48 authored Mar 21, 2024
2 parents 861438f + 7cc9fc4 commit 23948ab
Show file tree
Hide file tree
Showing 44 changed files with 877 additions and 70 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
DENO_ENV="production"
WEB_API_KEY=""
WEB_API_WHITELISTED_IP="127.0.0.1"
WEB_API_DMS_CONFIG_PATH="/tmp/docker-mailserver"
WEB_API_FAIL2BAN_SQLITE_PATH="/var/lib/fail2ban/fail2ban.sqlite3"
14 changes: 14 additions & 0 deletions bindings.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
type Environment = {
Bindings: EnvironmentBindings;
};

type EnvironmentBindings = {
remoteAddr: () => string;

// Define all environment variables
DENO_ENV: string;
WEB_API_KEY: string;
WEB_API_WHITELISTED_IP: string;
WEB_API_DMS_CONFIG_PATH: string;
WEB_API_FAIL2BAN_SQLITE_PATH: string;
};
30 changes: 11 additions & 19 deletions deno.jsonc
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"$schema": "https://raw.githubusercontent.com/denoland/deno/main/cli/schemas/config-file.v1.json",
"tasks": {
"start": "deno run --allow-all dist/main.esm.js",
"dev": "deno run --allow-all --watch src/main.ts",
"start": "deno run --allow-all --unstable-ffi dist/main.esm.js",
"dev": "deno run --allow-all --unstable-ffi --watch src/main.ts",
"deps": "deno cache --reload --lock=deno.lock --lock-write deps.ts build.ts",
"build": "deno run --allow-all build.ts",
"test": "deno test --allow-all"
Expand All @@ -16,10 +16,12 @@
"hono/cors": "https://deno.land/x/[email protected]/middleware/cors/index.ts",
"hono/pretty-json": "https://deno.land/x/[email protected]/middleware/pretty-json/index.ts",
"hono/serve-static": "https://deno.land/x/[email protected]/middleware/serve-static/index.ts",
"hono/logger": "https://deno.land/x/[email protected]/middleware/logger/index.ts",
"hono/swagger-ui": "https://cdn.jsdelivr.net/npm/@hono/[email protected]/+esm",
"status-code": "https://deno.land/x/[email protected]/mod.ts",
"zod": "https://deno.land/x/[email protected]/mod.ts",
"readline": "https://deno.land/x/[email protected]/mod.ts"
"readline": "https://deno.land/x/[email protected]/mod.ts",
"sqlite3": "https://deno.land/x/[email protected]/mod.ts"
},
"scopes": {
"https://deno.land/x/[email protected]/": {
Expand All @@ -28,9 +30,7 @@
},
"compilerOptions": {
"allowJs": false,
"lib": [
"deno.window"
],
"lib": ["deno.window"],
"strict": true,
"strictPropertyInitialization": false,
"strictNullChecks": false,
Expand All @@ -39,16 +39,10 @@
},
"lint": {
"rules": {
"tags": [
"recommended"
],
"exclude": [
"no-explicit-any"
]
"tags": ["recommended"],
"exclude": ["no-explicit-any"]
},
"include": [
"src/"
]
"include": ["src/"]
},
"fmt": {
"useTabs": false,
Expand All @@ -57,8 +51,6 @@
"semiColons": true,
"singleQuote": false,
"proseWrap": "preserve",
"include": [
"src/"
]
"include": ["src/"]
}
}
}
65 changes: 65 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ export * from "hono";
export * from "hono/cors";
export * from "hono/pretty-json";
export * from "hono/serve-static";
export * from "hono/logger";
export * from "hono/swagger-ui";
export * from "status-code";
export * as z from "zod";
export * from "readline";
export * as sqlite3 from "sqlite3";
2 changes: 2 additions & 0 deletions scripts/fail2ban-banned-ip.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
fail2ban-client set custom banip "$1"
2 changes: 2 additions & 0 deletions scripts/fail2ban-unbanned-ip.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
fail2ban-client set custom unbanip "$1"
25 changes: 25 additions & 0 deletions scripts/logs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash
SERVICE_NAME=$1
SERVICE_LOGS=""
LIMIT=$2

case "$SERVICE_NAME" in
fail2ban)
SERVICE_LOGS="/var/log/mail/fail2ban.log"
;;
clamav)
SERVICE_LOGS="/var/log/mail/clamav.log"
;;
rspamd)
SERVICE_LOGS="/var/log/mail/rspamd.log"
;;
mail)
SERVICE_LOGS="/var/log/mail/mail.log"
;;
*)
echo "Invalid service name `${SERVICE_NAME}`"
exit 1
;;
esac

tail -"$LIMIT" "$SERVICE_LOGS"
2 changes: 1 addition & 1 deletion scripts/user-patches.sh
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ autorestart=true
stdout_logfile=/var/log/supervisor/%(program_name)s.log
stderr_logfile=/var/log/supervisor/%(program_name)s.log
directory=${DMS_CONFIG}/webapi
command=/bin/bash -c "deno run --allow-all main.esm.js"
command=/bin/bash -c "deno run --allow-all --unstable-ffi main.esm.js"
EOL
}

Expand Down
12 changes: 6 additions & 6 deletions src/controllers/V1/dovecot/dovecot-master.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ const getAccount: Handler = async (c) => {

const createAccount: Handler = async (c) => {
const bodyParse = await c.req.json();
const { email, password } =
const { username, password } =
DovecotMasterValidation.createAcoount.parse(bodyParse);

const responseFormat = new ResponseFormat(c);
const dovecotMasterService = new DovecotMasterService();

const account = await dovecotMasterService.createAccount(email, password);
const account = await dovecotMasterService.createAccount(username, password);
return responseFormat
.withRequestData({
timestamp: new Date(),
Expand All @@ -37,13 +37,13 @@ const createAccount: Handler = async (c) => {

const updatePassword: Handler = async (c) => {
const bodyParse = await c.req.json();
const { email, password } =
const { username, password } =
DovecotMasterValidation.updatePassword.parse(bodyParse);

const responseFormat = new ResponseFormat(c);
const dovecotMasterService = new DovecotMasterService();

const account = await dovecotMasterService.updatePassword(email, password);
const account = await dovecotMasterService.updatePassword(username, password);
return responseFormat
.withRequestData({
timestamp: new Date(),
Expand All @@ -55,12 +55,12 @@ const updatePassword: Handler = async (c) => {

const removeAccount: Handler = async (c) => {
const queryParse = c.req.query();
const { email } = DovecotMasterValidation.removeAccount.parse(queryParse);
const { username } = DovecotMasterValidation.removeAccount.parse(queryParse);

const responseFormat = new ResponseFormat(c);
const dovecotMasterService = new DovecotMasterService();

const account = await dovecotMasterService.removeAccount(email);
const account = await dovecotMasterService.removeAccount(username);
return responseFormat
.withRequestData({
timestamp: new Date(),
Expand Down
58 changes: 58 additions & 0 deletions src/controllers/V1/fail2ban/fail2ban.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { Handler, StatusCodes } from "../../../../deps.ts";
import { Fail2banService } from "../../../services/fail2ban/fail2ban.service.ts";
import { ResponseFormat } from "../../../utils/api-response.ts";
import { Fail2banValidation } from "../../../validations/V1/fail2ban/fail2ban.validation.ts";

const getJail: Handler = (c) => {
const responseFormat = new ResponseFormat(c);
const fail2banService = new Fail2banService();

const jail = fail2banService.getJail();
return responseFormat
.withRequestData({
timestamp: new Date(),
method: c.req.method,
path: c.req.path,
})
.json(jail, StatusCodes.OK);
};

const banIpAddress: Handler = (c) => {
const paramParse = c.req.param();
const { ip } = Fail2banValidation.banIpAddress.parse(paramParse);

const responseFormat = new ResponseFormat(c);
const fail2banService = new Fail2banService();

const banned = fail2banService.banIpAddress(ip);
return responseFormat
.withRequestData({
timestamp: new Date(),
method: c.req.method,
path: c.req.path,
})
.json(banned, StatusCodes.OK);
};

const unbanIpAddress: Handler = (c) => {
const paramParse = c.req.param();
const { ip } = Fail2banValidation.unbanIpAddress.parse(paramParse);

const responseFormat = new ResponseFormat(c);
const fail2banService = new Fail2banService();

const unbanned = fail2banService.unbanIpAddress(ip);
return responseFormat
.withRequestData({
timestamp: new Date(),
method: c.req.method,
path: c.req.path,
})
.json(unbanned, StatusCodes.OK);
};

export const Fail2banController = {
getJail,
banIpAddress,
unbanIpAddress,
};
62 changes: 62 additions & 0 deletions src/controllers/V1/relay/relay.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { Handler, StatusCodes } from "../../../../deps.ts";
import { RelayService } from "../../../services/relay/relay.service.ts";
import { ResponseFormat } from "../../../utils/api-response.ts";
import { RelayValidation } from "../../../validations/V1/relay/relay.validation.ts";

const addAuth: Handler = async (c) => {
const bodyParse = await c.req.json();
const { domain, username, password } =
RelayValidation.addAuth.parse(bodyParse);

const responseFormat = new ResponseFormat(c);
const relayService = new RelayService();

const auth = relayService.addAuth(domain, username, password);
return responseFormat
.withRequestData({
timestamp: new Date(),
method: c.req.method,
path: c.req.path,
})
.json(auth, StatusCodes.OK);
};

const addDomain: Handler = async (c) => {
const bodyParse = await c.req.json();
const { domain, host, port } = RelayValidation.addDomain.parse(bodyParse);

const responseFormat = new ResponseFormat(c);
const relayService = new RelayService();

const domains = relayService.addDomain(domain, host, port);
return responseFormat
.withRequestData({
timestamp: new Date(),
method: c.req.method,
path: c.req.path,
})
.json(domains, StatusCodes.OK);
};

const excludeDomain: Handler = async (c) => {
const bodyParse = await c.req.json();
const { domain } = RelayValidation.addDomain.parse(bodyParse);

const responseFormat = new ResponseFormat(c);
const relayService = new RelayService();

const exclude = relayService.excludeDomain(domain);
return responseFormat
.withRequestData({
timestamp: new Date(),
method: c.req.method,
path: c.req.path,
})
.json(exclude, StatusCodes.OK);
};

export const RelayController = {
addAuth,
addDomain,
excludeDomain,
};
Loading

0 comments on commit 23948ab

Please sign in to comment.