Skip to content
This repository has been archived by the owner on Jun 19, 2023. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into jt/con-439_streamTest
Browse files Browse the repository at this point in the history
  • Loading branch information
John-LittleBearLabs committed Aug 11, 2022
2 parents fd56251 + da01296 commit 819d243
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 21 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"type": "module",
"scripts": {
"build": "aegir build",
"clean": "rm -rfv node_modules dist *.lock *-lock.json ",
"test": "aegir test",
"test:browser": "aegir test --target browser",
"format": "prettier --write src/*.ts"
Expand All @@ -34,6 +33,7 @@
"@libp2p/logger": "^2.0.0",
"@libp2p/multistream-select": "^3.0.0",
"@libp2p/peer-id": "^1.1.15",
"@multiformats/multiaddr": "../js-multiaddr/",
"abortable-iterator": "^4.0.2",
"it-merge": "^1.0.4",
"p-defer": "^4.0.0",
Expand Down
13 changes: 13 additions & 0 deletions src/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export class WebRTCTransportError extends Error {
constructor(msg: string) {
super(msg);
this.name = 'WebRTCTransportError';
}
}

export class InvalidArgumentError extends WebRTCTransportError {
constructor(msg: string) {
super(msg);
this.name = 'WebRTC/InvalidArgumentError';
}
}
47 changes: 27 additions & 20 deletions src/sdp.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { InvalidArgumentError } from './error.js'
import { logger } from '@libp2p/logger';
import { Multiaddr } from '@multiformats/multiaddr';

const log = logger('libp2p:webrtc:sdp');

const P_XWEBRTC: number = 0x115;
const CERTHASH_CODE: number = 466;
const ANSWER_SDP_FORMAT: string = `
v=0
o=- 0 0 IN %s %s
Expand Down Expand Up @@ -37,38 +38,44 @@ function port(ma: Multiaddr): number {
return ma.toOptions().port;
}
function certhash(ma: Multiaddr): string {
let webrtc_value = ma
.stringTuples()
.filter((tup) => tup[0] == P_XWEBRTC)
let tups = ma.stringTuples();
let certhash_value = tups
.filter((tup) => tup[0] == CERTHASH_CODE)
.map((tup) => tup[1])[0];
if (webrtc_value) {
return webrtc_value.split('/')[1];
if (certhash_value) {
return certhash_value;
} else {
throw new Error("Couldn't find a webrtc component of multiaddr:" + ma.toString());
throw new Error("Couldn't find a certhash component of multiaddr:" + ma.toString());
}
}

function ma2sdp(ma: Multiaddr, ufrag: string): string {
return ANSWER_SDP_FORMAT.replace('/%s/', ipv(ma))
.replace('/%s/', ip(ma))
.replace('/%s/', ipv(ma))
.replace('/%s/', ip(ma))
.replace('/%s/', port(ma).toString())
.replace('/%s/', ufrag)
.replace('/%s/', ufrag)
.replace('/%s/', certhash(ma));
return ANSWER_SDP_FORMAT
.replace('%s', ipv(ma))
.replace('%s', ip(ma))
.replace('%s', ipv(ma))
.replace('%s', ip(ma))
.replace('%d', port(ma).toString())
.replace('%s', ufrag)
.replace('%s', ufrag)
.replace('%s', certhash(ma));
}

export function fromMultiAddr(ma: Multiaddr, ufrag: string): RTCSessionDescriptionInit {
return {
type: 'offer',
type: 'answer',
sdp: ma2sdp(ma, ufrag),
};
}

export function munge(desc: RTCSessionDescriptionInit, ufrag: string): RTCSessionDescriptionInit {
//TODO
// desc.sdp.replaceAll(/^a=ice-ufrag=(.*)/, 'a=ice-ufrag=' + ufrag);
// desc.sdp.replaceAll(/^a=ice-pwd=(.*)/, 'a=ice-pwd=' + ufrag);
return desc;
if (desc.sdp) {
desc.sdp = desc.sdp
.replace(/\na=ice-ufrag:[^\n]*\n/, '\na=ice-ufrag:' + ufrag + '\n')
.replace(/\na=ice-pwd:[^\n]*\n/, '\na=ice-pwd:' + ufrag + '\n')
;
return desc;
} else {
throw new InvalidArgumentError("Can't munge a missing SDP");
}
}
51 changes: 51 additions & 0 deletions test/sdp.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { Multiaddr } from '@multiformats/multiaddr';
import { expect } from 'chai';
import * as underTest from '../src/sdp.js';

const an_sdp = `
v=0
o=- 0 0 IN IP4 192.168.0.152
s=-
c=IN IP4 192.168.0.152
t=0 0
m=application 2345 UDP/DTLS/SCTP webrtc-datachannel
a=mid:0
a=ice-options:ice2
a=ice-ufrag:MyUserFragment
a=ice-pwd:MyUserFragment
a=fingerprint:mTXVsdGliYXNlIGlzIGF3ZXNvbWUhIFxvLw
a=setup:actpass
a=sctp-port:5000
a=max-message-size:100000
`;

describe('SDP creation', () => {
it('handles simple blue sky easily enough', async () => {
let ma = new Multiaddr('/ip4/192.168.0.152/udp/2345/webrtc/certhash/zYAjKoNbau5KiqmHPmSxYCvn66dA1vLmwbt');
let ufrag = 'MyUserFragment';
let sdp = underTest.fromMultiAddr(ma, ufrag);
expect(sdp.sdp).to.equal(an_sdp);
});
});

describe('SDP munging', () => {
it('does a simple replacement', () => {
let result = underTest.munge({type:'answer',sdp: an_sdp},'someotheruserfragmentstring');
expect(result.sdp).to.equal(`
v=0
o=- 0 0 IN IP4 192.168.0.152
s=-
c=IN IP4 192.168.0.152
t=0 0
m=application 2345 UDP/DTLS/SCTP webrtc-datachannel
a=mid:0
a=ice-options:ice2
a=ice-ufrag:someotheruserfragmentstring
a=ice-pwd:someotheruserfragmentstring
a=fingerprint:mTXVsdGliYXNlIGlzIGF3ZXNvbWUhIFxvLw
a=setup:actpass
a=sctp-port:5000
a=max-message-size:100000
`);
});
});

0 comments on commit 819d243

Please sign in to comment.