• Das Erstellen neuer Accounts wurde ausgesetzt. Bei berechtigtem Interesse bitte Kontaktaufnahme über die üblichen Wege. Beste Grüße der Admin

Auf anderen Webserver einloggen und Cookie nutzen

Fohnbit

New member
Hallo!

Ich bin noch neu in node.js und möchte folgendes umsetzen:
Ich habe die Webapplication auf meinem Server:

Das ganze läuft.

Nun möchte ich über node.js die api davon nutzen.
In Github finde ich:
Javascript:
app.GET(util.BasePath+"/test-hash", handler.GetHashesChanges(db), handler.ValidSession)
    app.GET(util.BasePath+"/about", handler.AboutPage())
    app.GET(util.BasePath+"/_health", handler.Health())
    app.GET(util.BasePath+"/favicon", handler.Favicon())
    app.POST(util.BasePath+"/new-client", handler.NewClient(db), handler.ValidSession, handler.ContentTypeJson)
    app.POST(util.BasePath+"/update-client", handler.UpdateClient(db), handler.ValidSession, handler.ContentTypeJson)
    app.POST(util.BasePath+"/email-client", handler.EmailClient(db, sendmail, defaultEmailSubject, defaultEmailContent), handler.ValidSession, handler.ContentTypeJson)
    app.POST(util.BasePath+"/client/set-status", handler.SetClientStatus(db), handler.ValidSession, handler.ContentTypeJson)
    app.POST(util.BasePath+"/remove-client", handler.RemoveClient(db), handler.ValidSession, handler.ContentTypeJson)
    app.GET(util.BasePath+"/download", handler.DownloadClient(db), handler.ValidSession)
    app.GET(util.BasePath+"/wg-server", handler.WireGuardServer(db), handler.ValidSession, handler.NeedsAdmin)
    app.POST(util.BasePath+"/wg-server/interfaces", handler.WireGuardServerInterfaces(db), handler.ValidSession, handler.ContentTypeJson, handler.NeedsAdmin)
    app.POST(util.BasePath+"/wg-server/keypair", handler.WireGuardServerKeyPair(db), handler.ValidSession, handler.ContentTypeJson, handler.NeedsAdmin)
    app.GET(util.BasePath+"/global-settings", handler.GlobalSettings(db), handler.ValidSession, handler.NeedsAdmin)
    app.POST(util.BasePath+"/global-settings", handler.GlobalSettingSubmit(db), handler.ValidSession, handler.ContentTypeJson, handler.NeedsAdmin)
    app.GET(util.BasePath+"/status", handler.Status(db), handler.ValidSession)
    app.GET(util.BasePath+"/api/clients", handler.GetClients(db), handler.ValidSession)
    app.GET(util.BasePath+"/api/client/:id", handler.GetClient(db), handler.ValidSession)
    app.GET(util.BasePath+"/api/machine-ips", handler.MachineIPAddresses(), handler.ValidSession)
    app.GET(util.BasePath+"/api/suggest-client-ips", handler.SuggestIPAllocation(db), handler.ValidSession)
    app.POST(util.BasePath+"/api/apply-wg-config", handler.ApplyServerConfig(db, tmplDir), handler.ValidSession, handler.ContentTypeJson)
    app.GET(util.BasePath+"/wake_on_lan_hosts", handler.GetWakeOnLanHosts(db), handler.ValidSession)
    app.POST(util.BasePath+"/wake_on_lan_host", handler.SaveWakeOnLanHost(db), handler.ValidSession, handler.ContentTypeJson)
    app.DELETE(util.BasePath+"/wake_on_lan_host/:mac_address", handler.DeleteWakeOnHost(db), handler.ValidSession, handler.ContentTypeJson)
    app.PUT(util.BasePath+"/wake_on_lan_host/:mac_address", handler.WakeOnHost(db), handler.ValidSession, handler.ContentTypeJson)

Ich muss mich aber zuvor einloggen, sonst klappt es nicht.
Das Einloggen scheint zu funktionieren, da ich "Login Successfully" bei console.log(loginform.text); zurück bekomme.

Aber beim Post von "NewClient" erhalte ich:
Code:
(node:64820) UnhandledPromiseRejectionWarning: Error: socket hang up
    at connResetException (internal/errors.js:609:14)
    at Socket.socketOnEnd (_http_client.js:458:23)
    at Socket.emit (events.js:326:22)
    at endReadableNT (_stream_readable.js:1241:12)
    at processTicksAndRejections (internal/process/task_queues.js:84:21)
(node:64820) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:64820) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Liegt es eventuell daran, dasirgendwelche Cookies fehlen?

Soweit habe ich es:
Javascript:
const wireguard_ui = async () => {
                    let loginform = await superagent
                        .post(server + '/login')
                        .send({
                            "username": "controller",
                            "password": "-- hide --",
                            "rememberMe": false
                        })
                        .set('Cotent-Type', 'application/json');

                    console.log(loginform.text);
                   // bis hier klappt es

                    const generatedPkey = child_process.execSync('wg genkey').toString().trim();
                    const generatedPubkey = child_process.execSync(
                        "echo '" + generatedPkey + "' | wg pubkey"
                    ).toString().trim();

                    let new_client = await superagent
                        .post(server + '/new-client')
                        .send({
                            "id": host,
                            "private_key": generatedPkey,
                            "public_key": generatedPubkey,
                            "name": host,
                            "email": "",
                            "allocated_ips": [
                                ip
                            ],
                            "allowed_ips": [
                                "10.11.0.0/16"
                            ],
                            "extra_allowed_ips": [],
                            "use_server_dns": false,
                            "enabled": true
                        })
                        .set('Cotent-Type', 'application/json');
                    console.log(new_client.text);
                    res.end(new_client.text);
                };

                wireguard_ui();
 
Habs nun doch selber gefunden:
Cookie vom Login speichern und in neuen Post nutzen:

Javascript:
const wireguard_ui = async () => {
                    let loginform = await superagent
                        .post(server + '/login')
                        .send({
                            "username": "controller",
                            "password": "-- hide --n",
                            "rememberMe": false
                        })
                        .set('Cotent-Type', 'application/json');

                    // console.log(loginform.text);
                    var cookie = loginform.headers['set-cookie'];

                    const generatedPkey = child_process.execSync('wg genkey').toString().trim();
                    const generatedPubkey = child_process.execSync(
                        "echo '" + generatedPkey + "' | wg pubkey"
                    ).toString().trim();

                    let new_client = await superagent
                        .post(server + '/new-client')
                        .set('Cookie', cookie)
                        .send({
                            "id": host,
                            "private_key": generatedPkey,
                            "public_key": generatedPubkey,
                            "name": host,
                            "email": "",
                            "allocated_ips": [
                                ip
                            ],
                            "allowed_ips": [
                                "10.11.0.0/16"
                            ],
                            "extra_allowed_ips": [],
                            "use_server_dns": false,
                            "enabled": true
                        })
                        .set('Cotent-Type', 'application/json');
                    // console.log(new_client.text);
                    res.end(new_client.text);
 
Zurück
Oben