91 lines
3.5 KiB
Java
91 lines
3.5 KiB
Java
package io.github.lonamiwebs.overgram.client;
|
|
|
|
import io.github.lonamiwebs.overgram.crypto.AuthKey;
|
|
import io.github.lonamiwebs.overgram.network.MTProtoSender;
|
|
import io.github.lonamiwebs.overgram.network.MTProtoState;
|
|
import io.github.lonamiwebs.overgram.network.connection.Connection;
|
|
import io.github.lonamiwebs.overgram.network.connection.TcpFull;
|
|
import io.github.lonamiwebs.overgram.session.BinarySession;
|
|
import io.github.lonamiwebs.overgram.session.Session;
|
|
import io.github.lonamiwebs.overgram.tl.Abstract;
|
|
import io.github.lonamiwebs.overgram.tl.Functions;
|
|
import io.github.lonamiwebs.overgram.tl.TLRequest;
|
|
|
|
import java.io.IOException;
|
|
import java.util.concurrent.ExecutionException;
|
|
import java.util.concurrent.Future;
|
|
|
|
public abstract class TelegramBaseClient {
|
|
private final Session session;
|
|
private final int apiId;
|
|
private final String apiHash;
|
|
private final MTProtoSender sender;
|
|
private final Functions.InitConnection init;
|
|
|
|
public static final int DEFAULT_DC_ID = 4;
|
|
public static final String DEFAULT_IPV4_IP = "149.154.167.51";
|
|
public static final String DEFAULT_IPV6_IP = "[2001:67c:4e8:f002::a]";
|
|
public static final int DEFAULT_PORT = 443;
|
|
|
|
public TelegramBaseClient(final String session, final int apiId, final String apiHash) {
|
|
this(new BinarySession(session), apiId, apiHash, new TcpFull(), new Functions.InitConnection().apiId(apiId)
|
|
.deviceModel(String.format("Java %s",
|
|
System.getProperty("java.version")
|
|
))
|
|
.systemVersion(String.format("%s %s %s",
|
|
System.getProperty("os.name"),
|
|
System.getProperty("os.version"),
|
|
System.getProperty("os.arch")
|
|
))
|
|
.appVersion("0.1")
|
|
.langCode(System.getProperty("user.language"))
|
|
.systemLangCode(System.getProperty("user.language"))
|
|
.langPack(""));
|
|
}
|
|
|
|
public TelegramBaseClient(final Session session, final int apiId, final String apiHash,
|
|
final Connection connection, final Functions.InitConnection init) {
|
|
session.load();
|
|
this.session = session;
|
|
this.apiId = apiId;
|
|
this.apiHash = apiHash;
|
|
this.init = init;
|
|
|
|
final byte[] authKey = session.getAuthKey(session.getDcId());
|
|
this.sender = new MTProtoSender(new MTProtoState(
|
|
authKey == null ? null : new AuthKey(authKey)), connection);
|
|
}
|
|
|
|
public void connect() throws IOException {
|
|
String ip = session.getServerAddress(false);
|
|
int port = session.getServerPort();
|
|
if (ip == null || ip.isEmpty()) {
|
|
ip = DEFAULT_IPV4_IP;
|
|
port = DEFAULT_PORT;
|
|
}
|
|
|
|
sender.connect(ip, port);
|
|
session.setAuthKey(DEFAULT_DC_ID, sender.state.authKey.key);
|
|
final Future result = sender.send(new Functions.InvokeWithLayer()
|
|
.layer(Abstract.LAYER)
|
|
.query(init.query(new Functions.help.GetConfig())));
|
|
|
|
try {
|
|
result.get();
|
|
} catch (ExecutionException | InterruptedException ignored) {
|
|
throw new IOException();
|
|
}
|
|
}
|
|
|
|
public void disconnect() {
|
|
sender.disconnect();
|
|
session.save();
|
|
}
|
|
|
|
@SuppressWarnings("unchecked")
|
|
public <T> T send(final TLRequest<T> request) throws IOException, InterruptedException, ExecutionException {
|
|
final Future result = sender.send(request);
|
|
return (T) result.get();
|
|
}
|
|
}
|