AuthHookManager

This commit is contained in:
Gravit 2018-12-31 14:51:49 +07:00
parent 726bcff1b4
commit fe2177151e
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
3 changed files with 71 additions and 1 deletions

View file

@ -277,6 +277,8 @@ public static void main(String... args) throws Throwable {
public final SessionManager sessionManager; public final SessionManager sessionManager;
public final SocketHookManager socketHookManager; public final SocketHookManager socketHookManager;
public final AuthHookManager authHookManager;
// Server // Server
public final ModulesManager modulesManager; public final ModulesManager modulesManager;
@ -393,6 +395,7 @@ public LaunchServer(Path dir) throws IOException, InvalidKeySpecException {
reloadManager = new ReloadManager(); reloadManager = new ReloadManager();
reconfigurableManager = new ReconfigurableManager(); reconfigurableManager = new ReconfigurableManager();
socketHookManager = new SocketHookManager(); socketHookManager = new SocketHookManager();
authHookManager = new AuthHookManager();
GarbageManager.registerNeedGC(sessionManager); GarbageManager.registerNeedGC(sessionManager);
GarbageManager.registerNeedGC(limiter); GarbageManager.registerNeedGC(limiter);
@ -538,11 +541,13 @@ private void generateConfigIfNotExists() throws IOException {
newConfig.launch4j = new ExeConf(); newConfig.launch4j = new ExeConf();
newConfig.launch4j.copyright = "© GravitLauncher Team"; newConfig.launch4j.copyright = "© GravitLauncher Team";
newConfig.launch4j.fileDesc = "GravitLauncher ".concat(Launcher.getVersion().getVersionString()); newConfig.launch4j.fileDesc = "GravitLauncher ".concat(Launcher.getVersion().getVersionString());
newConfig.launch4j.fileVer = Launcher.getVersion().getVersionString(); newConfig.launch4j.fileVer = Launcher.getVersion().getVersionString().concat(".").concat(String.valueOf(Launcher.getVersion().patch));
newConfig.launch4j.internalName = "Launcher"; newConfig.launch4j.internalName = "Launcher";
newConfig.launch4j.trademarks = "This product is licensed under GPLv3"; newConfig.launch4j.trademarks = "This product is licensed under GPLv3";
newConfig.launch4j.txtFileVersion = "%s, build %d"; newConfig.launch4j.txtFileVersion = "%s, build %d";
newConfig.launch4j.txtProductVersion = "%s, build %d"; newConfig.launch4j.txtProductVersion = "%s, build %d";
newConfig.launch4j.productName = "GravitLauncher";
newConfig.launch4j.productVer = newConfig.launch4j.fileVer;
newConfig.buildPostTransform = new PostBuildTransformConf(); newConfig.buildPostTransform = new PostBuildTransformConf();
newConfig.env = LauncherConfig.LauncherEnvironment.STD; newConfig.env = LauncherConfig.LauncherEnvironment.STD;
newConfig.authHandler = new MemoryAuthHandler(); newConfig.authHandler = new MemoryAuthHandler();

View file

@ -0,0 +1,45 @@
package ru.gravit.launchserver.manangers;
import ru.gravit.launchserver.response.auth.AuthResponse;
import ru.gravit.launchserver.socket.Client;
import java.util.HashSet;
import java.util.Set;
public class AuthHookManager {
private Set<AuthPreHook> PRE_HOOKS = new HashSet<>();
private Set<AuthPostHook> POST_HOOKS = new HashSet<>();
@FunctionalInterface
public interface AuthPreHook
{
void preAuthHook(AuthResponse.AuthContext context, Client client);
}
public interface AuthPostHook
{
void postAuthHook(AuthResponse.AuthContext context, Client client);
}
public void registerPostHook(AuthPostHook hook)
{
if(POST_HOOKS == null) POST_HOOKS = new HashSet<>();
POST_HOOKS.add(hook);
}
public void registerPreHook(AuthPreHook hook)
{
if(PRE_HOOKS == null) PRE_HOOKS = new HashSet<>();
PRE_HOOKS.add(hook);
}
public void preHook(AuthResponse.AuthContext context, Client client)
{
for(AuthPreHook preHook : PRE_HOOKS)
{
preHook.preAuthHook(context,client);
}
}
public void postHook(AuthResponse.AuthContext context, Client client)
{
for(AuthPostHook postHook : POST_HOOKS)
{
postHook.postAuthHook(context, client);
}
}
}

View file

@ -35,7 +35,24 @@ private static String echo(int length) {
public AuthResponse(LaunchServer server, long session, HInput input, HOutput output, String ip) { public AuthResponse(LaunchServer server, long session, HInput input, HOutput output, String ip) {
super(server, session, input, output, ip); super(server, session, input, output, ip);
} }
public static class AuthContext
{
public AuthContext(long session, String login, int password_lenght, String client, String hwid, boolean isServerAuth) {
this.session = session;
this.login = login;
this.password_lenght = password_lenght;
this.client = client;
this.hwid = hwid;
this.isServerAuth = isServerAuth;
}
public long session;
public String login;
public int password_lenght; //Use AuthProvider for get password
public String client;
public String hwid;
public boolean isServerAuth;
}
@Override @Override
public void reply() throws Exception { public void reply() throws Exception {
String login = input.readString(SerializeLimits.MAX_LOGIN); String login = input.readString(SerializeLimits.MAX_LOGIN);
@ -63,7 +80,9 @@ public void reply() throws Exception {
AuthProvider provider = server.config.authProvider[auth_id]; AuthProvider provider = server.config.authProvider[auth_id];
Client clientData = server.sessionManager.getClient(session); Client clientData = server.sessionManager.getClient(session);
clientData.type = Client.Type.USER; clientData.type = Client.Type.USER;
AuthContext context = new AuthContext(session,login,password.length(),client,hwid_str,false);
try { try {
server.authHookManager.preHook(context,clientData);
if (server.limiter.isLimit(ip)) { if (server.limiter.isLimit(ip)) {
AuthProvider.authError(server.config.authRejectString); AuthProvider.authError(server.config.authRejectString);
return; return;
@ -91,6 +110,7 @@ public void reply() throws Exception {
} }
} }
server.config.hwidHandler.check(OshiHWID.gson.fromJson(hwid_str, OshiHWID.class), result.username); server.config.hwidHandler.check(OshiHWID.gson.fromJson(hwid_str, OshiHWID.class), result.username);
server.authHookManager.postHook(context,clientData);
} catch (AuthException | HWIDException e) { } catch (AuthException | HWIDException e) {
if(e.getMessage() == null) LogHelper.error(e); if(e.getMessage() == null) LogHelper.error(e);
requestError(e.getMessage()); requestError(e.getMessage());