Пример #1
0
 private void TL_auth_Authorization(TL.Object obj) {
   //	int expires = obj.getInt("expires");
   bind = true;
   dcState.set("bind", bind);
   cb.onAuth(obj.getObject("user"));
   cb.onBind();
 }
Пример #2
0
  private void TL_RpcError(TL.Object obj) {
    int code = obj.getInt("error_code");
    String msg = obj.getString("error_message");

    Common.logError(String.format("rpc_error: %s (%d)\n", msg, code));

    Message req_msg = TLMessage.get(req_msg_id);
    if (req_msg == null) return;
    Common.logError("message object: " + req_msg.obj.name + ":" + req_msg.obj.type);

    int idx = msg.indexOf("_MIGRATE_");
    if (idx > 0) {
      String type = msg.substring(0, idx);

      String num = msg.substring(idx + 9);
      if ((idx = num.indexOf(":")) > 0) num = num.substring(0, idx);
      int dc_id = Integer.parseInt(num);
      Common.logError("redirect to dc: " + dc_id);

      MTProto m = MTProto.getConnection(dc_id, cb, reuseFlag);
      cb.onRedirect(m);
      if (type.equals("PHONE") || type.equals("NETWORK") || type.equals("USER")) dc_this = dc_id;

      m.sendMessage(req_msg);
    }
  }
Пример #3
0
  private void TL_BadMsgNotification(TL.Object obj) {
    int error_code = obj.getInt("error_code");

    Message msg = TLMessage.get(obj.getLong("bad_msg_id"));
    Common.logError("bad_msg: " + error_code + " " + msg.obj.name + ":" + msg.obj.type);

    if (error_code == 16 || error_code == 17) {
      time_delta = (int) ((cur_message_id >> 32) - Common.getUnixTime());
      last_message_id = 0;
    }

    if (error_code == 32 || error_code == 33) {
      Common.logError("cur seq: " + cur_msg_seq);
      Common.logError("old seq: " + seqno);
      if (!bad_seq) {
        session = GEN_session_id();
        seqno = 0;
        send_ping();
        bad_seq = true;
      }

      // seqno = cur_msg_seq + (cur_msg_seq % 2) + 100;
      //	session = GEN_session_id();
      //	seqno = 0;
    }

    if (obj.id == 0xedab447b) { // bad_server_salt
      server_salt = obj.getLong("new_server_salt");
      dcState.set("server_salt", server_salt);
    }

    retry(obj.getLong("bad_msg_id"));
  }
Пример #4
0
  private void send_client_DH_inner_data(long retry_id) {
    byte[] b_data = new byte[256];
    Common.random.nextBytes(b_data);

    BigInteger b = new BigInteger(1, b_data);
    BigInteger g_b = g.modPow(b, dh_prime);
    Common.logError(
        "g_b length: " + g_b.toByteArray().length + " -> " + Common.toBytes(g_b).length);

    BigInteger akey = g_a.modPow(b, dh_prime);
    Common.logError("auth_key: " + akey.toString());
    setAuthKey(Common.toBytes(akey));

    // gen data (client_DH_inner_data)
    TL.Object data_obj = TL.newObject("client_DH_inner_data", cl_nonce, sv_nonce, retry_id, g_b);
    byte[] data = data_obj.serialize();
    byte[] hash = Common.getSHA1(data);

    byte[] data_with_hash = new byte[(hash.length + data.length + 15) / 16 * 16];
    System.arraycopy(hash, 0, data_with_hash, 0, hash.length);
    System.arraycopy(data, 0, data_with_hash, hash.length, data.length);

    // send set_client_DH_params
    TL.Object req_obj =
        TL.newObject("set_client_DH_params", cl_nonce, sv_nonce, aes.IGE(data_with_hash, true));
    send(req_obj, false, false);
  }
Пример #5
0
  public void onAuthorized() {
    dcState.set("auth_key", auth_key);
    dcState.set("server_salt", server_salt);

    long upd_delta = (long) Common.getUnixTime() - (long) dc_date;
    if (Main.mtp == this && upd_delta > 5 * 60) api_help_getConfig();

    if (!bind && this != Main.mtp) {
      final MTProto m = this;
      Common.logError("exportAuthorization");
      Main.mtp.api(
          new TL.OnResultRPC() {
            @Override
            public void onResultRPC(TL.Object result, Object param, boolean error) {
              if (!error) return;
              if (result.id != 0xdf969c2d) return; // auth.ExportedAuthorization
              Common.logError("importAuthorization");
              m.api(
                  null,
                  null,
                  "auth.importAuthorization",
                  result.getInt("id"),
                  result.getBytes("bytes"));
            }
          },
          null,
          "auth.exportAuthorization",
          dcState.getInt("id"));
      return;
    }

    if (bind) cb.onBind();
    send_queue();
  }
Пример #6
0
 private void TL_Server_DH_inner_data(TL.Object obj) {
   g = BigInteger.valueOf(obj.getInt("g"));
   dh_prime = new BigInteger(1, obj.getBytes("dh_prime"));
   g_a = new BigInteger(1, obj.getBytes("g_a"));
   time_delta =
       (int) ((long) obj.getInt("server_time") - (long) System.currentTimeMillis() / 1000L);
   last_message_id = 0;
   send_client_DH_inner_data(0);
 }
Пример #7
0
 private void TL_contacts_ImportedContacts(TL.Object obj) {
   User.addUsers(obj.getVector("users"));
   TL.Vector imported = obj.getVector("imported");
   for (int i = 0; i < imported.count; i++) {
     TL.Object c = imported.getObject(i);
     User.getUser(c.getInt("user_id")).client_id = c.getLong("client_id");
   }
   SyncUtils.updateContacts(Main.main);
 }
Пример #8
0
 private void TL_RpcResult(TL.Object obj) {
   req_msg_id = obj.getLong("req_msg_id");
   TL.Object result = obj.getObject("result");
   Message msg = TLMessage.get(req_msg_id);
   if (msg != null && msg.result != null)
     msg.result.onResultRPC(
         result, msg.param, result.name != null && result.name.equals("rpc_error"));
   process(result);
   TLMessage.delete(req_msg_id);
 }
Пример #9
0
  private void TL_MessageContainer(TL.Object obj) {
    TL.Vector v = (TL.Vector) obj;

    int count = v.count;
    Common.logDebug("msg_count: " + count);
    for (int i = 0; i < v.count; i++) {
      TL.Object message = v.getObject(i);
      ack_message(message.getInt("seqno"), message.getLong("msg_id"));
      process(message.getObject("body"));
    }
  }
Пример #10
0
 public void onConnect() {
   connected = true;
   Common.logInfo(
       "connected to dc["
           + dcState.getInt("id")
           + "] "
           + dcState.getString("ip")
           + ":"
           + dcState.getInt("port"));
   if (auth_key == null || auth_key.length == 0) {
     auth();
   } else onAuthorized();
 }
Пример #11
0
 private void TL_MsgsAck(TL.Object obj) {
   TL.Vector msg_ids = obj.getVector("msg_ids");
   for (int i = 0; i < msg_ids.count; i++) {
     Message msg = TLMessage.get(msg_ids.getLong(i));
     if (msg != null) msg.accepted = true;
     // TLMessage.delete(msg_ids.getLong(i));
   }
 }
Пример #12
0
  private void TL_messages_StatedMessage(TL.Object obj) {
    setUpdate(-1, obj.getInt("pts"), obj.getInt("seq"));

    User.addUsers(obj.getVector("users"));
    TL.Object message = obj.getObject("message");
    Dialog d = Dialog.getDialog(message.getInt("from_id"), message.getObject("to_id"), true);
    Dialog.newMessage(message, Dialog.MSG_INCOMING);
  }
Пример #13
0
  private void TL_Server_DH_Params(TL.Object obj) {
    if (obj.name.equals("server_DH_params_ok")) {
      aes.prepare(cl_nonce, sv_nonce, new_nonce);
      byte[] answer = aes.IGE(obj.getBytes("encrypted_answer"), false);

      ByteBuffer btmp = ByteBuffer.wrap(answer);
      btmp.order(ByteOrder.LITTLE_ENDIAN);
      btmp.position(20);
      process(TL.deserialize(btmp));
    }

    if (obj.name.equals("server_DH_params_fail")) transport.disconnect();
  }
Пример #14
0
  private void TL_Pong(TL.Object obj) {
    if (obj.name.equals("ping"))
      send(TL.newObject("pong", 0L, obj.getLong("ping_id")), true, false);

    if (obj.name.equals("pong")) {
      /*
      if (!ready) {
      	send_queue();
      	ready = true;
      }
      */
    }
  }
Пример #15
0
  public void setAuthKey(byte[] value) {
    if (value == null || value.length == 0) {
      auth_key = new byte[0];
      auth_key_aux_hash = auth_key_id = 0;
      return;
    }
    auth_key = value;
    dcState.set("auth_key", auth_key);

    byte[] auth_hash = Common.getSHA1(auth_key);
    auth_key_aux_hash = Common.toLong(Common.ASUB(auth_hash, 0, 8));
    auth_key_id = Common.toLong(Common.ASUB(auth_hash, 12, 8));
  }
Пример #16
0
  private void TL_messages_StatedMessages(TL.Object obj) {
    setUpdate(-1, obj.getInt("pts"), obj.getInt("seq"));

    User.addUsers(obj.getVector("users"));
    TL.Vector messages = obj.getVector("messages");
    for (int i = 0; i < messages.count; i++) {
      TL.Object message = messages.getObject(i);
      Dialog d = Dialog.getDialog(message.getInt("from_id"), message.getObject("to_id"), true);
      Dialog.newMessage(message, Dialog.MSG_INCOMING);
    }
  }
Пример #17
0
  // MTProto
  public MTProto(int dc_id, MTProtoListener callback, int reuseFlag) {
    this.reuseFlag = reuseFlag;
    cb = callback;
    for (int i = 0; i < dcStates.size(); i++)
      if (dcStates.get(i).getInt("id") == dc_id) {
        dcState = dcStates.get(i);
        // Common.hexStringToByteArray("23D03699CE2AB29BA2273084D95DA126EC3A1D55BE4C317615CE66609D5562BDC0EFDE5AE1F9185001C35781F622B31DF5294685559340DE7D5CC8D7F6F86AE049107D8E498EB2AC3D6FA735DF90648EEC34A6B7BE3A5075A455F5696DB39280BF68C1637E1580E1EBA3F0C12EF2C03B8E9B5ECCFD3E4885BF636863388E3EC9E9EF60C722FF9B45CD93FA5E8D0D277B45A6A9370860582A159187F2F352D418D195D8E9310B5559E170F51CB2056F6CB6DB586E9349192A1B7EAA50887C115A14F996F5A855E90E47635A81EA3048615F4FD91347D73335E5503179857D0D29132483271B28E6591172C3D94686BD96E91FDB7AD9591A526218B3DDFC7A2A09")
        setAuthKey(dcState.getBytes("auth_key"));
        bind = dcState.getBool("bind");
        seqno = dcState.getInt("seqno");
        session = dcState.getLong("session");
        server_salt = dcState.getLong("server_salt");
        Common.logError("session: " + session + " seqno: " + seqno);

        connected = false;
        Thread netThread = new Thread(transport = new TransportTCP(this, true));
        netThread.setPriority(Thread.MIN_PRIORITY);
        netThread.start();

        return;
      }
    Common.logError("dc not found: " + dc_id);
  }
Пример #18
0
  public void TL_messages_Dialogs(TL.Object obj) {
    User.addUsers(obj.getVector("users"));
    Chat.addChats(obj.getVector("chats"));

    // dialogs
    TL.Vector dialogs = obj.getVector("dialogs");
    for (int i = 0; i < dialogs.count; i++) {
      TL.Object dobj = dialogs.getObject(i);
      Dialog d = Dialog.getDialog(-1, dobj.getObject("peer"), true);
      d.updating = true;
    }

    // messages
    TL.Vector messages = obj.getVector("messages");
    for (int i = 0; i < messages.count; i++)
      cb.onMessage(messages.getObject(i), Dialog.MSG_HISTORY);

    for (int i = 0; i < dialogs.count; i++) {
      TL.Object dobj = dialogs.getObject(i);
      Dialog d = Dialog.getDialog(-1, dobj.getObject("peer"), true);
      if (dobj.getInt("top_message") == -7) // config_dialogs magic
      d.noHistory = true;
      d.unread_count = dobj.getInt("unread_count");
      d.updating = false;
      Main.main.updateDialog(d);
    }

    Common.logError("dialogs: " + Dialog.dialogs.size());

    // slice
    if (obj.name.equals("messages.dialogsSlice") && dialogs.count > 0) {
      int limit = obj.getInt("count") - Dialog.dialogs.size();
      if (limit > 0) api_messages_getDialogs(Dialog.dialogs.size(), 0, limit);
    }

    if (Main.main != null) Main.main.resetDialogs();
  }
Пример #19
0
  private void TL_ResPQ(TL.Object obj) {
    sv_nonce = obj.getBytes("server_nonce");
    BigInteger pq = new BigInteger(1, obj.getBytes("pq"));
    TL.Vector v_fp = obj.getVector("server_public_key_fingerprints");
    fp = v_fp.getLong(0);
    Common.logError("pq: " + pq.toString());

    // prime factorization for pq
    BigInteger q = Common.rho(pq);
    BigInteger p = pq.divide(q);
    if (p.compareTo(q) > 0) {
      BigInteger t = p;
      p = q;
      q = t;
    }
    SecureRandom rnd = new SecureRandom();
    new_nonce = new byte[32];
    rnd.nextBytes(new_nonce);

    // generate encrypted_data
    TL.Object data_obj = TL.newObject("p_q_inner_data", pq, p, q, cl_nonce, sv_nonce, new_nonce);

    byte[] data = data_obj.serialize();
    byte[] hash = Common.getSHA1(data);

    byte[] data_with_hash = new byte[255];
    System.arraycopy(hash, 0, data_with_hash, 0, hash.length);
    System.arraycopy(data, 0, data_with_hash, hash.length, data.length);
    GEN_random_bytes(data_with_hash, data.length + hash.length, 255);

    byte[] encrypted_data = Common.RSA(RSA_MODULUS, RSA_EXPONENT, data_with_hash);

    // req_DH_params
    TL.Object req_obj = TL.newObject("req_DH_params", cl_nonce, sv_nonce, p, q, fp, encrypted_data);
    send(req_obj, false, false);
  }
Пример #20
0
  private void TL_updates_Difference(TL.Object obj) {
    if (obj.name.equals("updates.differenceEmpty"))
      setUpdate(obj.getInt("date"), -1, obj.getInt("seq"));

    if (obj.name.equals("updates.difference") || obj.name.equals("updates.differenceSlice")) {

      TL.Vector other_updates = obj.getVector("other_updates");
      for (int i = 0; i < other_updates.count; i++)
        onUpdate(other_updates.getObject(i), obj.getVector("users"), obj.getVector("chats"));

      TL.Vector new_messages = obj.getVector("new_messages");
      for (int i = 0; i < new_messages.count; i++)
        Dialog.newMessage(new_messages.getObject(i), Dialog.MSG_HISTORY);

      //			if (new_messages.count > 0)
      //				Dialog.vibrate();

      if (obj.name.equals("updates.differenceSlice")) {
        TL_updates_State(obj.getObject("intermediate_state"));
        this.api_updates_getDifference();
      } else TL_updates_State(obj.getObject("state"));
    }
  }
Пример #21
0
 private void TL_updateUserPhoto(TL.Object update) {
   User user = User.getUser(update.getInt("user_id"));
   if (user != null) user.setPhoto(update.getObject("photo"));
 }
Пример #22
0
 private void TL_updateUserName(TL.Object update) {
   cb.onUserName(
       update.getInt("user_id"), update.getString("first_name"), update.getString("last_name"));
 }
Пример #23
0
 private void TL_updateUserStatus(TL.Object update) {
   User user = User.getUser(update.getInt("user_id"));
   if (user != null) user.setStatus(update.getObject("status"));
 }
Пример #24
0
 private void TL_updateChatParticipants(TL.Object update) {
   TL.Object p = update.getObject("participants");
   Chat chat = Chat.getChat(p.getInt("chat_id"));
   if (chat != null) chat.setParticipants(p);
 }
Пример #25
0
 private void TL_updateChatUserTyping(TL.Object update) {
   userTyping(update.getInt("chat_id"), update.getInt("user_id"));
 }
Пример #26
0
 private void TL_updateUserTyping(TL.Object update) {
   userTyping(-1, update.getInt("user_id"));
 }
Пример #27
0
 private void TL_updateRestoreMessages(TL.Object update) {
   setUpdate(-1, update.getInt("pts"), -1);
   TL.Vector messages = update.getVector("messages");
   for (int i = 0; i < messages.count; i++) Dialog.messageRestore(messages.getInt(i));
 }
Пример #28
0
 private void TL_updateMessageID(TL.Object update) {
   Dialog.messageUpdateID(update.getLong("random_id"), update.getInt("id"), null);
 }
Пример #29
0
 private void TL_updateNewMessage(TL.Object update) {
   cb.onMessage(update.getObject("message"), Dialog.MSG_INCOMING | Dialog.MSG_HISTORY);
   setUpdate(-1, update.getInt("pts"), -1);
 }
Пример #30
0
 private void TL_updates_State(TL.Object obj) {
   setUpdate(obj.getInt("date"), obj.getInt("pts"), obj.getInt("seq"));
   // unread_count:int
 }