|
| 1 | +import { InvalidArgumentError } from './error.js' |
1 | 2 | import { logger } from '@libp2p/logger';
|
2 | 3 | import { Multiaddr } from '@multiformats/multiaddr';
|
3 | 4 |
|
4 | 5 | const log = logger('libp2p:webrtc:sdp');
|
5 | 6 |
|
6 |
| -const P_XWEBRTC: number = 0x115; |
| 7 | +const CERTHASH_CODE: number = 466; |
7 | 8 | const ANSWER_SDP_FORMAT: string = `
|
8 | 9 | v=0
|
9 | 10 | o=- 0 0 IN %s %s
|
@@ -37,38 +38,44 @@ function port(ma: Multiaddr): number {
|
37 | 38 | return ma.toOptions().port;
|
38 | 39 | }
|
39 | 40 | function certhash(ma: Multiaddr): string {
|
40 |
| - let webrtc_value = ma |
41 |
| - .stringTuples() |
42 |
| - .filter((tup) => tup[0] == P_XWEBRTC) |
| 41 | + let tups = ma.stringTuples(); |
| 42 | + let certhash_value = tups |
| 43 | + .filter((tup) => tup[0] == CERTHASH_CODE) |
43 | 44 | .map((tup) => tup[1])[0];
|
44 |
| - if (webrtc_value) { |
45 |
| - return webrtc_value.split('/')[1]; |
| 45 | + if (certhash_value) { |
| 46 | + return certhash_value; |
46 | 47 | } else {
|
47 |
| - throw new Error("Couldn't find a webrtc component of multiaddr:" + ma.toString()); |
| 48 | + throw new Error("Couldn't find a certhash component of multiaddr:" + ma.toString()); |
48 | 49 | }
|
49 | 50 | }
|
50 | 51 |
|
51 | 52 | function ma2sdp(ma: Multiaddr, ufrag: string): string {
|
52 |
| - return ANSWER_SDP_FORMAT.replace('/%s/', ipv(ma)) |
53 |
| - .replace('/%s/', ip(ma)) |
54 |
| - .replace('/%s/', ipv(ma)) |
55 |
| - .replace('/%s/', ip(ma)) |
56 |
| - .replace('/%s/', port(ma).toString()) |
57 |
| - .replace('/%s/', ufrag) |
58 |
| - .replace('/%s/', ufrag) |
59 |
| - .replace('/%s/', certhash(ma)); |
| 53 | + return ANSWER_SDP_FORMAT |
| 54 | + .replace('%s', ipv(ma)) |
| 55 | + .replace('%s', ip(ma)) |
| 56 | + .replace('%s', ipv(ma)) |
| 57 | + .replace('%s', ip(ma)) |
| 58 | + .replace('%d', port(ma).toString()) |
| 59 | + .replace('%s', ufrag) |
| 60 | + .replace('%s', ufrag) |
| 61 | + .replace('%s', certhash(ma)); |
60 | 62 | }
|
61 | 63 |
|
62 | 64 | export function fromMultiAddr(ma: Multiaddr, ufrag: string): RTCSessionDescriptionInit {
|
63 | 65 | return {
|
64 |
| - type: 'offer', |
| 66 | + type: 'answer', |
65 | 67 | sdp: ma2sdp(ma, ufrag),
|
66 | 68 | };
|
67 | 69 | }
|
68 | 70 |
|
69 | 71 | export function munge(desc: RTCSessionDescriptionInit, ufrag: string): RTCSessionDescriptionInit {
|
70 |
| - //TODO |
71 |
| - // desc.sdp.replaceAll(/^a=ice-ufrag=(.*)/, 'a=ice-ufrag=' + ufrag); |
72 |
| - // desc.sdp.replaceAll(/^a=ice-pwd=(.*)/, 'a=ice-pwd=' + ufrag); |
73 |
| - return desc; |
| 72 | + if (desc.sdp) { |
| 73 | + desc.sdp = desc.sdp |
| 74 | + .replace(/\na=ice-ufrag:[^\n]*\n/, '\na=ice-ufrag:' + ufrag + '\n') |
| 75 | + .replace(/\na=ice-pwd:[^\n]*\n/, '\na=ice-pwd:' + ufrag + '\n') |
| 76 | + ; |
| 77 | + return desc; |
| 78 | + } else { |
| 79 | + throw new InvalidArgumentError("Can't munge a missing SDP"); |
| 80 | + } |
74 | 81 | }
|
0 commit comments