Compare commits

..

No commits in common. "6a5b5cee47e602288cde28c0c264219aef5f11a9" and "abe9c6994d156a30d6468624af1971e5c7373f48" have entirely different histories.

8 changed files with 20 additions and 109 deletions

3
.gitignore vendored
View File

@ -137,7 +137,4 @@ drizzle/
bun.lockb
migrate.txt
db.sqlite3
ratelimit.json
# Docker
docker-compose.yml

View File

@ -1,6 +1,8 @@
# mail-connect
API bridge for docker-mailserver. **Still in beta.**
API bridge for docker-mailserver
*mail-connect is still in early beta*
## What is it
@ -10,7 +12,7 @@ We provide an extendable API which interacts with the `setup` utility via a Dock
## What this API is NOT
This API is insecure by nature, however not completely. `mail-connect` is intended to be an API which is used internally **only**. The systems connected to this API should have proper protections against abuse. Think about it... would you like me to direct your mailserver security? I sure hope not...
This API is insecure by nature, however not completely. `mail-connect` is intended to be an API which is used internally _only_. The systems connected to this API should have proper protections against abuse. Think about it... would you like me to direct your mailserver security? I sure hope not...
As such, users who have access to this API are able to create unlimited accounts, and modify anyone's email address. Thus, your code should be the only user of this API. Once again, **do not make this API public**.
@ -35,13 +37,12 @@ All features marked with an **E** are extended features, and are not a part of t
bunx drizzle-kit migrate
```
3. **Copy, move, and modify necessary files**
3. **Copy/modify necessary files**
```bash
mv docker-compose.yml.example docker-compose.yml # depending on your use case, you might have to change some things here
touch migrate.txt # put emails (one per line) which already exist on the server which users can claim
mv .env.example .env # you don't need to change anything here
mv ratelimit.json.example ratelimit.json # customize to your liking
cp .env.example .env # you don't need to change anything here
vim ratelimit.json # optional, customize to your liking...
```
**Note:** If you are running mail-connect outside a Docker container (or changing the binds), please change the `MAILCONNECT_ROOT_DIR` to match your environment.
@ -58,13 +59,13 @@ All features marked with an **E** are extended features, and are not a part of t
### Email
- [X] Create account/email
- [X] List accounts
- [X] **E** View individual account details
- [X] **E** Create an account from file
- [X] Create email
- [X] List emails
- [X] **E** View individual user details
- [X] **E** Create email from file
- [X] Change password
- [X] Delete account/email
- [ ] Restrict account
- [ ] Delete email
- [ ] Restrict email
### Alias
@ -112,3 +113,8 @@ All features marked with an **E** are extended features, and are not a part of t
I plan to implement a *much* more powerful API, when everything else has settled. I will be taking a look at the setup utility itself, and seeing how a more efficient approach can be taken.
Since `docker-mailserver` is built on Dovecot and Postfix, I am confident we can improve this API to be speedy and efficient as ever.
## To-Do
- [ ] Implement aforementioned features
- [ ] Swagger support

View File

@ -1,7 +1,6 @@
{
"name": "mail-connect",
"module": "src/server.ts",
"version": "0.1.2",
"type": "module",
"scripts": {
"start": "bun src/server.js",

View File

@ -1,80 +0,0 @@
import { Request, Response } from "express";
import { accounts } from "../../db/schema";
import { eq } from "drizzle-orm";
import { validateEmail } from "../../utils/validators";
import { drizzle } from "drizzle-orm/bun-sqlite";
import { containerExec } from "../../utils/docker";
import { updateAccountsCache } from "../../utils/updateAccountsCache";
const db = drizzle(process.env.DB_FILE_NAME!);
export const deleteAccount = async (req: Request, res: Response): Promise<void> => {
const { email } = req.body;
if (!email) {
console.log("[!] Error\nTASK| deleteAccount\nERR | Missing email\nACC |", email);
res.status(400).json({ success: false, error: "Missing email" });
return
}
if (!validateEmail(email)) {
console.log("[!] Error\nTASK| deleteAccount\nERR | Invalid email format\nACC |", email);
res.status(400).json({ success: false, error: "Invalid email format" });
return;
}
const accInDb = await db
.select()
.from(accounts)
.where(eq(accounts.email, email))
.limit(1);
if (!accInDb || accInDb.length === 0) {
console.log("[!] Error\nTASK| deleteAccount\nERR | Account not found\nACC |", email);
res.status(404).json({ success: false, error: "Account not found" });
return;
}
console.log("[*] Task started\nTASK| deleteAccount\nACC |", email);
try {
await db.delete(accounts).where(eq(accounts.email, email));
console.log("[+] Internal account deleted\nTASK| deleteAccount\nACC |", email);
try {
const output = await containerExec(["setup", "email", "del", email]);
if (/ERROR/i.test(output)) {
console.log("[!] Error\nTASK| deleteAccount\nERR | Mail account removal failed\nACC |", email);
res.status(500).json({ success: false, error: "Mail account removal failed" });
return;
} else {
console.log("[+] Mail account removed\nTASK| deleteAccount\nACC |", email);
}
} catch (err) {
console.log("[!] Error\nTASK| deleteAccount\nERR | Mail account removal failed\nACC |", email);
res.status(500).json({ success: false, error: "Mail account removal failed" });
return;
}
console.log("[-] Updating accounts cache");
await updateAccountsCache();
console.log("[-] Performing final checks");
const accInDb = await db
.select()
.from(accounts)
.where(eq(accounts.email, email))
.limit(1);
if (!accInDb || accInDb.length === 0) {
console.log("[*] Task completed\nTASK| deleteAccount\nACC |", email);
res.json({ success: true });
} else {
console.log("[!] Error\nTASK| deleteAccount\nERR | Account not found\nACC |", email);
res.status(404).json({ success: false, error: "Account not found" });
}
} catch (err) {
console.log("[!] Error\nTASK| deleteAccount\nERR | Unspecified error\nACC |", email);
res.status(500).json({ success: false, error: (err as Error).message });
}
};

View File

@ -6,8 +6,6 @@ import { getUserAccount } from "./actions/accounts/getUserAccount";
import { updatePassword } from "./actions/accounts/updatePassword";
import { addAccount } from "./actions/accounts/addAccount";
import initialChecks from "./actions/initialChecks";
import { deleteAccount } from "./actions/accounts/deleteAccount";
import { updateAccountsCache } from "./utils/updateAccountsCache";
const app = express();
app.use(express.json());
@ -21,11 +19,6 @@ if (!rCResult) {
console.log("\n==== SELF CHECK PASS ====\n");
}
// Get version
const pkg = Bun.file("package.json");
const pkgData = await pkg.json();
const version = pkgData.version;
interface RateLimitOptions {
windowMs: number;
limit: number;
@ -44,22 +37,19 @@ app.get("/accounts/list", listAccounts);
app.post("/accounts/user", getUserAccount);
app.post("/accounts/update/password", updatePassword);
app.post("/accounts/add", addAccount);
app.post("/accounts/delete", deleteAccount);
const PORT = 3000;
app.listen(PORT, () => {
figlet('mail-connect', (err, data) => {
if (err) {
console.log('mail-connect');
console.log(`Version: ${version}`);
console.log('Version: 0.1.1');
console.log(`API listening on port ${PORT}\n`);
console.log("[!] " + err);
updateAccountsCache();
} else {
console.log(data);
console.log(`Version: ${version}`);
console.log('Version: 0.1.1');
console.log(`API listening on port ${PORT}\n`);
updateAccountsCache();
}
});
});

View File

@ -14,5 +14,4 @@ export const updateAccountsCache = async () => {
.insert(cacheInfo)
.values({ lastUpdated: Date.now() })
.onConflictDoUpdate({ target: cacheInfo.id, set: { lastUpdated: Date.now() } });
console.log("[+] Accounts cache updated");
};