-
Notifications
You must be signed in to change notification settings - Fork 1
/
LoginHandler.java
208 lines (189 loc) · 8.22 KB
/
LoginHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
* This file is part of CustomLauncherRewrite.
*
* CustomLauncherRewrite is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CustomLauncherRewrite is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CustomLauncherRewrite. If not, see <https://www.gnu.org/licenses/>.
*/
package lol.hyper.customlauncher.login;
import lol.hyper.customlauncher.CustomLauncherRewrite;
import lol.hyper.customlauncher.login.windows.TwoFactorAuth;
import lol.hyper.customlauncher.tools.ExceptionWindow;
import lol.hyper.customlauncher.tools.PopUpWindow;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.entity.UrlEncodedFormEntity;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.NameValuePair;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.message.BasicNameValuePair;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONObject;
import javax.swing.*;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.TimeUnit;
public class LoginHandler {
/**
* The LoginHandler logger.
*/
public static final Logger logger = LogManager.getLogger(LoginHandler.class);
/**
* TTR's API URL for logins.
*/
private static final String REQUEST_URL = "https://www.toontownrewritten.com/api/login?format=json";
/**
* Track how many attempts for login.
*/
private int attempts = 0;
/**
* The current login request details.
*/
private final Map<String, String> loginRequest;
/**
* Starts the login process.
*
* @param loginRequest The login request to process. This is simple a key/value Map.
*/
public LoginHandler(Map<String, String> loginRequest) {
this.loginRequest = loginRequest;
}
/**
* Handle a login request. This will act based on whatever was received in the request.
*/
public void login() {
Map<String, String> receivedRequest;
try {
logger.info("Sending login request...");
// send the login request to TTR
receivedRequest = sendHttpRequest(loginRequest);
attempts += 1;
} catch (Exception exception) {
logger.error("Unable to send login request to TTR!", exception);
new ExceptionWindow(exception);
return;
}
// if the login failed, don't continue
// sendRequest() will display & log errors for us
if (receivedRequest.isEmpty()) {
return;
}
logger.info("Attempt: " + attempts);
logger.info("Received login response:");
// get the login status
String status = receivedRequest.get("success");
String banner = receivedRequest.get("banner");
String eta = receivedRequest.get("eta");
// log the request details
logger.info(receivedRequest);
// act based on the login status
// TTR has different statuses for login responses
switch (status) {
case "false" -> // false is invalid login details / maintenance
new PopUpWindow(null, banner);
case "partial" -> // partial is used for 2FA or ToonGuard
SwingUtilities.invokeLater(() -> {
TwoFactorAuth twoFactorAuth = new TwoFactorAuth(banner, receivedRequest.get("responseToken"));
twoFactorAuth.setVisible(true);
});
case "true" -> // login was successful
{
logger.info("Login was successful, launching game...");
String gameServer = receivedRequest.get("gameserver");
String cookie = receivedRequest.get("cookie");
String manifest = receivedRequest.get("manifest");
LaunchGame launchGame = new LaunchGame(cookie, gameServer, manifest);
launchGame.start();
}
case "delayed" -> // login request was put into a queue
{
// if the queue is over 5, tell the user
// the queue is almost always 0
// TTR saves your request to queueToken, so just send that back
// to get an updated response
if (Integer.parseInt(eta) >= 5) {
new PopUpWindow(null, "You were placed in a queue. Press OK to try again in 5 seconds.");
// send the login request again after 5 seconds
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException exception) {
logger.error(exception);
}
}
// send the request with the queueToken
loginRequest.clear();
loginRequest.put("queueToken", receivedRequest.get("queueToken"));
login();
}
default -> // TTR sent back a weird status that we don't know about
{
logger.error("Weird login response: " + status);
logger.info(receivedRequest);
new PopUpWindow(null, "TTR sent back a weird response, or we got an invalid response.\nCheck the log for more information.");
}
}
}
/**
* Send the login request to TTR.
*
* @param loginRequest The login request to process.
* @return The request that is sent back.
*/
private Map<String, String> sendHttpRequest(Map<String, String> loginRequest) {
HttpPost post = new HttpPost(REQUEST_URL);
post.setHeader("User-Agent", CustomLauncherRewrite.userAgent);
post.setHeader("Content-type", "application/x-www-form-urlencoded");
List<NameValuePair> urlParameters = new ArrayList<>();
for (Map.Entry<String, String> entry : loginRequest.entrySet()) {
urlParameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
post.setEntity(new UrlEncodedFormEntity(urlParameters));
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response;
try {
response = httpClient.execute(post);
} catch (IOException exception) {
logger.error("Unable to send login request!", exception);
new ExceptionWindow(exception);
return Collections.emptyMap();
}
String responseData;
try {
responseData = EntityUtils.toString(response.getEntity());
} catch (IOException | ParseException exception) {
logger.error("Unable to send login request!", exception);
new ExceptionWindow(exception);
return Collections.emptyMap();
}
JSONObject responseJSON = new JSONObject(responseData);
HashMap<String, String> receivedDetails = new HashMap<>();
for (String x : responseJSON.keySet()) {
if (!responseJSON.isNull(x)) {
receivedDetails.put(x, responseJSON.getString(x));
}
else {
receivedDetails.put(x, null);
logger.warn("Value of '" + x + "' in login response was null.");
}
}
try {
httpClient.close();
response.close();
} catch (IOException exception) {
throw new RuntimeException(exception);
}
return receivedDetails;
}
}