UNPKG

socks5-addr-extract

Version:

Simple functions of extracting address from SOCKS5 RFC1928 request

86 lines (80 loc) 2.37 kB
const { ipv4Extractor, ipv6Extractor, domainExtractor } = require("./extractors"); describe("extractors", function() { it("ipv4", function() { const buf = Buffer.alloc(10); buf.writeUInt8(0x00, 0); buf.writeUInt8(0x00, 1); buf.writeUInt8(0x00, 2); //typ buf.writeUInt8(0x01, 3); //ipv4 buf.writeUInt8(0x0a, 4); buf.writeUInt8(0x11, 5); buf.writeUInt8(0x12, 6); buf.writeUInt8(0x21, 7); //port buf.writeUInt16BE(0x276a, 8); const eqfn = ipv4Extractor.eqFunc; expect(eqfn).toBeInstanceOf(Function); expect(eqfn(buf)).toBe(true); const getAddr = ipv4Extractor.getAddr; expect(getAddr).toBeInstanceOf(Function); const addr = getAddr(buf); expect(addr.address).toEqual("10.17.18.33"); expect(addr.port).toBe(10090); }); it("ipv6", function () { const buf = Buffer.alloc(22) buf.writeUInt8(0x00, 0); buf.writeUInt8(0x00, 1); buf.writeUInt8(0x00, 2); //typ buf.writeUInt8(0x04, 3) //ipv6 buf.writeUInt16BE(0x2001, 4) buf.writeUInt16BE(0x0db8, 6) buf.writeUInt16BE(0x85a3, 8) buf.writeUInt16BE(0x0000, 10) buf.writeUInt16BE(0x0000, 12) buf.writeUInt16BE(0x8a2e, 14) buf.writeUInt16BE(0x0370, 16) buf.writeUInt16BE(0x7334, 18) //port buf.writeUInt16BE(0x276a, 20); const eqfn = ipv6Extractor.eqFunc expect(eqfn).toBeInstanceOf(Function); expect(eqfn(buf)).toBe(true) const getAddr = ipv6Extractor.getAddr expect(getAddr).toBeInstanceOf(Function) const addr = getAddr(buf) expect(addr.address).toEqual("2001:0db8:85a3:0000:0000:8a2e:0370:7334") expect(addr.port).toBe(10090); }) it("domain", function () { const str = "httpbin.org" const len = str.length const buf = Buffer.alloc(len + 6 + 1) buf.writeUInt8(0x00, 0); buf.writeUInt8(0x00, 1); buf.writeUInt8(0x00, 2); //typ buf.writeUInt8(0x03, 3) //domain buf.writeUInt8(len, 4) buf.write(str, 5, len, "utf8") //port buf.writeUInt16BE(0x276b, 5 + len); const eqfn = domainExtractor.eqFunc expect(eqfn).toBeInstanceOf(Function); expect(eqfn(buf)).toBe(true) const getAddr = domainExtractor.getAddr expect(getAddr).toBeInstanceOf(Function) const addr = getAddr(buf) expect(addr.address).toEqual(str) expect(addr.port).toBe(10091); }) });