Skynet project LUA code simple encryption

Source: Internet
Author: User

recently looking at Skynet's code, the overall feeling is similar to Erlang, and the idea is based on the actor model, where everything is actor,actor to communicate by sending messages. (The word "everything" here tends to indicate an individual who has an active role and has independent behavior.) )
the difference is thatSkynet is implemented using C and LUA, which are more prevalent than Erlang. Skynet's actorrefers to Skynet service, Skynet service similar to the Erlang process, the scheduling method is also a bit close, Skynet startup will initialize a number of scheduling threads, for processing multiple services at the same time, each service has a certain number of scheduling constraints, suspended or to achieve the constraints of the loss of scheduling rights. However, the dispatch of the Skynet service is not polling, but the message-driven way (who dispatches the message, first the message is dispatched). Implementation, each service is a LUA state, which is a LUA virtual machine, and the effect is the same as a sandbox for isolation. In addition, the LUA state itself is not multi-threaded, and the Skynet implementation runs multiple LUA state instances on a single thread.
well, cut into today's theme, Skynet project LUA code encryption. This paper uses LUA to compile the binary to realize the encryption, the method is relatively simple.
LUA compilationLua code can be compiled with binary files, that is, LUA can support both source code loading and binary loading two ways. However, Lua is compiled into binary with a little use limit, and LUA binaries support cross-platform, cross-version, but must be on the same word length machine. This means that 32-bit LUA-compiled binaries cannot be used under 64-bit. In addition, although the official said that the cross-version, but also declared that the cross-version may be incompatible with the situation, not specified. Another point, the LUA code compiled into binary, does not mean that the execution is faster, the process is simply precompiled, the code compiled into bytecode (bytecodes). Because the LUA source code is also generated before the bytecode, so the binary mode to some extent, improve the speed of code loading.

Lua CompilerLUAC [Options] [filenames ]Options Description:
Options
Description
Generate visual data for Lua compiled bytecode, which is useful for learning LUA virtual machines
-o file Compile the LUA code, and the output files are file. Default Build luac.out
-V Display version Information


compiling LUA scripts for Skynet projectsSkynet does not provide a tool to generate, so here's a simple script to illustrate. Currently Skynet does not support Luajit
#!/bin/bash
Luac=./3rd/lua/luacMkdir-p bin
luas= ' Find-name ' *.lua 'For file in $Luas Dofilename = ' basename $file '$LUAC-o bin/$filename $file Done
When the script finishes executing, the compiled code is output to the bin directory.in order for the code to be executed, the configuration file needs to be modified, and all LUA files are looked up from the bin path. taking Examples/config as an example
root = "./"thread = 8logger = NilLogPath = "."Harbor = 1address = "127.0.0.1:2526"master = "127.0.0.1:2013"start = "Main"--main scriptbootstrap = "Snlua Bootstrap"-The service for Bootstrapstandalone = "0.0.0.0:2013"Luaservice = root: " Bin/?. Lua; "Lualoader = root: " Bin/loader.lua "Snax = root: " Bin/?. Lua "--Snax_interface_g = "Snax_g"CPath = root: " cservice/?. So "--Daemon = "./skynet.pid"


another way to encrypt LUA codeIt is sufficient to compile the LUA code into binary for encryption purposes as described earlier. If you want to encrypt your code further, you can use the following method. Luac compile, output to a file through the following function,This is illustrated by an example of XOR or encryption.
Luac.c
static int writer (lua_state* L, const void* p, size_t size, void* u) {int ret =! 0;size_t i =0;char* pp = (char *) malloc (sizeof (char*) *size);UNUSED (L);memcpy (PP, p, size);For (; i<size;i++) pp[i] ^=250;ret = (fwrite ((void*) pp, size,1, (file*) u)!=1) && (size!=0);Free (PP);return ret;}
The Lua execution code is also modified accordingly, otherwise it cannot be executed.
//LAUXLIB.C
Static const char *GETF (lua_state *l, void *ud, size_t *size) {LOADF *LF = (LOADF *) ud;(void) L; /* Not used * /if (lf->n > 0) {/* is there pre-read characters to be read * **size = lf->n; /* return them (chars already in buffer) */lf->n = 0; /* No more pre-read characters * /  }else {/* read a block from file * // * ' fread ' can return > 0 *and* set the EOF flag. If Next call to' getf ' called ' fread ', it might still wait for user input.The next check avoids this problem. * *if (feof (lf->f)) return NULL;*size = Fread (lf->buff, 1, sizeof (Lf->buff), lf->f); /* Read Block * /  } do{size_t i=0; for (;i<*size;) lf->buff[i++] ^= 250;} while (0); return lf->buff;}
This is possible, but note that you cannot modify both of the above, compile and execute separately, or you will not be able to compile LUA code. (Different versions of Skynet, code can be different, reference only)

Luajit CompilingAccording to Skynet's plan, LUA is likely to switch to Luajit. Luajit also supports binary compilation, and LUA code is encrypted in a similar way. Luajit-b filename NewFile
Last WordsFinally, the article uses LUA to compile into binary encryption, compiled into bytecode, can be directly loaded by LUA, more practical. In comparison, the compiler code is not very suitable, here is just to provide a possible idea. All right, here we go today. Further study the realization of Skynet, continue to do share.
Reference: http://blog.csdn.net/mycwq/article/details/47406337

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Skynet project LUA code simple encryption

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.