JavaScript connection filter

This filter simply executes its JavaScript code.

The following variables will be accessible from your code, in the context object:

socket will be a Java socket object from which you can get the client's address, for instance:

let clientAddress = context.socket.getRemoteSocketAddress().toString();

Note that, in this example, the address will typically start with a slash, e.g. /123.45.67.89

result is a result object, it has two attributes you can change: success (boolean) and errorMessage (string).

Example code

A (slightly contrived) example that checks if the last number of an IP4 address is 200 or more:

const clientAddress = context.socket.getRemoteSocketAddress().getAddress().toString();
const lastDotIdx = clientAddress.lastIndexOf(".");
if (lastDotIdx > 0) {
const finalNumStr = clientAddress.substring(lastDotIdx + 1);
const finalNum = parseInt(finalNumStr);
if (finalNum >== 200) {
context.result.success = false;
context.result.errorMessage = "No IPs ending over 200";
}
}

Note that your JavaScript code has full access to JavaScript and the underlying Java environment. For instance, you could make a call to an external service using Java with something like:

try {
const JavaURL = Java.type("java.net.URL");
const url = new JavaURL("
https://myservice");
const conn =
url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");

if (conn.getResponseCode() != 200) {
throw "
Response code was not 200";
}
const JavaInputStreamReader = Java.type("
java.io.InputStreamReader");
const
in = new JavaInputStreamReader(conn.getInputStream());
const JavaBufferedReader = Java.type("
java.io.BufferedReader");
const inReader = new JavaBufferedReader(in);
while (var line = inReader.readLine()) {
log.debug("Received line from service: " + line);
}

}
catch(e) {
throw "Unable to fetch from service: " + e;
}

You can extend your logic by installing additional Java libraries in the Gallium Data server.

Also note that your JavaScript code can have full access to almost any Node.js libraries, provided that they are installed on the Gallium Data server.