Skip to content

Commit

Permalink
Merge pull request #1 from cwisniew/client-server-merge
Browse files Browse the repository at this point in the history
  • Loading branch information
cwisniew authored Dec 17, 2020
2 parents 8f740ad + b6a45ca commit 670e807
Show file tree
Hide file tree
Showing 45 changed files with 2,045 additions and 377 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,6 @@ dependencies {

// RPTool Libs
implementation 'net.rptools.decktool:decktool:1.0.b1'
implementation 'com.github.RPTools:clientserver:1.4.0.0'
implementation 'com.github.RPTools:maptool-resources:1.6.0'
implementation 'com.github.RPTools:parser:1.8.3'
implementation 'com.github.RPTools:dicelib:1.7.0'
Expand Down Expand Up @@ -292,7 +291,7 @@ dependencies {
implementation 'net.java.abeille:abeille-formsrt:2.0'
implementation 'org.hibernate:antlr:2.7.5H3'
implementation 'org.reflections:reflections:0.9.11'
implementation 'com.caucho.hessian:hessian:3.1.6'
implementation 'com.caucho:hessian:4.0.63'
implementation 'org.mozilla:rhino:1.7.12'
implementation 'ca.odell.renderpack:renderpack:1.2004'
implementation 'net.tsc.servicediscovery:servicediscovery:1.0.b5'
Expand Down Expand Up @@ -357,6 +356,7 @@ dependencies {

// Noise Generator
implementation 'com.github.cwisniew:NoiseLib:1.0.0-rc3' // The most recent version, 1.0.0 is build for a later java version: major version 55 is newer than 54, the highest major version supported by this compiler

}


Expand Down
38 changes: 38 additions & 0 deletions src/main/java/net/rptools/clientserver/ActivityListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* This software Copyright by the RPTools.net development team, and
* licensed under the Affero GPL Version 3 or, at your option, any later
* version.
*
* MapTool Source Code is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public
* License * along with this source Code. If not, please visit
* <http://www.gnu.org/licenses/> and specifically the Affero license
* text at <http://www.gnu.org/licenses/agpl.html>.
*/
package net.rptools.clientserver;

/**
* @author drice
* <p>TODO To change the template for this generated type comment go to Window - Preferences -
* Java - Code Style - Code Templates
*/
public interface ActivityListener {
public static enum Direction {
Inbound,
Outbound
};

public static enum State {
Start,
Progress,
Complete
};

public static final int CHUNK_SIZE = 4 * 1024;

public void notify(
Direction direction, State state, int totalTransferSize, int currentTransferSize);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* This software Copyright by the RPTools.net development team, and
* licensed under the Affero GPL Version 3 or, at your option, any later
* version.
*
* MapTool Source Code is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public
* License * along with this source Code. If not, please visit
* <http://www.gnu.org/licenses/> and specifically the Affero license
* text at <http://www.gnu.org/licenses/agpl.html>.
*/
package net.rptools.clientserver.hessian;

import com.caucho.hessian.io.HessianInput;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.GZIPInputStream;

/**
* @author drice
* <p>TODO To change the template for this generated type comment go to Window - Preferences -
* Java - Code Style - Code Templates
*/
public abstract class AbstractMethodHandler implements MethodHandler {

/* (non-Javadoc)
* @see clientserver.simple.MessageHandler#handleMessage(java.lang.String, byte[])
*/
public void handleMessage(String id, byte[] message) {
try {

HessianInput in = null;
try {
in =
HessianUtils.createSafeHessianInput(
new GZIPInputStream(new ByteArrayInputStream(message)));
} catch (IOException ioe) {
in = HessianUtils.createSafeHessianInput(new ByteArrayInputStream(message));
}
in.startCall();
List<Object> arguments = new ArrayList<Object>();
while (!in.isEnd()) {
arguments.add(in.readObject());
}
in.completeCall();

handleMethod(id, in.getMethod(), arguments.toArray());
} catch (IOException e) {
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* This software Copyright by the RPTools.net development team, and
* licensed under the Affero GPL Version 3 or, at your option, any later
* version.
*
* MapTool Source Code is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public
* License * along with this source Code. If not, please visit
* <http://www.gnu.org/licenses/> and specifically the Affero license
* text at <http://www.gnu.org/licenses/agpl.html>.
*/
package net.rptools.clientserver.hessian;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

public class HessianSecurity {
private final Set<String> allowed;
private final Set<String> denied;

public HessianSecurity() {

Set<String> allow = new HashSet<>();
// Safe java.lang stuff
allow.add("java.lang.Boolean");
allow.add("java.lang.Byte");
allow.add("java.lang.Character");
allow.add("java.lang.Double");
allow.add("java.lang.Float");
allow.add("java.lang.Long");
allow.add("java.lang.Short");
allow.add("java.lang.String");

allow.add("java.awt.geom.*");
allow.add("sun.awt.geom.*");
allow.add("java.awt.BasicStroke");

allow.add("net.rptools.maptool.client.walker.*");
allow.add("net.rptools.maptool.common.*");
allow.add("net.rptools.maptool.model.*");

allow.add("net.rptools.lib.MD5Key");

Set<String> deny = new HashSet<>();
deny.add("*");

allowed = Collections.unmodifiableSet(allow);

denied = Collections.unmodifiableSet(deny);
}

public Collection<String> getAllowed() {
return allowed;
}

public Collection<String> getDenied() {
return denied;
}
}
75 changes: 75 additions & 0 deletions src/main/java/net/rptools/clientserver/hessian/HessianUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* This software Copyright by the RPTools.net development team, and
* licensed under the Affero GPL Version 3 or, at your option, any later
* version.
*
* MapTool Source Code is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public
* License * along with this source Code. If not, please visit
* <http://www.gnu.org/licenses/> and specifically the Affero license
* text at <http://www.gnu.org/licenses/agpl.html>.
*/
package net.rptools.clientserver.hessian;

import com.caucho.hessian.io.HessianFactory;
import com.caucho.hessian.io.HessianInput;
import com.caucho.hessian.io.HessianOutput;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.GZIPOutputStream;

/** @author drice */
public class HessianUtils {

public static HessianInput createSafeHessianInput(InputStream is) {
HessianFactory hessianFactory = new HessianFactory();
HessianSecurity hessianSecurity = new HessianSecurity();
HessianInput in = hessianFactory.createHessianInput(is);
in.getSerializerFactory().setAllowNonSerializable(true);
in.getSerializerFactory().getClassFactory().setWhitelist(true);
hessianSecurity.getAllowed().forEach(a -> in.getSerializerFactory().getClassFactory().allow(a));
hessianSecurity.getDenied().forEach(d -> in.getSerializerFactory().getClassFactory().allow(d));
return in;
}

public static final byte[] methodToBytes(String method, Object... parameters) {
ByteArrayOutputStream bout = new ByteArrayOutputStream();

HessianOutput hout = new HessianOutput(bout);
hout.getSerializerFactory().setAllowNonSerializable(true);

try {
hout.call(method, parameters);
} catch (IOException e) {
e.printStackTrace();
}

return bout.toByteArray();
}

public static final byte[] methodToBytesGZ(String method, Object... parameters) {

ByteArrayOutputStream bout = new ByteArrayOutputStream();

try {

ByteArrayOutputStream hessianBytes = new ByteArrayOutputStream();
HessianOutput hout = new HessianOutput(hessianBytes);
hout.getSerializerFactory().setAllowNonSerializable(true);
hout.call(method, parameters);

GZIPOutputStream gzip = new GZIPOutputStream(bout);
gzip.write(hessianBytes.toByteArray());
gzip.close();

} catch (IOException e) {
e.printStackTrace();
}

return bout.toByteArray();
}
}
27 changes: 27 additions & 0 deletions src/main/java/net/rptools/clientserver/hessian/MethodHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* This software Copyright by the RPTools.net development team, and
* licensed under the Affero GPL Version 3 or, at your option, any later
* version.
*
* MapTool Source Code is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public
* License * along with this source Code. If not, please visit
* <http://www.gnu.org/licenses/> and specifically the Affero license
* text at <http://www.gnu.org/licenses/agpl.html>.
*/
package net.rptools.clientserver.hessian;

import net.rptools.clientserver.simple.MessageHandler;

/**
* @author drice
* <p>TODO To change the template for this generated type comment go to Window - Preferences -
* Java - Code Style - Code Templates
*/
public interface MethodHandler extends MessageHandler {

public void handleMethod(String id, String method, Object... parameters);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* This software Copyright by the RPTools.net development team, and
* licensed under the Affero GPL Version 3 or, at your option, any later
* version.
*
* MapTool Source Code is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public
* License * along with this source Code. If not, please visit
* <http://www.gnu.org/licenses/> and specifically the Affero license
* text at <http://www.gnu.org/licenses/agpl.html>.
*/
package net.rptools.clientserver.hessian.client;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import net.rptools.clientserver.hessian.HessianUtils;

/** @author drice */
public class ClientConnection extends net.rptools.clientserver.simple.client.ClientConnection {

public ClientConnection(String host, int port, String id)
throws UnknownHostException, IOException {
super(host, port, id);
}

public ClientConnection(Socket socket, String id) throws IOException {
super(socket, id);
}

public void callMethod(String method, Object... parameters) {

byte[] message = HessianUtils.methodToBytesGZ(method, parameters);
sendMessage(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* This software Copyright by the RPTools.net development team, and
* licensed under the Affero GPL Version 3 or, at your option, any later
* version.
*
* MapTool Source Code is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public
* License * along with this source Code. If not, please visit
* <http://www.gnu.org/licenses/> and specifically the Affero license
* text at <http://www.gnu.org/licenses/agpl.html>.
*/
package net.rptools.clientserver.hessian.server;

import java.io.IOException;
import net.rptools.clientserver.hessian.HessianUtils;

/** @author drice */
public class ServerConnection extends net.rptools.clientserver.simple.server.ServerConnection {
public ServerConnection(int port) throws IOException {
super(port);
}

public void broadcastCallMethod(String method, Object... parameters) {
broadcastMessage(HessianUtils.methodToBytesGZ(method, parameters));
}

public void broadcastCallMethod(String[] exclude, String method, Object... parameters) {
byte[] data = HessianUtils.methodToBytesGZ(method, parameters);
broadcastMessage(exclude, data);
}

public void callMethod(String id, String method, Object... parameters) {
byte[] data = HessianUtils.methodToBytesGZ(method, parameters);
sendMessage(id, null, data);
}

public void callMethod(String id, Object channel, String method, Object... parameters) {
byte[] data = HessianUtils.methodToBytesGZ(method, parameters);
sendMessage(id, channel, data);
}
}
Loading

0 comments on commit 670e807

Please sign in to comment.