All files contact.js

80% Statements 100/125
75.55% Branches 34/45
100% Functions 9/9
80% Lines 100/125

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 1261x 1x 29x 29x 29x 29x 29x 29x 29x 29x 1x 1x 1x 1x 8x 8x 8x 8x 8x 8x 8x 2x 2x 6x     6x     6x 6x 6x 8x 8x 8x 8x 1x 1x 21x 21x 21x 21x 1x 21x 21x 21x             21x 21x 21x 21x 7x 7x 7x 14x 14x 14x 2x 2x 12x 12x 12x 14x           21x 2x 2x 21x 21x 1x 21x 21x 21x 6x 6x 15x 3x 3x 3x 12x 12x 4x 4x     4x 8x           8x 8x 8x 8x 6x 6x 2x 2x 6x 8x 8x 2x 2x 8x 12x       21x 21x 21x 21x 1x 1x  
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Contact = void 0;
const dns_1 = require("dns");
const net_1 = require("net");
function dnsLookup(hostname) {
    return __awaiter(this, void 0, void 0, function* () {
        return new Promise((resolve, reject) => {
            // console.log('dnsLookup', hostname);
            (0, dns_1.lookup)(hostname, { all: true }, (err, addresses) => {
                // console.log('addresses', hostname, addresses);
                if (err) {
                    reject(err);
                }
                else if (addresses === undefined) {
                    reject(new Error('No addresses found (undefined)'));
                }
                else if (addresses.length === 0) {
                    reject(new Error('No addresses found (len 0)'));
                }
                else {
                    resolve(addresses[0].address);
                }
            });
        });
    });
}
class Contact {
    constructor() {
        this.address = null;
        this.port = null;
        this.is_valid = false;
    }
    static parse(raw) {
        const contact = new Contact();
        let items;
        if (raw.includes('[') && raw.includes(']')) {
            items = raw.split(']');
            items = [
                items[0].slice(1),
                items[1].slice(1),
            ];
        }
        else {
            items = raw.split(':');
        }
        if (items.length === 1) {
            contact.address = items[0];
            contact.port = null;
        }
        else if (items.length === 2) {
            contact.address = items[0];
            if (items[1] === '') {
                contact.port = null;
            }
            else {
                contact.port = parseInt(items[1]);
            }
        }
        else if (items.length > 2) {
            // IPv6 address with port
            contact.address = items.slice(0, items.length - 1).join(':');
            contact.port = parseInt(items[items.length - 1]);
        }
        if (contact.address === '') {
            contact.address = 'private';
        }
        return contact;
    }
    static resolve(raw, raddr = null) {
        return __awaiter(this, void 0, void 0, function* () {
            const contact = this.parse(raw);
            if (contact.address === 'public') {
                contact.address = raddr;
            }
            else if (contact.address === 'private') {
                contact.address = null;
                contact.port = null;
            }
            else if (contact.address !== null) {
                if ((0, net_1.isIPv4)(contact.address)) {
                    // Addr is IPv4 address, we can use it directly.
                    if (contact.address.slice(0, 4) === '127.') {
                        contact.address = null;
                    }
                }
                else if ((0, net_1.isIPv6)(contact.address)) {
                    // Addr is IPv6 address, we can use it directly.
                    if (contact.address == '::' || contact.address == '::1') {
                        contact.address = null;
                    }
                }
                else {
                    // Addr is host, we have to resolve it.
                    try {
                        const address = yield dnsLookup(contact.address);
                        if ((0, net_1.isIPv4)(address)) {
                            if (address.slice(0, 4) === '127.' || address == '::1') {
                                contact.address = null;
                            }
                        }
                    }
                    catch (error) {
                        contact.address = null;
                    }
                }
            }
            else {
                contact.address = null;
            }
            contact.is_valid = contact.address !== null && contact.port !== null;
            return contact;
        });
    }
}
exports.Contact = Contact;