-
-
Notifications
You must be signed in to change notification settings - Fork 318
Add network wake functionality to external integrations #2848
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a585e5d
Add network wake functionality to external integrations
e7fd19a
Refactor Wake-on-LAN integration to validate options and handle error…
f7f4212
Enhance Wake-on-LAN integration to validate IPv4 address and enforce …
a5df4b3
Fix default source port in Wake-on-LAN integration to use 0 instead of 9
25e29b0
Add error handling tests for Wake-on-LAN integration
21d5330
Refactor test setup to use sinon sandbox for improved isolation
0c0ef5d
Add network wake permissions and summary component to integration
7b7f8f3
Refactor validation tests for network_wake and fix import statement f…
ca0fd33
Update German translations for network wake integration and fix compo…
1d9c787
Update API documentation for network wake integration and refine erro…
6f4dab9
Merge branch 'master' into master
Pierre-Gilles File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
front/src/routes/integration/all/external-integration/components/NetworkWakeSummary.jsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import { Text } from 'preact-i18n'; | ||
|
|
||
| const NetworkWakeSummary = ({ networkWake }) => { | ||
| if (!networkWake) { | ||
| return null; | ||
| } | ||
|
|
||
| return ( | ||
| <div class="mb-4"> | ||
| <h4> | ||
| <i class="fe fe-power mr-1" /> | ||
| <Text id="integration.externalIntegration.install.networkWakeTitle" /> | ||
| </h4> | ||
|
|
||
| <p class="text-muted small mb-0"> | ||
| <Text id="integration.externalIntegration.install.networkWakeText" /> | ||
| </p> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export default NetworkWakeSummary; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
server/lib/external-integration/externalIntegration.wakeOnLan.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| const dgram = require('dgram'); | ||
| const net = require('net'); | ||
|
|
||
| const { BadParameters, ForbiddenError } = require('../../utils/coreErrors'); | ||
|
|
||
| const DEFAULT_SOURCE_PORT = 0; | ||
| const DEFAULT_PORT = 9; | ||
| const DEFAULT_ADDRESS = '255.255.255.255'; | ||
|
|
||
| /** | ||
| * @description Normalize a MAC address to a Buffer. | ||
| * @param {string} mac - MAC address in format xx:xx:xx:xx:xx:xx. | ||
| * @returns {Buffer<ArrayBuffer>} - MAC address as a Buffer. | ||
| * @example | ||
| * normalizeMac('00:11:22:33:44:55'); | ||
| */ | ||
| function normalizeMac(mac) { | ||
| if (typeof mac !== 'string') { | ||
| throw new BadParameters('MAC address must be a string'); | ||
| } | ||
|
|
||
| const normalized = mac.replace(/[:-]/g, ''); | ||
|
|
||
| if (!/^[0-9a-fA-F]{12}$/.test(normalized)) { | ||
| throw new BadParameters('Invalid MAC address'); | ||
| } | ||
|
|
||
| return Buffer.from(normalized, 'hex'); | ||
| } | ||
|
|
||
| /** | ||
| * @description Build a Wake-on-LAN magic packet. | ||
| * @param {string} mac - MAC address in format xx:xx:xx:xx:xx:xx. | ||
| * @returns {Buffer<ArrayBuffer>} Magic packet as a Buffer. | ||
| * @example | ||
| * buildMagicPacket('00:11:22:33:44:55'); | ||
| */ | ||
| function buildMagicPacket(mac) { | ||
| const macBuffer = normalizeMac(mac); | ||
|
|
||
| return Buffer.concat([Buffer.alloc(6, 0xff), ...Array.from({ length: 16 }, () => macBuffer)]); | ||
| } | ||
|
|
||
| /** | ||
| * @description Send a Wake-on-LAN magic packet to a target device. | ||
| * @param {object} service - The external integration service (plain object). | ||
| * @param {object} service.manifest - The external integration manifest. | ||
| * @param {boolean} [service.manifest.network_wake] - Whether Wake-on-LAN is allowed for this integration. | ||
| * @param {object} options - Wake-on-LAN options. | ||
| * @param {string} options.mac - Target MAC address. | ||
| * @param {string} [options.address] - Destination/broadcast address. | ||
| * @param {number} [options.port] - Destination UDP port. | ||
| * @param {number} [options.sourcePort] - Source UDP port. | ||
| * @returns {Promise<void>} Promise that resolves when the magic packet is sent. | ||
| * @example | ||
| * await gladys.externalIntegration.wakeOnLan(service, { mac: '00:11:22:33:44:55' }); | ||
| */ | ||
| async function wakeOnLan(service, options) { | ||
| if (!options || typeof options !== 'object' || Array.isArray(options)) { | ||
| throw new BadParameters('Invalid Wake-on-LAN options'); | ||
| } | ||
|
|
||
| const { mac, address = DEFAULT_ADDRESS, port = DEFAULT_PORT, sourcePort = DEFAULT_SOURCE_PORT } = options; | ||
|
|
||
| if (!net.isIPv4(address)) { | ||
| throw new BadParameters('Invalid IPv4 address'); | ||
| } | ||
|
|
||
| if (!Number.isInteger(port) || port < 1 || port > 65535) { | ||
| throw new BadParameters('port: must be an integer between 1 and 65535'); | ||
| } | ||
|
cursor[bot] marked this conversation as resolved.
|
||
|
|
||
| if (!Number.isInteger(sourcePort) || sourcePort < 0 || sourcePort > 65535) { | ||
| throw new BadParameters('sourcePort: must be an integer between 0 and 65535'); | ||
| } | ||
|
|
||
| if (!service.manifest || service.manifest.network_wake !== true) { | ||
| throw new ForbiddenError('Wake-on-LAN is not allowed for this integration'); | ||
| } | ||
|
cursor[bot] marked this conversation as resolved.
|
||
| const payload = buildMagicPacket(mac); | ||
|
cursor[bot] marked this conversation as resolved.
|
||
|
|
||
| const socket = dgram.createSocket({ | ||
| type: 'udp4', | ||
| reuseAddr: true, | ||
| }); | ||
|
|
||
| await new Promise((resolve, reject) => { | ||
| let settled = false; | ||
|
|
||
| const fail = (error) => { | ||
| if (settled) { | ||
| return; | ||
| } | ||
|
|
||
| settled = true; | ||
|
|
||
| socket.close(() => { | ||
| reject(error); | ||
| }); | ||
| }; | ||
|
|
||
| socket.once('error', fail); | ||
|
|
||
| socket.bind(sourcePort, () => { | ||
| try { | ||
| socket.setBroadcast(true); | ||
|
|
||
| socket.send(payload, port, address, (error) => { | ||
| if (settled) { | ||
| return; | ||
| } | ||
|
|
||
| if (error) { | ||
| fail(error); | ||
| return; | ||
| } | ||
|
|
||
| settled = true; | ||
|
|
||
| socket.close(() => { | ||
| resolve(); | ||
| }); | ||
| }); | ||
| } catch (error) { | ||
| fail(error); | ||
| } | ||
| }); | ||
|
vincentBesseau marked this conversation as resolved.
|
||
| }); | ||
| } | ||
|
|
||
| module.exports = { | ||
| wakeOnLan, | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| const { expect } = require('chai'); | ||
|
|
||
| const getRoutes = require('../../api/routes'); | ||
| const { buildSupervisor } = require('../lib/external-integration/testUtils.test'); | ||
|
|
||
| describe('API routes', () => { | ||
| it('should register the Wake-on-LAN external integration route', () => { | ||
| const { externalIntegration } = buildSupervisor(); | ||
|
|
||
| const mockedGladys = { | ||
| externalIntegration, | ||
| service: { | ||
| getServices: () => [], | ||
| }, | ||
| }; | ||
|
|
||
| const routes = getRoutes(mockedGladys); | ||
|
|
||
| const route = routes['post /api/integration/v1/network/wake']; | ||
|
|
||
| expect(route).to.not.equal(undefined); | ||
| expect(route.authenticated).to.equal(false); | ||
| expect(route.externalIntegrationAuth).to.equal(true); | ||
| expect(route.controller).to.be.a('function'); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.