Last active
February 23, 2023 02:09
-
-
Save ScarletLovell/6836875baeffe9592592f035557f8d04 to your computer and use it in GitHub Desktop.
Adds both new & old authentication methods for servers through one function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private String tryHandshake(String urlStr, String userOrSession) throws IOException { | |
URL url = new URL( | |
urlStr + /*joinserver.jsp*/ "?" + | |
"user=" + this.mc.session.username + | |
"&sessionId=" + this.mc.session.sessionId + | |
"&serverId=" + userOrSession | |
); | |
// Create URL with timeout | |
HttpURLConnection huc = (HttpURLConnection) url.openConnection(); | |
HttpURLConnection.setFollowRedirects(false); | |
huc.setConnectTimeout(10 * 1000); | |
huc.setReadTimeout(10 * 1000); | |
huc.connect(); | |
// Get input stream & read result | |
InputStream input = huc.getInputStream(); | |
InputStreamReader reader = new InputStreamReader(input); | |
BufferedReader br = new BufferedReader(reader); | |
String line = br.readLine(); | |
// Disconnect everything | |
huc.disconnect(); | |
br.close(); | |
reader.close(); | |
// return result from input | |
return line; | |
} | |
private Boolean handshakeOK(String handshakeResult) { | |
return handshakeResult.equalsIgnoreCase("ok"); | |
} | |
public void handleHandshake(Packet2Handshake handshakePacket) { | |
String userOrSession = handshakePacket.username; | |
if (userOrSession.equals("-")) { | |
this.addToSendQueue(new Packet1Login(this.mc.session.username, 14)); | |
} else { | |
String handshake = "Exception Unknown"; | |
String[] URLs = new String[]{ | |
"http://session.minecraft.net/game/joinserver.jsp", | |
"http://www.minecraft.net/game/joinserver.jsp" | |
}; | |
for(String url : URLs) { | |
if (!handshakeOK(handshake)) { | |
try { | |
handshake = tryHandshake(url, userOrSession); | |
} catch (IOException e) { | |
handshake = e.getMessage(); | |
} | |
} | |
} | |
if(!handshakeOK(handshake)) | |
this.netManager.networkShutdown("disconnect.loginFailedInfo", handshake); | |
else | |
this.addToSendQueue(new Packet1Login(this.mc.session.username, 14)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment