git ssb

1+

cel / wpa-conf-codec



Tree: 7a38947d54fe9264b5f5439a6d63342d00ccaae5

Files: 7a38947d54fe9264b5f5439a6d63342d00ccaae5 / lib / encode.js

2594 bytesRaw
1function encodeQuotedString(value) {
2 return '"' + value + '"'
3}
4
5function encodeSet(values) {
6 if (!values) return ''
7 if (!Array.isArray(values)) values = [values]
8 return values.join(' ')
9}
10
11var encodeValueFns = {
12 anonymous_identity: encodeQuotedString,
13 ca_cert: encodeQuotedString,
14 domain: encodeQuotedString,
15 client_cert: encodeQuotedString,
16 domain_suffix_match: encodeQuotedString,
17 excluded_ssid: encodeQuotedString,
18 imsi: encodeQuotedString,
19 milenage: encodeQuotedString,
20 provisioning_sp: encodeQuotedString,
21 realm: encodeQuotedString,
22 roaming_consortium: encodeQuotedString,
23 roaming_partner: encodeQuotedString,
24 username: encodeQuotedString,
25 engine_id: encodeQuotedString,
26 identity: encodeQuotedString,
27 key_id: encodeQuotedString,
28 password: encodeQuotedString,
29 pcsc: encodeQuotedString,
30 pin: encodeQuotedString,
31 private_key: encodeQuotedString,
32 private_key_passwd: encodeQuotedString,
33 ssid: encodeQuotedString,
34 phase1: encodeQuotedString,
35 phase2: encodeQuotedString,
36 psk: function (value) {
37 if (/[0-9a-f]{40}/i.test(value)) return value
38 else return encodeQuotedString(value)
39 },
40}
41
42function defaultEncodeValue(value) {
43 switch (typeof value) {
44 case 'object':
45 if (value === null)
46 return ''
47 if (Array.isArray(value)) {
48 return value.join(' ')
49 }
50 var items = []
51 for (var k in value) {
52 items.push(encodeProperty(k, value[k]))
53 }
54 return items.join(' ')
55 case 'string': return value
56 case 'number': return String(value)
57 case 'boolean': return value ? '1' : '0'
58 case 'undefined': return ''
59 default: throw new Error("weird value: " + value)
60 }
61}
62
63module.exports = function encodeConfig(conf) {
64 if (!conf) return ''
65 var lines = []
66 for (var k in conf) {
67 var value = conf[k]
68 if (k !== 'networks') {
69 lines.push(encodeProperty(k, value))
70 }
71 }
72 if (Array.isArray(conf.networks)) {
73 lines.push('')
74 conf.networks.forEach(function (network) {
75 lines.push(encodeNetwork(network), '')
76 })
77 }
78 return lines.join('\n')
79}
80
81function encodeNetwork(network) {
82 if (!network) return
83 var lines = []
84 if (network._comment) {
85 lines.push('#' + network._comment.replace(/\n/g, '\n#'))
86 }
87 lines.push('network={')
88 for (var k in network) {
89 if (k !== '_comment')
90 lines.push('\t' + encodeProperty(k, network[k]))
91 }
92 lines.push('}')
93 return lines.join('\n')
94}
95
96function encodeProperty(key, value) {
97 var encodeValue = encodeValueFns[key] || defaultEncodeValue
98 return key + '=' + encodeValue(value)
99}
100

Built with git-ssb-web