<commit_before>module.exports = function(RED) {
	"use strict";
	var https = require("follow-redirects").https;
	var http = require("follow-redirects").http; // Only for local GMQ connects.
	var urllib = require("url");
	var querystring = require("querystring");
	var fs = require('fs');
	var exec = require('child_process').exec;

	/**********************************************************************/
	var hasGWE = false;
	var hasGMQ = false;
	try {
		fs.statSync("/usr/local/bin/gwe");
		hasGWE = true;
	} catch(err) {
		hasGWE = false;
	}
	if (hasGWE) {
		try {
			fs.statSync("/usr/local/bin/gmq");
			hasGMQ = true;
		} catch(err) {
			hasGMQ = false;
		}
	}

	RED.httpAdmin.get('/exosite-config-features',
		RED.auth.needsPermission('exosite-config-features.read'),
		function(req,res) {
		res.json({"GWE":hasGWE, "GMQ":hasGMQ});
	});

	/**********************************************************************/
	function shttps(opts) {
		if (opts.protocol == 'http:') {
			return http;
		} else {
			return https;
		}
	}

	/**********************************************************************/
	function ExositeConfigureClient(config) {
		RED.nodes.createNode(this,config);
		var cfgNode = this;
		this.productID = config.productID;
		this.serialNumber = config.serialNumber;
		this.connectBy = config.connectBy;

		this.host = function() {
			if (cfgNode.connectBy == "GWE" || cfgNode.connectBy == "GMQ") {
				return "http://localhost:8090";
			} else {
				return 'https://'+cfgNode.productID + ".m2.exosite.com";
			}
		}

		this.configuredOptions = function(node, callback) {
			var Ropts = urllib.parse(cfgNode.host()+'/onep:v1/stack/alias');
			Ropts.headers = {};
			Ropts.headers['content-type'] = 'application/x-www-form-urlencoded; charset=utf-8';
			Ropts.headers['Accept'] = 'application/x-www-form-urlencoded; charset=utf-8';

			if (hasGMQ && cfgNode.connectBy == "GMQ") {
				Ropts.headers['X-Exosite-VMS'] = cfgNode.productID + " " + cfgNode.productID + " " + cfgNode.serialNumber;
				callback(Ropts);

			} else if (cfgNode.credentials.cik != null && cfgNode.credentials.cik != "") {
				Ropts.headers['X-Exosite-CIK'] = cfgNode.credentials.cik;
				callback(Ropts);

			} else if (hasGWE && cfgNode.connectBy == "GWE") {
				node.status({fill:"blue",shape:"ring",text:"activating"});
				node.log("Fetching the GWE CIK");
				exec("/usr/local/bin/gwe --gateway-cik", function(err,stdout,stderr) {
					if (err) {
						RED.log.info("Failed to fetch Gateway's CIK");
						node.status({fill:"red",shape:"ring",text:"Failed to fetch CIK"});
					}
					else {
						try {
							var info = stdout.trim();
							cfgNode.credentials.cik = info;
							Ropts.headers['X-Exosite-CIK'] = cfgNode.credentials.cik;
							node.status({});
							callback(Ropts);
						}
						catch(e) {
							RED.log.info("Failed to parse CIK",stdout.trim());
							node.status({fill:"red",shape:"ring",text:"Failed to parse CIK"});
						}
					}
				});

			} else {
				// Not yet, Need to activate.
				node.status({fill:"blue",shape:"ring",text:"activating"});
				node.log("Going to activate "+cfgNode.productID+"; " + cfgNode.serialNumber);
				var opts = urllib.parse(cfgNode.host()+'/provision/activate')
				opts.method = 'POST';
				opts.headers = {};
				opts.headers['content-type'] = 'application/x-www-form-urlencoded; charset=utf-8';
				var payload = querystring.stringify({
					vendor: cfgNode.productID,
					model: cfgNode.productID,
					sn: cfgNode.serialNumber
				});
				opts.headers['content-length'] = Buffer.byteLength(payload);

				var recievedCIK = "";
				var req = https.request(opts, function(result){
					result.on('data', function (chunk) {
						recievedCIK = recievedCIK + chunk;
					});
					result.on('end',function() {
						if (result.statusCode != 200) {
							var msg = {}
							msg.title = 'Activation Error'
							msg.payload = {code: result.statusCode, body: recievedCIK}
							node.error("Failed to activate : " + recievedCIK, msg);
							node.status({fill:"red",shape:"ring",text:recievedCIK});<commit_msg>Use CIK header, even with GMQ.<commit_after>module.exports = function(RED) {
	"use strict";
	var https = require("follow-redirects").https;
	var http = require("follow-redirects").http; // Only for local GMQ connects.
	var urllib = require("url");
	var querystring = require("querystring");
	var fs = require('fs');
	var exec = require('child_process').exec;

	/**********************************************************************/
	var hasGWE = false;
	var hasGMQ = false;
	try {
		fs.statSync("/usr/local/bin/gwe");
		hasGWE = true;
	} catch(err) {
		hasGWE = false;
	}
	if (hasGWE) {
		try {
			fs.statSync("/usr/local/bin/gmq");
			hasGMQ = true;
		} catch(err) {
			hasGMQ = false;
		}
	}

	RED.httpAdmin.get('/exosite-config-features',
		RED.auth.needsPermission('exosite-config-features.read'),
		function(req,res) {
		res.json({"GWE":hasGWE, "GMQ":hasGMQ});
	});

	/**********************************************************************/
	function shttps(opts) {
		if (opts.protocol == 'http:') {
			return http;
		} else {
			return https;
		}
	}

	/**********************************************************************/
	function ExositeConfigureClient(config) {
		RED.nodes.createNode(this,config);
		var cfgNode = this;
		this.productID = config.productID;
		this.serialNumber = config.serialNumber;
		this.connectBy = config.connectBy;

		this.host = function() {
			if (cfgNode.connectBy == "GMQ") {
				return "http://localhost:8090";
			} else {
				return 'https://'+cfgNode.productID + ".m2.exosite.com";
			}
		}

		this.configuredOptions = function(node, callback) {
			var Ropts = urllib.parse(cfgNode.host()+'/onep:v1/stack/alias');
			Ropts.headers = {};
			Ropts.headers['content-type'] = 'application/x-www-form-urlencoded; charset=utf-8';
			Ropts.headers['Accept'] = 'application/x-www-form-urlencoded; charset=utf-8';

			if (cfgNode.credentials.cik != null && cfgNode.credentials.cik != "") {
				Ropts.headers['X-Exosite-CIK'] = cfgNode.credentials.cik;
				callback(Ropts);

			} else if (hasGWE && cfgNode.connectBy == "GWE") {
				node.status({fill:"blue",shape:"ring",text:"activating"});
				node.log("Fetching the GWE CIK");
				exec("/usr/local/bin/gwe --gateway-cik", function(err,stdout,stderr) {
					if (err) {
						RED.log.info("Failed to fetch Gateway's CIK");
						node.status({fill:"red",shape:"ring",text:"Failed to fetch CIK"});
					}
					else {
						try {
							var info = stdout.trim();
							cfgNode.credentials.cik = info;
							Ropts.headers['X-Exosite-CIK'] = cfgNode.credentials.cik;
							node.status({});
							callback(Ropts);
						}
						catch(e) {
							RED.log.info("Failed to parse CIK",stdout.trim());
							node.status({fill:"red",shape:"ring",text:"Failed to parse CIK"});
						}
					}
				});

			} else {
				// Not yet, Need to activate.
				node.status({fill:"blue",shape:"ring",text:"activating"});
				node.log("Going to activate "+cfgNode.productID+"; " + cfgNode.serialNumber);
				var opts = urllib.parse(cfgNode.host()+'/provision/activate')
				opts.method = 'POST';
				opts.headers = {};
				opts.headers['content-type'] = 'application/x-www-form-urlencoded; charset=utf-8';
				var payload = querystring.stringify({
					vendor: cfgNode.productID,
					model: cfgNode.productID,
					sn: cfgNode.serialNumber
				});
				opts.headers['content-length'] = Buffer.byteLength(payload);

				var recievedCIK = "";
				var req = shttps(opts).request(opts, function(result){
					result.on('data', function (chunk) {
						recievedCIK = recievedCIK + chunk;
					});
					result.on('end',function() {
						if (result.statusCode != 200) {
							var msg = {}
							msg.title = 'Activation Error'
							msg.payload = {code: result.statusCode, body: recievedCIK}
							node.error("Failed to activate : " + recievedCIK, msg);
							node.status({fill:"red",shape:"ring",text:recievedCIK});
{'fraction_non_alphanumeric': 0.21997233052446233, 'fraction_numerical': 0.0030184882404728965, 'mean_word_length': 3.53363740022805, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '491134', 'n_tokens_mistral': 3256, 'n_tokens_neox': 2628, 'n_words': 446}