From ed4885a611e708add00330f695294d0027e80478 Mon Sep 17 00:00:00 2001 From: internxt-yu Date: Wed, 18 Nov 2026 10:09:53 +0100 Subject: [PATCH] temp mail fix --- src/lib/mail-tm.ts | 98 +++++++++++++++++++++++++++++++--------------- 1 file changed, 66 insertions(+), 32 deletions(-) diff --git a/src/lib/mail-tm.ts b/src/lib/mail-tm.ts index 1bf4dc96f..9f62749e6 100644 --- a/src/lib/mail-tm.ts +++ b/src/lib/mail-tm.ts @@ -1,16 +1,11 @@ -import axios from 'axios'; import crypto from 'crypto'; import { isIP } from 'node:net'; +import tls from 'node:tls'; import { MessageObjProps } from '@/components/temp-email/types/types'; -const MAIL_TM_API = 'https://api.mail.tm'; - -const mailTm = axios.create({ - baseURL: MAIL_TM_API, - headers: { accept: 'application/json' }, - timeout: 15000, -}); +const MAIL_TM_HOST = 'api.mail.tm'; +const REQUEST_TIMEOUT_MS = 15000; interface MailTmAddress { address: string; @@ -28,9 +23,64 @@ interface MailTmMessage { html?: string[]; } -const randomHash = (bytes: number) => crypto.randomBytes(bytes).toString('hex'); +interface RequestOptions { + body?: unknown; + jwt?: string; + clientIp?: string; +} + +const send = (payload: string) => + new Promise<{ status: number; body: string }>((resolve, reject) => { + const socket = tls.connect({ host: MAIL_TM_HOST, port: 443, servername: MAIL_TM_HOST }, () => + socket.write(payload), + ); + let raw = ''; + + socket.setEncoding('utf8'); + socket.setTimeout(REQUEST_TIMEOUT_MS, () => socket.destroy(new Error('mail.tm request timed out'))); + socket.on('data', (chunk: string) => (raw += chunk)); + socket.on('error', reject); + socket.on('end', () => { + const headersEnd = raw.indexOf('\r\n\r\n'); -const forwardClientIp = (clientIp?: string) => (clientIp && isIP(clientIp) ? { 'x-forwarded-for': clientIp } : {}); + if (headersEnd < 0) return reject(new Error('mail.tm sent a malformed response')); + + resolve({ status: Number(raw.slice(9, 12)), body: raw.slice(headersEnd + 4) }); + }); + }); + +const request = async (method: string, url: string, options: RequestOptions = {}): Promise => { + const payload = options.body === undefined ? '' : JSON.stringify(options.body); + const headers = [ + `${method} ${url} HTTP/1.0`, + `Host: ${MAIL_TM_HOST}`, + 'Accept: application/json', + 'Connection: close', + ]; + + if (options.jwt) headers.push(`Authorization: Bearer ${options.jwt}`); + if (payload) headers.push('Content-Type: application/json', `Content-Length: ${Buffer.byteLength(payload)}`); + if (options.clientIp && isIP(options.clientIp)) headers.push(`X-Forwarded-For: ${options.clientIp}`); + + const { status, body } = await send(`${headers.join('\r\n')}\r\n\r\n${payload}`); + + if (status < 200 || status >= 300) { + throw Object.assign(new Error(`mail.tm responded ${status} for ${url}`), { + config: { url }, + response: { status, data: body }, + }); + } + + return body ? JSON.parse(body) : (undefined as T); +}; + +const getJwt = async (address: string, password: string, clientIp?: string): Promise => { + const { token } = await request<{ token: string }>('POST', '/token', { body: { address, password }, clientIp }); + + return token; +}; + +const randomHash = (bytes: number) => crypto.randomBytes(bytes).toString('hex'); const toAddress = (to: MailTmAddress | MailTmAddress[]) => (Array.isArray(to) ? to[0]?.address : to?.address); @@ -46,9 +96,7 @@ const toMessageObj = (message: MailTmMessage): MessageObjProps => ({ }); export const createAccount = async (clientIp?: string): Promise<{ address: string; token: string }> => { - const headers = forwardClientIp(clientIp); - - const { data: domains } = await mailTm.get<{ domain: string; isActive: boolean }[]>('/domains?page=1', { headers }); + const domains = await request<{ domain: string; isActive: boolean }[]>('GET', '/domains?page=1', { clientIp }); const domain = domains.find((item) => item.isActive)?.domain; @@ -57,29 +105,17 @@ export const createAccount = async (clientIp?: string): Promise<{ address: strin const address = `${randomHash(6)}@${domain}`; const password = randomHash(12); - await mailTm.post('/accounts', { address, password }, { headers }); + await request('POST', '/accounts', { body: { address, password }, clientIp }); return { address, token: password }; }; -const getJwt = async (address: string, password: string, clientIp?: string): Promise => { - const { data } = await mailTm.post<{ token: string }>( - '/token', - { address, password }, - { headers: forwardClientIp(clientIp) }, - ); - - return data.token; -}; - export const getInbox = async (address: string, password: string, clientIp?: string): Promise => { const jwt = await getJwt(address, password, clientIp); - const { data } = await mailTm.get('/messages?page=1', { - headers: { ...forwardClientIp(clientIp), authorization: `Bearer ${jwt}` }, - }); + const messages = await request('GET', '/messages?page=1', { jwt, clientIp }); - return data.map(toMessageObj); + return messages.map(toMessageObj); }; export const getMessage = async ( @@ -90,9 +126,7 @@ export const getMessage = async ( ): Promise => { const jwt = await getJwt(address, password, clientIp); - const { data } = await mailTm.get(`/messages/${encodeURIComponent(messageId)}`, { - headers: { ...forwardClientIp(clientIp), authorization: `Bearer ${jwt}` }, - }); + const message = await request('GET', `/messages/${encodeURIComponent(messageId)}`, { jwt, clientIp }); - return toMessageObj(data); + return toMessageObj(message); };