| 1 | package tw.nekomimi.nekogram |
| 2 | |
| 3 | import cn.hutool.crypto.digest.DigestUtil |
| 4 | import org.telegram.messenger.MessagesController |
| 5 | import org.telegram.tgnet.ConnectionsManager |
| 6 | import org.telegram.tgnet.SerializedData |
| 7 | import org.telegram.tgnet.TLRPC |
| 8 | import java.math.BigInteger |
| 9 | import java.nio.ByteBuffer |
| 10 | import java.security.interfaces.RSAPublicKey |
| 11 | |
| 12 | object DataCenter { |
| 13 | |
| 14 | // func calcAuthKeyId(keyData []byte) int64 { |
| 15 | // sha1 := Sha1Digest(keyData) |
| 16 | // // Lower 64 bits = 8 bytes of 20 byte SHA1 hash. |
| 17 | // return int64(binary.LittleEndian.Uint64(sha1[12:])) |
| 18 | // } |
| 19 | @JvmStatic |
| 20 | fun calcAuthKeyId(publicKey: RSAPublicKey): Long { |
| 21 | |
| 22 | val key = SerializedData() |
| 23 | |
| 24 | key.writeByteArray(publicKey.modulus.toByteArray()) |
| 25 | key.writeByteArray(publicKey.publicExponent.toByteArray()) |
| 26 | |
| 27 | return BigInteger(DigestUtil.sha1(key.toByteArray()).slice(12 until 20).toByteArray()).toLong() |
| 28 | |
| 29 | } |
| 30 | |
| 31 | @JvmStatic |
| 32 | fun applyOfficalDataCanter(account: Int) { |
| 33 | |
| 34 | MessagesController.getMainSettings(account).edit().remove("layer").remove("custom_dc").apply() |
| 35 | |
| 36 | if (ConnectionsManager.native_isTestBackend(account) != 0) { |
| 37 | |
| 38 | ConnectionsManager.getInstance(account).switchBackend() |
| 39 | |
| 40 | } |
| 41 | |
| 42 | applyDataCanter(account, 1, "149.154.175.50", "2001:b28:f23d:f001:0000:0000:0000:000a") |
| 43 | applyDataCanter(account, 2, "149.154.167.51", "2001:67c:4e8:f002:0000:0000:0000:000a") |
| 44 | applyDataCanter(account, 3, "149.154.175.100", "2001:b28:f23d:f003:0000:0000:0000:000a") |
| 45 | applyDataCanter(account, 4, "149.154.167.91", "2001:67c:4e8:f004:0000:0000:0000:000a") |
| 46 | applyDataCanter(account, 5, "149.154.171.5", "2001:67c:4e8:f005:0000:0000:0000:000a") |
| 47 | |
| 48 | ConnectionsManager.native_cleanUp(account, true) |
| 49 | ConnectionsManager.native_setLayer(account, TLRPC.LAYER) |
| 50 | |
| 51 | repeat(5) { |
| 52 | |
| 53 | ConnectionsManager.native_setDatacenterPublicKey(account, it + 1, "", 0) |
| 54 | |
| 55 | } |
| 56 | |
| 57 | } |
| 58 | |
| 59 | @JvmStatic |
| 60 | fun applyTestDataCenter(account: Int) { |
| 61 | |
| 62 | MessagesController.getMainSettings(account).edit().remove("layer").remove("custom_dc").apply() |
| 63 | |
| 64 | if (ConnectionsManager.native_isTestBackend(account) == 0) { |
| 65 | |
| 66 | ConnectionsManager.getInstance(account).switchBackend() |
| 67 | |
| 68 | } |
| 69 | |
| 70 | ConnectionsManager.native_setLayer(account, TLRPC.LAYER) |
| 71 | |
| 72 | } |
| 73 | |
| 74 | @JvmStatic |
| 75 | fun applyCustomDataCenter(account: Int, ipv4Address: String = "", ipv6Address: String = "", port: Int, layer: Int, publicKey: String, fingerprint: Long) { |
| 76 | |
| 77 | MessagesController.getMainSettings(account).edit().putInt("layer", layer).putBoolean("custom_dc", true).apply() |
| 78 | |
| 79 | if (ConnectionsManager.native_isTestBackend(account) != 0) { |
| 80 | |
| 81 | ConnectionsManager.getInstance(account).switchBackend() |
| 82 | |
| 83 | } |
| 84 | |
| 85 | repeat(5) { |
| 86 | |
| 87 | ConnectionsManager.native_setDatacenterAddress(account, it + 1, ipv4Address, ipv6Address, port) |
| 88 | |
| 89 | } |
| 90 | |
| 91 | ConnectionsManager.native_saveDatacenters(account) |
| 92 | ConnectionsManager.native_setLayer(account, layer) |
| 93 | |
| 94 | repeat(5) { |
| 95 | |
| 96 | ConnectionsManager.native_setDatacenterPublicKey(account, it + 1, publicKey, fingerprint); |
| 97 | |
| 98 | } |
| 99 | |
| 100 | |
| 101 | } |
| 102 | |
| 103 | private fun applyDataCanter(account: Int, dataCenterId: Int, ipv4Address: String, ipv6Address: String, port: Int = 443) { |
| 104 | |
| 105 | ConnectionsManager.native_setDatacenterAddress(account, dataCenterId, ipv4Address, ipv4Address, port) |
| 106 | |
| 107 | } |
| 108 | |
| 109 | } |