<commit_before>Promise = require('bluebird')
connman = Promise.promisifyAll(require('connman-simplified-resin')())
express = require('express')
app = express()
bodyParser = require('body-parser')
iptables = Promise.promisifyAll(require('./iptables'))
spawn = require('child_process').spawn
execAsync = Promise.promisify(require('child_process').exec)
os = require('os')
async = require('async')
fs = Promise.promisifyAll(require('fs'))

config = require('./wifi.json')
ssid = process.env.PORTAL_SSID or config.ssid
passphrase = process.env.PORTAL_PASSPHRASE or config.passphrase
port = process.env.PORTAL_PORT or config.port

server = null
ssidList = null
dnsServer = null

connectionFile = '/data/connections.json'
try
	connectionsFromFile = require(connectionFile)
catch
	connectionsFromFile = []

ignore = ->

getIptablesRules = (callback) ->
	async.retry {times: 100, interval: 200}, (cb) ->
		Promise.try ->
			os.networkInterfaces().tether[0].address
		.nodeify(cb)
	, (err, myIP) ->
		return callback(err) if err?
		callback null, [
				table: 'nat'
				rule: 'PREROUTING -i tether -j TETHER'
			,
				table: 'nat'
				rule: "TETHER -p tcp --dport 80 -j DNAT --to-destination #{myIP}:#{port}"
			,
				table: 'nat'
				rule: "TETHER -p tcp --dport 443 -j DNAT --to-destination #{myIP}:#{port}"
			,
				table: 'nat'
				rule: "TETHER -p udp --dport 53 -j DNAT --to-destination #{myIP}:53"
		]

reboot = ->
	execAsync('sync')
	.then ->
		execAsync('dbus-send --print-reply --reply-timeout=2000 --type=method_call --system --dest=org.freedesktop.systemd1 /org/freedesktop/systemd1 org.freedesktop.systemd1.Manager.Reboot')
	.then ->
		process.exit()

loadModuleForStation = ->
	console.log("Removing bcm module")
	return Promise.delay(5000)
	.then ->
		execAsync 'modprobe -r bcm4334x'
	.catch(ignore)
	.delay(5000)
	.then ->
		console.log("Reloading bcm module")
		execAsync 'modprobe bcm4334x'
	.catch(ignore)
	.delay(5000)


loadModuleForAP = (wifi) ->
	console.log("Removing bcm module")
	execAsync 'modprobe -r bcm4334x'
	.catch(ignore)
	.delay(5000)
	.then ->
		console.log("Reloading bcm module")
		execAsync 'modprobe bcm4334x op_mode=2'
	.catch(ignore)
	.delay(5000)

openHotspot = (wifi, ssid, passphrase) ->
	loadModuleForAP(wifi)
	.then ->
		console.log("Opening hotspot")
		wifi.openHotspotAsync(ssid, passphrase)
	.catch (err) ->
		console.log(err)

closeHotspot = (wifi) ->
	console.log("Closing hotspot")
	wifi.closeHotspotAsync()
	.then ->
		loadModuleForStation(wifi)

connectOrStartServer = (wifi, retryCallback) ->
	fs.exists connectionFile, (exists) ->
		if exists
			conn = require(connectionFile)
			wifi.joinWithAgentAsync(conn.ssid, conn.passphrase)
			.then ->
				console.log('Joined! Exiting.')
				retryCallback()
			.catch ->
				startServer(wifi)
		else
			startServer(wifi)

startServer = (wifi) ->
	console.log('Getting networks list')
	wifi.getNetworksAsync()
	.catch (err) ->
		throw err unless err.message == 'No WiFi networks found'
		return []
	.then (list) ->
		ssidList = list
		openHotspot(wifi, ssid, passphrase)<commit_msg>Use shell to open hotspot<commit_after>Promise = require('bluebird')
connman = Promise.promisifyAll(require('connman-simplified-resin')())
express = require('express')
app = express()
bodyParser = require('body-parser')
iptables = Promise.promisifyAll(require('./iptables'))
spawn = require('child_process').spawn
execAsync = Promise.promisify(require('child_process').exec)
os = require('os')
async = require('async')
fs = Promise.promisifyAll(require('fs'))

config = require('./wifi.json')
ssid = process.env.PORTAL_SSID or config.ssid
passphrase = process.env.PORTAL_PASSPHRASE or config.passphrase
port = process.env.PORTAL_PORT or config.port

server = null
ssidList = null
dnsServer = null

connectionFile = '/data/connections.json'
try
	connectionsFromFile = require(connectionFile)
catch
	connectionsFromFile = []

ignore = ->

getIptablesRules = (callback) ->
	async.retry {times: 100, interval: 200}, (cb) ->
		Promise.try ->
			os.networkInterfaces().tether[0].address
		.nodeify(cb)
	, (err, myIP) ->
		return callback(err) if err?
		callback null, [
				table: 'nat'
				rule: 'PREROUTING -i tether -j TETHER'
			,
				table: 'nat'
				rule: "TETHER -p tcp --dport 80 -j DNAT --to-destination #{myIP}:#{port}"
			,
				table: 'nat'
				rule: "TETHER -p tcp --dport 443 -j DNAT --to-destination #{myIP}:#{port}"
			,
				table: 'nat'
				rule: "TETHER -p udp --dport 53 -j DNAT --to-destination #{myIP}:53"
		]

reboot = ->
	execAsync('sync')
	.then ->
		execAsync('dbus-send --print-reply --reply-timeout=2000 --type=method_call --system --dest=org.freedesktop.systemd1 /org/freedesktop/systemd1 org.freedesktop.systemd1.Manager.Reboot')
	.then ->
		process.exit()

loadModuleForStation = ->
	console.log("Removing bcm module")
	return Promise.delay(5000)
	.then ->
		execAsync 'modprobe -r bcm4334x'
	.catch(ignore)
	.delay(5000)
	.then ->
		console.log("Reloading bcm module")
		execAsync 'modprobe bcm4334x'
	.catch(ignore)
	.delay(5000)


loadModuleForAP = (wifi) ->
	console.log("Removing bcm module")
	execAsync 'modprobe -r bcm4334x'
	.catch(ignore)
	.delay(5000)
	.then ->
		console.log("Reloading bcm module")
		execAsync 'modprobe bcm4334x op_mode=2'
	.catch(ignore)
	.delay(5000)

openHotspot = (wifi, ssid, passphrase) ->
	loadModuleForAP(wifi)
	.then ->
		console.log("Opening hotspot")
		execAsync("connmanctl tether wifi on #{ssid} #{passphrase}")
	.catch (err) ->
		console.log(err)

closeHotspot = (wifi) ->
	console.log("Closing hotspot")
	wifi.closeHotspotAsync()
	.then ->
		loadModuleForStation(wifi)

connectOrStartServer = (wifi, retryCallback) ->
	fs.exists connectionFile, (exists) ->
		if exists
			conn = require(connectionFile)
			wifi.joinWithAgentAsync(conn.ssid, conn.passphrase)
			.then ->
				console.log('Joined! Exiting.')
				retryCallback()
			.catch ->
				startServer(wifi)
		else
			startServer(wifi)

startServer = (wifi) ->
	console.log('Getting networks list')
	wifi.getNetworksAsync()
	.catch (err) ->
		throw err unless err.message == 'No WiFi networks found'
		return []
	.then (list) ->
		ssidList = list
		openHotspot(wifi, ssid, passphrase)
{'fraction_non_alphanumeric': 0.15190283400809718, 'fraction_numerical': 0.019433198380566803, 'mean_word_length': 4.087314662273476, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1403343', 'n_tokens_mistral': 2477, 'n_tokens_neox': 2233, 'n_words': 473}