werift 0.15.4 → 0.15.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/common/src/promise.d.ts +4 -3
- package/lib/common/src/promise.js +11 -5
- package/lib/common/src/promise.js.map +1 -1
- package/lib/dtls/src/context/cipher.js +2 -2
- package/lib/dtls/src/context/cipher.js.map +1 -1
- package/lib/ice/src/dns/lookup.d.ts +9 -0
- package/lib/ice/src/dns/lookup.js +66 -0
- package/lib/ice/src/dns/lookup.js.map +1 -0
- package/lib/ice/src/ice.d.ts +2 -0
- package/lib/ice/src/ice.js +20 -17
- package/lib/ice/src/ice.js.map +1 -1
- package/lib/ice/src/transport.js +2 -1
- package/lib/ice/src/transport.js.map +1 -1
- package/lib/ice/src/utils.d.ts +1 -0
- package/lib/ice/src/utils.js +9 -1
- package/lib/ice/src/utils.js.map +1 -1
- package/lib/webrtc/src/peerConnection.d.ts +3 -1
- package/lib/webrtc/src/peerConnection.js +55 -31
- package/lib/webrtc/src/peerConnection.js.map +1 -1
- package/lib/webrtc/src/transport/ice.d.ts +1 -1
- package/lib/webrtc/src/transport/ice.js +3 -3
- package/lib/webrtc/src/transport/ice.js.map +1 -1
- package/package.json +1 -1
- package/src/peerConnection.ts +124 -96
- package/src/transport/ice.ts +3 -3
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
export declare class PromiseQueue {
|
|
2
2
|
queue: {
|
|
3
|
-
promise: () => Promise<
|
|
4
|
-
done: () => void;
|
|
3
|
+
promise: () => Promise<unknown>;
|
|
4
|
+
done: (...args: any[]) => void;
|
|
5
|
+
failed: (...args: any[]) => void;
|
|
5
6
|
}[];
|
|
6
7
|
running: boolean;
|
|
7
|
-
push: (promise: () => Promise<
|
|
8
|
+
push: <T>(promise: () => Promise<T>) => Promise<T>;
|
|
8
9
|
private run;
|
|
9
10
|
}
|
|
@@ -5,18 +5,24 @@ class PromiseQueue {
|
|
|
5
5
|
constructor() {
|
|
6
6
|
this.queue = [];
|
|
7
7
|
this.running = false;
|
|
8
|
-
this.push = (promise) => new Promise((r) => {
|
|
9
|
-
this.queue.push({ promise, done: r });
|
|
10
|
-
if (!this.running)
|
|
8
|
+
this.push = (promise) => new Promise((r, f) => {
|
|
9
|
+
this.queue.push({ promise, done: r, failed: f });
|
|
10
|
+
if (!this.running) {
|
|
11
11
|
this.run();
|
|
12
|
+
}
|
|
12
13
|
});
|
|
13
14
|
}
|
|
14
15
|
async run() {
|
|
15
16
|
const task = this.queue.shift();
|
|
16
17
|
if (task) {
|
|
17
18
|
this.running = true;
|
|
18
|
-
|
|
19
|
-
|
|
19
|
+
try {
|
|
20
|
+
const res = await task.promise();
|
|
21
|
+
task.done(res);
|
|
22
|
+
}
|
|
23
|
+
catch (error) {
|
|
24
|
+
task.failed(error);
|
|
25
|
+
}
|
|
20
26
|
this.run();
|
|
21
27
|
}
|
|
22
28
|
else {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"promise.js","sourceRoot":"","sources":["../../../../common/src/promise.ts"],"names":[],"mappings":";;;AAAA,MAAa,YAAY;IAAzB;QACE,UAAK,
|
|
1
|
+
{"version":3,"file":"promise.js","sourceRoot":"","sources":["../../../../common/src/promise.ts"],"names":[],"mappings":";;;AAAA,MAAa,YAAY;IAAzB;QACE,UAAK,GAIC,EAAE,CAAC;QACT,YAAO,GAAG,KAAK,CAAC;QAEhB,SAAI,GAAG,CAAI,OAAyB,EAAE,EAAE,CACtC,IAAI,OAAO,CAAI,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE;YACtB,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,CAAC,CAAC;YACjD,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE;gBACjB,IAAI,CAAC,GAAG,EAAE,CAAC;aACZ;QACH,CAAC,CAAC,CAAC;IAmBP,CAAC;IAjBS,KAAK,CAAC,GAAG;QACf,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC;QAChC,IAAI,IAAI,EAAE;YACR,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC;YAEpB,IAAI;gBACF,MAAM,GAAG,GAAG,MAAM,IAAI,CAAC,OAAO,EAAE,CAAC;gBACjC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;aAChB;YAAC,OAAO,KAAK,EAAE;gBACd,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;aACpB;YAED,IAAI,CAAC,GAAG,EAAE,CAAC;SACZ;aAAM;YACL,IAAI,CAAC,OAAO,GAAG,KAAK,CAAC;SACtB;IACH,CAAC;CACF;AAjCD,oCAiCC","sourcesContent":["export class PromiseQueue {\n queue: {\n promise: () => Promise<unknown>;\n done: (...args: any[]) => void;\n failed: (...args: any[]) => void;\n }[] = [];\n running = false;\n\n push = <T>(promise: () => Promise<T>) =>\n new Promise<T>((r, f) => {\n this.queue.push({ promise, done: r, failed: f });\n if (!this.running) {\n this.run();\n }\n });\n\n private async run() {\n const task = this.queue.shift();\n if (task) {\n this.running = true;\n\n try {\n const res = await task.promise();\n task.done(res);\n } catch (error) {\n task.failed(error);\n }\n\n this.run();\n } else {\n this.running = false;\n }\n }\n}\n"]}
|
|
@@ -32,7 +32,7 @@ const x509_1 = require("@fidm/x509");
|
|
|
32
32
|
const webcrypto_1 = require("@peculiar/webcrypto");
|
|
33
33
|
const x509 = __importStar(require("@peculiar/x509"));
|
|
34
34
|
const binary_data_1 = require("binary-data");
|
|
35
|
-
const crypto_1 = require("crypto");
|
|
35
|
+
const crypto_1 = __importStar(require("crypto"));
|
|
36
36
|
const addYears_1 = __importDefault(require("date-fns/addYears"));
|
|
37
37
|
const const_1 = require("../cipher/const");
|
|
38
38
|
const prf_1 = require("../cipher/prf");
|
|
@@ -165,7 +165,7 @@ CipherContext.createSelfSignedCertificateWithKey = async (signatureHash, namedCu
|
|
|
165
165
|
})();
|
|
166
166
|
const keys = await crypto.subtle.generateKey(alg, true, ["sign", "verify"]);
|
|
167
167
|
const cert = await x509.X509CertificateGenerator.createSelfSigned({
|
|
168
|
-
serialNumber: "
|
|
168
|
+
serialNumber: crypto_1.default.randomBytes(8).toString("hex"),
|
|
169
169
|
name: "C=AU, ST=Some-State, O=Internet Widgits Pty Ltd",
|
|
170
170
|
notBefore: new Date(),
|
|
171
171
|
notAfter: (0, addYears_1.default)(Date.now(), 10),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cipher.js","sourceRoot":"","sources":["../../../../../dtls/src/context/cipher.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,qCAAqD;AACrD,mDAA6C;AAC7C,qDAAuC;AACvC,6CAAoD;AACpD,mCAAoC;AACpC,iEAAyC;AAEzC,2CAQyB;AAEzB,uCAAyE;AACzE,wDAAsE;AAEtE,gDAAsD;AAItD,MAAM,MAAM,GAAG,IAAI,kBAAM,EAAE,CAAC;AAC5B,IAAI,CAAC,cAAc,CAAC,GAAG,CAAC,MAAa,CAAC,CAAC;AAEvC,MAAa,aAAa;IAcxB,YACS,WAAyB,EACzB,OAAgB,EAChB,MAAe,EACtB,sBAAsC;QAH/B,gBAAW,GAAX,WAAW,CAAc;QACzB,YAAO,GAAP,OAAO,CAAS;QAChB,WAAM,GAAN,MAAM,CAAS;QAGtB,IAAI,OAAO,IAAI,MAAM,IAAI,sBAAsB,EAAE;YAC/C,IAAI,CAAC,SAAS,CAAC,OAAO,EAAE,MAAM,EAAE,sBAAsB,CAAC,CAAC;SACzD;IACH,CAAC;IA2ED,aAAa,CAAC,GAAkB;QAC9B,MAAM,MAAM,GAAG,GAAG,CAAC,iBAAiB,CAAC;QACrC,MAAM,GAAG,GAAG,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,WAAW,EAAE,GAAG,CAAC,QAAQ,EAAE;YAC9D,IAAI,EAAE,MAAM,CAAC,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AAC,QAAQ,CAAC,KAAK,CAAC,CAAC;IACrC,MAAM,MAAM,GAAG,IAAI,CAAC,YAAY,CAAC,MAAM,CACrC,MAAM,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,OAAO,EAAE,IAAI,CAAC,UAAiB,CAAC,EAC9D,aAAa,CACd,CAAC;IAEF,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,CAAC;AAC5C,CAAE,CAAA","sourcesContent":["import { Certificate, PrivateKey } from \"@fidm/x509\";\nimport { Crypto } from \"@peculiar/webcrypto\";\nimport * as x509 from \"@peculiar/x509\";\nimport { decode, encode, types } from \"binary-data\";\nimport { createSign } from \"crypto\";\nimport addYears from \"date-fns/addYears\";\n\nimport {\n CipherSuites,\n CurveType,\n HashAlgorithm,\n NamedCurveAlgorithm,\n NamedCurveAlgorithms,\n SignatureAlgorithm,\n SignatureHash,\n} from \"../cipher/const\";\nimport { NamedCurveKeyPair } from \"../cipher/namedCurve\";\nimport { prfVerifyDataClient, prfVerifyDataServer } from \"../cipher/prf\";\nimport { SessionType, SessionTypes } from \"../cipher/suites/abstract\";\nimport AEADCipher from \"../cipher/suites/aead\";\nimport { ProtocolVersion } from \"../handshake/binary\";\nimport { DtlsRandom } from \"../handshake/random\";\nimport { DtlsPlaintext } from \"../record/message/plaintext\";\n\nconst crypto = new Crypto();\nx509.cryptoProvider.set(crypto as any);\n\nexport class CipherContext {\n localRandom!: DtlsRandom;\n remoteRandom!: DtlsRandom;\n cipherSuite!: CipherSuites;\n remoteCertificate?: Buffer;\n remoteKeyPair!: Partial<NamedCurveKeyPair>;\n localKeyPair!: NamedCurveKeyPair;\n masterSecret!: Buffer;\n cipher!: AEADCipher;\n namedCurve!: NamedCurveAlgorithms;\n signatureHashAlgorithm?: SignatureHash;\n localCert!: Buffer;\n localPrivateKey!: PrivateKey;\n\n constructor(\n public sessionType: SessionTypes,\n public certPem?: string,\n public keyPem?: string,\n signatureHashAlgorithm?: SignatureHash\n ) {\n if (certPem && keyPem && signatureHashAlgorithm) {\n this.parseX509(certPem, keyPem, signatureHashAlgorithm);\n }\n }\n\n /**\n *\n * @param signatureHash\n * @param namedCurveAlgorithm necessary when use ecdsa\n */\n static createSelfSignedCertificateWithKey = async (\n signatureHash: SignatureHash,\n namedCurveAlgorithm?: NamedCurveAlgorithms\n ) => {\n const signatureAlgorithmName = (() => {\n switch (signatureHash.signature) {\n case SignatureAlgorithm.rsa_1:\n return \"RSASSA-PKCS1-v1_5\";\n case SignatureAlgorithm.ecdsa_3:\n return \"ECDSA\";\n }\n })();\n const hash = (() => {\n switch (signatureHash.hash) {\n case HashAlgorithm.sha256_4:\n return \"SHA-256\";\n }\n })();\n const namedCurve = (() => {\n switch (namedCurveAlgorithm) {\n case NamedCurveAlgorithm.secp256r1_23:\n return \"P-256\";\n case NamedCurveAlgorithm.x25519_29:\n // todo fix (X25519 not supported with ECDSA)\n if (signatureAlgorithmName === \"ECDSA\") {\n return \"P-256\";\n }\n return \"X25519\";\n default: {\n if (signatureAlgorithmName === \"ECDSA\") return \"P-256\";\n if (signatureAlgorithmName === \"RSASSA-PKCS1-v1_5\") return \"X25519\";\n }\n }\n })();\n const alg = (() => {\n switch (signatureAlgorithmName) {\n case \"ECDSA\":\n return { name: signatureAlgorithmName, hash, namedCurve };\n case \"RSASSA-PKCS1-v1_5\":\n return {\n name: signatureAlgorithmName,\n hash,\n publicExponent: new Uint8Array([1, 0, 1]),\n modulusLength: 2048,\n };\n }\n })();\n\n const keys = await crypto.subtle.generateKey(alg, true, [\"sign\", \"verify\"]);\n\n const cert = await x509.X509CertificateGenerator.createSelfSigned({\n serialNumber: \"01\",\n name: \"C=AU, ST=Some-State, O=Internet Widgits Pty Ltd\",\n notBefore: new Date(),\n notAfter: addYears(Date.now(), 10),\n signingAlgorithm: alg,\n keys,\n });\n\n const certPem = cert.toString(\"pem\");\n const keyPem = x509.PemConverter.encode(\n await crypto.subtle.exportKey(\"pkcs8\", keys.privateKey as any),\n \"private key\"\n );\n\n return { certPem, keyPem, signatureHash };\n };\n\n encryptPacket(pkt: DtlsPlaintext) {\n const header = pkt.recordLayerHeader;\n const enc = this.cipher.encrypt(this.sessionType, pkt.fragment, {\n type: header.contentType,\n version: decode(\n Buffer.from(encode(header.protocolVersion, ProtocolVersion).slice()),\n { version: types.uint16be }\n ).version,\n epoch: header.epoch,\n sequenceNumber: header.sequenceNumber,\n });\n pkt.fragment = enc;\n pkt.recordLayerHeader.contentLen = enc.length;\n return pkt;\n }\n\n decryptPacket(pkt: DtlsPlaintext) {\n const header = pkt.recordLayerHeader;\n const dec = this.cipher.decrypt(this.sessionType, pkt.fragment, {\n type: header.contentType,\n version: decode(\n Buffer.from(encode(header.protocolVersion, ProtocolVersion).slice()),\n { version: types.uint16be }\n ).version,\n epoch: header.epoch,\n sequenceNumber: header.sequenceNumber,\n });\n return dec;\n }\n\n verifyData(buf: Buffer) {\n if (this.sessionType === SessionType.CLIENT)\n return prfVerifyDataClient(this.masterSecret, buf);\n else return prfVerifyDataServer(this.masterSecret, buf);\n }\n\n signatureData(data: Buffer, hash: string) {\n const signature = createSign(hash).update(data);\n const key = this.localPrivateKey.toPEM().toString();\n const signed = signature.sign(key);\n return signed;\n }\n\n generateKeySignature(hashAlgorithm: string) {\n const clientRandom =\n this.sessionType === SessionType.CLIENT\n ? this.localRandom\n : this.remoteRandom;\n const serverRandom =\n this.sessionType === SessionType.SERVER\n ? this.localRandom\n : this.remoteRandom;\n\n const sig = this.valueKeySignature(\n clientRandom.serialize(),\n serverRandom.serialize(),\n this.localKeyPair.publicKey,\n this.namedCurve\n );\n\n const enc = this.localPrivateKey.sign(sig, hashAlgorithm);\n return enc;\n }\n\n parseX509(certPem: string, keyPem: string, signatureHash: SignatureHash) {\n const cert = Certificate.fromPEM(Buffer.from(certPem));\n const sec = PrivateKey.fromPEM(Buffer.from(keyPem));\n this.localCert = cert.raw;\n this.localPrivateKey = sec;\n this.signatureHashAlgorithm = signatureHash;\n }\n\n private valueKeySignature(\n clientRandom: Buffer,\n serverRandom: Buffer,\n publicKey: Buffer,\n namedCurve: number\n ) {\n const serverParams = Buffer.from(\n encode(\n {\n type: CurveType.named_curve_3,\n curve: namedCurve,\n len: publicKey.length,\n },\n { type: types.uint8, curve: types.uint16be, len: types.uint8 }\n ).slice()\n );\n return Buffer.concat([clientRandom, serverRandom, serverParams, publicKey]);\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"cipher.js","sourceRoot":"","sources":["../../../../../dtls/src/context/cipher.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,qCAAqD;AACrD,mDAA6C;AAC7C,qDAAuC;AACvC,6CAAoD;AACpD,iDAAgD;AAChD,iEAAyC;AAEzC,2CAQyB;AAEzB,uCAAyE;AACzE,wDAAsE;AAEtE,gDAAsD;AAItD,MAAM,MAAM,GAAG,IAAI,kBAAM,EAAE,CAAC;AAC5B,IAAI,CAAC,cAAc,CAAC,GAAG,CAAC,MAAa,CAAC,CAAC;AAEvC,MAAa,aAAa;IAcxB,YACS,WAAyB,EACzB,OAAgB,EAChB,MAAe,EACtB,sBAAsC;QAH/B,gBAAW,GAAX,WAAW,CAAc;QACzB,YAAO,GAAP,OAAO,CAAS;QAChB,WAAM,GAAN,MAAM,CAAS;QAGtB,IAAI,OAAO,IAAI,MAAM,IAAI,sBAAsB,EAAE;YAC/C,IAAI,CAAC,SAAS,CAAC,OAAO,EAAE,MAAM,EAAE,sBAAsB,CAAC,CAAC;SACzD;IACH,CAAC;IA2ED,aAAa,CAAC,GAAkB;QAC9B,MAAM,MAAM,GAAG,GAAG,CAAC,iBAAiB,CAAC;QACrC,MAAM,GAAG,GAAG,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,WAAW,EAAE,GAAG,CAAC,QAAQ,EAAE;YAC9D,IAAI,EAAE,MAAM,CAAC,WAAW;YACxB,OAAO,EAAE,IAAA,oBAAM,EACb,MAAM,CAAC,IAAI,CAAC,IAAA,oBAA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AC,KAAK,CAAC,CAAC;IACrC,MAAM,MAAM,GAAG,IAAI,CAAC,YAAY,CAAC,MAAM,CACrC,MAAM,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,OAAO,EAAE,IAAI,CAAC,UAAiB,CAAC,EAC9D,aAAa,CACd,CAAC;IAEF,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,CAAC;AAC5C,CAAE,CAAA","sourcesContent":["import { Certificate, PrivateKey } from \"@fidm/x509\";\nimport { Crypto } from \"@peculiar/webcrypto\";\nimport * as x509 from \"@peculiar/x509\";\nimport { decode, encode, types } from \"binary-data\";\nimport nodeCrypto, { createSign } from \"crypto\";\nimport addYears from \"date-fns/addYears\";\n\nimport {\n CipherSuites,\n CurveType,\n HashAlgorithm,\n NamedCurveAlgorithm,\n NamedCurveAlgorithms,\n SignatureAlgorithm,\n SignatureHash,\n} from \"../cipher/const\";\nimport { NamedCurveKeyPair } from \"../cipher/namedCurve\";\nimport { prfVerifyDataClient, prfVerifyDataServer } from \"../cipher/prf\";\nimport { SessionType, SessionTypes } from \"../cipher/suites/abstract\";\nimport AEADCipher from \"../cipher/suites/aead\";\nimport { ProtocolVersion } from \"../handshake/binary\";\nimport { DtlsRandom } from \"../handshake/random\";\nimport { DtlsPlaintext } from \"../record/message/plaintext\";\n\nconst crypto = new Crypto();\nx509.cryptoProvider.set(crypto as any);\n\nexport class CipherContext {\n localRandom!: DtlsRandom;\n remoteRandom!: DtlsRandom;\n cipherSuite!: CipherSuites;\n remoteCertificate?: Buffer;\n remoteKeyPair!: Partial<NamedCurveKeyPair>;\n localKeyPair!: NamedCurveKeyPair;\n masterSecret!: Buffer;\n cipher!: AEADCipher;\n namedCurve!: NamedCurveAlgorithms;\n signatureHashAlgorithm?: SignatureHash;\n localCert!: Buffer;\n localPrivateKey!: PrivateKey;\n\n constructor(\n public sessionType: SessionTypes,\n public certPem?: string,\n public keyPem?: string,\n signatureHashAlgorithm?: SignatureHash\n ) {\n if (certPem && keyPem && signatureHashAlgorithm) {\n this.parseX509(certPem, keyPem, signatureHashAlgorithm);\n }\n }\n\n /**\n *\n * @param signatureHash\n * @param namedCurveAlgorithm necessary when use ecdsa\n */\n static createSelfSignedCertificateWithKey = async (\n signatureHash: SignatureHash,\n namedCurveAlgorithm?: NamedCurveAlgorithms\n ) => {\n const signatureAlgorithmName = (() => {\n switch (signatureHash.signature) {\n case SignatureAlgorithm.rsa_1:\n return \"RSASSA-PKCS1-v1_5\";\n case SignatureAlgorithm.ecdsa_3:\n return \"ECDSA\";\n }\n })();\n const hash = (() => {\n switch (signatureHash.hash) {\n case HashAlgorithm.sha256_4:\n return \"SHA-256\";\n }\n })();\n const namedCurve = (() => {\n switch (namedCurveAlgorithm) {\n case NamedCurveAlgorithm.secp256r1_23:\n return \"P-256\";\n case NamedCurveAlgorithm.x25519_29:\n // todo fix (X25519 not supported with ECDSA)\n if (signatureAlgorithmName === \"ECDSA\") {\n return \"P-256\";\n }\n return \"X25519\";\n default: {\n if (signatureAlgorithmName === \"ECDSA\") return \"P-256\";\n if (signatureAlgorithmName === \"RSASSA-PKCS1-v1_5\") return \"X25519\";\n }\n }\n })();\n const alg = (() => {\n switch (signatureAlgorithmName) {\n case \"ECDSA\":\n return { name: signatureAlgorithmName, hash, namedCurve };\n case \"RSASSA-PKCS1-v1_5\":\n return {\n name: signatureAlgorithmName,\n hash,\n publicExponent: new Uint8Array([1, 0, 1]),\n modulusLength: 2048,\n };\n }\n })();\n\n const keys = await crypto.subtle.generateKey(alg, true, [\"sign\", \"verify\"]);\n\n const cert = await x509.X509CertificateGenerator.createSelfSigned({\n serialNumber: nodeCrypto.randomBytes(8).toString(\"hex\"),\n name: \"C=AU, ST=Some-State, O=Internet Widgits Pty Ltd\",\n notBefore: new Date(),\n notAfter: addYears(Date.now(), 10),\n signingAlgorithm: alg,\n keys,\n });\n\n const certPem = cert.toString(\"pem\");\n const keyPem = x509.PemConverter.encode(\n await crypto.subtle.exportKey(\"pkcs8\", keys.privateKey as any),\n \"private key\"\n );\n\n return { certPem, keyPem, signatureHash };\n };\n\n encryptPacket(pkt: DtlsPlaintext) {\n const header = pkt.recordLayerHeader;\n const enc = this.cipher.encrypt(this.sessionType, pkt.fragment, {\n type: header.contentType,\n version: decode(\n Buffer.from(encode(header.protocolVersion, ProtocolVersion).slice()),\n { version: types.uint16be }\n ).version,\n epoch: header.epoch,\n sequenceNumber: header.sequenceNumber,\n });\n pkt.fragment = enc;\n pkt.recordLayerHeader.contentLen = enc.length;\n return pkt;\n }\n\n decryptPacket(pkt: DtlsPlaintext) {\n const header = pkt.recordLayerHeader;\n const dec = this.cipher.decrypt(this.sessionType, pkt.fragment, {\n type: header.contentType,\n version: decode(\n Buffer.from(encode(header.protocolVersion, ProtocolVersion).slice()),\n { version: types.uint16be }\n ).version,\n epoch: header.epoch,\n sequenceNumber: header.sequenceNumber,\n });\n return dec;\n }\n\n verifyData(buf: Buffer) {\n if (this.sessionType === SessionType.CLIENT)\n return prfVerifyDataClient(this.masterSecret, buf);\n else return prfVerifyDataServer(this.masterSecret, buf);\n }\n\n signatureData(data: Buffer, hash: string) {\n const signature = createSign(hash).update(data);\n const key = this.localPrivateKey.toPEM().toString();\n const signed = signature.sign(key);\n return signed;\n }\n\n generateKeySignature(hashAlgorithm: string) {\n const clientRandom =\n this.sessionType === SessionType.CLIENT\n ? this.localRandom\n : this.remoteRandom;\n const serverRandom =\n this.sessionType === SessionType.SERVER\n ? this.localRandom\n : this.remoteRandom;\n\n const sig = this.valueKeySignature(\n clientRandom.serialize(),\n serverRandom.serialize(),\n this.localKeyPair.publicKey,\n this.namedCurve\n );\n\n const enc = this.localPrivateKey.sign(sig, hashAlgorithm);\n return enc;\n }\n\n parseX509(certPem: string, keyPem: string, signatureHash: SignatureHash) {\n const cert = Certificate.fromPEM(Buffer.from(certPem));\n const sec = PrivateKey.fromPEM(Buffer.from(keyPem));\n this.localCert = cert.raw;\n this.localPrivateKey = sec;\n this.signatureHashAlgorithm = signatureHash;\n }\n\n private valueKeySignature(\n clientRandom: Buffer,\n serverRandom: Buffer,\n publicKey: Buffer,\n namedCurve: number\n ) {\n const serverParams = Buffer.from(\n encode(\n {\n type: CurveType.named_curve_3,\n curve: namedCurve,\n len: publicKey.length,\n },\n { type: types.uint8, curve: types.uint16be, len: types.uint8 }\n ).slice()\n );\n return Buffer.concat([clientRandom, serverRandom, serverParams, publicKey]);\n }\n}\n"]}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import worker_thread from "worker_threads";
|
|
3
|
+
export declare class DnsLookup {
|
|
4
|
+
thread: worker_thread.Worker;
|
|
5
|
+
cache: Map<string, Promise<string>>;
|
|
6
|
+
constructor();
|
|
7
|
+
lookup(host: string): Promise<string>;
|
|
8
|
+
close(): Promise<number>;
|
|
9
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.DnsLookup = void 0;
|
|
7
|
+
const worker_threads_1 = __importDefault(require("worker_threads"));
|
|
8
|
+
class DnsLookup {
|
|
9
|
+
constructor() {
|
|
10
|
+
this.cache = new Map();
|
|
11
|
+
const lookupWorkerFunction = () => {
|
|
12
|
+
const worker_thread = global.require("worker_threads");
|
|
13
|
+
const { lookup } = global.require("dns");
|
|
14
|
+
const dnsLookup = (host) => lookup(host, (err, address, family) => {
|
|
15
|
+
const res = {
|
|
16
|
+
err: err?.message,
|
|
17
|
+
address,
|
|
18
|
+
family,
|
|
19
|
+
host,
|
|
20
|
+
};
|
|
21
|
+
worker_thread.parentPort?.postMessage(res);
|
|
22
|
+
process.exit();
|
|
23
|
+
});
|
|
24
|
+
worker_thread.parentPort?.on("message", (message) => {
|
|
25
|
+
const { host } = message;
|
|
26
|
+
dnsLookup(host);
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
const lookupEval = `(${lookupWorkerFunction})()`;
|
|
30
|
+
this.thread = new worker_threads_1.default.Worker(lookupEval, {
|
|
31
|
+
eval: true,
|
|
32
|
+
});
|
|
33
|
+
this.thread.setMaxListeners(100);
|
|
34
|
+
}
|
|
35
|
+
async lookup(host) {
|
|
36
|
+
let cached = this.cache.get(host);
|
|
37
|
+
if (cached) {
|
|
38
|
+
return cached;
|
|
39
|
+
}
|
|
40
|
+
cached = new Promise((r, f) => {
|
|
41
|
+
const exitListener = (exitCode) => f(new Error(`dns.lookup thread exited unexpectedly: ${exitCode}`));
|
|
42
|
+
const threadMessageListener = (result) => {
|
|
43
|
+
if (result.host !== host) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
this.thread.removeListener("message", threadMessageListener);
|
|
47
|
+
this.thread.removeListener("exit", exitListener);
|
|
48
|
+
if (!result.address)
|
|
49
|
+
return f(new Error(result.err || "dns.lookup thread unknown error"));
|
|
50
|
+
r(result.address);
|
|
51
|
+
};
|
|
52
|
+
this.thread.on("message", threadMessageListener);
|
|
53
|
+
this.thread.on("exit", exitListener);
|
|
54
|
+
this.thread.postMessage({
|
|
55
|
+
host,
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
this.cache.set(host, cached);
|
|
59
|
+
return cached;
|
|
60
|
+
}
|
|
61
|
+
close() {
|
|
62
|
+
return this.thread.terminate();
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
exports.DnsLookup = DnsLookup;
|
|
66
|
+
//# sourceMappingURL=lookup.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lookup.js","sourceRoot":"","sources":["../../../../../ice/src/dns/lookup.ts"],"names":[],"mappings":";;;;;;AAAA,oEAA2C;AAY3C,MAAa,SAAS;IAIpB;QAFA,UAAK,GAAG,IAAI,GAAG,EAA2B,CAAC;QAGzC,MAAM,oBAAoB,GAAG,G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AC,QAAgB,EAAE,EAAE,CACxC,CAAC,CAAC,IAAI,KAAK,CAAC,0CAA0C,QAAQ,EAAE,CAAC,CAAC,CAAC;YAErE,MAAM,qBAAqB,GAAG,CAAC,MAAuB,EAAE,EAAE;gBACxD,IAAI,MAAM,CAAC,IAAI,KAAK,IAAI,EAAE;oBACxB,OAAO;iBACR;gBAED,IAAI,CAAC,MAAM,CAAC,cAAc,CAAC,SAAS,EAAE,qBAAqB,CAAC,CAAC;gBAC7D,IAAI,CAAC,MAAM,CAAC,cAAc,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;gBAEjD,IAAI,CAAC,MAAM,CAAC,OAAO;oBACjB,OAAO,CAAC,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,GAAG,IAAI,iCAAiC,CAAC,CAAC,CAAC;gBACvE,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;YACpB,CAAC,CAAC;YAEF,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,SAAS,EAAE,qBAAqB,CAAC,CAAC;YACjD,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;YAErC,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC;gBACtB,IAAI;aACe,CAAC,CAAC;QACzB,CAAC,CAAC,CAAC;QAEH,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;QAC7B,OAAO,MAAM,CAAC;IAChB,CAAC;IAED,KAAK;QACH,OAAO,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE,CAAC;IACjC,CAAC;CACF;AAzED,8BAyEC","sourcesContent":["import worker_thread from \"worker_threads\";\n\ninterface DnsLookupRequest {\n host: string;\n}\n\ninterface DnsLookupResult extends DnsLookupRequest {\n err?: string;\n address?: string;\n family?: number;\n}\n\nexport class DnsLookup {\n thread: worker_thread.Worker;\n cache = new Map<string, Promise<string>>();\n\n constructor() {\n const lookupWorkerFunction = () => {\n const worker_thread = global.require(\"worker_threads\");\n const { lookup } = global.require(\"dns\");\n\n const dnsLookup = (host: string) =>\n lookup(host, (err: Error, address: string, family: number) => {\n const res: DnsLookupResult = {\n err: err?.message,\n address,\n family,\n host,\n };\n worker_thread.parentPort?.postMessage(res);\n process.exit();\n });\n\n worker_thread.parentPort?.on(\"message\", (message: DnsLookupRequest) => {\n const { host } = message;\n dnsLookup(host);\n });\n };\n\n const lookupEval = `(${lookupWorkerFunction})()`;\n\n this.thread = new worker_thread.Worker(lookupEval, {\n eval: true,\n });\n\n this.thread.setMaxListeners(100);\n }\n\n async lookup(host: string): Promise<string> {\n let cached = this.cache.get(host);\n if (cached) {\n return cached;\n }\n cached = new Promise((r, f) => {\n const exitListener = (exitCode: number) =>\n f(new Error(`dns.lookup thread exited unexpectedly: ${exitCode}`));\n\n const threadMessageListener = (result: DnsLookupResult) => {\n if (result.host !== host) {\n return;\n }\n\n this.thread.removeListener(\"message\", threadMessageListener);\n this.thread.removeListener(\"exit\", exitListener);\n\n if (!result.address)\n return f(new Error(result.err || \"dns.lookup thread unknown error\"));\n r(result.address);\n };\n\n this.thread.on(\"message\", threadMessageListener);\n this.thread.on(\"exit\", exitListener);\n\n this.thread.postMessage({\n host,\n } as DnsLookupRequest);\n });\n\n this.cache.set(host, cached);\n return cached;\n }\n\n close() {\n return this.thread.terminate();\n }\n}\n"]}
|
package/lib/ice/src/ice.d.ts
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
import PCancelable from "p-cancelable";
|
|
3
3
|
import { Event } from "rx.mini";
|
|
4
4
|
import { Candidate } from "./candidate";
|
|
5
|
+
import { DnsLookup } from "./dns/lookup";
|
|
5
6
|
import { Future } from "./helper";
|
|
6
7
|
import { Message } from "./stun/message";
|
|
7
8
|
import { Address, Protocol } from "./types/model";
|
|
@@ -24,6 +25,7 @@ export declare class Connection {
|
|
|
24
25
|
_localCandidatesEnd: boolean;
|
|
25
26
|
_tieBreaker: BigInt;
|
|
26
27
|
state: IceState;
|
|
28
|
+
dnsLookup?: DnsLookup;
|
|
27
29
|
readonly onData: Event<[Buffer, number]>;
|
|
28
30
|
readonly stateChanged: Event<[IceState]>;
|
|
29
31
|
private _remoteCandidates;
|
package/lib/ice/src/ice.js
CHANGED
|
@@ -29,7 +29,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
29
29
|
exports.validateAddress = exports.serverReflexiveCandidate = exports.getHostAddresses = exports.candidatePairPriority = exports.sortCandidatePairs = exports.validateRemoteCandidate = exports.CandidatePairState = exports.CandidatePair = exports.Connection = void 0;
|
|
30
30
|
const crypto_1 = require("crypto");
|
|
31
31
|
const debug_1 = __importDefault(require("debug"));
|
|
32
|
-
const dns_1 = __importDefault(require("dns"));
|
|
33
32
|
const int64_buffer_1 = require("int64-buffer");
|
|
34
33
|
const nodeIp = __importStar(require("ip"));
|
|
35
34
|
const isEqual_1 = __importDefault(require("lodash/isEqual"));
|
|
@@ -39,13 +38,14 @@ const os_1 = __importDefault(require("os"));
|
|
|
39
38
|
const p_cancelable_1 = __importDefault(require("p-cancelable"));
|
|
40
39
|
const rx_mini_1 = require("rx.mini");
|
|
41
40
|
const promises_1 = __importDefault(require("timers/promises"));
|
|
42
|
-
const util_1 = __importDefault(require("util"));
|
|
43
41
|
const candidate_1 = require("./candidate");
|
|
42
|
+
const lookup_1 = require("./dns/lookup");
|
|
44
43
|
const helper_1 = require("./helper");
|
|
45
44
|
const const_1 = require("./stun/const");
|
|
46
45
|
const message_1 = require("./stun/message");
|
|
47
46
|
const protocol_1 = require("./stun/protocol");
|
|
48
47
|
const protocol_2 = require("./turn/protocol");
|
|
48
|
+
const utils_1 = require("./utils");
|
|
49
49
|
const log = (0, debug_1.default)("werift-ice : packages/ice/src/ice.ts : log");
|
|
50
50
|
class Connection {
|
|
51
51
|
constructor(iceControlling, options) {
|
|
@@ -246,8 +246,9 @@ class Connection {
|
|
|
246
246
|
const candidateAddress = [address, protocol.getExtraInfo()[1]];
|
|
247
247
|
protocol.localCandidate = new candidate_1.Candidate((0, candidate_1.candidateFoundation)("host", "udp", candidateAddress[0]), component, "udp", (0, candidate_1.candidatePriority)(component, "host"), candidateAddress[0], candidateAddress[1], "host");
|
|
248
248
|
candidates.push(protocol.localCandidate);
|
|
249
|
-
if (cb)
|
|
249
|
+
if (cb) {
|
|
250
250
|
cb(protocol.localCandidate);
|
|
251
|
+
}
|
|
251
252
|
}
|
|
252
253
|
// # query STUN server for server-reflexive candidates (IPv4 only)
|
|
253
254
|
const stunServer = this.stunServer;
|
|
@@ -318,6 +319,8 @@ class Connection {
|
|
|
318
319
|
// # perform checks
|
|
319
320
|
// 5.8. Scheduling Checks
|
|
320
321
|
for (;;) {
|
|
322
|
+
if (this.state === "closed")
|
|
323
|
+
break;
|
|
321
324
|
if (!this.schedulingChecks())
|
|
322
325
|
break;
|
|
323
326
|
await promises_1.default.setTimeout(20);
|
|
@@ -415,6 +418,7 @@ class Connection {
|
|
|
415
418
|
}
|
|
416
419
|
this.protocols = [];
|
|
417
420
|
this.localCandidates = [];
|
|
421
|
+
await this.dnsLookup?.close();
|
|
418
422
|
}
|
|
419
423
|
setState(state) {
|
|
420
424
|
this.state = state;
|
|
@@ -434,18 +438,17 @@ class Connection {
|
|
|
434
438
|
return;
|
|
435
439
|
}
|
|
436
440
|
if (remoteCandidate.host.includes(".local")) {
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
.
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
remoteCandidate.host =
|
|
441
|
+
try {
|
|
442
|
+
if (this.state === "closed")
|
|
443
|
+
return;
|
|
444
|
+
if (!this.dnsLookup) {
|
|
445
|
+
this.dnsLookup = new lookup_1.DnsLookup();
|
|
446
|
+
}
|
|
447
|
+
const host = await this.dnsLookup.lookup(remoteCandidate.host);
|
|
448
|
+
remoteCandidate.host = host;
|
|
445
449
|
}
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
remoteCandidate.host = "127.0.0.1";
|
|
450
|
+
catch (error) {
|
|
451
|
+
return;
|
|
449
452
|
}
|
|
450
453
|
}
|
|
451
454
|
try {
|
|
@@ -783,7 +786,7 @@ function nodeIpAddress(family) {
|
|
|
783
786
|
};
|
|
784
787
|
}
|
|
785
788
|
}
|
|
786
|
-
const addresses = interfaces[nic].filter((details) => details.family
|
|
789
|
+
const addresses = interfaces[nic].filter((details) => (0, utils_1.normalizeFamilyNodeV18)(details.family) === family &&
|
|
787
790
|
!nodeIp.isLoopback(details.address));
|
|
788
791
|
return {
|
|
789
792
|
nic,
|
|
@@ -802,9 +805,9 @@ function nodeIpAddress(family) {
|
|
|
802
805
|
function getHostAddresses(useIpv4, useIpv6) {
|
|
803
806
|
const address = [];
|
|
804
807
|
if (useIpv4)
|
|
805
|
-
address.push(...nodeIpAddress(
|
|
808
|
+
address.push(...nodeIpAddress(4));
|
|
806
809
|
if (useIpv6)
|
|
807
|
-
address.push(...nodeIpAddress(
|
|
810
|
+
address.push(...nodeIpAddress(6));
|
|
808
811
|
return address;
|
|
809
812
|
}
|
|
810
813
|
exports.getHostAddresses = getHostAddresses;
|