📜 ⬆️ ⬇️

Own VPN client on JavaScript. Part 4 - Configs Component

PS Each part is a part, by itself it does not make sense to get the necessary context and not to experience cognitive dissonance from the lack of so necessary blocks of text, start reading from part 1

Configs - Component responsible for storing and loading OpenVPN configs.

Folder structure

configs │ │ configs.json //     │ get.js │ index.js │ load.js │ parse-configs.js │ └───base //         ovpn empty.file //             


parse-configs.js - Function for parsing configs from API .
')
Example

I think, looking closer, you will understand that the logic of sorting is obvious.

 const body = ',,,3,4,6,8,9,6' const configs = parseConfigs(body) configs /* [{ "": 3, "": 4, "": 6 }, { "": 8, "": 9, "": 6 }] */ 

The contents of the parse-configs.js file .

Code
 module.exports = body => body .replace(/[^#]+/, '') .replace(/#/, '') .replace(/\r\n/gi, ',') //     .split(',') //     .reduce((ctx, elem, index) => { if (index > 14) { //   const key = ctx.name_tmp[ctx.config_name_index] ctx.config_tmp[key] = elem ctx.config_name_index++ if (ctx.config_name_index > 14) { ctx.configs.push(ctx.config_tmp) ctx.config_tmp = {} ctx.config_name_index = 0 } } else { //  14      : ping, , etc ctx.name_tmp.push(elem) } return ctx }, { name_tmp: [], config_name_index: 0, config_tmp: {}, configs: [] }).configs 


load.js - The function for accessing the API and creating configs files.

Content of load.js file.

Code
 const request = require('request') , fs = require('fs') , Base64 = require(`js-base64`).Base64 , parseConfigs = require('./parse-configs') module.exports = () => new Promise((resolve, reject) => { request(`http://130.158.6.57/api/iphone/`, (err, res, body) => { if (err || res.statusCode != 200) { return reject() } const configs = parseConfigs(body).map(config => { //      base64,    const decode_config = Base64.decode(config.OpenVPN_ConfigData_Base64) .replace(/dev tun/g, `dev tap`) , path = `${__dirname}/base/${config.HostName}.ovpn` fs.writeFileSync(path, decode_config) delete config.OpenVPN_ConfigData_Base64 config.path = path return config }) fs.writeFileSync(`${__dirname}/configs.json`, JSON.stringify(configs)) //      resolve(configs.length) }) }) 


get.js - Function for searching among downloaded configs.

The contents of the get.js file.

Code
 const fs = require('fs') , Base64 = require(`js-base64`).Base64 module.exports = ({ CountryLong = '-' , Ping = '-' , NumVpnSessions = '-' , Score = '-' , Speed = '-' , Uptime = '-' , TotalUsers = '-' , TotalTraffic = '-' }) => { try { //   const configs = fs.readFileSync(`${__dirname}/configs.json`, { encoding: 'utf8' }) //   ,       //     ,   //      return JSON.parse(configs) .filter(conf => (conf.CountryLong == CountryLong || CountryLong == '-') && (parseInt(conf.Ping) < parseInt(Ping) || Ping == '-') && (parseInt(conf.NumVpnSessions) > parseInt(NumVpnSessions) || NumVpnSessions == '-') && (parseInt(conf.Score) > parseInt(Score) || Score == '-') && (parseInt(conf.Speed) > parseInt(Speed) || Speed == '-') && (parseInt(conf.Uptime) > parseInt(Uptime) || Uptime == '-') && (parseInt(conf.TotalUsers) > parseInt(TotalUsers) || TotalUsers == '-') && (parseInt(conf.TotalTraffic) > parseInt(TotalTraffic) || TotalTraffic == '-') ) } catch (e) { return [] } } 


index.js - An object with get and load methods.

Contents of the index.js file.

Code
 module.exports = { load: require('./load'), get: require('./get') } 



Application
Application Usage: / app / Configs .

API

Interface component Configs.

 const Configs = require('./../../app/components/configs') Configs.load().then(length => { console.log(` : ${length}`) const search = Configs.get({ CountryLong: 'Japan' , Ping: 22 // 1-22 ms , NumVpnSessions: '-' // empty , Score: '-' // empty , Speed: 13311 // bit , Uptime: 3600000 // ms , TotalUsers: '-' // empty , TotalTraffic: '-' // empty }) console.log(` : ${search.length}`) }, () => { console.log(`  `) }) 

Test
Test version: / app_test / Configs .

image

Search object configs.
KeyType ofDescription
CountrylongStringCountry VPN server
PingNumberInformation delivery time (in milliseconds)
NumVpnSessionsNumberActive sessions
ScoreNumberQuality of connection
SpeedNumberConnection speed (in bytes)
UptimeNumberServer uptime (in milliseconds)
TotalusersNumberTotal connections while running
TotalTrafficNumberTotal traffic on the server during operation (in bytes)

5 part - Vpn component


VPN   JavaScript by JSus

Source: https://habr.com/ru/post/429432/


All Articles