update
This commit is contained in:
@ -5,6 +5,7 @@ import type { IAcmeOptions } from './models/interfaces.js';
|
||||
import { CertStore } from './cert-store.js';
|
||||
import type { AcmeStateManager } from './acme-state-manager.js';
|
||||
import { logger } from '../../core/utils/logger.js';
|
||||
import { SocketHandlers } from './utils/route-helpers.js';
|
||||
|
||||
export interface ICertStatus {
|
||||
domain: string;
|
||||
@ -694,70 +695,53 @@ export class SmartCertManager {
|
||||
},
|
||||
action: {
|
||||
type: 'socket-handler',
|
||||
socketHandler: (socket, context) => {
|
||||
// Wait for HTTP request data
|
||||
socket.once('data', async (data) => {
|
||||
const request = data.toString();
|
||||
const lines = request.split('\r\n');
|
||||
const [method, path] = lines[0].split(' ');
|
||||
|
||||
// Extract the token from the path
|
||||
const token = path?.split('/').pop();
|
||||
if (!token) {
|
||||
socket.write('HTTP/1.1 404 Not Found\r\n');
|
||||
socket.write('Content-Type: text/plain\r\n');
|
||||
socket.write('Content-Length: 9\r\n');
|
||||
socket.write('Connection: close\r\n');
|
||||
socket.write('\r\n');
|
||||
socket.write('Not found');
|
||||
socket.end();
|
||||
return;
|
||||
}
|
||||
|
||||
// Create mock request/response objects for SmartAcme
|
||||
const mockReq = {
|
||||
url: path,
|
||||
method: 'GET',
|
||||
headers: {}
|
||||
};
|
||||
socketHandler: SocketHandlers.httpServer((req, res) => {
|
||||
// Extract the token from the path
|
||||
const token = req.url?.split('/').pop();
|
||||
if (!token) {
|
||||
res.status(404);
|
||||
res.send('Not found');
|
||||
return;
|
||||
}
|
||||
|
||||
let responseData: any = null;
|
||||
const mockRes = {
|
||||
statusCode: 200,
|
||||
setHeader: (name: string, value: string) => {},
|
||||
end: (data: any) => {
|
||||
responseData = data;
|
||||
}
|
||||
};
|
||||
|
||||
// Use SmartAcme's handler
|
||||
const handled = await new Promise<boolean>((resolve) => {
|
||||
http01Handler.handleRequest(mockReq as any, mockRes as any, () => {
|
||||
resolve(false);
|
||||
});
|
||||
// Give it a moment to process
|
||||
setTimeout(() => resolve(true), 100);
|
||||
// Create mock request/response objects for SmartAcme
|
||||
let responseData: any = null;
|
||||
const mockReq = {
|
||||
url: req.url,
|
||||
method: req.method,
|
||||
headers: req.headers
|
||||
};
|
||||
|
||||
const mockRes = {
|
||||
statusCode: 200,
|
||||
setHeader: (name: string, value: string) => {},
|
||||
end: (data: any) => {
|
||||
responseData = data;
|
||||
}
|
||||
};
|
||||
|
||||
// Use SmartAcme's handler
|
||||
const handleAcme = () => {
|
||||
http01Handler.handleRequest(mockReq as any, mockRes as any, () => {
|
||||
// Not handled by ACME
|
||||
res.status(404);
|
||||
res.send('Not found');
|
||||
});
|
||||
|
||||
if (handled && responseData) {
|
||||
const body = String(responseData);
|
||||
socket.write(`HTTP/1.1 ${mockRes.statusCode} OK\r\n`);
|
||||
socket.write('Content-Type: text/plain\r\n');
|
||||
socket.write(`Content-Length: ${body.length}\r\n`);
|
||||
socket.write('Connection: close\r\n');
|
||||
socket.write('\r\n');
|
||||
socket.write(body);
|
||||
} else {
|
||||
socket.write('HTTP/1.1 404 Not Found\r\n');
|
||||
socket.write('Content-Type: text/plain\r\n');
|
||||
socket.write('Content-Length: 9\r\n');
|
||||
socket.write('Connection: close\r\n');
|
||||
socket.write('\r\n');
|
||||
socket.write('Not found');
|
||||
}
|
||||
socket.end();
|
||||
});
|
||||
}
|
||||
// Give it a moment to process, then send response
|
||||
setTimeout(() => {
|
||||
if (responseData) {
|
||||
res.header('Content-Type', 'text/plain');
|
||||
res.send(String(responseData));
|
||||
} else {
|
||||
res.status(404);
|
||||
res.send('Not found');
|
||||
}
|
||||
}, 100);
|
||||
};
|
||||
|
||||
handleAcme();
|
||||
})
|
||||
}
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user