-
-
Notifications
You must be signed in to change notification settings - Fork 35
Improve LAN IP Detection and User Selection for WebSocket Server #35
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -9,6 +9,7 @@ export const Route = createFileRoute('/settings')({ | |||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| function SettingsPage() { | ||||||||||||||||||||||||
| const [ip, setIp] = useState(''); | ||||||||||||||||||||||||
| const [availableIps, setAvailableIps] = useState<string[]>([]); | ||||||||||||||||||||||||
| const [frontendPort, setFrontendPort] = useState(String(CONFIG.FRONTEND_PORT)); | ||||||||||||||||||||||||
| const [invertScroll, setInvertScroll] = useState(CONFIG.MOUSE_INVERT); | ||||||||||||||||||||||||
| const [sensitivity, setSensitivity] = useState(CONFIG.MOUSE_SENSITIVITY); | ||||||||||||||||||||||||
|
|
@@ -61,7 +62,16 @@ function SettingsPage() { | |||||||||||||||||||||||
| const data = JSON.parse(event.data); | ||||||||||||||||||||||||
| if (data.type === 'server-ip' && data.ip) { | ||||||||||||||||||||||||
| console.log('Auto-detected IP:', data.ip); | ||||||||||||||||||||||||
| setIp(data.ip); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // Only override if user hasn't set one, or if it's default | ||||||||||||||||||||||||
| const stored = localStorage.getItem('rein_ip'); | ||||||||||||||||||||||||
| if (!stored || stored === 'localhost' || stored === '127.0.0.1' || stored === window.location.hostname) { | ||||||||||||||||||||||||
| setIp(data.ip); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| if (data.ips && Array.isArray(data.ips)) { | ||||||||||||||||||||||||
| setAvailableIps(data.ips); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| socket.close(); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } catch (e) { | ||||||||||||||||||||||||
|
|
@@ -95,6 +105,22 @@ function SettingsPage() { | |||||||||||||||||||||||
| value={ip} | ||||||||||||||||||||||||
| onChange={(e) => setIp(e.target.value)} | ||||||||||||||||||||||||
| /> | ||||||||||||||||||||||||
| {availableIps.length > 0 && ( | ||||||||||||||||||||||||
| <div className="pt-2"> | ||||||||||||||||||||||||
| <span className="text-xs opacity-50 block mb-1">Detected LAN IPs:</span> | ||||||||||||||||||||||||
| <div className="flex flex-wrap gap-2"> | ||||||||||||||||||||||||
| {availableIps.map(addr => ( | ||||||||||||||||||||||||
| <button | ||||||||||||||||||||||||
| key={addr} | ||||||||||||||||||||||||
| className="btn btn-xs btn-outline" | ||||||||||||||||||||||||
| onClick={() => setIp(addr)} | ||||||||||||||||||||||||
| > | ||||||||||||||||||||||||
|
Comment on lines
+113
to
+117
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add explicit Per the Biome lint finding ( Proposed fix <button
key={addr}
+ type="button"
className="btn btn-xs btn-outline"
onClick={() => setIp(addr)}
>📝 Committable suggestion
Suggested change
🧰 Tools🪛 Biome (2.3.13)[error] 111-115: Provide an explicit type prop for the button element. The default type of a button is submit, which causes the submission of a form when placed inside a (lint/a11y/useButtonType) 🤖 Prompt for AI Agents |
||||||||||||||||||||||||
| {addr} | ||||||||||||||||||||||||
| </button> | ||||||||||||||||||||||||
| ))} | ||||||||||||||||||||||||
| </div> | ||||||||||||||||||||||||
| </div> | ||||||||||||||||||||||||
| )} | ||||||||||||||||||||||||
| <label className="label"> | ||||||||||||||||||||||||
| <span className="label-text-alt opacity-50">This Computer's LAN IP</span> | ||||||||||||||||||||||||
| </label> | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -5,26 +5,32 @@ import fs from 'fs'; | |||||||||||||||||||||||||
| import { Server, IncomingMessage } from 'http'; | ||||||||||||||||||||||||||
| import { Socket } from 'net'; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| // Helper to find LAN IP | ||||||||||||||||||||||||||
| function getLocalIp() { | ||||||||||||||||||||||||||
| // Helper to find LAN IPs | ||||||||||||||||||||||||||
| function getLocalIps() { | ||||||||||||||||||||||||||
| const nets = os.networkInterfaces(); | ||||||||||||||||||||||||||
| const results: string[] = []; | ||||||||||||||||||||||||||
| for (const name of Object.keys(nets)) { | ||||||||||||||||||||||||||
| for (const net of nets[name]!) { | ||||||||||||||||||||||||||
| if (net.family === 'IPv4' && !net.internal) { | ||||||||||||||||||||||||||
| return net.address; | ||||||||||||||||||||||||||
| results.push(net.address); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| return 'localhost'; | ||||||||||||||||||||||||||
| return results.length > 0 ? results : ['localhost']; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| export function createWsServer(server: Server) { | ||||||||||||||||||||||||||
| const wss = new WebSocketServer({ noServer: true }); | ||||||||||||||||||||||||||
| const inputHandler = new InputHandler(); | ||||||||||||||||||||||||||
| const LAN_IP = getLocalIp(); | ||||||||||||||||||||||||||
| const LAN_IPS = getLocalIps(); | ||||||||||||||||||||||||||
| // Heuristic: Prefer 192.168.x.x, then 10.x.x.x, then first available | ||||||||||||||||||||||||||
| const BEST_IP = LAN_IPS.find(ip => ip.startsWith('192.168.')) | ||||||||||||||||||||||||||
| || LAN_IPS.find(ip => ip.startsWith('10.')) | ||||||||||||||||||||||||||
| || LAN_IPS[0]; | ||||||||||||||||||||||||||
|
Comment on lines
+26
to
+29
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing 172.16.0.0/12 private subnet in heuristic. The RFC 1918 private range Proposed fix const BEST_IP = LAN_IPS.find(ip => ip.startsWith('192.168.'))
|| LAN_IPS.find(ip => ip.startsWith('10.'))
+ || LAN_IPS.find(ip => {
+ const second = parseInt(ip.split('.')[1], 10);
+ return ip.startsWith('172.') && second >= 16 && second <= 31;
+ })
|| LAN_IPS[0];📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| console.log(`WebSocket Server initialized (Upgrade mode)`); | ||||||||||||||||||||||||||
| console.log(`WS LAN IP: ${LAN_IP}`); | ||||||||||||||||||||||||||
| console.log(`WS LAN IPS: ${LAN_IPS.join(', ')}`); | ||||||||||||||||||||||||||
| console.log(`Selected Best IP: ${BEST_IP}`); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| server.on('upgrade', (request: IncomingMessage, socket: Socket, head: Buffer) => { | ||||||||||||||||||||||||||
| const pathname = request.url; | ||||||||||||||||||||||||||
|
|
@@ -39,15 +45,15 @@ export function createWsServer(server: Server) { | |||||||||||||||||||||||||
| wss.on('connection', (ws: WebSocket) => { | ||||||||||||||||||||||||||
| console.log('Client connected to /ws'); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| ws.send(JSON.stringify({ type: 'connected', serverIp: LAN_IP })); | ||||||||||||||||||||||||||
| ws.send(JSON.stringify({ type: 'connected', serverIp: BEST_IP, ips: LAN_IPS })); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| ws.on('message', async (data: string) => { | ||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||
| const raw = data.toString(); | ||||||||||||||||||||||||||
| const msg = JSON.parse(raw); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if (msg.type === 'get-ip') { | ||||||||||||||||||||||||||
| ws.send(JSON.stringify({ type: 'server-ip', ip: LAN_IP })); | ||||||||||||||||||||||||||
| ws.send(JSON.stringify({ type: 'server-ip', ip: BEST_IP, ips: LAN_IPS })); | ||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.