示例#1
1
 private User(TL.Object user) {
   photo = null;
   //		photo_small = null;
   if (!user.name.equals("userFull")) {
     user =
         TL.newObject(
             "userFull",
             user,
             TL.newObject(
                 "contacts.link",
                 TL.newObject("contacts.myLinkEmpty"),
                 TL.newObject("contacts.foreignLinkUnknown"),
                 user),
             TL.newObject("photoEmpty", 0L),
             TL.newObject("peerNotifySettingsEmpty"),
             false,
             "",
             "");
     update(user);
     synchronized (fullInfoQuery) {
       fullInfoQuery.add(this);
     }
   } else update(user);
 }
示例#2
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);
  }
示例#3
0
  public void onReceive(ByteBuffer b) {
    if (b.remaining() == 4) {
      Common.logError("invalid packet:");
      byte[] data = new byte[b.remaining()];
      b.get(data);
      Common.print_dump(data);
      //	retry( TLMessage.keyAt(  TLMessage.size() - 1) );
      return;
    }

    try {
      if (b.getLong() == 0) { // auth_key_id
        b.getLong(); // message_id
        b.getInt(); // message length
        process(TL.deserialize(b));
      } else {
        byte[] msg_key = new byte[16];
        b.get(msg_key);
        byte[] data = new byte[b.remaining()];
        b.get(data);

        synchronized (aes) {
          aes.prepare(false, msg_key, auth_key);
          data = aes.IGE(data, false);
        }

        ByteBuffer btmp = ByteBuffer.wrap(data);
        btmp.order(ByteOrder.LITTLE_ENDIAN);
        server_salt = btmp.getLong();
        btmp.getLong(); // session_id
        cur_message_id = btmp.getLong(); // message_id
        cur_msg_seq = btmp.getInt(); // seq_no
        // if (cur_msg_seq > seqno)
        //	seqno = cur_msg_seq + cur_msg_seq % 2;
        int bsize = btmp.getInt();
        if (bsize < 0 || bsize > 1024 * 1024) {
          Common.print_dump(btmp.array());
          Common.logError(new String(btmp.array()));
          Common.logError("FFFUUUUUUUUUUU!!!");
        }

        b = BufferAlloc(bsize);
        btmp.get(b.array());
        b.getInt();
        ack_message(cur_msg_seq, cur_message_id);
        b.position(0);

        process(TL.deserialize(b));
        send_accept();
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
示例#4
0
  /*
  	public byte[] GEN_nonce_hash(int id) {
  		byte[] nonce_data = Common.ASUM(Common.ASUM(new_nonce, new byte[]{(byte) id}), auth_key_aux_hash);
  		return Common.ASUB(Common.getSHA1(nonce_data), 4, 16);
  	}
  */
  private void send_accept() {
    if (msg_ack.size() == 0) return;
    /*
    	Common.logInfo("send accept:");
    	for (int i = 0; i < msg_ack.size(); i++)
    		Common.logInfo(" - " + getMessageTime(msg_ack.get(i))  + " " + msg_ack.get(i));
    */
    TL.Object msgs_ack = TL.newObject("msgs_ack", TL.newVector(msg_ack.toArray()));

    send(msgs_ack, true, false);
    msg_ack.clear();
  }
	@Override
	public void onCreate() {
		super.onCreate();
		context = this;

		TL.initialize(getContext(), ApplicationConst.APP_ID, ApplicationConst.APP_KEY, ApplicationConst.APP_SITE);
	}
示例#6
0
  public static void init() {
    mtp.clear();
    dcStates.clear();
    //	dcStates.add(TL.newObject("joim.dcState", 0, "95.142.192.65", 80, new byte[0], false,
    // Common.random.nextLong(), 0L, 0));
    //	dcStates.add(TL.newObject("joim.dcState", 1, "173.240.5.253", 443, new byte[0], false,
    // Common.random.nextLong(), 0L, 0));
    dcStates.add(
        TL.newObject(
            "joim.dcState", 0, "173.240.5.1", 443, new byte[0], false, GEN_session_id(), 0L, 0));

    dc_date = dc_this = upd_date = upd_pts = upd_seq = 0;
  }
示例#7
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();
  }
示例#8
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;
      }
      */
    }
  }
示例#9
0
  @Override
  protected final Expr unLambda(final Name n) {
    final Map<Name, Expr> map = defs.unLambda().getMap();
    final LinkedList<Set<Name>> sorted = topoSort(map);

    Expr ex = getBody();
    for (final Set<Name> names : sorted) {
      if (names.size() == 1) {
        final Name nm = names.iterator().next();
        final Expr def = map.get(nm);
        final int uses = ex.numOfUses(nm);
        if (uses == 0) {
          // name unused, ignore it
          log.info("removing unused definition " + nm + ": " + def);
        } else if (uses == 1 || def instanceof Value) {
          // can be inlined
          log.info("inlining " + nm + ": " + def);
          ex = ex.inline(nm, def);
        } else {
          ex = App.create(new Lambda(nm.toString(), ex), map.get(nm));
        }
      } else {
        final Name[] na = names.toArray(new Name[names.size()]);

        Expr funs = NIL, res = K.app(ex);
        for (final Name element : na) {
          funs = CONS.app(map.get(element), funs);
        }

        funs = K.app(funs);

        for (final Name element : na) {
          funs = U.app(new Lambda(element.toString(), funs));
          if (res.hasFree(element)) {
            res = U.app(new Lambda(element.toString(), res));
          } else {
            // name unused, ignore it
            log.info("rewriting unused name: U { " + element + " -> f } ==> (f . tl)");
            final Name nm = Name.createName();
            res = new Lambda(nm.toString(), App.create(res, TL.app(nm)));
          }
        }

        ex = App.create(res, Y.app(funs));
      }
    }

    return ex.unLambda(n);
  }
示例#10
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);
  }
示例#11
0
  private void TL_Config(TL.Object obj) {
    dc_date = obj.getInt("date");
    MTProto.dc_this = obj.getInt("this_dc");
    dcState.set("id", dc_this);
    updateDcStates();

    TL.Vector dc_options = obj.getVector("dc_options");

    ArrayList<TL.Object> new_dcStates = new ArrayList<TL.Object>();

    for (int i = 0; i < dc_options.count; i++) {
      TL.Object dcObj = dc_options.getObject(i);
      int id = dcObj.getInt("id");

      TL.Object state =
          TL.newObject(
              "joim.dcState",
              id,
              dcObj.getString("ip_address"),
              dcObj.getInt("port"),
              new byte[0],
              false,
              GEN_session_id(),
              0L,
              0);

      for (int j = 0; j < MTProto.dcStates.size(); j++) {
        TL.Object item = MTProto.dcStates.get(j);
        if (id == item.getInt("id") && state.getString("ip").equals(item.getString("ip"))) {
          state.set("auth_key", item.getBytes("auth_key"));
          state.set("bind", item.getBool("bind"));
          state.set("session", item.getLong("session"));
          state.set("server_salt", item.getLong("server_salt"));
          state.set("seqno", item.getInt("seqno"));
          if (state.getInt("port") == item.getInt("port")) dcState = state;
          break;
        }
      }

      new_dcStates.add(state);
    }

    dcStates = new_dcStates;
    send_queue();
    cb.onReady();
  }
示例#12
0
  public static void init() {
    users.clear();
    fullInfoQuery.clear();

    self = addUser(TL.newObject("userEmpty", 0));
    contacts_hash = "";

    // update typing
    updater =
        new Updater(
            3 * 1000,
            new Runnable() {
              @Override
              public void run() {
                int time = Common.getUnixTime();
                for (int i = 0; i < User.users.size(); i++) {
                  User user = User.users.get(User.users.keyAt(i));
                  if (user.typing && user.typing_time < time)
                    user.setTyping(user.typing_chat_id, false);
                }
              }
            });
    updater.startUpdates();
  }
示例#13
0
 public TL.Object getInputPeer() {
   if (this == User.self) return TL.newObject("inputPeerSelf");
   if (!contact) return TL.newObject("inputPeerForeign", id, access_hash);
   return TL.newObject("inputPeerContact", id);
 }
示例#14
0
 private void send_ping() {
   send(TL.newObject("ping", Common.random.nextLong()), true, false);
 }
示例#15
0
 public static TL.Vector getVector() {
   ArrayList<TL.Object> res = new ArrayList<TL.Object>();
   for (int i = 0; i < User.users.size(); i++) res.add(User.users.get(User.users.keyAt(i)).object);
   return TL.newVector(res.toArray());
 }
示例#16
0
 public void auth() {
   Common.logError("auth");
   TL.Object req_pq = TL.newObject("req_pq", cl_nonce = GEN_random_bytes(16));
   send(req_pq, false, false);
 }
示例#17
0
 public void api_contacts_importContacts(ArrayList<TL.Object> contacts, boolean replace) {
   api(null, null, "contacts.importContacts", TL.newVector(contacts.toArray()), replace);
 }
示例#18
0
 // API
 public void api(TL.OnResultRPC result, Object param, String method, Object... params) {
   send(TL.newObject(method, params), true, true, result, param);
 }