Skip to content

Commit 001cf32

Browse files
WuzzyLVTuxCoding
authored andcommitted
Update CraftAPI to fix 403 Forbidden errors from Mojang API
Fixes #1306 Fixes #1305
1 parent c28c634 commit 001cf32

File tree

3 files changed

+27
-50
lines changed

3 files changed

+27
-50
lines changed

core/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@
203203
<dependency>
204204
<groupId>com.github.games647</groupId>
205205
<artifactId>craftapi</artifactId>
206-
<version>0.8.1</version>
206+
<version>1.0-SNAPSHOT</version>
207207
</dependency>
208208

209209
<!-- Database driver included in Spigot -->

core/src/main/java/com/github/games647/fastlogin/core/ProxyAgnosticMojangResolver.java

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@
2727

2828
import com.github.games647.craftapi.model.auth.Verification;
2929
import com.github.games647.craftapi.resolver.MojangResolver;
30+
import com.github.games647.craftapi.resolver.Options;
3031

3132
import java.io.IOException;
32-
import java.io.InputStream;
33-
import java.net.HttpURLConnection;
3433
import java.net.InetAddress;
3534
import java.util.Optional;
3635

@@ -44,38 +43,12 @@
4443
*/
4544
public class ProxyAgnosticMojangResolver extends MojangResolver {
4645

47-
private static final String HOST = "sessionserver.mojang.com";
48-
49-
/**
50-
* A formatting string containing a URL used to call the {@code hasJoined} method on mojang session servers.
51-
* <p>
52-
* Formatting parameters:
53-
* 1. The username of the player in question
54-
* 2. The serverId of this server
55-
*/
56-
public static final String ENDPOINT = "https://" + HOST + "/session/minecraft/hasJoined?username=%s&serverId=%s";
57-
46+
public ProxyAgnosticMojangResolver(Options options) {
47+
super(options);
48+
}
5849
@Override
5950
public Optional<Verification> hasJoined(String username, String serverHash, InetAddress hostIp)
6051
throws IOException {
61-
String url = String.format(ENDPOINT, username, serverHash);
62-
63-
HttpURLConnection conn = this.getConnection(url);
64-
int responseCode = conn.getResponseCode();
65-
66-
Verification verification = null;
67-
68-
// Mojang session servers send HTTP 204 (NO CONTENT) when the authentication seems invalid
69-
// If that's not our case, the authentication is valid, and so we can parse the response.
70-
if (responseCode != HttpURLConnection.HTTP_NO_CONTENT) {
71-
verification = this.parseRequest(conn, this::parseVerification);
72-
}
73-
74-
return Optional.ofNullable(verification);
75-
}
76-
77-
// Functional implementation of InputStreamAction, used in hasJoined method in parseRequest call
78-
protected Verification parseVerification(InputStream input) throws IOException {
79-
return this.readJson(input, Verification.class);
52+
return super.hasJoined(username, serverHash, null);
8053
}
8154
}

core/src/main/java/com/github/games647/fastlogin/core/shared/FastLoginCore.java

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
package com.github.games647.fastlogin.core.shared;
2727

2828
import com.github.games647.craftapi.resolver.MojangResolver;
29+
import com.github.games647.craftapi.resolver.Options;
2930
import com.github.games647.craftapi.resolver.http.RotatingProxySelector;
3031
import com.github.games647.fastlogin.core.CommonUtil;
3132
import com.github.games647.fastlogin.core.ProxyAgnosticMojangResolver;
@@ -48,11 +49,9 @@
4849
import java.io.IOException;
4950
import java.io.InputStream;
5051
import java.io.Reader;
51-
import java.net.InetAddress;
5252
import java.net.InetSocketAddress;
5353
import java.net.Proxy;
5454
import java.net.Proxy.Type;
55-
import java.net.UnknownHostException;
5655
import java.nio.file.Files;
5756
import java.nio.file.Path;
5857
import java.time.Duration;
@@ -121,30 +120,35 @@ public void load() {
121120
return;
122121
}
123122

124-
// Initialize the resolver based on the config parameter
125-
this.resolver = this.config.getBoolean("useProxyAgnosticResolver", false)
126-
? new ProxyAgnosticMojangResolver() : new MojangResolver();
123+
Options resolverOptions = new Options();
124+
resolverOptions.setMaxNameRequests(config.getInt("mojang-request-limit", 600));
127125

128-
antiBot = createAntiBotService(config.getSection("anti-bot"));
129126
Set<Proxy> proxies = config.getStringList("proxies")
130127
.stream()
131128
.map(proxy -> proxy.split(":"))
132129
.map(proxy -> new InetSocketAddress(proxy[0], Integer.parseInt(proxy[1])))
133130
.map(sa -> new Proxy(Type.HTTP, sa))
134131
.collect(toSet());
135-
136-
Collection<InetAddress> addresses = new HashSet<>();
137-
for (String localAddress : config.getStringList("ip-addresses")) {
138-
try {
139-
addresses.add(InetAddress.getByName(localAddress.replace('-', '.')));
140-
} catch (UnknownHostException ex) {
141-
plugin.getLog().error("IP-Address is unknown to us", ex);
142-
}
132+
if (!proxies.isEmpty()) {
133+
resolverOptions.setProxySelector(new RotatingProxySelector(proxies));
143134
}
144135

145-
resolver.setMaxNameRequests(config.getInt("mojang-request-limit"));
146-
resolver.setProxySelector(new RotatingProxySelector(proxies));
147-
resolver.setOutgoingAddresses(addresses);
136+
// TODO: Not available currently in craftapi?
137+
// Collection<InetAddress> addresses = new HashSet<>();
138+
// for (String localAddress : config.getStringList("ip-addresses")) {
139+
// try {
140+
// addresses.add(InetAddress.getByName(localAddress.replace('-', '.')));
141+
// } catch (UnknownHostException ex) {
142+
// plugin.getLog().error("IP-Address is unknown to us", ex);
143+
// }
144+
// }
145+
// resolver.setOutgoingAddresses(addresses);
146+
147+
// Initialize the resolver based on the config parameter
148+
this.resolver = this.config.getBoolean("useProxyAgnosticResolver", false)
149+
? new ProxyAgnosticMojangResolver(resolverOptions) : new MojangResolver(resolverOptions);
150+
151+
antiBot = createAntiBotService(config.getSection("anti-bot"));
148152
}
149153

150154
private AntiBotService createAntiBotService(Configuration botSection) {

0 commit comments

Comments
 (0)