Guest User

Untitled

a guest
Jun 8th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.90 KB | None | 0 0
  1. package one.video.streaming.oktp;
  2.  
  3. import java.net.SocketTimeoutException;
  4. import java.nio.ByteBuffer;
  5. import java.util.Arrays;
  6. import one.video.streaming.oktp.packets.attr5DHReq;
  7. import one.video.streaming.oktp.packets.attr6DHResp;
  8. import one.video.streaming.util.Timer;
  9.  
  10. class DHConn {
  11.     private final Protocol _proto;
  12.     private byte[] _peerKey;
  13.     private final Timer _connectionTimer;
  14.     private final Timer _keyTimer;
  15.     private int _keyTimeout;
  16.     private int _connectionTimeout;
  17.     private DHCrypt _dhCrypt;
  18.  
  19.     DHConn(Protocol proto) {
  20.         super();
  21.         this._connectionTimer = new Timer();
  22.         this._keyTimer = new Timer();
  23.         this._proto = proto;
  24.         proto.setKey(HandsnakeSig._key);
  25.         this._dhCrypt = new DHCrypt();
  26.     }
  27.  
  28.     void reset(int ms) {
  29.         this._keyTimeout = 200;
  30.         this._connectionTimer.reset();
  31.         this._connectionTimeout = ms;
  32.     }
  33.  
  34.     void handle6_DHResp(int seq, Payload payload, ByteBuffer buf, byte[] myKey, byte[] peerKey) {
  35.         byte[] v0 = this._dhCrypt.peerKey();
  36.         if(v0 == null) {
  37.             this._dhCrypt.computeSharedKey(peerKey);
  38.             this._proto.setKey(this._dhCrypt.sharedKey());
  39.             this._proto.setSeq(seq);
  40.         }
  41.         else if(!Arrays.equals(myKey, v0)) {
  42.             this._proto.sendDHFailure(myKey);
  43.         }
  44.     }
  45.  
  46.     void resendKey(Payload payload) {
  47.         long ms = this._connectionTimer.elapsed(-1);
  48.         if(ms >= 0 && this._keyTimer.elapsed(9223372036854775807L) >= (((long)this._keyTimeout))) {
  49.             if(ms > (((long)this._connectionTimeout))) {
  50.                 throw new SocketTimeoutException("Oktp connect timeout");
  51.             }
  52.             else {
  53.                 this.sendKey(payload);
  54.                 this._keyTimer.reset();
  55.                 this._keyTimeout = Math.min(3000, this._keyTimeout * 15 / 10);
  56.             }
  57.         }
  58.     }
  59.  
  60.     void handle5_DHReq(Payload payload, ByteBuffer buf, byte[] peerKey) {
  61.         if(this._peerKey == null) {
  62.             this._peerKey = peerKey;
  63.             this._dhCrypt.computeSharedKey(peerKey);
  64.             this._proto.setKey(this._dhCrypt.sharedKey());
  65.         label_16:
  66.             ByteBuffer buf = payload.getBuffer(new attr6DHResp(0), false, -1);
  67.             Bytes.encode(buf, peerKey);
  68.             Bytes.encode(buf, this._dhCrypt.publicKey());
  69.             payload.encodeAttr();
  70.             payload.sendUnrel();
  71.         }
  72.         else if(!Arrays.equals(peerKey, this._peerKey)) {
  73.             this._proto.sendDHFailure(peerKey);
  74.         }
  75.         else {
  76.             goto label_16;
  77.         }
  78.     }
  79.  
  80.     private void sendKey(Payload payload) {
  81.         ByteBuffer buf = payload.getBuffer(new attr5DHReq(0), false, -1);
  82.         Bytes.encode(buf, this._dhCrypt.publicKey());
  83.         while(buf.remaining() > 0) {
  84.             buf.put(-52);
  85.         }
  86.  
  87.         payload.encodeAttr();
  88.     }
  89. }
Add Comment
Please, Sign In to add comment