2016-05-20 21:29:53 +02:00
|
|
|
'use strict'
|
|
|
|
|
2016-06-13 19:10:38 +02:00
|
|
|
const pull = require('pull-stream')
|
|
|
|
const Connection = require('interface-connection').Connection
|
2016-05-22 13:19:17 +02:00
|
|
|
|
2016-05-22 01:51:15 +02:00
|
|
|
const handshake = require('./handshake')
|
2016-06-13 19:10:38 +02:00
|
|
|
const State = require('./state')
|
2016-05-22 01:51:15 +02:00
|
|
|
|
2016-05-22 13:19:17 +02:00
|
|
|
exports.SecureSession = class SecureSession {
|
2016-05-22 01:51:15 +02:00
|
|
|
constructor (local, key, insecure) {
|
2016-06-13 19:10:38 +02:00
|
|
|
if (!local) {
|
2016-05-22 01:51:15 +02:00
|
|
|
throw new Error('no local id provided')
|
|
|
|
}
|
|
|
|
|
2016-06-13 19:10:38 +02:00
|
|
|
if (!key) {
|
2016-05-22 01:51:15 +02:00
|
|
|
throw new Error('no local private key provided')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!insecure) {
|
|
|
|
throw new Error('no insecure stream provided')
|
|
|
|
}
|
2016-05-23 17:33:16 +02:00
|
|
|
|
2016-06-13 19:10:38 +02:00
|
|
|
this.state = new State(local, key)
|
|
|
|
this.insecure = insecure
|
2016-05-23 17:33:16 +02:00
|
|
|
|
2016-06-13 19:10:38 +02:00
|
|
|
pull(
|
|
|
|
this.insecure,
|
|
|
|
handshake(this.state),
|
|
|
|
this.insecure
|
|
|
|
)
|
2016-05-22 22:39:36 +02:00
|
|
|
}
|
|
|
|
|
2016-06-13 19:10:38 +02:00
|
|
|
get secure () {
|
|
|
|
return new Connection(this.state.secure, this.insecure)
|
2016-05-22 01:51:15 +02:00
|
|
|
}
|
|
|
|
}
|