Skip to content

Commit

Permalink
refer to GeyserMC#3851 pull request from main branch
Browse files Browse the repository at this point in the history
  • Loading branch information
Shanwer committed Dec 24, 2023
1 parent b0ccf45 commit a0d6048
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ public interface GeyserConfiguration {

boolean isAllowThirdPartyEars();

String getAuthBaseUri();

String getSessionBaseUri();

String getShowCooldown();

boolean isShowCoordinates();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ public abstract class GeyserJacksonConfiguration implements GeyserConfiguration
@JsonProperty("allow-third-party-capes")
private boolean allowThirdPartyCapes = true;

@JsonProperty("auth-base-uri")
private String authBaseUri = "";

@JsonProperty("session-base-uri")
private String sessionBaseUri = "";

@JsonProperty("show-cooldown")
private String showCooldown = "title";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public PacketSignal handle(ResourcePackClientResponsePacket packet) {
session.authenticate(session.getAuthData().name());
} else if (!couldLoginUserByName(session.getAuthData().name())) {
// We must spawn the white world
session.connect();
session.connect();//TODO:locate by here
}
geyser.getLogger().info(GeyserLocale.getLocaleStringLog("geyser.network.connect", session.getAuthData().name()));
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@
package org.geysermc.geyser.session;

import com.github.steveice10.mc.auth.data.GameProfile;
import com.github.steveice10.mc.auth.exception.request.InvalidCredentialsException;
import com.github.steveice10.mc.auth.exception.request.RequestException;
import com.github.steveice10.mc.auth.service.AuthenticationService;
import com.github.steveice10.mc.auth.service.MojangAuthenticationService;
import com.github.steveice10.mc.auth.service.MsaAuthenticationService;
import com.github.steveice10.mc.auth.service.SessionService;
import com.github.steveice10.mc.protocol.MinecraftConstants;
import com.github.steveice10.mc.protocol.MinecraftProtocol;
import com.github.steveice10.mc.protocol.data.ProtocolState;
Expand Down Expand Up @@ -719,7 +723,60 @@ public void authenticate(String username) {
t.printStackTrace();
}
}
public void authenticate(String username,String password){
//All login activity is hereby seen as mojang login to be compatible with authlib-injector.
if (loggedIn) {
geyser.getLogger().severe(GeyserLocale.getLocaleStringLog("geyser.auth.already_loggedin", username));
return;
}
loggingIn = true;
// Use a future to prevent timeouts as all the authentication is handled sync
CompletableFuture.supplyAsync(() -> {
try {
if (password != null && !password.isEmpty()) {
AuthenticationService authenticationService = new MojangAuthenticationService();

String authBaseUri = geyser.getConfig().getAuthBaseUri();
if (!authBaseUri.isEmpty()) {
if (!authBaseUri.endsWith("/")) authBaseUri += "/";
authenticationService.setBaseUri(authBaseUri);
}
authenticationService.setUsername(username);
authenticationService.setPassword(password);
authenticationService.login();
GameProfile profile = authenticationService.getSelectedProfile();
if (profile == null) {
// Java account is offline
disconnect(GeyserLocale.getPlayerLocaleString("geyser.network.remote.invalid_account", clientData.getLanguageCode()));
return null;
}
protocol = new MinecraftProtocol(profile, authenticationService.getAccessToken());
}
}catch (InvalidCredentialsException | IllegalArgumentException e) {
geyser.getLogger().info(GeyserLocale.getLocaleStringLog("geyser.auth.login.invalid", username));
disconnect(GeyserLocale.getPlayerLocaleString("geyser.auth.login.invalid.kick", getClientData().getLanguageCode()));
} catch (RequestException ex) {
disconnect(ex.getMessage());
}
return null;
}).whenComplete((aVoid, ex) -> {
if (ex != null) {
disconnect(ex.toString());
}
if (this.closed) {
if (ex != null) {
geyser.getLogger().error("", ex);
}
// Client disconnected during the authentication attempt
return;
}
try {
connectDownstream();
} catch (Throwable t) {
t.printStackTrace();
}
});
}
public void authenticateWithRefreshToken(String refreshToken) {
if (loggedIn) {
geyser.getLogger().severe(GeyserLocale.getLocaleStringLog("geyser.auth.already_loggedin", getAuthData().name()));
Expand All @@ -730,14 +787,20 @@ public void authenticateWithRefreshToken(String refreshToken) {

CompletableFuture.supplyAsync(() -> {
MsaAuthenticationService service = new MsaAuthenticationService(GeyserImpl.OAUTH_CLIENT_ID);

service.setRefreshToken(refreshToken);
try {
service.login();
} catch (RequestException e) {
geyser.getLogger().error("Error while attempting to use refresh token for " + bedrockUsername() + "!", e);
return Boolean.FALSE;
}

AuthenticationService authenticationService = new MojangAuthenticationService();
String authBaseUri = geyser.getConfig().getAuthBaseUri();
if (!authBaseUri.isEmpty()) {
if (!authBaseUri.endsWith("/")) authBaseUri += "/";
authenticationService.setBaseUri(authBaseUri);
}
GameProfile profile = service.getSelectedProfile();
if (profile == null) {
// Java account is offline
Expand Down Expand Up @@ -878,6 +941,16 @@ private void connectDownstream() {
downstream = new LocalSession(this.remoteServer.address(), this.remoteServer.port(),
geyser.getBootstrap().getSocketAddress(), upstream.getAddress().getAddress().getHostAddress(),
this.protocol, this.protocol.createHelper());

String sessionBaseUri = geyser.getConfig().getSessionBaseUri();
if (!sessionBaseUri.isEmpty()) {
if (!sessionBaseUri.endsWith("/")) sessionBaseUri += "/";
if (!sessionBaseUri.endsWith("session/minecraft/")) sessionBaseUri += "session/minecraft/";

SessionService sessionService = new SessionService();
sessionService.setBaseUri(sessionBaseUri);
downstream.setFlag(MinecraftConstants.SESSION_SERVICE_KEY, sessionService);
}
this.downstream = new DownstreamSession(downstream);
} else {
downstream = new TcpClientSession(this.remoteServer.address(), this.remoteServer.port(), this.protocol);
Expand Down
11 changes: 11 additions & 0 deletions core/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,17 @@ debug-mode: false
# OptiFine capes, LabyMod capes, 5Zig capes and MinecraftCapes
allow-third-party-capes: false

# Set a custom base URI for the authentication server.
# Should look similar to the following: https://authserver.example.com/
# You don't need this, unless you run your own auth server.
# Leave empty to use the official auth servers.
auth-base-uri: ""
# Set a custom base URI for the session server.
# Should look similar to the following: https://session.example.com/
# You don't need this, unless you run your own session server.
# Leave empty to use the official session servers.
session-base-uri: ""

# Allow third party deadmau5 ears to be visible. Currently allowing:
# MinecraftCapes
allow-third-party-ears: false
Expand Down

0 comments on commit a0d6048

Please sign in to comment.