Do pre-processor directives protect server code from the client?

140 Views Asked by At

I'm developing a client-server library. Some of the classes can be used by either the client or the server but are executed differently and yield slightly different results for each. As well, the server code may contain additional methods that will not be called from the client build.

A class may look like this:

public class StuffDoer {

    public void DoStuff(object msg)
    {
        ServerDoStuff(msg);
        ClientDoStuff(msg);
    }

    [Conditional("SERVER")]
    private void ServerDoStuff(object msg) 
    {
        // Do secret server stuff...
    }

    [Conditional("CLIENT")]
    private void ClientDoStuff(object msg)
    {
        // Do client sutff...
    }

    [Conditional("SERVER")]
    public void DoCoolStuff(object msg)
    {
        // server does cool stuff...
    }

}

I've read that the Conditional attribute still compiles the code and would therefore be in the build, unlike pre-processor directives which would completely remove the code and not even compile it.

I'm concerned that a dishonest client may hack the product by unobfuscating the source code and figure out how the server works.

Are my fears unfounded or would I need to place pre-processor directives in order to hide the source code?

2

There are 2 best solutions below

0
On BEST ANSWER

When "SERVER" is not defined, The method be marked as "SERVER" will always compile to the final assembly but all of callings to the method will be removed.

This is code

This is decompiled result

2
On

According to the documentation:

Applying ConditionalAttribute to a method indicates to compilers that a call to the method should not be compiled into Microsoft intermediate language (MSIL) unless the conditional compilation symbol that is associated with ConditionalAttribute is defined.

When you compile with CLIENT defined, the code to call a method marked with SERVER will not be present in the final assembly. But the code for the method will be present. You cannot achieve what you need in this way.