Skip to content

Commit

Permalink
Bind to all interfaces for OSC, but only accept packets from the loca…
Browse files Browse the repository at this point in the history
…l host
  • Loading branch information
SenkyDragon committed Aug 10, 2023
1 parent 15ce2df commit acdc773
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
29 changes: 20 additions & 9 deletions src/main/OscConnection.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import osc from 'osc';
import type {OscMessage} from 'osc';
import dgram from 'dgram';
import dgram, {type RemoteInfo} from 'dgram';
import EventEmitter from "events"
import type TypedEmitter from "typed-emitter"
import {
Expand All @@ -9,7 +9,7 @@ import {
OSCQAccess,
} from "oscquery";
import portfinder from 'portfinder';
import ip from 'ip';
import * as os from "os";

type MyEvents = {
add: (key: string, value: OscValue) => void,
Expand Down Expand Up @@ -76,18 +76,14 @@ export default class OscConnection extends (EventEmitter as new () => TypedEmitt
}

private async openSocketUnsafe() {
const host = ip.address();
//const host = '127.0.0.1';
const port = await portfinder.getPortPromise({
host: host,
//port: Math.floor(Math.random()*100 + 43776),
port: 43858
});
this.log(`Selected port: ${host}:${port}`);
this.log(`Selected port: ${port}`);

this.log(`Starting OSCQuery server...`);
const oscQuery = this.oscQuery = new OSCQueryServer({
bindAddress: host,
httpPort: port,
serviceName: "OGB"
});
Expand All @@ -97,9 +93,17 @@ export default class OscConnection extends (EventEmitter as new () => TypedEmitt

let receivedOne = false;

const myAddresses = new Set(
Object.values(os.networkInterfaces())
.flatMap(infs => infs)
.map(inf => inf?.address)
.filter(address => address != undefined)
.map(address => address!)
);

this.log(`Starting OSC server...`);
const oscSocket = this.oscSocket = new osc.UDPPort({
localAddress: host,
localAddress: '0.0.0.0',
localPort: port,
remotePort: 9000,
metadata: true
Expand All @@ -119,7 +123,14 @@ export default class OscConnection extends (EventEmitter as new () => TypedEmitt
}
}
});
oscSocket.on('message', (oscMsg: OscMessage) => {
oscSocket.on('message', (oscMsg: OscMessage, timeTag: unknown, rinfo: RemoteInfo) => {
const from = rinfo.address;
if (!myAddresses.has(from)) {
//this.log(`Received OSC packet from unknown address: ${from}`);
return;
}

//this.log(`Received OSC packet from: ${from}`);
if (!receivedOne) {
receivedOne = true;
this.log("Received an OSC message. We are probably connected.");
Expand Down
4 changes: 3 additions & 1 deletion src/types/osc.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
declare module 'osc' {

import {RemoteInfo} from "dgram";

export class UDPPort {
constructor(opts: {
localAddress: string,
Expand All @@ -11,7 +13,7 @@ declare module 'osc' {
on(ev: 'ready', cb: () => void);
on(ev: 'error', cb: (e: unknown) => void);
on(ev: 'data', cb: (buffer: Buffer) => void);
on(ev: 'message', cb: (message: OscMessage, timeTag: unknown, rinfo: unknown) => void);
on(ev: 'message', cb: (message: OscMessage, timeTag: unknown, rinfo: RemoteInfo) => void);
open();
close();
send(OscMessage);
Expand Down

0 comments on commit acdc773

Please sign in to comment.