Skip to content

Commit

Permalink
[MG] use Simple Push since Telegram Web Push seems not reliable
Browse files Browse the repository at this point in the history
Mercurygram uses p2p.belloworld.it as rewite proxy to convert Telegram PUTs
to POSTs when needed (aka when ntfy is not used)

Privacy note:

The payload of the PUT request ONLY includes version=number and so no
personal information are included in the payload.
  • Loading branch information
Timothy Redaelli committed Jan 16, 2024
1 parent 5873215 commit 0193a25
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1525,7 +1525,7 @@ public void onRequestPushToken() {
ApplicationLoader.applicationContext,
"default",
new ArrayList<>(),
"Telegram WebPush"
"Telegram Simple Push"
);
} catch (Throwable e) {
FileLog.e(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,64 +47,9 @@ public void onNewEndpoint(Context context, String endpoint, String instance){

if (savedDistributor.equals("io.heckel.ntfy")) {
PushListenerController.sendRegistrationToServer(PushListenerController.PUSH_TYPE_SIMPLE, endpoint);
return;
}

if (!endpoint.equals(SharedConfig.pushString)) {
// Generate ECDH Keypair on P-256 curve
KeyPairGenerator ecdhKeyPairGenerator;
try {
ecdhKeyPairGenerator = KeyPairGenerator.getInstance("EC");
ecdhKeyPairGenerator.initialize(new ECGenParameterSpec("secp256r1"));
} catch (Throwable exception) {
FileLog.e(exception);
return;
}
KeyPair ecdhKeyPair = ecdhKeyPairGenerator.generateKeyPair();
SharedConfig.pushAuthKey = ecdhKeyPair.getPrivate().getEncoded();
SharedConfig.pushAuthPubKey = ecdhKeyPair.getPublic().getEncoded();
// Generate auth secret
SharedConfig.pushAuthSecret = new byte[16];
Utilities.random.nextBytes(SharedConfig.pushAuthSecret);
SharedConfig.saveConfig();

try {
jsonToken = new JSONStringer()
.object()
.key("endpoint").value(endpoint)
.key("keys")
.object()
.key("p256dh").value(Base64.encodeToString(convertECPubkeyToUncompressedOctetStream((ECPublicKey) ecdhKeyPair.getPublic()), Base64.URL_SAFE | Base64.NO_WRAP))
.key("auth").value(Base64.encodeToString(SharedConfig.pushAuthSecret, Base64.URL_SAFE | Base64.NO_WRAP))
.endObject()
.endObject()
.toString();
} catch (Throwable exception) {
FileLog.e(exception);
return;
}
} else {
try {
final KeyFactory ECKeyFactory = KeyFactory.getInstance("EC");
ECPublicKey pubKey = (ECPublicKey) ECKeyFactory.generatePublic(new X509EncodedKeySpec(SharedConfig.pushAuthPubKey));

jsonToken = new JSONStringer()
.object()
.key("endpoint").value(endpoint)
.key("keys")
.object()
.key("p256dh").value(Base64.encodeToString(convertECPubkeyToUncompressedOctetStream(pubKey), Base64.URL_SAFE | Base64.NO_WRAP))
.key("auth").value(Base64.encodeToString(SharedConfig.pushAuthSecret, Base64.URL_SAFE | Base64.NO_WRAP))
.endObject()
.endObject()
.toString();
} catch (Throwable exception) {
FileLog.e(exception);
return;
}
PushListenerController.sendRegistrationToServer(PushListenerController.PUSH_TYPE_SIMPLE, "https://p2p.belloworld.it/" + endpoint);
}

PushListenerController.sendRegistrationToServer(PushListenerController.PUSH_TYPE_WEBPUSH, jsonToken);
});
}

Expand Down Expand Up @@ -158,7 +103,7 @@ public void onRegistrationFailed(Context context, String instance){
Utilities.globalQueue.postRunnable(() -> {
SharedConfig.pushStringGetTimeEnd = SystemClock.elapsedRealtime();

PushListenerController.sendRegistrationToServer(PushListenerController.PUSH_TYPE_WEBPUSH, null);
PushListenerController.sendRegistrationToServer(PushListenerController.PUSH_TYPE_SIMPLE, null);
});
}

Expand All @@ -168,31 +113,7 @@ public void onUnregistered(Context context, String instance){
Utilities.globalQueue.postRunnable(() -> {
SharedConfig.pushStringGetTimeEnd = SystemClock.elapsedRealtime();

PushListenerController.sendRegistrationToServer(PushListenerController.PUSH_TYPE_WEBPUSH, null);
PushListenerController.sendRegistrationToServer(PushListenerController.PUSH_TYPE_SIMPLE, null);
});
}

static final private ECParameterSpec P256CurveSpec = new ECParameterSpec(
new EllipticCurve(
new ECFieldFp(new BigInteger("115792089210356248762697446949407573530086143415290314195533631308867097853951")),
new BigInteger("115792089210356248762697446949407573530086143415290314195533631308867097853948"),
new BigInteger("41058363725152142129326129780047268409114441015993725554835256314039467401291")
),
new ECPoint(
new BigInteger("48439561293906451759052585252797914202762949526041747995844080717082404635286"),
new BigInteger("36134250956749795798585127919587881956611106672985015071877198253568414405109")
),
new BigInteger("115792089210356248762697446949407573529996955224135760342422259061068512044369"), 0x1
);
static final private int ECCoordinateSize = (((ECFieldFp) P256CurveSpec.getCurve().getField()).getP().subtract(BigInteger.ONE).bitLength() + 7) / 8;
private static byte[] convertECPubkeyToUncompressedOctetStream(ECPublicKey pubkey) {
final byte[] stream = new byte[2 * ECCoordinateSize + 1];
final ECPoint pubkeyCurvePoint = pubkey.getW();
final byte[] pointX = pubkeyCurvePoint.getAffineX().toByteArray();
final byte[] pointY = pubkeyCurvePoint.getAffineY().toByteArray();
System.arraycopy(pointX, 0, stream, 1 + ECCoordinateSize - pointX.length, pointX.length);
System.arraycopy(pointY, 0, stream, 1 + 2 * ECCoordinateSize - pointY.length, pointY.length);
stream[0] = 0x04;
return stream;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ else if (position == unifiedPushDistributorRow) {
UnifiedPush.registerApp(ApplicationLoader.applicationContext,
"default",
new ArrayList<String>(),
"Telegram WebPush");
"Telegram Simple Push");
updateUnifiedPushDistributor = true;
adapter.notifyItemChanged(position);
dialogRef.get().dismiss();
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
#Sat Mar 12 05:53:50 MSK 2016
APP_VERSION_CODE=422803
APP_VERSION_NAME=10.5.0.3
APP_VERSION_CODE=422804
APP_VERSION_NAME=10.5.0.4
APP_PACKAGE=it.belloworld.mercurygram
org.gradle.jvmargs=-Xmx4096M -XX:MaxMetaspaceSize=4096m
org.gradle.daemon=true
Expand Down

0 comments on commit 0193a25

Please sign in to comment.