2022-07-31 05:36:06 +00:00
|
|
|
import BaseProvider from "./baseProvider.js";
|
2022-08-14 12:21:21 +00:00
|
|
|
import { OnBeforeRequestDetailsType, OnRequestDetailsType } from "../types.js";
|
|
|
|
import { isDomain, isIp } from "../util.js";
|
2022-08-22 03:36:56 +00:00
|
|
|
import { DNS_RECORD_TYPE, DNSRecord, DNSResult } from "@lumeweb/libresolver";
|
2022-07-31 05:36:06 +00:00
|
|
|
|
|
|
|
export default class ServerProvider extends BaseProvider {
|
|
|
|
async shouldHandleRequest(
|
|
|
|
details: OnBeforeRequestDetailsType
|
|
|
|
): Promise<boolean> {
|
2022-08-22 03:36:56 +00:00
|
|
|
let dnsResult: DNSResult | boolean = await this.resolveDns(details, [
|
|
|
|
DNS_RECORD_TYPE.A,
|
|
|
|
DNS_RECORD_TYPE.CNAME,
|
|
|
|
]);
|
|
|
|
|
|
|
|
if (!dnsResult) {
|
2022-08-22 01:48:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
2022-07-31 05:36:06 +00:00
|
|
|
|
2022-08-22 03:36:56 +00:00
|
|
|
dnsResult = dnsResult as DNSResult;
|
|
|
|
|
|
|
|
let dnsRecord = dnsResult.records.shift()?.value as string;
|
|
|
|
|
|
|
|
if (isDomain(dnsRecord) || isIp(dnsRecord)) {
|
|
|
|
this.setData(details, "server", dnsRecord);
|
2022-07-31 05:36:06 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
async handleProxy(details: OnRequestDetailsType): Promise<any> {
|
2022-08-22 03:36:56 +00:00
|
|
|
const server = this.getData(details, "server");
|
2022-07-31 05:36:06 +00:00
|
|
|
|
2022-08-22 03:36:56 +00:00
|
|
|
return { type: "http", host: server, port: 80 };
|
2022-07-31 05:36:06 +00:00
|
|
|
}
|
|
|
|
}
|