Is there a way in scala to execute arbitrary code on a remote node using RemoteActor?

For example, I have a trusted client and server. The client wants to execute arbitrary code on the server. Can this be done using RemoteActor (serialize the function and send it over the network, deserialize it and execute it)?

+4
source share
2 answers

Not at present. Serializing a function means storing its fields in the output stream of the object and deserializing it means reading the function object from the input stream of the object elsewhere. Deserialization assumes that the class of the object that is read from the input stream of the object is known by the JVM. Remember - functions are just objects behind the scene.

In this case, the server does not know the specific concrete class of the function object that you are serializing, it is possible that it implements the Function interface. To support this functionality, you need to find the class file of the corresponding function, send it to the server and upload it to the server using classloader . Then, if the object has any state, you can serialize the object on the client, send it over the network and deserialize it on the server. Only then can you use his methods. Assuming the objects in your function are stateless, which usually happens, you can skip the serialization / deserialization step.

EDIT:

Remember also that these functions may contain internal references to calling environments. This means that you can complete the serialization of the environment of the function object with it, which could potentially be your whole program.

+3
source

Here is an example of using URLClassLoader with remote participants, get classes via http from the client and perform general calculations on the server.

You may also be interested in the HotSwap method for serializable Akka Actors .

+4
source

All Articles