Reject folder syncs outside of game-dir
This commit is contained in:
parent
c7aeef43dd
commit
31ae7a6d3a
1 changed files with 34 additions and 22 deletions
|
@ -31,6 +31,9 @@ import java.util.function.Consumer;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
abstract public class DataExchange {
|
abstract public class DataExchange {
|
||||||
|
private final Path GAME_FOLDER = FabricLoader.getInstance()
|
||||||
|
.getGameDir()
|
||||||
|
.normalize();
|
||||||
public final static SyncFolderDescriptor SYNC_FOLDER = new SyncFolderDescriptor("BCLIB-SYNC", FabricLoader.getInstance()
|
public final static SyncFolderDescriptor SYNC_FOLDER = new SyncFolderDescriptor("BCLIB-SYNC", FabricLoader.getInstance()
|
||||||
.getGameDir()
|
.getGameDir()
|
||||||
.resolve("bclib-sync")
|
.resolve("bclib-sync")
|
||||||
|
@ -119,8 +122,8 @@ abstract public class DataExchange {
|
||||||
return folderID.hashCode();
|
return folderID.hashCode();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int fileCount(){
|
public int fileCount() {
|
||||||
return fileCache==null?0:fileCache.size();
|
return fileCache == null ? 0 : fileCache.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void invalidateCache() {
|
public void invalidateCache() {
|
||||||
|
@ -134,7 +137,7 @@ abstract public class DataExchange {
|
||||||
.toString(), FileHash.create(p.toFile()))));
|
.toString(), FileHash.create(p.toFile()))));
|
||||||
|
|
||||||
//this tests if we can trick the system to load files that are not beneath the base-folder
|
//this tests if we can trick the system to load files that are not beneath the base-folder
|
||||||
if (!BCLib.isClient()){
|
if (!BCLib.isClient()) {
|
||||||
fileCache.add(new SubFile("../breakout.json", FileHash.create(mapAbsolute("../breakout.json").toFile())));
|
fileCache.add(new SubFile("../breakout.json", FileHash.create(mapAbsolute("../breakout.json").toFile())));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -149,7 +152,7 @@ abstract public class DataExchange {
|
||||||
buf.writeInt(fileCache.size());
|
buf.writeInt(fileCache.size());
|
||||||
fileCache.forEach(fl -> {
|
fileCache.forEach(fl -> {
|
||||||
BCLib.LOGGER.info(" - " + fl.relPath);
|
BCLib.LOGGER.info(" - " + fl.relPath);
|
||||||
if (debugHashes){
|
if (debugHashes) {
|
||||||
BCLib.LOGGER.info(" " + fl.hash);
|
BCLib.LOGGER.info(" " + fl.hash);
|
||||||
}
|
}
|
||||||
fl.serialize(buf);
|
fl.serialize(buf);
|
||||||
|
@ -183,7 +186,10 @@ abstract public class DataExchange {
|
||||||
|
|
||||||
//Note: make sure loadCache was called before using this
|
//Note: make sure loadCache was called before using this
|
||||||
boolean hasRelativeFile(String relFile) {
|
boolean hasRelativeFile(String relFile) {
|
||||||
return fileCache.stream().filter(sf -> sf.equals(relFile)).findFirst().isPresent();
|
return fileCache.stream()
|
||||||
|
.filter(sf -> sf.equals(relFile))
|
||||||
|
.findFirst()
|
||||||
|
.isPresent();
|
||||||
}
|
}
|
||||||
|
|
||||||
//Note: make sure loadCache was called before using this
|
//Note: make sure loadCache was called before using this
|
||||||
|
@ -192,8 +198,11 @@ abstract public class DataExchange {
|
||||||
}
|
}
|
||||||
|
|
||||||
//Note: make sure loadCache was called before using this
|
//Note: make sure loadCache was called before using this
|
||||||
SubFile getLocalSubFile(String relPath){
|
SubFile getLocalSubFile(String relPath) {
|
||||||
return fileCache.stream().filter(sf -> sf.relPath.equals(relPath)).findFirst().orElse(null);
|
return fileCache.stream()
|
||||||
|
.filter(sf -> sf.relPath.equals(relPath))
|
||||||
|
.findFirst()
|
||||||
|
.orElse(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
Stream<SubFile> relativeFilesStream() {
|
Stream<SubFile> relativeFilesStream() {
|
||||||
|
@ -202,22 +211,24 @@ abstract public class DataExchange {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Path mapAbsolute(String relPath) {
|
public Path mapAbsolute(String relPath) {
|
||||||
return this.localFolder.resolve(relPath).normalize();
|
return this.localFolder.resolve(relPath)
|
||||||
|
.normalize();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Path mapAbsolute(SubFile subFile) {
|
public Path mapAbsolute(SubFile subFile) {
|
||||||
return this.localFolder.resolve(subFile.relPath).normalize();
|
return this.localFolder.resolve(subFile.relPath)
|
||||||
|
.normalize();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean acceptChildElements(Path absPath){
|
public boolean acceptChildElements(Path absPath) {
|
||||||
return PathUtil.isChildOf(this.localFolder, absPath);
|
return PathUtil.isChildOf(this.localFolder, absPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean acceptChildElements(SubFile subFile){
|
public boolean acceptChildElements(SubFile subFile) {
|
||||||
return acceptChildElements(mapAbsolute(subFile));
|
return acceptChildElements(mapAbsolute(subFile));
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean discardChildElements(SubFile subFile){
|
public boolean discardChildElements(SubFile subFile) {
|
||||||
return !acceptChildElements(subFile);
|
return !acceptChildElements(subFile);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -280,12 +291,7 @@ abstract public class DataExchange {
|
||||||
protected void initClientside() {
|
protected void initClientside() {
|
||||||
if (client != null) return;
|
if (client != null) return;
|
||||||
client = clientSupplier(this);
|
client = clientSupplier(this);
|
||||||
/*ClientLoginConnectionEvents.INIT.register((a, b) ->{
|
|
||||||
System.out.println("INIT");
|
|
||||||
});
|
|
||||||
ClientLoginConnectionEvents.QUERY_START.register((a, b) ->{
|
|
||||||
System.out.println("INIT");
|
|
||||||
});*/
|
|
||||||
ClientPlayConnectionEvents.INIT.register(client::onPlayInit);
|
ClientPlayConnectionEvents.INIT.register(client::onPlayInit);
|
||||||
ClientPlayConnectionEvents.JOIN.register(client::onPlayReady);
|
ClientPlayConnectionEvents.JOIN.register(client::onPlayReady);
|
||||||
ClientPlayConnectionEvents.DISCONNECT.register(client::onPlayDisconnect);
|
ClientPlayConnectionEvents.DISCONNECT.register(client::onPlayDisconnect);
|
||||||
|
@ -424,12 +430,18 @@ abstract public class DataExchange {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void registerSyncFolder(String folderID, Path localBaseFolder, boolean removeAdditionalFiles) {
|
protected void registerSyncFolder(String folderID, Path localBaseFolder, boolean removeAdditionalFiles) {
|
||||||
final SyncFolderDescriptor desc = new SyncFolderDescriptor(folderID, localBaseFolder, removeAdditionalFiles);
|
localBaseFolder = localBaseFolder.normalize();
|
||||||
if (this.syncFolderDescriptions.contains(desc)) {
|
if (PathUtil.isChildOf(GAME_FOLDER, localBaseFolder)) {
|
||||||
BCLib.LOGGER.warning("Tried to override Folder Sync '" + folderID + "' again.");
|
final SyncFolderDescriptor desc = new SyncFolderDescriptor(folderID, localBaseFolder, removeAdditionalFiles);
|
||||||
|
if (this.syncFolderDescriptions.contains(desc)) {
|
||||||
|
BCLib.LOGGER.warning("Tried to override Folder Sync '" + folderID + "' again.");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
this.syncFolderDescriptions.add(desc);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
this.syncFolderDescriptions.add(desc);
|
BCLib.LOGGER.error(localBaseFolder + " (from " + folderID + ") is outside the game directory " + GAME_FOLDER + ". Sync is not allowed.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue