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 01:48:46 +00:00
|
|
|
import { DNSRecord } 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 01:48:46 +00:00
|
|
|
let dns: DNSRecord | boolean = await this.resolveDns(details);
|
|
|
|
if (!dns) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
dns = dns as DNSRecord;
|
2022-07-31 05:36:06 +00:00
|
|
|
|
2022-08-22 01:48:46 +00:00
|
|
|
if (dns && (isDomain(dns.value) || isIp(dns.value))) {
|
2022-07-31 05:36:06 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
async handleProxy(details: OnRequestDetailsType): Promise<any> {
|
|
|
|
const dns = await this.resolveDns(details);
|
|
|
|
|
2022-08-01 04:28:07 +00:00
|
|
|
if (isIp(dns) || isDomain(dns)) {
|
|
|
|
return { type: "http", host: dns, port: 80 };
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2022-07-31 05:36:06 +00:00
|
|
|
}
|
|
|
|
}
|