-
Notifications
You must be signed in to change notification settings - Fork 1
/
candidate.js
84 lines (69 loc) · 2.93 KB
/
candidate.js
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
var debug = require('cog/logger')('rtc-validator');
var rePrefix = /^(?:a=)?candidate:/;
/*
validation rules as per:
http://tools.ietf.org/html/draft-ietf-mmusic-ice-sip-sdp-03#section-8.1
candidate-attribute = "candidate" ":" foundation SP component-id SP
transport SP
priority SP
connection-address SP ;from RFC 4566
port ;port from RFC 4566
SP cand-type
[SP rel-addr]
[SP rel-port]
*(SP extension-att-name SP
extension-att-value)
foundation = 1*32ice-char
component-id = 1*5DIGIT
transport = "UDP" / transport-extension
transport-extension = token ; from RFC 3261
priority = 1*10DIGIT
cand-type = "typ" SP candidate-types
candidate-types = "host" / "srflx" / "prflx" / "relay" / token
rel-addr = "raddr" SP connection-address
rel-port = "rport" SP port
extension-att-name = token
extension-att-value = *VCHAR
ice-char = ALPHA / DIGIT / "+" / "/"
*/
var partValidation = [
[ /.+/, 'invalid foundation component', 'foundation' ],
[ /\d+/, 'invalid component id', 'component-id' ],
[ /(UDP|TCP)/i, 'transport must be TCP or UDP', 'transport' ],
[ /\d+/, 'numeric priority expected', 'priority' ],
[ new RegExp(require('reu/ip').source + '|.*\.local$'), 'invalid connection address', 'connection-address' ],
[ /\d+/, 'invalid connection port', 'connection-port' ],
[ /typ/, 'Expected "typ" identifier', 'type classifier' ],
[ /.+/, 'Invalid candidate type specified', 'candidate-type' ]
];
/**
### `rtc-validator/candidate`
Validate that an `RTCIceCandidate` (or plain old object with data, sdpMid,
etc attributes) is a valid ice candidate.
Specs reviewed as part of the validation implementation:
- <http://tools.ietf.org/html/draft-ietf-mmusic-ice-sip-sdp-03#section-8.1>
- <http://tools.ietf.org/html/rfc5245>
**/
module.exports = function(data) {
var errors = [];
var candidate = data && (data.candidate || data);
var prefixMatch = candidate && rePrefix.exec(candidate);
var parts = prefixMatch && candidate.slice(prefixMatch[0].length).split(/\s/);
if (! candidate) {
return [ new Error('empty candidate') ];
}
// check that the prefix matches expected
if (! prefixMatch) {
return [ new Error('candidate did not match expected sdp line format') ];
}
// perform the part validation
errors = errors.concat(parts.map(validateParts)).filter(Boolean);
return errors;
};
function validateParts(part, idx) {
var validator = partValidation[idx];
if (validator && (! validator[0].test(part))) {
debug(validator[2] + ' part failed validation: ' + part);
return new Error(validator[1]);
}
}