<commit_before>connman = require('connman-simplified')()
express = require('express')
app = express()
bodyParser = require('body-parser')
iptables = require('./iptables')
spawn = require('child_process').spawn
os = require('os')
async = require('async')

ssid = process.env.SSID or 'ResinAP'
passphrase = process.env.PASSPHRASE or <PASSWORD>

port = process.env.PORT or 8080

server = null
ssidList = null
dnsServer = null

getIptablesRules = (callback) ->
	async.retry {times: 100, interval: 100}, (cb) ->
		try
			myIP = os.networkInterfaces().tether[0].address
			cb(null, myIP)
		catch err
			cb(err)
	, (err, myIP) ->
		throw 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}:8080"
			,
				table: 'nat'
				rule: "TETHER -p tcp --dport 443 -j DNAT --to-destination #{myIP}:8080"
			,
				table: 'nat'
				rule: "TETHER -p udp --dport 53 -j DNAT --to-destination #{myIP}:53"
		]


startServer = (wifi) ->
	wifi.getNetworks (err, list) ->
		throw err if err?
		ssidList = list
		wifi.openHotspot ssid, passphrase, (err) ->
			throw err if err?
			console.log("Hotspot enabled")
			dnsServer = spawn('named', ['-f'])
			getIptablesRules (err, iptablesRules) ->
				iptables.appendMany iptablesRules, (err) ->
					throw err if err?
					console.log("Captive portal enabled")
					server = app.listen port, ->
						console.log("Server listening")

console.log("Starting node connman app")
manageConnection = (retryCallback) ->
	connman.init (err) ->
		retryCallback(err) if err?
		console.log("Connman initialized")
		connman.initWiFi (err, wifi, properties) ->
			retryCallback(err) if err?
			console.log("WiFi initialized")

			app.use(bodyParser())
			app.use(express.static(__dirname + '/public'))
			app.get '/ssids', (req, res) ->
				res.send(ssidList)<commit_msg>Also log errors before retry<commit_after>connman = require('connman-simplified')()
express = require('express')
app = express()
bodyParser = require('body-parser')
iptables = require('./iptables')
spawn = require('child_process').spawn
os = require('os')
async = require('async')

ssid = process.env.SSID or 'ResinAP'
passphrase = process.env.PASSPHRASE or null

port = process.env.PORT or 8080

server = null
ssidList = null
dnsServer = null

getIptablesRules = (callback) ->
	async.retry {times: 100, interval: 100}, (cb) ->
		try
			myIP = os.networkInterfaces().tether[0].address
			cb(null, myIP)
		catch err
			cb(err)
	, (err, myIP) ->
		throw 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}:8080"
			,
				table: 'nat'
				rule: "TETHER -p tcp --dport 443 -j DNAT --to-destination #{myIP}:8080"
			,
				table: 'nat'
				rule: "TETHER -p udp --dport 53 -j DNAT --to-destination #{myIP}:53"
		]


startServer = (wifi) ->
	wifi.getNetworks (err, list) ->
		throw err if err?
		ssidList = list
		wifi.openHotspot ssid, passphrase, (err) ->
			throw err if err?
			console.log("Hotspot enabled")
			dnsServer = spawn('named', ['-f'])
			getIptablesRules (err, iptablesRules) ->
				iptables.appendMany iptablesRules, (err) ->
					throw err if err?
					console.log("Captive portal enabled")
					server = app.listen port, ->
						console.log("Server listening")

console.log("Starting node connman app")
manageConnection = (retryCallback) ->
	connman.init (err) ->
		if err?
			console.log(err)
			retryCallback(err)
		console.log("Connman initialized")
		connman.initWiFi (err, wifi, properties) ->
			if err?
				console.log(err)
				retryCallback(err)
			console.log("WiFi initialized")

			app.use(bodyParser())
			app.use(express.static(__dirname + '/public'))
			app.get '/ssids', (req, res) ->
				res.send(ssidList)
{'fraction_non_alphanumeric': 0.15344872126065617, 'fraction_numerical': 0.014466546112115732, 'mean_word_length': 3.4352806414662083, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 30, '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': '4389786', 'n_tokens_mistral': 1619, 'n_tokens_neox': 1376, 'n_words': 353}