Replies: 2 comments 5 replies
-
#783, probably the easiest solution is opening a port tunneling on guest OS (for example, with https://github.com/fatedier/frp) then you will have access via Fetch/XHR API or WebSocket. |
Beta Was this translation helpful? Give feedback.
-
I had this same use case and built tcpip.js to solve it - a virtual network stack that can run in the browser. It's built on top of lwIP compiled to WASM with bindings to hook into each layer of the network stack (L2, L3, L4). Example(same example from #1197) Set up plumbing that pipes ethernet packets between v86 and the virtual network stack. import { createStack } from 'tcpip';
import { createV86NetworkStream } from '@tcpip/v86';
const stack = await createStack();
// Tap interfaces hook into ethernet (L2) frames
const tapInterface = await stack.createTapInterface({
mac: '01:23:45:67:89:ab',
ip: '192.168.1.1/24',
});
const emulator = new V86();
// Adds net0 listener and exposes as readable/writable streams
const vmNic = createV86NetworkStream(emulator);
// Forward frames between the tap interface and the VM's NIC
tapInterface.readable.pipeTo(vmNic.writable);
vmNic.readable.pipeTo(tapInterface.writable); Now we can establish a TCP connection to the emulator from JS (assuming VM has IP const connection = await stack.connectTcp({
host: '192.168.1.2',
port: 80,
});
// connection.writable is a standard WritableStream
const writer = connection.writable.getWriter();
// Send data
await writer.write(new TextEncoder().encode('Hello, world!'));
await writer.close();
// Listen for incoming data
for await (const chunk of connection) {
console.log(new TextDecoder().decode(chunk));
} Or we can create a TCP server and listen for inbound connections: const listener = await stack.listenTcp({
port: 80,
});
// TcpListener is an async iterable that yields TcpConnections
for await (const connection of listener) {
const writer = connection.writable.getWriter();
// Send data
await writer.write(new TextEncoder().encode('Hello, world!'));
await writer.close();
// Listen for incoming data
for await (const chunk of connection) {
console.log(new TextDecoder().decode(chunk));
}
} Protocol supportRight now it supports outbound and inbound TCP connections. I'm in the middle of adding HTTP client/server support and will be exposing it using the same API as UDP, DNS, ICMP, etc support is also on the roadmap. |
Beta Was this translation helpful? Give feedback.
-
I have a server (for example http or websoket on port 3000) running on the emulator and I'm trying to establish a connection to it from the JavaScript API.
can someone please provide help on the steps required to make this work? Any help or pointers would be greatly appreciated.
Beta Was this translation helpful? Give feedback.
All reactions