From 0558360bed48a9fa3f8ff04962afa4142f0315e5 Mon Sep 17 00:00:00 2001 From: NetRiceCake Date: Sat, 29 Nov 2025 20:52:35 +0900 Subject: [PATCH] refactoring --- .gitignore | 2 +- .../java/com/github/netricecake/Main.java | 34 ++++- .../github/netricecake/kakao/KakaoApi.java | 19 ++- .../kakao/LocoSocketHandlerImpl.java | 5 +- .../github/netricecake/kakao/TalkClient.java | 127 +++++------------- .../exception/BadCredentialsException.java | 7 + .../exception/BookingFailedException.java | 3 + .../exception/InvalidDeviceNameException.java | 3 + .../exception/InvalidDeviceUUIDException.java | 3 + .../kakao/exception/LoginFailedException.java | 3 + .../UnregisteredDeviceException.java | 3 + .../netricecake/loco/LocoSocektHandler.java | 10 +- .../loco/packet/InboundPacket.java | 28 ++++ .../loco/packet/inbound/ChatInfoIn.java | 3 +- .../loco/packet/inbound/CheckInIn.java | 30 ++--- .../loco/packet/inbound/DelMemIn.java | 3 +- .../loco/packet/inbound/GetConfIn.java | 9 +- .../loco/packet/inbound/InfoLinkIn.java | 3 +- .../loco/packet/inbound/LoginListIn.java | 35 ++--- .../loco/packet/inbound/MessageIn.java | 3 +- .../loco/packet/inbound/NewMemIn.java | 3 +- .../loco/packet/inbound/PingIn.java | 4 +- .../loco/packet/inbound/WriteIn.java | 3 +- .../loco/packet/outbound/CheckInOut.java | 4 - 24 files changed, 190 insertions(+), 157 deletions(-) create mode 100644 src/main/java/com/github/netricecake/kakao/exception/BadCredentialsException.java create mode 100644 src/main/java/com/github/netricecake/kakao/exception/BookingFailedException.java create mode 100644 src/main/java/com/github/netricecake/kakao/exception/InvalidDeviceNameException.java create mode 100644 src/main/java/com/github/netricecake/kakao/exception/InvalidDeviceUUIDException.java create mode 100644 src/main/java/com/github/netricecake/kakao/exception/LoginFailedException.java create mode 100644 src/main/java/com/github/netricecake/kakao/exception/UnregisteredDeviceException.java create mode 100644 src/main/java/com/github/netricecake/loco/packet/InboundPacket.java diff --git a/.gitignore b/.gitignore index 935d2bb..ff52ddb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,11 +1,11 @@ target/ !.mvn/wrapper/maven-wrapper.jar -.mvn !**/src/main/**/target/ !**/src/test/**/target/ .kotlin ### IntelliJ IDEA ### +.idea .idea/modules.xml .idea/jarRepositories.xml .idea/compiler.xml diff --git a/src/main/java/com/github/netricecake/Main.java b/src/main/java/com/github/netricecake/Main.java index c7c3757..0c9a503 100644 --- a/src/main/java/com/github/netricecake/Main.java +++ b/src/main/java/com/github/netricecake/Main.java @@ -1,21 +1,23 @@ package com.github.netricecake; +import com.github.netricecake.kakao.KakaoApi; import com.github.netricecake.kakao.TalkClient; import com.github.netricecake.kakao.TalkHandler; +import com.github.netricecake.kakao.exception.*; import com.github.netricecake.kakao.structs.ChatRoom; import com.github.netricecake.kakao.structs.Member; import com.github.netricecake.kakao.structs.Message; import com.google.gson.JsonArray; import com.google.gson.JsonObject; -//TIP 코드를 실행하려면 을(를) 누르거나 -// 에디터 여백에 있는 아이콘을 클릭하세요. +import java.util.Map; + public class Main { - static String email = "invalid@example.com"; + static String email = "invalid@example.com"; // 이메일 말고 전화번호도 가능 static String password = "example"; static String deviceName = "SM-X930"; // 갤럭시 탭 s11 울트라, 지원되는 태블릿 모델명 넣으세요 - static String deviceUuid = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"; // 64자 랜덤 hex-string, 이것도 에시니까 무조건 다른걸로 바꾸세요. + static String deviceUuid = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"; // 64자 랜덤 hex-string public static void main(String[] args) throws Exception { TalkClient client = new TalkClient(email, password, deviceName, deviceUuid, new TalkHandler() { @@ -60,6 +62,28 @@ public class Main { getTalkClient().sendMessage(room, member.getName() + "님이 나갔습니다."); } }); - client.connect(); + + try { + client.connect(); + } catch (InvalidDeviceNameException e) { + System.out.println("서브 디바이스 로그인을 지원하지 않는 디바이스입니다."); + } catch (InvalidDeviceUUIDException e) { + System.out.println("Device UUID는 64자리 hex string이어야 합니다."); + } catch (BadCredentialsException e) { + System.out.println("이메일(전화번호)이나 비밀번호가 틀렸습니다."); + } catch (BookingFailedException e) { + System.out.println("Booking 서버와의 통신을 실패했습니다."); + } catch (LoginFailedException e) { + System.out.println("카카오톡 서버와 연결을 실패했습니다. 로그인 정보 파일을 삭제 후 다시 시도해보세요."); + } catch (UnregisteredDeviceException e) { + System.out.println("디바이스 등록이 필요합니다."); + Map.Entry registerInfo = KakaoApi.generatePasscode(email, password, deviceName, deviceUuid); + System.out.println("카카오톡 앱에서 " + registerInfo.getValue() + "초 안에 코드를 입력해주세요. 코드 : " + registerInfo.getKey()); + boolean registerResult = KakaoApi.registerDevice(email, password, deviceUuid); + if (!registerResult) { + System.out.println("디바이스 등록 실패"); + } + System.out.println("디바이스 등록 성공, 다시 실행하세요."); + } } } diff --git a/src/main/java/com/github/netricecake/kakao/KakaoApi.java b/src/main/java/com/github/netricecake/kakao/KakaoApi.java index bf036f7..cf93728 100644 --- a/src/main/java/com/github/netricecake/kakao/KakaoApi.java +++ b/src/main/java/com/github/netricecake/kakao/KakaoApi.java @@ -1,5 +1,9 @@ package com.github.netricecake.kakao; +import com.github.netricecake.kakao.exception.BadCredentialsException; +import com.github.netricecake.kakao.exception.InvalidDeviceNameException; +import com.github.netricecake.kakao.exception.InvalidDeviceUUIDException; +import com.github.netricecake.kakao.exception.UnregisteredDeviceException; import com.github.netricecake.loco.packet.inbound.GetConfIn; import com.github.netricecake.loco.packet.outbound.GetConfOut; import com.github.netricecake.loco.util.ByteUtil; @@ -48,9 +52,9 @@ public class KakaoApi { private final static Gson gson = new Gson(); - public static LoginData loginRequest(String email, String password, String deviceName, String deviceUuid) throws IOException, InvalidParameterException, IllegalStateException { - if (deviceUuid == null || deviceUuid.length() != UUID_LENGTH) throw new InvalidParameterException("invalid deviceUuid"); - if (!checkAllowedDevice(deviceName)) throw new InvalidParameterException("This device does not support sub device login"); + public static LoginData loginRequest(String email, String password, String deviceName, String deviceUuid) throws IOException, InvalidDeviceNameException, InvalidDeviceUUIDException, BadCredentialsException, UnregisteredDeviceException { + if (deviceUuid == null || deviceUuid.length() != UUID_LENGTH) throw new InvalidDeviceUUIDException(); + if (!checkAllowedDevice(deviceName)) throw new InvalidDeviceNameException(); RequestBody body = new FormBody.Builder().add("password", password) .add("device_name", deviceName) .add("foced", "false") @@ -68,10 +72,9 @@ public class KakaoApi { int status = jsonObject.get("status").getAsInt(); // 12 비번 틀림 30 이메일 틀림 - if (status == 12 || status == 30) throw new InvalidParameterException("Email or password is invalid"); - if (status == -100) throw new IllegalStateException("Register device before login"); + if (status == 12 || status == 30) throw new BadCredentialsException(); + if (status == -100) throw new UnregisteredDeviceException(); if (status == 0) { - LoginData data = new LoginData(); data.userId = jsonObject.get("userId").getAsLong(); data.countryIso = jsonObject.get("countryIso").getAsString(); @@ -218,7 +221,7 @@ public class KakaoApi { public String mainDeviceAppVersion; public String recipe; - public void fromJson(String json) { + public LoginData(String json) { JsonObject jsonObject = JsonParser.parseString(json).getAsJsonObject(); userId = jsonObject.get("userId").getAsLong(); countryIso = jsonObject.get("countryIso").getAsString(); @@ -234,6 +237,8 @@ public class KakaoApi { recipe = jsonObject.get("recipe").getAsString(); } + public LoginData() {} + public String toJson() { JsonObject jsonObject = new JsonObject(); jsonObject.addProperty("userId", userId); diff --git a/src/main/java/com/github/netricecake/kakao/LocoSocketHandlerImpl.java b/src/main/java/com/github/netricecake/kakao/LocoSocketHandlerImpl.java index 724758b..7d6038e 100644 --- a/src/main/java/com/github/netricecake/kakao/LocoSocketHandlerImpl.java +++ b/src/main/java/com/github/netricecake/kakao/LocoSocketHandlerImpl.java @@ -11,7 +11,7 @@ import com.github.netricecake.loco.packet.outbound.InfoLinkOut; import com.github.netricecake.loco.packet.outbound.MessageOut; import lombok.Getter; -public class LocoSocketHandlerImpl implements LocoSocektHandler { +public class LocoSocketHandlerImpl extends LocoSocektHandler { @Getter private TalkClient client; @@ -79,7 +79,8 @@ public class LocoSocketHandlerImpl implements LocoSocektHandler { @Override public void onDisconnect() { - + client.connected = false; + System.out.println("연결 끊어짐"); } @Override diff --git a/src/main/java/com/github/netricecake/kakao/TalkClient.java b/src/main/java/com/github/netricecake/kakao/TalkClient.java index d78f530..cc31a38 100644 --- a/src/main/java/com/github/netricecake/kakao/TalkClient.java +++ b/src/main/java/com/github/netricecake/kakao/TalkClient.java @@ -1,5 +1,6 @@ package com.github.netricecake.kakao; +import com.github.netricecake.kakao.exception.*; import com.github.netricecake.kakao.structs.ChatRoom; import com.github.netricecake.loco.LocoPacket; import com.github.netricecake.loco.LocoSocektHandler; @@ -18,6 +19,7 @@ import com.google.gson.JsonObject; import com.google.gson.JsonParser; import lombok.Getter; +import java.awt.print.Book; import java.io.File; import java.io.IOException; import java.nio.file.Files; @@ -39,7 +41,7 @@ public class TalkClient { private Map chatRooms = new HashMap<>(); @Getter - private boolean connected; + protected boolean connected; private KakaoApi.LoginData loginData; private GetConfIn bookingData; @@ -54,7 +56,7 @@ public class TalkClient { @Getter private LocoSocket socket; - public TalkClient(String email, String password, String deviceName, String deviceUuid, TalkHandler talkHandler) { + public TalkClient(String email, String password, String deviceName, String deviceUuid, TalkHandler talkHandler) throws IOException { this.email = email; this.password = password; this.deviceName = deviceName; @@ -63,77 +65,48 @@ public class TalkClient { this.talkHandler = talkHandler; talkHandler.setTalkClient(this); - new File(sessionDir).mkdir(); - loginData = readLoginData(); + new File(sessionDir).mkdirs(); + File loginDataFile = new File(sessionDir + "loginData.json"); + if (!loginDataFile.exists()) return; + String loginDataJson = Files.readString(Paths.get(loginDataFile.getAbsolutePath())); + loginData = new KakaoApi.LoginData(loginDataJson); } - public void connect() throws Exception { - if (this.connected) throw new Exception("이미 연결되어 있습니다."); - if (loginData == null) { - System.out.println("로그인 데이터가 없습니다. 새로 로그인을 시도합니다."); - try { - loginData = KakaoApi.loginRequest(email, password, deviceName, deviceUuid); - } catch (IllegalStateException e) { - Map.Entry registerInfo = KakaoApi.generatePasscode(email, password, deviceName, deviceUuid); - System.out.println("기기 등록이 필요합니다."); - System.out.println("카카오톡 앱에서 " + registerInfo.getValue() + "초 안에 코드를 입력해주세요. 코드 : " + registerInfo.getKey()); - boolean registerResult = KakaoApi.registerDevice(email, password, deviceUuid); - if (!registerResult) throw new IllegalStateException("기기등록 실패"); - System.out.println("기기 등록 성공"); - loginData = KakaoApi.loginRequest(email, password, deviceName, deviceUuid); - } - System.out.println("로그인이 완료되었습니다."); - writeLoginData(); + public void connect() throws IOException, InvalidDeviceNameException, InvalidDeviceUUIDException, BadCredentialsException, UnregisteredDeviceException, BookingFailedException, LoginFailedException { + if (this.connected) throw new IOException("Already connected."); + if (loginData == null) { // 저장된 로그인 데이터가 없는 경우 로그인 시도 + loginData = KakaoApi.loginRequest(email, password, deviceName, deviceUuid); + File loginDataFile = new File(sessionDir + "loginData.json"); + if (!loginDataFile.exists()) loginDataFile.createNewFile(); + Files.write(Paths.get(loginDataFile.getAbsolutePath()), loginData.toJson().getBytes()); } bookingData = KakaoApi.getBookingData(loginData.userId); + if (bookingData == null || bookingData.getStatus() != 0) throw new BookingFailedException(); LocoSocket checkInSocket = new LocoSocket(bookingData.getAddr(), bookingData.getPort(), new LocoSocektHandler() { - @Override - public void onPacket(LocoPacket packet) { - - } - - @Override - public void onConnect() { - - } - - @Override - public void onDisconnect() { - - } - @Override public void onError(Exception e) { e.printStackTrace(); } }, Executors.newFixedThreadPool(1)); - CheckInOut checkInRequest = new CheckInOut(loginData.userId); - byte[] body = checkInRequest.toBson(); + byte[] body = new CheckInOut(loginData.userId).toBson(); checkInSocket.connect(); - LocoPacket checkinResponse = checkInSocket.writeAndRead(new LocoPacket(1000, checkInRequest.getMethod(), body)); - checkInData = new CheckInIn(); - checkInData.fromBson(checkinResponse.getBody()); + LocoPacket checkinResponse = checkInSocket.writeAndRead(new LocoPacket(1000, "CHECKIN", body)); + checkInData = new CheckInIn(checkinResponse.getBody()); checkInSocket.close(); - - long lastTokenId = 0; long lbk = 0; byte[] rp = ByteUtil.hexStringToByteArray("0000ffff0000"); - try { - File loginListDataFile = new File(sessionDir + "loginListData.json"); - if (loginListDataFile.exists()) { - String loginDataJson = Files.readString(Paths.get(loginListDataFile.getAbsolutePath())); - JsonObject loginListData = JsonParser.parseString(loginDataJson).getAsJsonObject(); - lastTokenId = loginListData.getAsJsonPrimitive("lastTokenId").getAsLong(); - lbk = loginListData.getAsJsonPrimitive("lbk").getAsLong(); - rp = ByteUtil.hexStringToByteArray("0100ffff0100"); // 이게 도당체 뭐임 - } - } catch (IOException e) { - e.printStackTrace(); + File loginListDataFile = new File(sessionDir + "loginListData.json"); + if (loginListDataFile.exists()) { + String loginDataJson = Files.readString(Paths.get(loginListDataFile.getAbsolutePath())); + JsonObject loginListData = JsonParser.parseString(loginDataJson).getAsJsonObject(); + lastTokenId = loginListData.getAsJsonPrimitive("lastTokenId").getAsLong(); + lbk = loginListData.getAsJsonPrimitive("lbk").getAsLong(); + rp = ByteUtil.hexStringToByteArray("0100ffff0100"); // 이게 도대체 뭐임 } locoHandlerPool = Executors.newFixedThreadPool(1); @@ -149,21 +122,16 @@ public class TalkClient { loginListData = new LoginListIn(); loginListData.fromBson(socket.writeAndRead(new LocoPacket("LOGINLIST", req.toBson())).getBody()); if (loginListData.getStatus() != 0) { - System.out.println("카카오톡 서버와 연결을 실패했습니다. 로그인 정보 파일을 삭제 후 다시 시도해보세요."); - throw new Exception("카카오톡 서버와 연결을 실패했습니다. 로그인 정보 파일을 삭제 후 다시 시도해보세요."); + throw new LoginFailedException(); } - System.out.println("연결 성공"); - try { - File loginListDataFile = new File(sessionDir + "loginListData.json"); - if (!loginListDataFile.exists()) loginListDataFile.createNewFile(); - JsonObject jsonObject = new JsonObject(); - jsonObject.addProperty("lastTokenId", loginListData.getLastTokenId()); - jsonObject.addProperty("lbk", loginListData.getLbk()); - Files.write(Paths.get(loginListDataFile.getAbsolutePath()), new Gson().toJson(jsonObject).getBytes()); - } catch (IOException e) { - e.printStackTrace(); - } + if (!loginListDataFile.exists()) loginListDataFile.createNewFile(); + JsonObject jsonObject = new JsonObject(); + jsonObject.addProperty("lastTokenId", loginListData.getLastTokenId()); + jsonObject.addProperty("lbk", loginListData.getLbk()); + Files.write(Paths.get(loginListDataFile.getAbsolutePath()), new Gson().toJson(jsonObject).getBytes()); + + connected = true; new Thread(() -> { try { @@ -179,31 +147,6 @@ public class TalkClient { }); } - private KakaoApi.LoginData readLoginData() { - try { - File loginDataFile = new File(sessionDir + "loginData.json"); - if (!loginDataFile.exists()) return null; - String loginDataJson = Files.readString(Paths.get(loginDataFile.getAbsolutePath())); - KakaoApi.LoginData loginData = new KakaoApi.LoginData(); - loginData.fromJson(loginDataJson); - return loginData; - } catch (IOException e) { - e.printStackTrace(); - } - - return null; - } - - private void writeLoginData() { - try { - File loginDataFile = new File(sessionDir + "loginData.json"); - if (!loginDataFile.exists()) loginDataFile.createNewFile(); - Files.write(Paths.get(loginDataFile.getAbsolutePath()), loginData.toJson().getBytes()); - } catch (IOException e) { - e.printStackTrace(); - } - } - public boolean sendMessage(ChatRoom room, int type, String message, String extra) { WriteOut wo = new WriteOut(); wo.setChatId(room.getChatId()); diff --git a/src/main/java/com/github/netricecake/kakao/exception/BadCredentialsException.java b/src/main/java/com/github/netricecake/kakao/exception/BadCredentialsException.java new file mode 100644 index 0000000..90379e5 --- /dev/null +++ b/src/main/java/com/github/netricecake/kakao/exception/BadCredentialsException.java @@ -0,0 +1,7 @@ +package com.github.netricecake.kakao.exception; + +public class BadCredentialsException extends Exception { + /* + email, password mismatch + */ +} diff --git a/src/main/java/com/github/netricecake/kakao/exception/BookingFailedException.java b/src/main/java/com/github/netricecake/kakao/exception/BookingFailedException.java new file mode 100644 index 0000000..a78e4c4 --- /dev/null +++ b/src/main/java/com/github/netricecake/kakao/exception/BookingFailedException.java @@ -0,0 +1,3 @@ +package com.github.netricecake.kakao.exception; + +public class BookingFailedException extends Exception {} diff --git a/src/main/java/com/github/netricecake/kakao/exception/InvalidDeviceNameException.java b/src/main/java/com/github/netricecake/kakao/exception/InvalidDeviceNameException.java new file mode 100644 index 0000000..2c6ce40 --- /dev/null +++ b/src/main/java/com/github/netricecake/kakao/exception/InvalidDeviceNameException.java @@ -0,0 +1,3 @@ +package com.github.netricecake.kakao.exception; + +public class InvalidDeviceNameException extends Exception {} diff --git a/src/main/java/com/github/netricecake/kakao/exception/InvalidDeviceUUIDException.java b/src/main/java/com/github/netricecake/kakao/exception/InvalidDeviceUUIDException.java new file mode 100644 index 0000000..e8bbb31 --- /dev/null +++ b/src/main/java/com/github/netricecake/kakao/exception/InvalidDeviceUUIDException.java @@ -0,0 +1,3 @@ +package com.github.netricecake.kakao.exception; + +public class InvalidDeviceUUIDException extends Exception {} diff --git a/src/main/java/com/github/netricecake/kakao/exception/LoginFailedException.java b/src/main/java/com/github/netricecake/kakao/exception/LoginFailedException.java new file mode 100644 index 0000000..7789ac9 --- /dev/null +++ b/src/main/java/com/github/netricecake/kakao/exception/LoginFailedException.java @@ -0,0 +1,3 @@ +package com.github.netricecake.kakao.exception; + +public class LoginFailedException extends Exception {} diff --git a/src/main/java/com/github/netricecake/kakao/exception/UnregisteredDeviceException.java b/src/main/java/com/github/netricecake/kakao/exception/UnregisteredDeviceException.java new file mode 100644 index 0000000..b5a946b --- /dev/null +++ b/src/main/java/com/github/netricecake/kakao/exception/UnregisteredDeviceException.java @@ -0,0 +1,3 @@ +package com.github.netricecake.kakao.exception; + +public class UnregisteredDeviceException extends Exception {} diff --git a/src/main/java/com/github/netricecake/loco/LocoSocektHandler.java b/src/main/java/com/github/netricecake/loco/LocoSocektHandler.java index 3d8b94d..2321286 100644 --- a/src/main/java/com/github/netricecake/loco/LocoSocektHandler.java +++ b/src/main/java/com/github/netricecake/loco/LocoSocektHandler.java @@ -1,13 +1,13 @@ package com.github.netricecake.loco; -public interface LocoSocektHandler { +public class LocoSocektHandler { - void onPacket(LocoPacket packet); + public void onPacket(LocoPacket packet) {} - void onConnect(); + public void onConnect() {} - void onDisconnect(); + public void onDisconnect() {} - void onError(Exception e); + public void onError(Exception e) {} } diff --git a/src/main/java/com/github/netricecake/loco/packet/InboundPacket.java b/src/main/java/com/github/netricecake/loco/packet/InboundPacket.java new file mode 100644 index 0000000..86e09d1 --- /dev/null +++ b/src/main/java/com/github/netricecake/loco/packet/InboundPacket.java @@ -0,0 +1,28 @@ +package com.github.netricecake.loco.packet; + +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; + +public class InboundPacket { + + public String getStringOrNull(JsonObject jsonObject, String key) { + JsonElement jsonElement = jsonObject.get(key); + return jsonElement == null ? null : jsonElement.getAsString(); + } + + public int getIntOrNull(JsonObject jsonObject, String key) { + JsonElement jsonElement = jsonObject.get(key); + return jsonElement == null ? 0 : jsonElement.getAsInt(); + } + + public boolean getBoolOrNull(JsonObject jsonObject, String key) { + JsonElement jsonElement = jsonObject.get(key); + return jsonElement == null || jsonElement.getAsBoolean(); + } + + public long getLongOrNull(JsonObject jsonObject, String key) { + JsonElement jsonElement = jsonObject.get(key); + return jsonElement == null ? 0L : jsonElement.getAsLong(); + } + +} diff --git a/src/main/java/com/github/netricecake/loco/packet/inbound/ChatInfoIn.java b/src/main/java/com/github/netricecake/loco/packet/inbound/ChatInfoIn.java index 62ba02c..963b5f9 100644 --- a/src/main/java/com/github/netricecake/loco/packet/inbound/ChatInfoIn.java +++ b/src/main/java/com/github/netricecake/loco/packet/inbound/ChatInfoIn.java @@ -1,11 +1,12 @@ package com.github.netricecake.loco.packet.inbound; +import com.github.netricecake.loco.packet.InboundPacket; import com.github.netricecake.loco.util.BsonUtil; import com.google.gson.JsonObject; import lombok.Getter; @Getter -public class ChatInfoIn { +public class ChatInfoIn extends InboundPacket { private String type; diff --git a/src/main/java/com/github/netricecake/loco/packet/inbound/CheckInIn.java b/src/main/java/com/github/netricecake/loco/packet/inbound/CheckInIn.java index 450dc37..a96fabd 100644 --- a/src/main/java/com/github/netricecake/loco/packet/inbound/CheckInIn.java +++ b/src/main/java/com/github/netricecake/loco/packet/inbound/CheckInIn.java @@ -1,12 +1,13 @@ package com.github.netricecake.loco.packet.inbound; +import com.github.netricecake.loco.packet.InboundPacket; import com.github.netricecake.loco.util.BsonUtil; import com.google.gson.JsonObject; import lombok.Getter; import lombok.Setter; @Getter -public class CheckInIn { +public class CheckInIn extends InboundPacket { private int status; @@ -32,21 +33,20 @@ public class CheckInIn { private String MCCMNC; - public void fromBson(byte[] bson) - { + public CheckInIn(byte[] bson) { JsonObject json = BsonUtil.bsonToJsonObject(bson); - status = json.get("status").getAsInt(); - host = json.get("host").getAsString(); - host6 = json.get("host6").getAsString(); - port = json.get("port").getAsInt(); - cshost = json.get("cshost").getAsString(); - cshost6 = json.get("cshost6").getAsString(); - csport = json.get("csport").getAsInt(); - vsshost = json.get("vsshost").getAsString(); - vsshost6 = json.get("vsshost6").getAsString(); - vssport = json.get("vssport").getAsInt(); - cacheExpire = json.get("cacheExpire").getAsLong(); - MCCMNC = json.get("MCCMNC").getAsString(); + status = getIntOrNull(json, "status"); + host = getStringOrNull(json, "host"); + host6 = getStringOrNull(json, "host6"); + port = getIntOrNull(json, "port"); + cshost = getStringOrNull(json, "cshost"); + cshost6 = getStringOrNull(json, "cshost6"); + csport = getIntOrNull(json, "csport"); + vsshost = getStringOrNull(json, "vsshost"); + vsshost6 = getStringOrNull(json, "vsshost6"); + vssport = getIntOrNull(json, "vssport"); + cacheExpire = getLongOrNull(json, "cacheExpire"); + MCCMNC = getStringOrNull(json, "MCCMNC"); } } diff --git a/src/main/java/com/github/netricecake/loco/packet/inbound/DelMemIn.java b/src/main/java/com/github/netricecake/loco/packet/inbound/DelMemIn.java index b4f095d..63f8d26 100644 --- a/src/main/java/com/github/netricecake/loco/packet/inbound/DelMemIn.java +++ b/src/main/java/com/github/netricecake/loco/packet/inbound/DelMemIn.java @@ -1,12 +1,13 @@ package com.github.netricecake.loco.packet.inbound; +import com.github.netricecake.loco.packet.InboundPacket; import com.github.netricecake.loco.util.BsonUtil; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import lombok.Getter; @Getter -public class DelMemIn { +public class DelMemIn extends InboundPacket { private long chatId; diff --git a/src/main/java/com/github/netricecake/loco/packet/inbound/GetConfIn.java b/src/main/java/com/github/netricecake/loco/packet/inbound/GetConfIn.java index ee92d6d..368fffc 100644 --- a/src/main/java/com/github/netricecake/loco/packet/inbound/GetConfIn.java +++ b/src/main/java/com/github/netricecake/loco/packet/inbound/GetConfIn.java @@ -1,12 +1,13 @@ package com.github.netricecake.loco.packet.inbound; +import com.github.netricecake.loco.packet.InboundPacket; import com.github.netricecake.loco.util.BsonUtil; import com.google.gson.JsonObject; import lombok.Getter; import lombok.Setter; @Getter -public class GetConfIn { +public class GetConfIn extends InboundPacket { private int status; @@ -17,8 +18,10 @@ public class GetConfIn { public void fromBson(byte[] bson) { JsonObject jsonObject = BsonUtil.bsonToJsonObject(bson); status = jsonObject.get("status").getAsInt(); - addr = jsonObject.get("ticket").getAsJsonObject().get("lsl").getAsJsonArray().get(0).getAsString(); - port = jsonObject.get("wifi").getAsJsonObject().get("ports").getAsJsonArray().get(0).getAsInt(); + try { + addr = jsonObject.get("ticket").getAsJsonObject().get("lsl").getAsJsonArray().get(0).getAsString(); + port = jsonObject.get("wifi").getAsJsonObject().get("ports").getAsJsonArray().get(0).getAsInt(); + } catch (Exception e) {} } } diff --git a/src/main/java/com/github/netricecake/loco/packet/inbound/InfoLinkIn.java b/src/main/java/com/github/netricecake/loco/packet/inbound/InfoLinkIn.java index a01a2a3..994d69f 100644 --- a/src/main/java/com/github/netricecake/loco/packet/inbound/InfoLinkIn.java +++ b/src/main/java/com/github/netricecake/loco/packet/inbound/InfoLinkIn.java @@ -1,11 +1,12 @@ package com.github.netricecake.loco.packet.inbound; +import com.github.netricecake.loco.packet.InboundPacket; import com.github.netricecake.loco.util.BsonUtil; import com.google.gson.JsonObject; import lombok.Getter; @Getter -public class InfoLinkIn { +public class InfoLinkIn extends InboundPacket { private String name; diff --git a/src/main/java/com/github/netricecake/loco/packet/inbound/LoginListIn.java b/src/main/java/com/github/netricecake/loco/packet/inbound/LoginListIn.java index 0909402..7a3b387 100644 --- a/src/main/java/com/github/netricecake/loco/packet/inbound/LoginListIn.java +++ b/src/main/java/com/github/netricecake/loco/packet/inbound/LoginListIn.java @@ -1,5 +1,6 @@ package com.github.netricecake.loco.packet.inbound; +import com.github.netricecake.loco.packet.InboundPacket; import com.github.netricecake.loco.util.BsonUtil; import com.google.gson.JsonArray; import com.google.gson.JsonObject; @@ -8,7 +9,7 @@ import lombok.Getter; import java.util.Base64; @Getter -public class LoginListIn { +public class LoginListIn extends InboundPacket { private int status; @@ -45,21 +46,23 @@ public class LoginListIn { public void fromBson(byte[] bson) { JsonObject json = BsonUtil.bsonToJsonObject(bson); status = json.get("status").getAsInt(); - userId = json.get("userId").getAsLong(); - revision = json.get("revision").getAsInt(); - revisionInfo = json.get("revisionInfo").getAsString(); - rp = Base64.getDecoder().decode(json.get("rp").getAsJsonObject().get("$binary").getAsJsonObject().get("base64").getAsString()); - minLogId = json.get("minLogId").getAsLong(); - sb = json.get("sb").getAsInt(); - chatDatas = json.get("chatDatas").getAsJsonArray(); - delChatIds = json.get("delChatIds").getAsJsonArray(); - kc = json.get("kc").getAsJsonArray(); - mcmRevision = json.get("mcmRevision").getAsInt(); - lastTokenId = json.get("lastTokenId").getAsLong(); - lastChatId = json.get("lastChatId").getAsLong(); - ltk = json.get("ltk").getAsLong(); - lbk = json.get("lbk").getAsLong(); - eof = json.get("eof").getAsBoolean(); + try { + userId = json.get("userId").getAsLong(); + revision = json.get("revision").getAsInt(); + revisionInfo = json.get("revisionInfo").getAsString(); + rp = Base64.getDecoder().decode(json.get("rp").getAsJsonObject().get("$binary").getAsJsonObject().get("base64").getAsString()); + minLogId = json.get("minLogId").getAsLong(); + sb = json.get("sb").getAsInt(); + chatDatas = json.get("chatDatas").getAsJsonArray(); + delChatIds = json.get("delChatIds").getAsJsonArray(); + kc = json.get("kc").getAsJsonArray(); + mcmRevision = json.get("mcmRevision").getAsInt(); + lastTokenId = json.get("lastTokenId").getAsLong(); + lastChatId = json.get("lastChatId").getAsLong(); + ltk = json.get("ltk").getAsLong(); + lbk = json.get("lbk").getAsLong(); + eof = json.get("eof").getAsBoolean(); + } catch(Exception e) {} } } diff --git a/src/main/java/com/github/netricecake/loco/packet/inbound/MessageIn.java b/src/main/java/com/github/netricecake/loco/packet/inbound/MessageIn.java index c62a3c4..ed9cc24 100644 --- a/src/main/java/com/github/netricecake/loco/packet/inbound/MessageIn.java +++ b/src/main/java/com/github/netricecake/loco/packet/inbound/MessageIn.java @@ -1,11 +1,12 @@ package com.github.netricecake.loco.packet.inbound; +import com.github.netricecake.loco.packet.InboundPacket; import com.github.netricecake.loco.util.BsonUtil; import com.google.gson.JsonObject; import lombok.Getter; @Getter -public class MessageIn { +public class MessageIn extends InboundPacket { private long chatId; diff --git a/src/main/java/com/github/netricecake/loco/packet/inbound/NewMemIn.java b/src/main/java/com/github/netricecake/loco/packet/inbound/NewMemIn.java index 15f765c..dfe2ebe 100644 --- a/src/main/java/com/github/netricecake/loco/packet/inbound/NewMemIn.java +++ b/src/main/java/com/github/netricecake/loco/packet/inbound/NewMemIn.java @@ -1,12 +1,13 @@ package com.github.netricecake.loco.packet.inbound; +import com.github.netricecake.loco.packet.InboundPacket; import com.github.netricecake.loco.util.BsonUtil; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import lombok.Getter; @Getter -public class NewMemIn { +public class NewMemIn extends InboundPacket { private long chatId; diff --git a/src/main/java/com/github/netricecake/loco/packet/inbound/PingIn.java b/src/main/java/com/github/netricecake/loco/packet/inbound/PingIn.java index b0af053..9eda48d 100644 --- a/src/main/java/com/github/netricecake/loco/packet/inbound/PingIn.java +++ b/src/main/java/com/github/netricecake/loco/packet/inbound/PingIn.java @@ -1,4 +1,6 @@ package com.github.netricecake.loco.packet.inbound; -public class PingIn { +import com.github.netricecake.loco.packet.InboundPacket; + +public class PingIn extends InboundPacket { } diff --git a/src/main/java/com/github/netricecake/loco/packet/inbound/WriteIn.java b/src/main/java/com/github/netricecake/loco/packet/inbound/WriteIn.java index 5f9b42b..c76730c 100644 --- a/src/main/java/com/github/netricecake/loco/packet/inbound/WriteIn.java +++ b/src/main/java/com/github/netricecake/loco/packet/inbound/WriteIn.java @@ -1,11 +1,12 @@ package com.github.netricecake.loco.packet.inbound; +import com.github.netricecake.loco.packet.InboundPacket; import com.github.netricecake.loco.util.BsonUtil; import com.google.gson.JsonObject; import lombok.Getter; @Getter -public class WriteIn { +public class WriteIn extends InboundPacket { private int status; diff --git a/src/main/java/com/github/netricecake/loco/packet/outbound/CheckInOut.java b/src/main/java/com/github/netricecake/loco/packet/outbound/CheckInOut.java index 2dd7e37..e7cb8e8 100644 --- a/src/main/java/com/github/netricecake/loco/packet/outbound/CheckInOut.java +++ b/src/main/java/com/github/netricecake/loco/packet/outbound/CheckInOut.java @@ -26,10 +26,6 @@ public class CheckInOut { this.userId = userId; } - public String getMethod() { - return "CHECKIN"; - } - public byte[] toBson() { JsonObject checkInObject = new JsonObject(); checkInObject.addProperty("userId", userId);