Create a basic client and session storage

This commit is contained in:
Lonami Exo 2018-07-26 00:56:24 +02:00
parent 26c8660aba
commit a3bd98d7a2
6 changed files with 203 additions and 36 deletions

View File

@ -1,46 +1,16 @@
package io.github.lonamiwebs.overgram; package io.github.lonamiwebs.overgram;
import io.github.lonamiwebs.overgram.crypto.AuthKey; import io.github.lonamiwebs.overgram.client.TelegramBaseClient;
import io.github.lonamiwebs.overgram.network.MTProtoSender;
import io.github.lonamiwebs.overgram.network.MTProtoState;
import io.github.lonamiwebs.overgram.network.connection.TcpFull;
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.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
public class Overgram { public class Overgram {
private static void saveAuthKey(final AuthKey authKey, final File file) { public static void main(final String... args) throws Exception {
try (final FileOutputStream out = new FileOutputStream(file)) { final TelegramBaseClient client = new TelegramBaseClient(
out.write(authKey.key); "auth.key", Integer.parseInt(args[0]), args[1]) {};
} 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 {
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); client.connect();
saveAuthKey(sender.state.authKey, authKey);
final Future result = sender.send(new Functions.Ping());
System.out.println(result.get());
} finally { } finally {
sender.disconnect(); client.disconnect();
} }
} }
} }

View File

@ -0,0 +1,90 @@
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();
}
}

View File

@ -0,0 +1,7 @@
package io.github.lonamiwebs.overgram.client;
public class TelegramClient extends UserMethods {
public TelegramClient(String session, int apiId, String apiHash) {
super(session, apiId, apiHash);
}
}

View File

@ -0,0 +1,7 @@
package io.github.lonamiwebs.overgram.client;
public abstract class UserMethods extends TelegramBaseClient {
public UserMethods(String session, int apiId, String apiHash) {
super(session, apiId, apiHash);
}
}

View File

@ -0,0 +1,74 @@
package io.github.lonamiwebs.overgram.session;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class BinarySession implements Session {
protected final File file;
protected byte[] authKey;
protected int dcId;
protected String ip;
protected int port;
public BinarySession(final String file) {
this.file = new File(file).getAbsoluteFile();
}
@Override
public void load() {
try (final FileInputStream in = new FileInputStream(file)) {
authKey = new byte[256];
if (in.read(authKey) != authKey.length) {
authKey = null;
}
} catch (IOException ignored) {
}
}
@Override
public void save() {
if (authKey == null) {
return;
}
try (final FileOutputStream out = new FileOutputStream(file)) {
out.write(authKey);
} catch (IOException ignored) {
}
}
@Override
public void setPrimaryDc(int dc, String ipv4, String ipv6, int port) {
this.dcId = dc;
this.ip = ipv4;
this.port = port;
}
@Override
public int getDcId() {
return dcId;
}
@Override
public String getServerAddress(boolean ipv6) {
return ip;
}
@Override
public int getServerPort() {
return port;
}
@Override
public void setAuthKey(int dc, byte[] data) {
this.authKey = data;
}
@Override
public byte[] getAuthKey(int dc) {
return authKey;
}
}

View File

@ -0,0 +1,19 @@
package io.github.lonamiwebs.overgram.session;
public interface Session {
void load();
void save();
void setPrimaryDc(final int dc, final String ipv4, final String ipv6, final int port);
int getDcId();
String getServerAddress(final boolean ipv6);
int getServerPort();
void setAuthKey(final int dc, final byte[] data);
byte[] getAuthKey(final int dc);
}