Search code examples
javaruby-on-railsrubyjruby.class-file

AOT compiling with jRuby to obfuscate Rails code


We have a RoR application running in windows server 2012 R2, using IIS, jruby, and puma in a clients network. The production application runs great! The only problem now, is that we need to protect the source code in some manner through obfuscation. My initial thought was that we could compile the .rb files into .class files so that jruby could digest those instead of the .rb files. So I tried it with a single file to see if I could get it to work. I navigated to myapp/app/controllers and...

jruby -S jrubyc application_controller.rb

that created a compiled class file application_controller.class in that directory. I then tried the methods mentioned at this link for an older version of jRuby to see if they would work - https://github.com/jruby/jruby/wiki/RailsAOT

but none of them did, either giving me an uninitialized constant ApplicationController, or unable to load file application_controller.

I also tried instructions from this https://www.ruby-forum.com/topic/216572#939791 but given my lack of java knowledge, Im pretty sure I do not have it correct. This was the last state of application_controller.rb -

require 'java'
$CLASSPATH << 'lib'
java_import 'application_controller'

This gives me a 'cannot import application_controller as application_controller' Im guessing because of the underscores in the class name.

Essentially, I am trying to get my rails app to work with .class files in place of .rb files.

UPDATE: So I tried to use warbler to create a compiled war

jruby -S warble compiled war

I then extracted the .war into a tomcat container to look at the contents of the .rb files. They all looke like this:

load __FILE__.sub(/.rb$/,'.class')

So I tried taking that snippet and putting it in my application_controller.rb running on puma. I jruby -S rails server, and got the following error-

use 'java_import' to load normal Java classes: application_controller

So I then changed my application_controller.rb to

require 'java'
$CLASSPATH << 'app/controllers'
java_import 'application_controller'

That gave me the following error -

cannot import class 'application_controller' as 'application_controller'

Could it be that it needs to be camelcase? Also I am using jRuby 9.0.0.0, I think I might try and rollback to 1.7 and see what happens

UPDATE: IT WORKED! rolled back to jruby 1.7, ran jrubyc on the application_controller.rb and replaced application_controller.rb contents with

load __FILE__.sub(/.rb$/,'.class')

and my app loaded just fine using puma with IIS.


Solution

  • try Warbler with the compiled feature ... it does ~ the same but creates a dummy .rb file that loads the .class version (it's not clear from your notes or the old AOT wiki whether you tried that explicitly)

    you should look inside the generated .war file (it's a .zip) how the compiled .rb files got updated ...