Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MCL-19983 & WEB-1429 Fixed legacy skin loading & server authentication. #33

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ dependencies {
compile 'org.lwjgl.lwjgl:lwjgl:2.9.1'
compile 'org.apache.logging.log4j:log4j-core:2.0-beta9'
compile 'org.apache.logging.log4j:log4j-api:2.0-beta9'
compile 'com.mojang:authlib:2.3.31'
}

task sourcesJar(type: Jar) {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/net/minecraft/launchwrapper/Launch.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.net.URL;

import org.apache.logging.log4j.Level;

import net.minecraft.launchwrapper.protocol.LegacyProtocolURLStreamHandlerFactory;

public class Launch {
private static final String DEFAULT_TWEAK = "net.minecraft.launchwrapper.VanillaTweaker";
public static File minecraftHome;
Expand All @@ -31,6 +34,7 @@ public static void main(String[] args) {
public static LaunchClassLoader classLoader;

private Launch() {
URL.setURLStreamHandlerFactory(new LegacyProtocolURLStreamHandlerFactory());
final URLClassLoader ucl = (URLClassLoader) getClass().getClassLoader();
classLoader = new LaunchClassLoader(ucl.getURLs());
blackboard = new HashMap<String,Object>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package net.minecraft.launchwrapper.protocol;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
import java.net.Proxy;

import com.mojang.authlib.minecraft.MinecraftProfileTexture;
import com.mojang.authlib.ProfileLookupCallback;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.Agent;
import com.mojang.authlib.yggdrasil.YggdrasilAuthenticationService;

public class CapeURLConnection extends HttpURLConnection {
public CapeURLConnection(URL url) {
super(url);
}

@Override
public void disconnect() {
}

@Override
public boolean usingProxy() {
return false;
}

InputStream inputStream = null;
int responseCode = 200;

@Override
public void connect() throws IOException {
String url = this.url.toString();
String username = url.contains("/MinecraftCloaks/")
? url.substring(url.indexOf("/MinecraftCloaks/"))
.replace("/MinecraftCloaks/", "")
.replace(".png", "")
: url.substring(url.indexOf("/cloak/get.jsp?user="))
.replace("/cloak/get.jsp?user=", "");

try {
MinecraftProfileTexture cape = getUserCape(username);
inputStream = new URL(cape.getUrl()).openConnection().getInputStream();
} catch (Exception ex) {
responseCode = 404;
}
}

YggdrasilAuthenticationService authenticationService = new YggdrasilAuthenticationService(Proxy.NO_PROXY, (String)null);
GameProfile gameProfile = null;

private MinecraftProfileTexture getUserCape(String username) {
authenticationService.createProfileRepository().findProfilesByNames(new String[] { username }, Agent.MINECRAFT, new ProfileLookupCallback() {
public void onProfileLookupSucceeded(GameProfile paramGameProfile) {
gameProfile = paramGameProfile;
}
public void onProfileLookupFailed(GameProfile paramGameProfile, Exception paramException) {
}
});

if (gameProfile == null)
return null;

gameProfile = authenticationService.createMinecraftSessionService().fillProfileProperties(gameProfile, true);

Map<MinecraftProfileTexture.Type, MinecraftProfileTexture> textures = authenticationService.createMinecraftSessionService().getTextures(gameProfile, true);
if (textures.containsKey(MinecraftProfileTexture.Type.CAPE))
return textures.get(MinecraftProfileTexture.Type.CAPE);

return null;
}

@Override
public InputStream getInputStream() throws IOException {
return inputStream;
}

@Override
public int getResponseCode() {
return responseCode;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package net.minecraft.launchwrapper.protocol;

import sun.net.www.protocol.http.HttpURLConnection;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;

public class LegacyProtocolURLStreamHandler extends URLStreamHandler {
@Override
protected URLConnection openConnection(URL url) throws IOException {
// Skins are pulled from the new endpoint and converted to the legacy format as required.
if (url.toString().startsWith("http://s3.amazonaws.com/MinecraftSkins/") || url.toString().contains("/skin/"))
return new SkinURLConnection(url);
// Capes are pulled from the new endpoint, no conversion is required.
else if (url.toString().startsWith("http://s3.amazonaws.com//MinecraftCloaks//") || url.toString().contains("/cloak/get.jsp?user="))
return new CapeURLConnection(url);

return new HttpURLConnection(url, null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package net.minecraft.launchwrapper.protocol;

import java.net.URLStreamHandler;
import java.net.URLStreamHandlerFactory;

public class LegacyProtocolURLStreamHandlerFactory implements URLStreamHandlerFactory {
@Override
public URLStreamHandler createURLStreamHandler(String protocol) {
if ("http".equals(protocol)) {
return new LegacyProtocolURLStreamHandler();
}

return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
package net.minecraft.launchwrapper.protocol;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.imageio.ImageIO;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.awt.image.BufferedImage;
import java.awt.*;
import java.util.Map;
import java.net.Proxy;

import com.mojang.authlib.minecraft.MinecraftProfileTexture;
import com.mojang.authlib.ProfileLookupCallback;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.Agent;
import com.mojang.authlib.yggdrasil.YggdrasilAuthenticationService;

public class SkinURLConnection extends HttpURLConnection {
public SkinURLConnection(URL url) {
super(url);
}

@Override
public void disconnect() {
}

@Override
public boolean usingProxy() {
return false;
}

InputStream inputStream = null;
int responseCode = 200;

@Override
public void connect() throws IOException {
String url = this.url.toString();
String username = (url.contains("/MinecraftSkins/")
? url.substring(url.indexOf("/MinecraftSkins/"))
.replace("/MinecraftSkins/", "")
.replace(".png", "")
: url.substring(url.indexOf("/skin/")))
.replace("/skin/", "")
.replace(".png", "");

try {
MinecraftProfileTexture skin = getUserSkin(username);
boolean slim = "slim".equals(skin.getMetadata("model"));
inputStream = convertModernSkin(new URL(skin.getUrl()), slim);
} catch (Exception ex) {
responseCode = 404;
}
}

GameProfile gameProfile = null;
YggdrasilAuthenticationService authenticationService = new YggdrasilAuthenticationService(Proxy.NO_PROXY, (String)null);

private MinecraftProfileTexture getUserSkin(String username) {
authenticationService.createProfileRepository().findProfilesByNames(new String[] { username }, Agent.MINECRAFT, new ProfileLookupCallback() {
public void onProfileLookupSucceeded(GameProfile paramGameProfile) {
gameProfile = paramGameProfile;
}
public void onProfileLookupFailed(GameProfile paramGameProfile, Exception paramException) {
}
});

if (gameProfile == null)
return null;

gameProfile = authenticationService.createMinecraftSessionService().fillProfileProperties(gameProfile, true);

Map<MinecraftProfileTexture.Type, MinecraftProfileTexture> textures = authenticationService.createMinecraftSessionService().getTextures(gameProfile, true);

if (textures.containsKey(MinecraftProfileTexture.Type.SKIN))
return textures.get(MinecraftProfileTexture.Type.SKIN);

return null;
}

public static InputStream convertModernSkin(URL skinUrl, boolean slim) throws IOException {
craftycodie marked this conversation as resolved.
Show resolved Hide resolved
InputStream inputStream = skinUrl.openStream();
BufferedImage skin = ImageIO.read(inputStream);
boolean tall = skin.getHeight() > 32;
BufferedImage movePart = null;
Graphics2D graphics = skin.createGraphics();
AlphaComposite alpha = AlphaComposite.getInstance(AlphaComposite.SRC_OVER);
graphics.setComposite(alpha);

if (slim) {
// Convert alex to steve.

// Stretch right arm.
movePart = skin.getSubimage(45, 16, 9, 16);
graphics.drawImage(movePart, 46, 16, null);
movePart = skin.getSubimage(49, 16, 2, 4);
graphics.drawImage(movePart, 50, 16, null);
movePart = skin.getSubimage(53, 20, 2, 12);
graphics.drawImage(movePart, 54, 20, null);

if (tall) {
// Stretch right sleeve.
movePart = skin.getSubimage(45, 32, 9, 16);
graphics.drawImage(movePart, 46, 32, null);
movePart = skin.getSubimage(49, 32, 2, 4);
graphics.drawImage(movePart, 50, 32, null);
movePart = skin.getSubimage(53, 36, 2, 12);
graphics.drawImage(movePart, 54, 36, null);

// Stretch left arm.
movePart = skin.getSubimage(37, 48, 9, 16);
graphics.drawImage(movePart, 38, 48, null);
movePart = skin.getSubimage(41, 48, 2, 4);
graphics.drawImage(movePart, 42, 32, null);
movePart = skin.getSubimage(45, 52, 2, 12);
graphics.drawImage(movePart, 46, 36, null);

// Stretch left sleeve.
movePart = skin.getSubimage(53, 48, 9, 16);
graphics.drawImage(movePart, 54, 48, null);
movePart = skin.getSubimage(57, 48, 2, 4);
graphics.drawImage(movePart, 58, 32, null);
movePart = skin.getSubimage(61, 52, 2, 12);
graphics.drawImage(movePart, 62, 36, null);
}
}

if (tall) {
// Flatten second layers.
movePart = skin.getSubimage(0, 32, 56, 16);
graphics.drawImage(movePart, 0, 16, null);
}

graphics.dispose();

// Crop
BufferedImage croppedSkin = skin.getSubimage(0, 0, 64, 32);
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(croppedSkin, "png", os);
byte[] bytes = os.toByteArray();
return new ByteArrayInputStream(bytes);
}

@Override
public InputStream getInputStream() throws IOException {
return inputStream;
}

@Override
public int getResponseCode() {
return responseCode;
}
}