Web terminal for Lua http server

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

Web terminal for Lua http server

Jon Smirl
Node.js has a really cool terminal server...
https://github.com/chjj/tty.js
You can open multiple windows and it has xterm support.

Is there anything similar in the Lua world? I'm running Luci on
OpenWRT. It would be neat to incorporate a web terminal into Luci.

--
Jon Smirl
[hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Web terminal for Lua http server

Jorge Visca
On 08/06/13 19:39, [hidden email] wrote:
Is there anything similar in the Lua world? I'm running Luci on
OpenWRT. It would be neat to incorporate a web terminal into Luci.

Not a terminal, but my multitask environment for Lua, https://github.com/xopxe/Lumen , comes with a toy http server with support for websockets, and a simple lua shell that can be accessed from a sample http page :)
Just run the tests/test-http-server.lua, and point your browser to localhost:8080/shell.html

The shell can be also accessed over a plain socket, obviously, but I could not find a telnet signaling libray in pure Lua, nor have i looked how complicated is that.

Lumen works pretty good under OpenWRT, as it can run only depending on nixio, which is the library behind luci. I use it pretty a lot.

It's not what you need, but you could pick some bits from there. Personally, I will be watching this thread closely :)

Jorge

Reply | Threaded
Open this post in threaded view
|

Re: Web terminal for Lua http server

Jon Smirl
I see now that the web server is in C (uhttpd) and only Luci is in Lua.
My current plan is to try shellinabox. It is a standalone C app and I
should be able to use it in an iframe.

On Sun, Jun 9, 2013 at 9:16 PM, Jorge <[hidden email]> wrote:

> On 08/06/13 19:39, [hidden email] wrote:
>
> Is there anything similar in the Lua world? I'm running Luci on
> OpenWRT. It would be neat to incorporate a web terminal into Luci.
>
>
> Not a terminal, but my multitask environment for Lua,
> https://github.com/xopxe/Lumen , comes with a toy http server with support
> for websockets, and a simple lua shell that can be accessed from a sample
> http page :)
> Just run the tests/test-http-server.lua, and point your browser to
> localhost:8080/shell.html
>
> The shell can be also accessed over a plain socket, obviously, but I could
> not find a telnet signaling libray in pure Lua, nor have i looked how
> complicated is that.
>
> Lumen works pretty good under OpenWRT, as it can run only depending on
> nixio, which is the library behind luci. I use it pretty a lot.
>
> It's not what you need, but you could pick some bits from there. Personally,
> I will be watching this thread closely :)
>
> Jorge
>



--
Jon Smirl
[hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Web terminal for Lua http server

koba
In reply to this post by Jon Smirl
On Sat, Jun 08, 2013 at 06:39:38PM -0400, [hidden email] wrote:

> Node.js has a really cool terminal server...
> https://github.com/chjj/tty.js
> You can open multiple windows and it has xterm support.
>
> Is there anything similar in the Lua world? I'm running Luci on
> OpenWRT. It would be neat to incorporate a web terminal into Luci.

I've written a basic rpc-server + a "plugin" engine (for executing
"standalone" lua scripts) in lua with a frontend in coffescript based on
https://github.com/replit/jq-console and
https://github.com/gimmi/jsonrpcjs

Still not public available (not yet finished) but if you contact me
offlist we can organize something (and then publish it).

bye,
K.

Reply | Threaded
Open this post in threaded view
|

Re: Web terminal for Lua http server

steve donovan
That uses Orbiter (which is more of a 'personal' web server for local apps) but would probably work with Orbit as well.

Reply | Threaded
Open this post in threaded view
|

Re: Web terminal for Lua http server

David Favro
In reply to this post by Jon Smirl
On 06/10/2013 12:01 AM, [hidden email] wrote:
> I see now that the web server is in C (uhttpd) and only Luci is in Lua.
> My current plan is to try shellinabox. It is a standalone C app and I
> should be able to use it in an iframe.

It should be fairly easy to extend uhttpd to provide a simple HTTP form into
which you can type Lua, send it off, execute, and return any output for
display in the browser (and also make use of various Lua APIs provided by
OpenWRT to control your box) -- security considerations aside, obviously.
It could be done almost no additional memory/storage requirements if you're
already running Luci.  The same for any server that has a Lua "handler" or
API option.

A full-blown interpreter that remembers state from one request to the next
might be a bit more work however.

-- David


Reply | Threaded
Open this post in threaded view
|

Re: Web terminal for Lua http server

Jon Smirl
Have you seen repl.it?


http://repl.it/languages/Lua

On Mon, Jun 10, 2013 at 1:03 PM, David Favro <[hidden email]> wrote:

> On 06/10/2013 12:01 AM, [hidden email] wrote:
>>
>> I see now that the web server is in C (uhttpd) and only Luci is in Lua.
>> My current plan is to try shellinabox. It is a standalone C app and I
>> should be able to use it in an iframe.
>
>
> It should be fairly easy to extend uhttpd to provide a simple HTTP form into
> which you can type Lua, send it off, execute, and return any output for
> display in the browser (and also make use of various Lua APIs provided by
> OpenWRT to control your box) -- security considerations aside, obviously. It
> could be done almost no additional memory/storage requirements if you're
> already running Luci.  The same for any server that has a Lua "handler" or
> API option.
>
> A full-blown interpreter that remembers state from one request to the next
> might be a bit more work however.
>
> -- David
>
>



--
Jon Smirl
[hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Web terminal for Lua http server

David Favro
On 06/10/2013 08:57 PM, [hidden email] wrote:
> Have you seen repl.it?

That is indeed interesting, but it looks to be in the browser: I had thought
based on your original post that you were looking for server-side execution
("Lua http server", "integrated with Luci").

For browser-side execution, there are several options, this one is probably
similar to what the repl.it people are doing, but perhaps with the very
important difference of providing access to the DOM and other JS facilities:
http://kripken.github.io/lua.vm.js/repl.html

I say 'perhaps' because I don't know exactly what repl.it provides, except
that I don't see anything in _G that would suggest an API for the DOM or
anything else that reflects the browser environment.

-- David


Reply | Threaded
Open this post in threaded view
|

Re: Web terminal for Lua http server

Jon Smirl
I believe repl.it is the Ace editor on the left:
http://ace.ajax.org/#nav=about

tty.js on the right.
https://github.com/chjj/tty.js/

And it is using an embedded Lua interpreter at the server.


On Tue, Jun 11, 2013 at 3:33 PM, David Favro <[hidden email]> wrote:

> On 06/10/2013 08:57 PM, [hidden email] wrote:
>>
>> Have you seen repl.it?
>
>
> That is indeed interesting, but it looks to be in the browser: I had thought
> based on your original post that you were looking for server-side execution
> ("Lua http server", "integrated with Luci").
>
> For browser-side execution, there are several options, this one is probably
> similar to what the repl.it people are doing, but perhaps with the very
> important difference of providing access to the DOM and other JS facilities:
> http://kripken.github.io/lua.vm.js/repl.html
>
> I say 'perhaps' because I don't know exactly what repl.it provides, except
> that I don't see anything in _G that would suggest an API for the DOM or
> anything else that reflects the browser environment.
>
> -- David
>
>



--
Jon Smirl
[hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Web terminal for Lua http server

David Favro
On 06/11/2013 03:45 PM, [hidden email] wrote:
> I believe repl.it is the Ace editor on the left:
> http://ace.ajax.org/#nav=about
>
> tty.js on the right.
> https://github.com/chjj/tty.js/
>
> And it is using an embedded Lua interpreter at the server.

Nice stuff, to be sure -- but a little hard to find much info on their site;
I got confused by their "About Us" page [1]:
 > All our interpreters are written in (or compiled to) JavaScript, and run
 > completely on the user's device, regardless or whether it's a desktop,
 > laptop or phone.

Which sure doesn't sound like it runs on the server.

-- David

[1]: http://repl.it/about


Reply | Threaded
Open this post in threaded view
|

Re: Web terminal for Lua http server

Jon Smirl
This is running in the node.js part -- isn't it calling the lua engine?

this['Lua'] = {
  isInitialize: null,
  state: null,
  streamName: null,
  reportError: function () {
    _lua_settop(this.state, 0);
  },
  // Allocates a string on the heap.
  allocateString: function(str) {
    var arr = intArrayFromString(str);
    return allocate(arr, 'i8', 0);  // ALLOC_NORMAL
  },
  // Parses Lua code and loads it into the execution buffer. Returns true on
  // success.
  parse: function (command) {
    // =x commands explicitly ask for the result.
    if (command.match(/^=(?!=)/)) command = 'return ' + command.slice(1);
    // Prepare the command, as an expression and a statement.
    var commandPtr = this.allocateString(command);
    var evalCommand = 'return ' + command;
    var evalCommandPtr = this.allocateString(evalCommand);

    // Try parsing the command, first as an expression, then if that fails, as a
    // statement.
    var parseFailed = _luaL_loadbuffer(
        this.state, evalCommandPtr, evalCommand.length, this.streamName);
    if (parseFailed) {
      _lua_settop(this.state, -2)  // Discard error message.
      parseFailed = _luaL_loadbuffer(
        this.state, commandPtr, command.length, this.streamName);
    }

    // Cleanup.
    _free(commandPtr);
    _free(evalCommandPtr);

    return !parseFailed;
  },
  // Returns the string representation of the value at the top of the Lua stack.
  popStack: function () {
    var ret;
    var type = _lua_type(this.state, -1);
    switch (type) {
      case -1:  // LUA_TNONE
      case 0:   // LUA_TNIL
        ret = null;
        break;
      case 1:   // LUA_TBOOLEAN
        var result = _lua_toboolean(this.state, -1);
        ret = result ? 'true' : 'false';
        break;
      case 3:   // LUA_TNUMBER
        ret = _lua_tonumber(this.state, -1);
        break;
      case 4:   // LUA_TSTRING
        var ptr = _lua_tolstring(this.state, -1, 0);
        var len = _lua_objlen(this.state, -1);
        var buffer = [];
        for (var i = 0; i < len; i++) {
          buffer.push(String.fromCharCode(HEAP[ptr + i]));
        }
        ret = buffer.join('');
        break;
      default:
        var ptr = _lua_typename(this.state, type);
        var typename = Pointer_stringify(ptr);
        var address = _lua_topointer(this.state, -1);
        ret = typename + ': 0x' + address.toString(16);
    }
    _lua_settop(this.state, -2);
    return ret;
  },
  // Initializes the Lua runtime with optional standard I/O callbacks. The input
  // callback is asynchronous and takes a callback which should be passed a
  // string. The output and error callbacks are synchronous and are passed
  // character codes.
  'initialize': function (input, output, error) {
    if (this.isInitialize) throw new Error('Lua already initialized.');
    FS.init(null, output, function (chr) { /* Ignore stderr. */ });
    if (error) {
      this.reportError = function(defaultMessage) {
        var errorMessage = this.popStack();
        if (!errorMessage.length) errorMessage = defaultMessage;
        for (var i = 0; i < errorMessage.length; i++) {
          error(errorMessage.charCodeAt(i));
        }
        _lua_settop(this.state, 0);
      }
    }
    run();
    this.streamName = this.allocateString('stdin');
    this.state = _luaL_newstate();
    _luaL_openlibs(this.state);
    this.isInitialize = true;
  },
  // Checks whether a command is finished and does not require more input.
  // Useful when running a REPL.
  'isFinished': function (command) {
    if (!this.isInitialize) throw new Error('Lua not initialized.');
    var parseSuccess = this.parse(command);
    if (!parseSuccess) this.popStack();
    return parseSuccess;
  },
  // Evaluates Lua code and returns the result.
  //   1. If an expression is passed, returns the representation of the value of
  //      this expression, or null if the value is nil.
  //   2. If a statement or set of statements is passed, returns null.
  //   3. If an error occurs, returns undefined.
  'eval': function (command) {
    if (!this.isInitialize) throw new Error('Lua not initialized.');

    // Parse and load the command.
    var parseSuccess = this.parse(command);
    if (!parseSuccess) {
      this.reportError('Unknown parsing error.');
    }

    // Execute the code.
    var result = _lua_pcall(this.state, 0, 1, 0);

    if (result == 0) {  // LUA_OK
      return _lua_gettop(this.state) > 0 ? this.popStack() : null;
    } else {  // LUA_ERR*
      this.reportError('Unknown evaluation error.');
    }
  }
};

On Tue, Jun 11, 2013 at 4:11 PM, David Favro <[hidden email]> wrote:

> On 06/11/2013 03:45 PM, [hidden email] wrote:
>>
>> I believe repl.it is the Ace editor on the left:
>> http://ace.ajax.org/#nav=about
>>
>> tty.js on the right.
>> https://github.com/chjj/tty.js/
>>
>> And it is using an embedded Lua interpreter at the server.
>
>
> Nice stuff, to be sure -- but a little hard to find much info on their site;
> I got confused by their "About Us" page [1]:
>> All our interpreters are written in (or compiled to) JavaScript, and run >
>> completely on the user's device, regardless or whether it's a desktop,
>> laptop or phone.
>
> Which sure doesn't sound like it runs on the server.
>
> -- David
>
> [1]: http://repl.it/about
>
>



--
Jon Smirl
[hidden email]