How to hide library source code in Google way?

前端 未结 2 704
失恋的感觉
失恋的感觉 2021-01-07 01:33

For instance, I have a library and I would like to protect the source code to being viewed. The first method that comes to mind is to create public wrappers for private func

相关标签:
2条回答
  • 2021-01-07 02:07

    Edit: changed my answer to reflect the fact that an exception's stacktrace will contain the library project key.

    In this example, MyLibraryB is a library included by MyLibraryA. Both are shared publicly to view (access controls) but only MyLibraryA's project key is made known. It appears it would be very difficult for an attacker to see the code in MyLibraryB:

    //this function is in your MyLibraryA, and you share its project key
    function executeMyCoolFunction(param1, param2, param3) {
      for (var i = 0; i < 1000000; i++) {
        debugger; //forces a breakpoint that the IDE cannot? step over
      }
      //... your code goes here
      //don't share MyLibraryB project key
      MyLibraryB.doSomething(args...); 
    }
    

    but as per the @megabyte1024's comments, if you were to cause an exception in MyLibraryB.doSomething(), the stacktrace would contain the project key to MyLibraryB.

    0 讨论(0)
  • 2021-01-07 02:21

    I don't know what google does, but you could do something like this (not tested! just an idea):

    function declarations:

    var myApp = {
      foo: function { /**/ },
      bar: function { /**/ }
    };
    

    and then, in another place, an anonymous function writes foo() and bar():

    (function(a) {
      a['\u0066\u006F\u006F'] = function(){
        // here code for foo
      };
      a['\u0062\u0061\u0072'] = function(){
        // here code for bar
      };
    })(myApp);
    

    You can pack or minify to obfuscate even more.

    0 讨论(0)
提交回复
热议问题