mirror of
https://github.com/2006-Scape/Parabot.git
synced 2026-07-02 16:49:10 +00:00
Merge pull request #305 from Parabot/feature/hooks-authentication
[FEATURE] Added hooks authentication
This commit is contained in:
@@ -1,24 +1,29 @@
|
||||
package org.parabot.core.asm.hooks;
|
||||
|
||||
import org.parabot.core.forum.AccountManager;
|
||||
import org.parabot.core.parsers.hooks.HookParser;
|
||||
import org.parabot.core.parsers.hooks.JSONHookParser;
|
||||
import org.parabot.core.parsers.hooks.XMLHookParser;
|
||||
import org.parabot.environment.api.utils.WebUtil;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
|
||||
public class HookFile {
|
||||
public static final int TYPE_XML = 0;
|
||||
public static final int TYPE_XML = 0;
|
||||
public static final int TYPE_JSON = 1;
|
||||
|
||||
private URL url;
|
||||
private int type;
|
||||
|
||||
private boolean isLocal;
|
||||
|
||||
public HookFile(File file, int type) throws MalformedURLException {
|
||||
this(file.toURI().toURL(), type);
|
||||
this.isLocal = true;
|
||||
}
|
||||
|
||||
public HookFile(URL url, int type) {
|
||||
@@ -26,17 +31,22 @@ public class HookFile {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
private void setType(int type) {
|
||||
if (type < 0 || type > 1) {
|
||||
throw new IllegalArgumentException("This type does not exist");
|
||||
}
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public InputStream getInputStream() {
|
||||
return WebUtil.getInputStream(url);
|
||||
}
|
||||
|
||||
public InputStream getInputStream(AccountManager manager) {
|
||||
if (isLocal) {
|
||||
return this.getInputStream();
|
||||
} else {
|
||||
try {
|
||||
return WebUtil.getConnection(url, "apikey=" + manager.getAccount().getApi()).getInputStream();
|
||||
} catch (IOException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public HookParser getParser() {
|
||||
switch (type) {
|
||||
case TYPE_XML:
|
||||
@@ -47,4 +57,11 @@ public class HookFile {
|
||||
return null;
|
||||
}
|
||||
|
||||
private void setType(int type) {
|
||||
if (type < 0 || type > 1) {
|
||||
throw new IllegalArgumentException("This type does not exist");
|
||||
}
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import org.json.simple.JSONObject;
|
||||
import org.parabot.core.Configuration;
|
||||
import org.parabot.core.Context;
|
||||
import org.parabot.core.Core;
|
||||
import org.parabot.core.parsers.hooks.HookParser;
|
||||
import org.parabot.core.parsers.scripts.BDNScripts;
|
||||
import org.parabot.core.parsers.servers.PublicServers;
|
||||
import org.parabot.core.ui.components.VerboseLoader;
|
||||
@@ -48,6 +49,7 @@ public final class AccountManager {
|
||||
accessors.add(PublicServers.MANAGER_FETCHER);
|
||||
accessors.add(PublicServerExecuter.MANAGER_FETCHER);
|
||||
accessors.add(PBPreferences.MANAGER_FETCHER);
|
||||
accessors.add(HookParser.MANAGER_FETCHER);
|
||||
|
||||
for (final AccountManagerAccess accessor : accessors) {
|
||||
accessor.setManager(instance);
|
||||
|
||||
@@ -3,6 +3,9 @@ package org.parabot.core.parsers.hooks;
|
||||
import org.parabot.core.asm.hooks.HookFile;
|
||||
import org.parabot.core.asm.interfaces.Injectable;
|
||||
import org.parabot.core.asm.wrappers.*;
|
||||
import org.parabot.core.forum.AccountManager;
|
||||
import org.parabot.core.forum.AccountManagerAccess;
|
||||
import org.parabot.environment.api.utils.PBPreferences;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
@@ -17,6 +20,14 @@ import java.util.Map;
|
||||
*/
|
||||
public abstract class HookParser {
|
||||
|
||||
protected static AccountManager manager;
|
||||
public static final AccountManagerAccess MANAGER_FETCHER = new AccountManagerAccess() {
|
||||
@Override
|
||||
public final void setManager(AccountManager manager) {
|
||||
HookParser.manager = manager;
|
||||
}
|
||||
};
|
||||
|
||||
public HookParser(HookFile hookFile) {
|
||||
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ public class XMLHookParser extends HookParser {
|
||||
DocumentBuilderFactory dbFactory = DocumentBuilderFactory
|
||||
.newInstance();
|
||||
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
|
||||
doc = dBuilder.parse(hookFile.getInputStream());
|
||||
doc = dBuilder.parse(hookFile.getInputStream(manager));
|
||||
doc.getDocumentElement().normalize();
|
||||
if (!doc.getDocumentElement().getNodeName().equals("injector")) {
|
||||
throw new RuntimeException("Incorrect hook file.");
|
||||
|
||||
Reference in New Issue
Block a user