This commit is contained in:
Michael Wain 2024-06-03 20:02:36 +03:00
parent ff362593d8
commit 0dc0f0b9ce
24 changed files with 60 additions and 108 deletions

View File

@ -10,9 +10,7 @@ import javax.net.ssl.SSLSocket;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
@Slf4j
public class TcpConnection extends Thread {
@ -24,11 +22,11 @@ public class TcpConnection extends Thread {
private int token;
@Getter
private Map<Integer, Processor> processors;
private final Map<Integer, Processor> processors;
public TcpConnection(SSLSocket socket, Set<Class<? extends Processor>> processorClasses) {
public TcpConnection(SSLSocket socket, Map<Integer, Processor> processors) {
this.fromClient = socket;
this.init(processorClasses);
this.processors = processors;
this.start();
}
@ -47,18 +45,6 @@ public class TcpConnection extends Thread {
this.outToClient.flush();
}
private void init(Set<Class<? extends Processor>> classes) {
this.processors = new HashMap<>();
classes.forEach(c -> {
try {
var ci = c.getDeclaredConstructor().newInstance();
this.processors.put(ci.getProcessorId(), ci);
} catch (Exception e) {
log.error(e.getMessage());
}
});
}
private void processPacket( BattleNetPacket packet ) {
try {
Processor is = this.processors.get(Integer.parseUnsignedInt(Integer.toUnsignedString(packet.getHeader().getServiceId())));

View File

@ -2,9 +2,9 @@ package com.alterdekim.hearthhack.component;
import com.alterdekim.hearthhack.component.processor.Processor;
import com.alterdekim.hearthhack.reflect.ReflectionLoader;
import com.alterdekim.hearthhack.util.Util;
import lombok.extern.slf4j.Slf4j;
import org.reflections.Reflections;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@ -17,19 +17,17 @@ import java.util.Set;
@Slf4j
@Component
public class TcpServer {
public class TcpServer extends ReflectionLoader<Processor> {
private final int socketPort = 1119;
private SSLServerSocket serverSocket;
private List<TcpConnection> connections;
private Set<Class<? extends Processor>> processorClasses;
@Scheduled(fixedDelay = 5000)
private void start() {
try {
processorClasses = new Reflections(this.getClass().getPackageName()).getSubTypesOf(Processor.class);
this.initReflect(Processor.class);
if( serverSocket != null && !serverSocket.isClosed() ) {
serverSocket.close();
serverSocket = null;
@ -44,7 +42,7 @@ public class TcpServer {
while(true) {
SSLSocket s = (SSLSocket) serverSocket.accept();
TcpConnection c = new TcpConnection(s, processorClasses);
TcpConnection c = new TcpConnection(s, this.getParsers());
connections.add(c);
log.info("New Connection Established From {}", s.getInetAddress().toString());
}

View File

@ -4,25 +4,23 @@ package com.alterdekim.hearthhack.component.processor;
import com.alterdekim.Protocol;
import com.alterdekim.hearthhack.component.TcpConnection;
import com.alterdekim.hearthhack.component.processor.client.request.ClientRequestParser;
import com.alterdekim.hearthhack.reflect.ReflectionLoader;
import com.alterdekim.hearthhack.util.*;
import com.google.protobuf.InvalidProtocolBufferException;
import lombok.extern.slf4j.Slf4j;
import org.reflections.Reflections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
@Slf4j
public class GameUtilitiesProcessor extends Processor {
private Map<Integer, ClientRequestParser> parsers;
private ReflectionLoader<ClientRequestParser> parsers;
public GameUtilitiesProcessor() {
this.setProcessorId(9);
this.init();
this.parsers = new ReflectionLoader<>();
this.parsers.initReflect(ClientRequestParser.class);
}
private ClientRequestBody parseClientRequest(Protocol.ClientRequest request) {
@ -86,19 +84,6 @@ public class GameUtilitiesProcessor extends Processor {
});
}
private void init() {
this.parsers = new HashMap<>();
Set<Class<? extends ClientRequestParser>> classes = new Reflections(ClientRequestParser.class.getPackageName()).getSubTypesOf(ClientRequestParser.class);
classes.forEach(c -> {
try {
var ci = c.getDeclaredConstructor().newInstance();
this.parsers.put(ci.getId(), ci);
} catch (Exception e) {
log.error(e.getMessage());
}
});
}
private void processClientRequest(BattleNetPacket packet, TcpConnection conn) throws Exception {
Protocol.ClientRequest cr = Protocol.ClientRequest.parseFrom(packet.getBody());
@ -163,11 +148,11 @@ public class GameUtilitiesProcessor extends Processor {
conn.send(new BattleNetPacket(header, new byte[0]));
}
if( !this.parsers.containsKey(type) ) {
if( !this.parsers.getParsers().containsKey(type) ) {
log.error("Unknown ClientRequest type #{}", type);
return;
}
this.parsers.get(type).parse(packet, p, conn);
this.parsers.getParsers().get(type).parse(packet, p, conn);
}
@Override

View File

@ -1,14 +1,16 @@
package com.alterdekim.hearthhack.component.processor.client.request;
import com.alterdekim.hearthhack.component.TcpConnection;
import com.alterdekim.hearthhack.reflect.AbstractParser;
import com.alterdekim.hearthhack.util.BattleNetPacket;
import com.alterdekim.hearthhack.util.ClientRequestBody;
import lombok.NoArgsConstructor;
@NoArgsConstructor
public abstract class ClientRequestParser {
public abstract class ClientRequestParser implements AbstractParser {
public abstract void parse(BattleNetPacket packet, ClientRequestBody body, TcpConnection conn) throws Exception;
@Override
public abstract int getId();
}

View File

@ -28,7 +28,7 @@ public class AccountLicensesInfo extends GenericParser {
}
@Override
public GetAccountInfoRequest getId() {
return ACCOUNT_LICENSES;
public int getId() {
return ACCOUNT_LICENSES.getValue();
}
}

View File

@ -43,7 +43,7 @@ public class AvailableFeatures extends GenericParser {
}
@Override
public GetAccountInfoRequest getId() {
return FEATURES;
public int getId() {
return FEATURES.getValue();
}
}

View File

@ -28,7 +28,7 @@ public class Boosters extends GenericParser {
}
@Override
public GetAccountInfoRequest getId() {
return BOOSTERS;
public int getId() {
return BOOSTERS.getValue();
}
}

View File

@ -30,7 +30,7 @@ public class CardBacks extends GenericParser {
}
@Override
public GetAccountInfoRequest getId() {
return CARD_BACKS;
public int getId() {
return CARD_BACKS.getValue();
}
}

View File

@ -43,7 +43,7 @@ public class CardValues extends GenericParser {
}
@Override
public GetAccountInfoRequest getId() {
return CARD_VALUES;
public int getId() {
return CARD_VALUES.getValue();
}
}

View File

@ -50,7 +50,7 @@ public class ClientOptions extends GenericParser {
}
@Override
public GetAccountInfoRequest getId() {
return CLIENT_OPTIONS;
public int getId() {
return CLIENT_OPTIONS.getValue();
}
}

View File

@ -28,7 +28,7 @@ public class Collection extends GenericParser {
}
@Override
public GetAccountInfoRequest getId() {
return COLLECTION;
public int getId() {
return COLLECTION.getValue();
}
}

View File

@ -79,7 +79,7 @@ public class DeckList extends GenericParser {
}
@Override
public GetAccountInfoRequest getId() {
return DECK_LIST;
public int getId() {
return DECK_LIST.getValue();
}
}

View File

@ -31,7 +31,7 @@ public class DustBalance extends GenericParser {
}
@Override
public GetAccountInfoRequest getId() {
return ARCANE_DUST_BALANCE;
public int getId() {
return ARCANE_DUST_BALANCE.getValue();
}
}

View File

@ -74,7 +74,7 @@ public class FavoriteHeroes extends GenericParser {
}
@Override
public GetAccountInfoRequest getId() {
return FAVORITE_HEROES;
public int getId() {
return FAVORITE_HEROES.getValue();
}
}

View File

@ -2,44 +2,24 @@ package com.alterdekim.hearthhack.component.processor.client.request.generic;
import com.alterdekim.Protocol;
import com.alterdekim.hearthhack.component.TcpConnection;
import com.alterdekim.hearthhack.reflect.ReflectionLoader;
import com.alterdekim.hearthhack.util.GetAccountInfoRequest;
import lombok.extern.slf4j.Slf4j;
import org.reflections.Reflections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
@Slf4j
public class GeneralGenericParser {
private Map<GetAccountInfoRequest, GenericParser> parsers;
public class GeneralGenericParser extends ReflectionLoader<GenericParser> {
public GeneralGenericParser() {
init();
}
private void init() {
this.parsers = new HashMap<>();
Set<Class<? extends GenericParser>> classes = new Reflections(this.getClass().getPackageName()).getSubTypesOf(GenericParser.class);
classes.forEach(c -> {
try {
var ci = c.getDeclaredConstructor().newInstance();
this.parsers.put(ci.getId(), ci);
} catch (Exception e) {
log.error(e.getMessage());
}
});
this.initReflect(GenericParser.class);
}
public void parseGenericRequest(int token, Protocol.UtilGenericRequest request, TcpConnection conn) {
GetAccountInfoRequest req = GetAccountInfoRequest.parseFromInt(request.getRequestSubId());
if( !this.parsers.containsKey(req) ) {
if( !this.getParsers().containsKey(req.getValue()) ) {
log.warn("Unknown generic request: {}", req);
return;
}
try {
this.parsers.get(req).parseGenericRequest(token, conn);
this.getParsers().get(req.getValue()).parseGenericRequest(token, conn);
} catch (Exception e) {
log.error(e.getMessage());
}

View File

@ -33,7 +33,7 @@ public class GoldBalance extends GenericParser{
}
@Override
public GetAccountInfoRequest getId() {
return GOLD_BALANCE;
public int getId() {
return GOLD_BALANCE.getValue();
}
}

View File

@ -147,7 +147,7 @@ public class HeroXP extends GenericParser {
}
@Override
public GetAccountInfoRequest getId() {
return HERO_XP;
public int getId() {
return HERO_XP.getValue();
}
}

View File

@ -49,7 +49,7 @@ public class MedalInfo extends GenericParser {
}
@Override
public GetAccountInfoRequest getId() {
return MEDAL_INFO;
public int getId() {
return MEDAL_INFO.getValue();
}
}

View File

@ -130,7 +130,7 @@ public class NotSoMassiveLoginReply extends GenericParser {
}
@Override
public GetAccountInfoRequest getId() {
return NOT_SO_MASSIVE_LOGIN;
public int getId() {
return NOT_SO_MASSIVE_LOGIN.getValue();
}
}

View File

@ -53,7 +53,7 @@ public class PlayerRecords extends GenericParser {
}
@Override
public GetAccountInfoRequest getId() {
return PLAYER_RECORD;
public int getId() {
return PLAYER_RECORD.getValue();
}
}

View File

@ -27,7 +27,7 @@ public class ProfileNotices extends GenericParser {
}
@Override
public GetAccountInfoRequest getId() {
return NOTICES;
public int getId() {
return NOTICES.getValue();
}
}

View File

@ -31,7 +31,7 @@ public class ProfileProgress extends GenericParser {
}
@Override
public GetAccountInfoRequest getId() {
return CAMPAIGN_INFO;
public int getId() {
return CAMPAIGN_INFO.getValue();
}
}

View File

@ -51,7 +51,7 @@ public class RewardProgress extends GenericParser {
}
@Override
public GetAccountInfoRequest getId() {
return REWARD_PROGRESS;
public int getId() {
return REWARD_PROGRESS.getValue();
}
}

View File

@ -1,5 +1,6 @@
package com.alterdekim.hearthhack.reflect;
import com.alterdekim.hearthhack.component.processor.Processor;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.reflections.Reflections;
@ -8,14 +9,14 @@ import java.util.HashMap;
import java.util.Map;
import java.util.Set;
@Getter
@Slf4j
public class ReflectionLoader<V extends AbstractParser> {
@Getter
private Map<Integer, V> parsers;
public void initReflect(Class<V> vClass) {
this.parsers = new HashMap<>();
Set<Class<? extends V>> processorClasses = new Reflections(this.getClass().getPackageName()).getSubTypesOf(vClass);
Set<Class<? extends V>> processorClasses = new Reflections(vClass.getPackageName()).getSubTypesOf(vClass);
processorClasses.forEach(c -> {
try {
var ci = c.getDeclaredConstructor().newInstance();