Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package one.video.streaming.oktp;
- import java.net.SocketTimeoutException;
- import java.nio.ByteBuffer;
- import java.util.Arrays;
- import one.video.streaming.oktp.packets.attr5DHReq;
- import one.video.streaming.oktp.packets.attr6DHResp;
- import one.video.streaming.util.Timer;
- class DHConn {
- private final Protocol _proto;
- private byte[] _peerKey;
- private final Timer _connectionTimer;
- private final Timer _keyTimer;
- private int _keyTimeout;
- private int _connectionTimeout;
- private DHCrypt _dhCrypt;
- DHConn(Protocol proto) {
- super();
- this._connectionTimer = new Timer();
- this._keyTimer = new Timer();
- this._proto = proto;
- proto.setKey(HandsnakeSig._key);
- this._dhCrypt = new DHCrypt();
- }
- void reset(int ms) {
- this._keyTimeout = 200;
- this._connectionTimer.reset();
- this._connectionTimeout = ms;
- }
- void handle6_DHResp(int seq, Payload payload, ByteBuffer buf, byte[] myKey, byte[] peerKey) {
- byte[] v0 = this._dhCrypt.peerKey();
- if(v0 == null) {
- this._dhCrypt.computeSharedKey(peerKey);
- this._proto.setKey(this._dhCrypt.sharedKey());
- this._proto.setSeq(seq);
- }
- else if(!Arrays.equals(myKey, v0)) {
- this._proto.sendDHFailure(myKey);
- }
- }
- void resendKey(Payload payload) {
- long ms = this._connectionTimer.elapsed(-1);
- if(ms >= 0 && this._keyTimer.elapsed(9223372036854775807L) >= (((long)this._keyTimeout))) {
- if(ms > (((long)this._connectionTimeout))) {
- throw new SocketTimeoutException("Oktp connect timeout");
- }
- else {
- this.sendKey(payload);
- this._keyTimer.reset();
- this._keyTimeout = Math.min(3000, this._keyTimeout * 15 / 10);
- }
- }
- }
- void handle5_DHReq(Payload payload, ByteBuffer buf, byte[] peerKey) {
- if(this._peerKey == null) {
- this._peerKey = peerKey;
- this._dhCrypt.computeSharedKey(peerKey);
- this._proto.setKey(this._dhCrypt.sharedKey());
- label_16:
- ByteBuffer buf = payload.getBuffer(new attr6DHResp(0), false, -1);
- Bytes.encode(buf, peerKey);
- Bytes.encode(buf, this._dhCrypt.publicKey());
- payload.encodeAttr();
- payload.sendUnrel();
- }
- else if(!Arrays.equals(peerKey, this._peerKey)) {
- this._proto.sendDHFailure(peerKey);
- }
- else {
- goto label_16;
- }
- }
- private void sendKey(Payload payload) {
- ByteBuffer buf = payload.getBuffer(new attr5DHReq(0), false, -1);
- Bytes.encode(buf, this._dhCrypt.publicKey());
- while(buf.remaining() > 0) {
- buf.put(-52);
- }
- payload.encodeAttr();
- }
- }
Add Comment
Please, Sign In to add comment