Persist authorization key
Useful until step 2 stops failing half the time
This commit is contained in:
parent
7cfd9034d0
commit
757f684c6b
|
@ -7,3 +7,6 @@ build/
|
||||||
**/tl/Abstract.java
|
**/tl/Abstract.java
|
||||||
**/tl/Types.java
|
**/tl/Types.java
|
||||||
**/tl/Functions.java
|
**/tl/Functions.java
|
||||||
|
|
||||||
|
# Authorization Key
|
||||||
|
*.key
|
||||||
|
|
|
@ -1,19 +1,42 @@
|
||||||
package io.github.lonamiwebs.overgram;
|
package io.github.lonamiwebs.overgram;
|
||||||
|
|
||||||
|
import io.github.lonamiwebs.overgram.crypto.AuthKey;
|
||||||
import io.github.lonamiwebs.overgram.network.MTProtoSender;
|
import io.github.lonamiwebs.overgram.network.MTProtoSender;
|
||||||
import io.github.lonamiwebs.overgram.network.MTProtoState;
|
import io.github.lonamiwebs.overgram.network.MTProtoState;
|
||||||
import io.github.lonamiwebs.overgram.network.connection.TcpFull;
|
import io.github.lonamiwebs.overgram.network.connection.TcpFull;
|
||||||
import io.github.lonamiwebs.overgram.tl.Functions;
|
import io.github.lonamiwebs.overgram.tl.Functions;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
import java.util.concurrent.Future;
|
import java.util.concurrent.Future;
|
||||||
|
|
||||||
public class Overgram {
|
public class Overgram {
|
||||||
|
private static void saveAuthKey(final AuthKey authKey, final File file) {
|
||||||
|
try (final FileOutputStream out = new FileOutputStream(file)) {
|
||||||
|
out.write(authKey.key);
|
||||||
|
} catch (IOException ignored) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static AuthKey loadAuthKey(final File file) {
|
||||||
|
try (final FileInputStream in = new FileInputStream(file)) {
|
||||||
|
final byte[] key = new byte[256];
|
||||||
|
assert in.read(key) == key.length;
|
||||||
|
return new AuthKey(key);
|
||||||
|
} catch (IOException ignored) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static void main(final String... args) throws IOException, InterruptedException, ExecutionException {
|
public static void main(final String... args) throws IOException, InterruptedException, ExecutionException {
|
||||||
final MTProtoSender sender = new MTProtoSender(new MTProtoState(), new TcpFull());
|
final File authKey = new File("auth.key");
|
||||||
|
final MTProtoSender sender = new MTProtoSender(new MTProtoState(loadAuthKey(authKey)), new TcpFull());
|
||||||
try {
|
try {
|
||||||
sender.connect("149.154.167.91", 443);
|
sender.connect("149.154.167.91", 443);
|
||||||
|
saveAuthKey(sender.state.authKey, authKey);
|
||||||
final Future result = sender.send(new Functions.Ping());
|
final Future result = sender.send(new Functions.Ping());
|
||||||
System.out.println(result.get());
|
System.out.println(result.get());
|
||||||
} finally {
|
} finally {
|
||||||
|
|
|
@ -15,7 +15,7 @@ public class MTProtoPlainSender {
|
||||||
|
|
||||||
public MTProtoPlainSender(final Connection connection) {
|
public MTProtoPlainSender(final Connection connection) {
|
||||||
this.connection = connection;
|
this.connection = connection;
|
||||||
this.state = new MTProtoState();
|
this.state = new MTProtoState(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T extends TLObject> T send(final TLRequest request) throws IOException, ClassNotFoundException {
|
public <T extends TLObject> T send(final TLRequest request) throws IOException, ClassNotFoundException {
|
||||||
|
|
|
@ -16,7 +16,7 @@ import java.util.concurrent.LinkedBlockingQueue;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
public class MTProtoSender {
|
public class MTProtoSender {
|
||||||
private final MTProtoState state;
|
public final MTProtoState state;
|
||||||
private final Connection connection;
|
private final Connection connection;
|
||||||
private String ip;
|
private String ip;
|
||||||
private int port;
|
private int port;
|
||||||
|
|
|
@ -23,13 +23,13 @@ public class MTProtoState {
|
||||||
private int sequence;
|
private int sequence;
|
||||||
private long lastMsgId;
|
private long lastMsgId;
|
||||||
|
|
||||||
public MTProtoState() {
|
public MTProtoState(final AuthKey authKey) {
|
||||||
|
this.authKey = authKey;
|
||||||
|
salt = 0;
|
||||||
id = new SecureRandom().nextLong();
|
id = new SecureRandom().nextLong();
|
||||||
timeOffset = 0;
|
timeOffset = 0;
|
||||||
sequence = 0;
|
sequence = 0;
|
||||||
lastMsgId = 0;
|
lastMsgId = 0;
|
||||||
authKey = null;
|
|
||||||
salt = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public TLMessage createMessage(final TLObject object) {
|
public TLMessage createMessage(final TLObject object) {
|
||||||
|
|
Loading…
Reference in New Issue