Skip to content

Commit

Permalink
feat: server mode sets host: 0.0.0.0
Browse files Browse the repository at this point in the history
- Sets `host: 0.0.0.0` via env var
- Replaces the "0.0.0.0" in detected app URL with the user's LAN IP

Closes #16
  • Loading branch information
psychedelicious committed Dec 22, 2024
1 parent 39a3edf commit db69f96
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 11 deletions.
21 changes: 21 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"@electron/fuses": "^1.8.0",
"@invoke-ai/eslint-config-react": "^0.0.14",
"@invoke-ai/prettier-config-react": "^0.0.7",
"@types/ip": "^1.1.3",
"@types/react": "^18.3.12",
"@types/react-dom": "^18.3.1",
"@typescript-eslint/eslint-plugin": "^5.62.0",
Expand Down Expand Up @@ -87,6 +88,7 @@
"electron-context-menu": "^4.0.4",
"electron-squirrel-startup": "^1.0.1",
"electron-store": "^10.0.0",
"ip": "^2.0.1",
"linkify-react": "^4.2.0",
"linkifyjs": "^4.2.0",
"lodash-es": "^4.17.21",
Expand Down
25 changes: 15 additions & 10 deletions src/main/invoke-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { type ChildProcess, execFile } from 'child_process';
import { BrowserWindow, ipcMain, shell } from 'electron';
import type Store from 'electron-store';
import fs from 'fs/promises';
import ip from 'ip';
import { join } from 'path';
import { assert } from 'tsafe';

Expand Down Expand Up @@ -73,12 +74,14 @@ export class InvokeManager {
this.log.info('Preparing first run of this install - may take a minute or two...\r\n');
}

const invokeProcess = execFile(dirDetails.invokeExecPath, [], {
env: {
...process.env,
INVOKEAI_ROOT: location,
},
});
const env: NodeJS.ProcessEnv = { ...process.env, INVOKEAI_ROOT: location };

// If server mode is enabled, set the host to 0.0.0.0 to enable LAN access
if (this.store.get('serverMode')) {
env.INVOKEAI_HOST = '0.0.0.0';
}

const invokeProcess = execFile(dirDetails.invokeExecPath, [], { env });
this.process = invokeProcess;

invokeProcess.on('spawn', () => {
Expand Down Expand Up @@ -141,13 +144,15 @@ export class InvokeManager {
invokeProcess?.stderr?.off('data', urlWatcher.checkForMatch);
invokeProcess?.stdout?.off('data', urlWatcher.checkForMatch);

// Open the window only when _not_ in server mode
// If running with `host: 0.0.0.0`, replace with the local IP address
const urlProcessed = url.replace('0.0.0.0', ip.address());

// If server mode is enabled, don't open the window
if (!this.store.get('serverMode')) {
this.createWindow(url);
this.createWindow(urlProcessed);
}

// Update the status to running
this.updateStatus({ type: 'running', data: { url } });
this.updateStatus({ type: 'running', data: { url: urlProcessed } });

if (isFirstRun) {
// This is the first run after an install or update - remove the first run marker
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export const SettingsModalServerMode = memo(() => {
<Checkbox isChecked={serverMode} onChange={onChange} />
</Flex>
<FormHelperText>
When enabled, the UI won&apos;t open when the app starts up. You must open the displayed URL in a web browser.
When enabled, the launcher will run Invoke in &quot;headless&quot; mode with no UI. You can access Invoke on any
computer on your local network at the displayed URL.
</FormHelperText>
</FormControl>
);
Expand Down

0 comments on commit db69f96

Please sign in to comment.