freeschema 1.0.4 → 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/bin/freeschema.js +46 -1
  2. package/package.json +1 -1
package/bin/freeschema.js CHANGED
@@ -114,6 +114,44 @@ function generateJwt() {
114
114
  return crypto.randomBytes(32).toString('hex');
115
115
  }
116
116
 
117
+ // Mosquitto 2.0 $7$ PBKDF2-HMAC-SHA512 password entry
118
+ function generateMosquittoPassword(username, password) {
119
+ const iterations = 101;
120
+ const salt = crypto.randomBytes(12);
121
+ const hash = crypto.pbkdf2Sync(password, salt, iterations, 64, 'sha512');
122
+ return `${username}:$7$${iterations}$${salt.toString('base64')}$${hash.toString('base64')}\n`;
123
+ }
124
+
125
+ function ensureMosquittoConfig(cwd) {
126
+ const dir = (sub) => path.join(cwd, 'mosquitto', sub);
127
+ ['config', 'data', 'log'].forEach(d => {
128
+ if (!fs.existsSync(dir(d))) fs.mkdirSync(dir(d), { recursive: true });
129
+ });
130
+
131
+ const confDest = path.join(dir('config'), 'mosquitto.conf');
132
+ if (!fs.existsSync(confDest)) {
133
+ const confSrc = path.join(TEMPLATES_DIR, 'mosquitto', 'config', 'mosquitto.conf');
134
+ if (fs.existsSync(confSrc)) fs.copyFileSync(confSrc, confDest);
135
+ else fs.writeFileSync(confDest, [
136
+ 'listener 1883', 'allow_anonymous false',
137
+ 'password_file /mosquitto/config/password.txt', '',
138
+ 'listener 9001', 'protocol websockets', 'allow_anonymous false', '',
139
+ 'persistence true', 'persistence_location /mosquitto/data/',
140
+ 'log_dest file /mosquitto/log/mosquitto.log', '',
141
+ ].join('\n'));
142
+ console.log(' Created mosquitto/config/mosquitto.conf');
143
+ }
144
+
145
+ const pwDest = path.join(dir('config'), 'password.txt');
146
+ if (!fs.existsSync(pwDest)) {
147
+ const env = readEnv();
148
+ const mqttUser = env.MQTT_USERNAME || 'freeschema';
149
+ const mqttPass = env.MQTT_PASSWORD || 'freeschema';
150
+ fs.writeFileSync(pwDest, generateMosquittoPassword(mqttUser, mqttPass));
151
+ console.log(' Created mosquitto/config/password.txt');
152
+ }
153
+ }
154
+
117
155
  function fmtBytes(b) {
118
156
  if (b >= 1073741824) return (b / 1073741824).toFixed(1) + ' GB';
119
157
  if (b >= 1048576) return (b / 1048576).toFixed(1) + ' MB';
@@ -340,6 +378,10 @@ async function cmdInit() {
340
378
  const mqttPass = await prompt(' MQTT password', readEnvVar(envPath, 'MQTT_PASSWORD') || 'freeschema');
341
379
  setEnvVar(envPath, 'MQTT_USERNAME', mqttUser);
342
380
  setEnvVar(envPath, 'MQTT_PASSWORD', mqttPass);
381
+ // Write password.txt with correct hash for the chosen credentials
382
+ const pwDir = path.join(target, 'mosquitto', 'config');
383
+ if (!fs.existsSync(pwDir)) fs.mkdirSync(pwDir, { recursive: true });
384
+ fs.writeFileSync(path.join(pwDir, 'password.txt'), generateMosquittoPassword(mqttUser, mqttPass));
343
385
  console.log(' MQTT configured (local) ✓');
344
386
  } else {
345
387
  console.log('\n External MQTT broker:');
@@ -476,7 +518,10 @@ function cmdStart() {
476
518
  const profiles = [];
477
519
  const flags = args.slice(1);
478
520
  if (flags.includes('--local-db') || flags.includes('--db')) profiles.push('--profile', 'local-db');
479
- if (flags.includes('--local-mqtt') || flags.includes('--mqtt')) profiles.push('--profile', 'local-mqtt');
521
+ if (flags.includes('--local-mqtt') || flags.includes('--mqtt')) {
522
+ profiles.push('--profile', 'local-mqtt');
523
+ ensureMosquittoConfig(cwd);
524
+ }
480
525
 
481
526
  const seedIdx = args.indexOf('--seed');
482
527
  const seedFile = seedIdx !== -1 ? args[seedIdx + 1] : null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "freeschema",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
4
4
  "description": "FreeSchema — self-hosted deployment CLI",
5
5
  "keywords": [
6
6
  "freeschema",