133 lines
4.1 KiB
TypeScript
133 lines
4.1 KiB
TypeScript
import https from 'node:https'
|
|
import { performance } from 'node:perf_hooks'
|
|
import { randomBytes } from 'node:crypto'
|
|
|
|
const HOSTNAME = 'www.keyescape.com'
|
|
|
|
export const EDGE_USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/151.0.0.0 Safari/537.36 Edg/151.0.0.0'
|
|
|
|
export interface EdgeHeaderOptions {
|
|
accept?: string
|
|
referer?: string
|
|
navigation?: boolean
|
|
xhr?: boolean
|
|
}
|
|
|
|
export function edgeBrowserHeaders(options: EdgeHeaderOptions = {}): Record<string, string> {
|
|
const headers: Record<string, string> = {
|
|
Accept: options.accept ?? (options.navigation
|
|
? 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8'
|
|
: '*/*'),
|
|
'Accept-Language': 'ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7',
|
|
'User-Agent': EDGE_USER_AGENT,
|
|
'Sec-CH-UA': '"Not=A?Brand";v="99", "Microsoft Edge";v="151", "Chromium";v="151"',
|
|
'Sec-CH-UA-Mobile': '?0',
|
|
'Sec-CH-UA-Platform': '"Windows"',
|
|
'Sec-Fetch-Site': 'same-origin',
|
|
'Sec-Fetch-Mode': options.navigation ? 'navigate' : 'cors',
|
|
'Sec-Fetch-Dest': options.navigation ? 'document' : 'empty',
|
|
}
|
|
if (options.navigation) {
|
|
headers['Sec-Fetch-User'] = '?1'
|
|
headers['Upgrade-Insecure-Requests'] = '1'
|
|
}
|
|
if (options.xhr) headers['X-Requested-With'] = 'XMLHttpRequest'
|
|
if (options.referer) headers.Referer = options.referer
|
|
return headers
|
|
}
|
|
|
|
export const keyescapeAgent = new https.Agent({
|
|
keepAlive: true,
|
|
keepAliveMsecs: 15_000,
|
|
maxSockets: 2,
|
|
maxFreeSockets: 2,
|
|
scheduling: 'lifo',
|
|
})
|
|
|
|
export interface KeyescapeResponse {
|
|
statusCode: number
|
|
headers: https.IncomingHttpHeaders
|
|
body: Buffer
|
|
sentAtMs: number
|
|
receivedAtMs: number
|
|
sentMonoMs: number
|
|
receivedMonoMs: number
|
|
rttMs: number
|
|
remoteIp: string
|
|
reusedSocket: boolean
|
|
}
|
|
|
|
export interface KeyescapeRequestOptions {
|
|
method: 'GET' | 'HEAD' | 'POST'
|
|
path: string
|
|
headers?: Record<string, string>
|
|
body?: Buffer
|
|
timeoutMs?: number
|
|
}
|
|
|
|
export function requestKeyescape(options: KeyescapeRequestOptions): Promise<KeyescapeResponse> {
|
|
return new Promise((resolve, reject) => {
|
|
const sentAtMs = Date.now()
|
|
const sentMonoMs = performance.now()
|
|
let remoteIp = ''
|
|
|
|
const request = https.request({
|
|
hostname: HOSTNAME,
|
|
port: 443,
|
|
method: options.method,
|
|
path: options.path,
|
|
agent: keyescapeAgent,
|
|
headers: {
|
|
...edgeBrowserHeaders(),
|
|
Connection: 'keep-alive',
|
|
...options.headers,
|
|
},
|
|
}, (response) => {
|
|
const receivedAtMs = Date.now()
|
|
const receivedMonoMs = performance.now()
|
|
const chunks: Buffer[] = []
|
|
response.on('data', (chunk: Buffer) => chunks.push(Buffer.from(chunk)))
|
|
response.on('end', () => {
|
|
resolve({
|
|
statusCode: response.statusCode ?? 0,
|
|
headers: response.headers,
|
|
body: Buffer.concat(chunks),
|
|
sentAtMs,
|
|
receivedAtMs,
|
|
sentMonoMs,
|
|
receivedMonoMs,
|
|
rttMs: receivedMonoMs - sentMonoMs,
|
|
remoteIp,
|
|
reusedSocket: request.reusedSocket,
|
|
})
|
|
})
|
|
})
|
|
|
|
request.setTimeout(options.timeoutMs ?? 12_000, () => {
|
|
request.destroy(new Error('예약 서버 응답 시간이 초과되었습니다.'))
|
|
})
|
|
request.on('socket', (socket) => {
|
|
const captureAddress = () => { remoteIp = socket.remoteAddress ?? remoteIp }
|
|
socket.once('connect', captureAddress)
|
|
socket.once('secureConnect', captureAddress)
|
|
if (socket.remoteAddress) captureAddress()
|
|
})
|
|
request.on('error', reject)
|
|
if (options.body) request.write(options.body)
|
|
request.end()
|
|
})
|
|
}
|
|
|
|
export function encodeMultipart(fields: Record<string, string>) {
|
|
const boundary = `----WebKitFormBoundary${randomBytes(12).toString('base64url').slice(0, 16)}`
|
|
const chunks: Buffer[] = []
|
|
for (const [name, value] of Object.entries(fields)) {
|
|
chunks.push(Buffer.from(
|
|
`--${boundary}\r\nContent-Disposition: form-data; name="${name.replaceAll('"', '')}"\r\n\r\n${value}\r\n`,
|
|
'utf8',
|
|
))
|
|
}
|
|
chunks.push(Buffer.from(`--${boundary}--\r\n`, 'utf8'))
|
|
return { boundary, body: Buffer.concat(chunks) }
|
|
}
|