Add Config class and registration functionality to AgrarmonitorLib
This commit is contained in:
parent
17654a4def
commit
65a1ce3d7c
36
src/config.js
Normal file
36
src/config.js
Normal file
@ -0,0 +1,36 @@
|
||||
// Konfigurationsklasse für AgrarmonitorLib
|
||||
|
||||
|
||||
const { CookieJar } = require('tough-cookie');
|
||||
const fs = require('fs');
|
||||
|
||||
class Config {
|
||||
/**
|
||||
* @param {Object} options
|
||||
* @param {string} options.cookiePath - Pfad zum Cookie-Speicher
|
||||
* @param {string} options.username - Benutzername
|
||||
* @param {string} options.password - Passwort
|
||||
* @param {string} options.baseUrl - Basis-URL für Agrarmonitor
|
||||
*/
|
||||
constructor({ cookiePath, username, password, baseUrl }) {
|
||||
this.cookiePath = cookiePath;
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
this.baseUrl = baseUrl || 'https://admin7.agrarmonitor.de/';
|
||||
this.cookieJar = this.loadCookieJar();
|
||||
}
|
||||
|
||||
loadCookieJar() {
|
||||
try {
|
||||
if (fs.existsSync(this.cookiePath)) {
|
||||
const data = fs.readFileSync(this.cookiePath, 'utf8');
|
||||
return CookieJar.fromJSON(data);
|
||||
}
|
||||
} catch (e) {
|
||||
// Fehler beim Laden, leere Jar zurückgeben
|
||||
}
|
||||
return new CookieJar();
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Config;
|
||||
@ -1,9 +1,15 @@
|
||||
// AgrarmonitorLib Beispielcode
|
||||
const Config = require('./config');
|
||||
const registriert = require('./registriert');
|
||||
const registrieren = require('./registrieren');
|
||||
|
||||
function hello() {
|
||||
return "Hallo von AgrarmonitorLib!";
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
hello
|
||||
hello,
|
||||
Config,
|
||||
registriert,
|
||||
registrieren
|
||||
};
|
||||
|
||||
120
src/registrieren.js
Normal file
120
src/registrieren.js
Normal file
@ -0,0 +1,120 @@
|
||||
// Funktion zur Registrierung bei Agrarmonitor
|
||||
const axios = require('axios');
|
||||
const fs = require('fs');
|
||||
const { JSDOM } = require('jsdom');
|
||||
|
||||
/**
|
||||
* Registriert ein Gerät bei Agrarmonitor.
|
||||
* @param {Config} config - Instanz der Konfigurationsklasse
|
||||
* @param {string} agrarmonitorId - Die Agrarmonitor-ID (Firma)
|
||||
* @param {string} pcName - Der PC-Name für die Registrierung
|
||||
* @returns {Promise<boolean>} true wenn erfolgreich registriert, false wenn nicht
|
||||
*/
|
||||
async function registrieren(config, agrarmonitorId, pcName) {
|
||||
const cookieJar = config.cookieJar;
|
||||
|
||||
try {
|
||||
if (!agrarmonitorId || !pcName) {
|
||||
console.error('❌ AgrarmonitorID oder PC-Name fehlt');
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
// Schritt 1: Freischaltungsseite aufrufen und Nonce extrahieren
|
||||
const freischaltungResponse = await axios.get(config.baseUrl + 'freischaltung/', {
|
||||
maxRedirects: 5,
|
||||
jar: cookieJar,
|
||||
withCredentials: true
|
||||
});
|
||||
|
||||
const responseContent = freischaltungResponse.data;
|
||||
|
||||
// HTML parsen mit jsdom
|
||||
const dom = new JSDOM(responseContent);
|
||||
const document = dom.window.document;
|
||||
|
||||
// Nonce aus dem HTML extrahieren
|
||||
const nonceElement = document.getElementById('nonce');
|
||||
if (!nonceElement) {
|
||||
throw new Error('Nonce-Element nicht gefunden in der Freischaltungsseite');
|
||||
}
|
||||
|
||||
const nonce = nonceElement.getAttribute('value') || nonceElement.value || '';
|
||||
if (!nonce) {
|
||||
throw new Error('Nonce-Wert ist leer');
|
||||
}
|
||||
|
||||
// Schritt 2: Registrierungsanfrage senden
|
||||
const postRegister = {
|
||||
firma: agrarmonitorId,
|
||||
name: pcName,
|
||||
nonce: nonce
|
||||
};
|
||||
|
||||
const payload = JSON.stringify(postRegister);
|
||||
|
||||
const registerResponse = await axios.post(
|
||||
config.baseUrl + 'freischaltung/api/register.php',
|
||||
payload,
|
||||
{
|
||||
jar: cookieJar,
|
||||
withCredentials: true,
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
// Cookies speichern
|
||||
try {
|
||||
const cookieData = JSON.stringify(cookieJar.toJSON());
|
||||
fs.writeFileSync(config.cookiePath, cookieData, 'utf8');
|
||||
} catch (error) {
|
||||
console.error('❌ Fehler beim Speichern der Cookies:', error);
|
||||
}
|
||||
|
||||
const success = registerResponse.status >= 200 && registerResponse.status < 300;
|
||||
|
||||
if (success) {
|
||||
console.log(`✅ Agrarmonitor-Registrierung erfolgreich`);
|
||||
} else {
|
||||
console.log(`❌ Agrarmonitor-Registrierung fehlgeschlagen (Status: ${registerResponse.status})`);
|
||||
}
|
||||
|
||||
return success;
|
||||
|
||||
} catch (error) {
|
||||
// Bei HTTP-Fehlern
|
||||
if (error.response) {
|
||||
console.log(`❌ Agrarmonitor-Registrierung fehlgeschlagen (Status: ${error.response.status})`);
|
||||
|
||||
// Cookies auch bei Fehlern speichern
|
||||
try {
|
||||
const cookieData = JSON.stringify(cookieJar.toJSON());
|
||||
fs.writeFileSync(config.cookiePath, cookieData, 'utf8');
|
||||
} catch (saveError) {
|
||||
console.error('❌ Fehler beim Speichern der Cookies:', saveError);
|
||||
}
|
||||
|
||||
return false;
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ Fehler bei der Agrarmonitor-Registrierung:', error);
|
||||
|
||||
// Cookies auch bei Fehlern speichern
|
||||
try {
|
||||
const cookieData = JSON.stringify(cookieJar.toJSON());
|
||||
fs.writeFileSync(config.cookiePath, cookieData, 'utf8');
|
||||
} catch (saveError) {
|
||||
console.error('❌ Fehler beim Speichern der Cookies:', saveError);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = registrieren;
|
||||
56
src/registriert.js
Normal file
56
src/registriert.js
Normal file
@ -0,0 +1,56 @@
|
||||
// Funktion zur Prüfung der Agrarmonitor-Registrierung
|
||||
const axios = require('axios');
|
||||
const fs = require('fs');
|
||||
|
||||
/**
|
||||
* Prüft, ob ein Gerät bei Agrarmonitor registriert ist.
|
||||
* @param {Config} config - Instanz der Konfigurationsklasse
|
||||
* @returns {Promise<boolean>} true wenn registriert, false wenn nicht
|
||||
*/
|
||||
async function registriert(config) {
|
||||
const url = config.baseUrl;
|
||||
const cookieJar = config.cookieJar;
|
||||
try {
|
||||
// HTTP-Anfrage an Agrarmonitor-Startseite
|
||||
const response = await axios.get(url, {
|
||||
maxRedirects: 5,
|
||||
jar: cookieJar,
|
||||
withCredentials: true
|
||||
});
|
||||
|
||||
const pageContent = response.data || '';
|
||||
const hasRegistrationText = pageContent.includes('Neues Gerät registrieren');
|
||||
const isRegistered = !hasRegistrationText;
|
||||
|
||||
// Cookies speichern
|
||||
try {
|
||||
const cookieData = JSON.stringify(cookieJar.toJSON());
|
||||
fs.writeFileSync(config.cookiePath, cookieData, 'utf8');
|
||||
} catch (error) {
|
||||
console.error('❌ Fehler beim Speichern der Cookies:', error);
|
||||
}
|
||||
|
||||
return isRegistered;
|
||||
} catch (error) {
|
||||
if (error.response) {
|
||||
const pageContent = error.response.data || '';
|
||||
const hasRegistrationText = pageContent.includes('Neues Gerät registrieren');
|
||||
const isRegistered = !hasRegistrationText;
|
||||
|
||||
// Cookies auch bei Fehlern speichern
|
||||
try {
|
||||
const cookieData = JSON.stringify(cookieJar.toJSON());
|
||||
fs.writeFileSync(config.cookiePath, cookieData, 'utf8');
|
||||
} catch (error) {
|
||||
console.error('❌ Fehler beim Speichern der Cookies:', error);
|
||||
}
|
||||
|
||||
return isRegistered;
|
||||
} else {
|
||||
console.error('❌ Fehler bei der Agrarmonitor-Registrierungsprüfung:', error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = registriert;
|
||||
Loading…
Reference in New Issue
Block a user