#!/usr/bin/env node

'use strict'

require('../lib/prototype');
var Http = require('../lib/http');
var net = require('net')
var os = require('os')

var processArgs = require('../lib/args')
var argMapping = processArgs(process.argv.slice(2))

for (var key in argMapping) {
	switch (key) {
		case "-?":
		case "/?":
		case "-h":
		case "--help":
			console.log(`
--------------------
Node.js网卡测试
--------------------

当系统中存在多张网卡时，同时测试这些网卡哪个有网络。
'`);
			process.exit(0);
	}
}

;
(async () => {
	var interfaces = os.networkInterfaces()
	var InterfaceMap = []
	for (let inter in interfaces) {
		var items = interfaces[inter].filter(n => !n.internal && net.isIPv4(n.address))
		if (items && items.length > 0) {
			InterfaceMap.push({ name: inter, ip: items[0].address })
		}
	}
	var promises = []
	for (let { name, ip } of InterfaceMap) {
		promises.push(
			Http.Request({
				requrl: `https://www.baidu.com/`,
				method: 'GET',
				opts: {
					localAddress: ip
				},
				rejectUnauthorized: false,
				timeout: 3000,
			})
		)
	}
	var ResultList = await Promise.all(promises)
	for (let i = 0; i < ResultList.length; i++) {
		let { statusCode: code, time } = ResultList[i];
		if (code >= 200 && code < 400) {
			console.log(`${InterfaceMap[i].name}\t\t --> ${time}ms`)
		}
	}
	// var result = await http.DownloadParallel({
	// 	requrl,
	// 	saveurl,
	// 	cpus,
	// 	seconds,
	// 	headers,
	// 	log: true,
	// });
	// // var result = await http.Download(requrl, saveas, maxlen);

	// console.log(result)
	// if (result.receive == result.maxLength) {
	// 	console.log(`文件保存到： ${saveurl}`)
	// }
})();


