Execute R code on server via node - javascript

I made a website where I can write code into a textfield and I want to send this code to my nodes.js server and call R, which is installed on my server, and make it process the written R code.
What I have trouble with is, how can I start R on my server and input code into it via Javascript?
The security aspects are not relevant for this because it will never go live and will always stay localhost.
Any ideas on how to approach this problem?

If you use an R web framework, you can then use the eval function to run the contents of the text box as code.
Obviously, this is very risky, but you've said you only want this to run locally.
You could use other languages with an exec function to spawn a child process. Write the code to file, compile with exec, then run with the exec function.
In node you could use a child process to achieve this
https://nodejs.org/api/child_process.html
In PHP you could use exec to the same end
http://php.net/manual/en/function.exec.php
All of these options carry the same risk.
If you want to safely execute R code on the server, you could implement an RPC mechanism, a simple one would consist of the name of the command and the parameters to be passed. This way, you can effectively whitelist what is allowed to execute, but it does mean you can't excecute arbitrary R code.

Related

Create Callback from Python

I have a python script that gets called from some javascript code, that I have running. And when the python script is finished I want a call back to go to a Java program of mine, I was trying to make a html page and then check with that, but I am running the Java program locally and can't connect to the FTP?
How could this be accomplished?
Thanks
EDIT
Here is how the flow works
Java calls a javascript function in browser on my local machine ->
Javascript calls python with POST Request on my Server ->
I want a callback to Java to know when Python is done ->
So that Java can move on
Do you see know how it works?
If you are running from the command line you could use pipes | which will call the next command in line with the standard out of the first command. As follows:
firstCommand | secondCommand
So if your Python script writes to standard out you could invoke it from the command line, and have the Java execute from the command line also as the second command receiving it's input from standard in.

Javascript function like PHP exec()

I need to be able to execute a shell command through javascript, similar to the php function "exec()". I understand that this may be impractical in javascript because of security reasons, but my javascript code is running on the server, and no clients have direct access to the file.
Users make a request to the server for some data, and the server-side javascript code is called. From the javascript file, I need to execute a program in order to gather data based on user input, then pass this data back.
If this isn't possible in vanilla Javascript, please point me towards a library or tool that can do this, preferably in javascript/frameworks on javascript.
JavaScript has no 'exec' function like PHP does. It's all because JavaScript runs on the client and don't have access to the server part.
However you can create PHP page and send AJAX requests to it to execute particular command.
ALTHOUGH, you need to be VERY, VERY and VERY cautious about which commands to run.
It's very dangerous to do like that. I don't advice you to do like that, however, it's possible.
Good luck!
If it's client side - You can't
If it's node.js:
var exec = require('child_process').exec;
child = exec("command", function (error, stdout, stderr)
{
// handle the output
});
This might be what you're looking for: https://github.com/arturadib/shelljs
Or, if you want to have direct access to commands, perhaps this will help: http://nodejs.org/api/child_process.html

Calling C functions inside javascript

The javascript is printing out the HTML onto the page example below, is it possible to call a C function on it for example in C to convert something to another language there is a function LANG_Str("text") which converts the text into the specified language. Would it be possible to use this function on the below text inside Javascript?.
"<tr><th>Service</th><th>Target Allocation (%)</th><th></th>"
EDIT:
I'm basically wanting to do a human language translation. The site already supports multi-language, the problem is on the custom screen like the one shown above which gets generated in Javascript, cannot use the function used to translate text the way its done normally in C.
If it's running in the browser: no. Sorry.
You might be able to do it in server-side code beforehand (e.g. Python or PHP which can call C) when putting together the page content. Alternatively you can make an AJAX request to a server which exposes the C function as an HTTP API/Endpoint (via, GCI, FCGI or Python/PHP/Perl). But not in the browser.
This is because the JS runs in a sandboxed virtual environment which has no access to system calls or anything outside the runtime.
EDIT
In response to your comment "The script is ran in the C using HTML_WriteToCgi", this suggests that you are putting together the HTML in C on your server. If this is correct, go for my option 1 above, by injecting the values directly into the JS source code if all values come out of some data known by the server.
You might consider moving some functionality out of browser JS and back into server-side code to solve your problem.
You can make a special request, so the webserver can use that request and send it to the webpage.
JavaScript can't access any other processes directly, but it can make a server request for the information. The server can call a C function if need be.
In the end, it's not JavaScript calling the C function, it's the server (and whatever language it's using: Python, PHP, ASP.NET, JSP, etc) that would be calling the C function.
My interpretation is that your goal is to call a C function within HTML / Javascript and capture the output.
What you could do is make a VM. Basically, you have a huge array "memory", a couple of "registers", etc... The hardest part is to make sure that they instruction set and the bytecodes of your VM mirrors some common instruction set that there is a C compiler for. You compile the C code that VM on your computer, save it to a file, and run it on the VM. If doing that is too hard, you could just get a C to assembly converter, and just define a couple of Assembly instructions instead. There is a Linux emulator in pure javascript with no server calls that does precisely that.
You might consider creating a RESTful web service on your server that will receive the source text and target language id, then return the translated text. You could then access it from your webpage via an ajax call.
I’m not an expert on web development. But isn’t it possible for javascript to invoke c using webassembly?
Not sure of it’s limitation/constraints though - such as memory
Something like this?
https://developer.mozilla.org/en-US/docs/WebAssembly/C_to_wasm

How to unpack Javascript in Python

I would like to retrieve the contents of a javascript script instead of executing it upon requesting it.
EDIT: I understand that Python is not executing the javascript code. The issue is that when I request this online JS script it gets executed. I'm unable to retrieve the contents of the script. Maybe what I want is to decode the script like so http://jsunpack.jeek.org/dec/go
That's what my code looks like to request the js file:
def request(self, uri):
data = None
req = urllib2.Request(uri, data, self.header)
response = urllib2.urlopen(req)
html_text = response.read()
return html_text.decode()
I know approximately what the insides of the script look like but all I get after the request is issued is a 'loaded' message. My guess is that the JS code gets executed. Is there any way to just request the code?
There is no HTML or JavaScript interpreter in urllib2. This module does nothing but fetch the resource and return it to you raw; it certainly will not attempt to execute any JavaScript code it receives. If you are not receiving the response you expect, check the URL with a tool like wget or monitor the network connection with Wireshark or Fiddler to see what the server is actually returning.
(decode() here only converts the bytes of the HTTP response body to Unicode characters—using the default character encoding, which probably isn't a good idea.)
ETA:
I guess what I want is to decode the Javascript like so jsunpack.jeek.org/dec/go
Ah, well that's a different game entirely. You can get the source for that here, though you'll also need to install SpiderMonkey, the JavaScript engine from Mozilla, to allow it to run the downloaded JavaScript.
There's no way to automatically ‘unpack’ obfuscated JavaScript without running it, since the packing code can do anything at all and JS is a Turing-complete language. All this tool does is run it with some wrapper code for functions like eval which packers/obfuscators typically use. Unfortunately, this sabotage is easily detectable, so if it's malware you're trying to unpack you'll find this fails as often as it succeeds.
I'm not sure I understand. If I do a simplified version of your code and run it on a URI that's sure to have some javascript:
>>> import urllib2
>>> res = urllib2.urlopen("http://stackoverflow.com/questions/6946867/how-to-unpack-javascript-in-python")
And you print res (or res.decode()), the javascript is intact.
Doing urlopen should retrieve whatever character stream the source provides. It's up to you to do something with it (render it as html, interpret it as javascript, etc).

JavaScript Glib.spawn_async stdout file descriptor

I want to spawn a process using spawn_async in the GLib bindings in javascript in a gnome3 shell-extension.
I need something like the "standard_output=True" parameter in the python doc http://developer.gnome.org/pygobject/stable/glib-functions.html which, when enabled, returns a filedescriptor to stdout of the process. The python API and C API differ heavily in this point.
Unfortunately, I cannot find any precise documentation of the JS API to GTK anywhere, the official page doesn't even list it though the shell is written in js to big parts...
The background of my question is that I call a python script doing serial communication, since I saw no other way to let JS get its data from such a script but through spawning a process.
Do you have any guess how to get the stdout of a process started like this?
The pygobject documentation you referenced is for the static libraries. Since Seed works through GObject introspection, you're safer trusting the C documentation. (Seed is the GObject introspecting Java Script library)
Perhaps you can roll your own function that does what you want in C and expose it to Seed: http://developer.gnome.org/seed/stable/seed-Native-Functions.html
This page includes info about http://developer.gnome.org/seed/3.0/seed-Modules.html embedding/utilizing your "c-module" in the javascript. An example taken from the page:
hello = imports.hello;
hello.say_hello_to("Tim");

Categories