see #7 for details
This commit is contained in:
yushijinhun 2018-02-13 11:33:23 +08:00
parent 1731f0b0cb
commit 8aa3f20a60
No known key found for this signature in database
GPG key ID: 5BC167F73EA558E4
2 changed files with 111 additions and 2 deletions

View file

@ -1,12 +1,23 @@
package org.to2mbn.authlibinjector.transform; package org.to2mbn.authlibinjector.transform;
import java.util.function.Function;
import java.util.regex.Pattern; import java.util.regex.Pattern;
public class YggdrasilApiTransformUnit extends RegexTransformUnit { public class YggdrasilApiTransformUnit extends RegexTransformUnit {
// TODO: If java.util.regex supports branch reset, we can use the following regex:
// ^https?:\/\/(?|((?<subdomain>(api|authserver|sessionserver))\.(?<domain>mojang\.com))|((?<subdomain>(skins))\.(?<domain>minecraft\.net)))(?<path>\/.*)$
// => ${apiRoot}${subdomain}${path}
//
// But now it's not supported, so we use a workaround here:
// ^https?:\/\/(?:(?:(api|authserver|sessionserver)\.(mojang\.com))|(?:(skins)\.(minecraft\.net)))(?<path>\/.*)$
// => ${apiRoot}$1$3${path}
public static final Pattern REGEX = Pattern.compile("^https?:\\/\\/(?:(?:(api|authserver|sessionserver)\\.(mojang\\.com))|(?:(skins)\\.(minecraft\\.net)))(?<path>\\/.*)$");
public static final Function<String, String> REPLACEMENT = apiRoot -> apiRoot + "$1$3${path}";
public YggdrasilApiTransformUnit(String apiRoot) { public YggdrasilApiTransformUnit(String apiRoot) {
// ^https:\/\/(api|authserver|sessionserver)\.mojang\.com\/(.*)$ super(REGEX, REPLACEMENT.apply(apiRoot));
super(Pattern.compile("^https:\\/\\/(api|authserver|sessionserver)\\.mojang\\.com\\/(.*)$"), apiRoot + "$1/$2");
} }
@Override @Override

View file

@ -0,0 +1,98 @@
package org.to2mbn.authlibinjector.test;
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import java.util.Collection;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
import org.to2mbn.authlibinjector.transform.YggdrasilApiTransformUnit;
@RunWith(Parameterized.class)
public class UrlReplaceTest {
private static final String apiRoot = "https://yggdrasil.example.com/";
@Parameters
public static Collection<Object[]> data() {
// @formatter:off
return Arrays.asList(new Object[][] {
{
// from: [com.mojang:authlib:1.5.24]/com.mojang.authlib.yggdrasil.YggdrasilGameProfileRepository
"https://api.mojang.com/profiles/",
"https://yggdrasil.example.com/api/profiles/"
},
{
// from: [com.mojang:authlib:1.5.24]/com.mojang.authlib.yggdrasil.YggdrasilMinecraftSessionService
"https://sessionserver.mojang.com/session/minecraft/join",
"https://yggdrasil.example.com/sessionserver/session/minecraft/join"
},
{
// from: [com.mojang:authlib:1.5.24]/com.mojang.authlib.yggdrasil.YggdrasilMinecraftSessionService
"https://sessionserver.mojang.com/session/minecraft/hasJoined",
"https://yggdrasil.example.com/sessionserver/session/minecraft/hasJoined"
},
{
// from: [com.mojang:authlib:1.5.24]/com.mojang.authlib.yggdrasil.YggdrasilUserAuthentication
"https://authserver.mojang.com/authenticate",
"https://yggdrasil.example.com/authserver/authenticate"
},
{
// from: [com.mojang:authlib:1.5.24]/com.mojang.authlib.yggdrasil.YggdrasilUserAuthentication
"https://authserver.mojang.com/refresh",
"https://yggdrasil.example.com/authserver/refresh"
},
{
// from: [com.mojang:authlib:1.5.24]/com.mojang.authlib.yggdrasil.YggdrasilUserAuthentication
"https://authserver.mojang.com/validate",
"https://yggdrasil.example.com/authserver/validate"
},
{
// from: [com.mojang:authlib:1.5.24]/com.mojang.authlib.yggdrasil.YggdrasilUserAuthentication
"https://authserver.mojang.com/invalidate",
"https://yggdrasil.example.com/authserver/invalidate"
},
{
// from: [com.mojang:authlib:1.5.24]/com.mojang.authlib.yggdrasil.YggdrasilUserAuthentication
"https://authserver.mojang.com/signout",
"https://yggdrasil.example.com/authserver/signout"
},
{
// from: [mcp940]/net.minecraft.client.entity.AbstractClientPlayer
// issue: to2mbn/authlib-injector#7 <https://github.com/to2mbn/authlib-injector/issues/7>
"http://skins.minecraft.net/MinecraftSkins/%s.png",
"https://yggdrasil.example.com/skins/MinecraftSkins/%s.png"
},
{
// from: [bungeecord@806a6dfacaadb7538860889f8a50612bb496a2d3]/net.md_5.bungee.connection.InitialHandler
// url: https://github.com/SpigotMC/BungeeCord/blob/806a6dfacaadb7538860889f8a50612bb496a2d3/proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java#L409
"https://sessionserver.mojang.com/session/minecraft/hasJoined?username=",
"https://yggdrasil.example.com/sessionserver/session/minecraft/hasJoined?username="
},
{
// from: [wiki.vg]/Mojang_API/Username -> UUID at time
// url: http://wiki.vg/Mojang_API#Username_-.3E_UUID_at_time
// issue: to2mbn/authlib-injector#6 <https://github.com/to2mbn/authlib-injector/issues/6>
"https://api.mojang.com/users/profiles/minecraft/",
"https://yggdrasil.example.com/api/users/profiles/minecraft/"
}
});
// @formatter:on
}
@Parameter(0)
public String input;
@Parameter(1)
public String output;
@Test
public void test() {
assertEquals(output,
YggdrasilApiTransformUnit.REGEX.matcher(input).replaceAll(
YggdrasilApiTransformUnit.REPLACEMENT.apply(apiRoot)));
}
}