diff --git a/cli/ops/net.rs b/cli/ops/net.rs index 650127fadc..a9a62b1489 100644 --- a/cli/ops/net.rs +++ b/cli/ops/net.rs @@ -32,11 +32,18 @@ pub fn op_accept( None => Err(deno_error::bad_resource()), Some(server_resource) => { let op = tokio_util::accept(server_resource) - .map_err(ErrBox::from) .and_then(move |(tcp_stream, _socket_addr)| { + let local_addr = tcp_stream.local_addr()?; + let remote_addr = tcp_stream.peer_addr()?; let tcp_stream_resource = resources::add_tcp_stream(tcp_stream); + Ok((tcp_stream_resource, local_addr, remote_addr)) + }) + .map_err(ErrBox::from) + .and_then(move |(tcp_stream_resource, local_addr, remote_addr)| { futures::future::ok(json!({ - "rid": tcp_stream_resource.rid + "rid": tcp_stream_resource.rid, + "localAddr": local_addr.to_string(), + "remoteAddr": remote_addr.to_string(), })) }); @@ -64,14 +71,22 @@ pub fn op_dial( state.check_net(&address)?; let op = resolve_addr(&address).and_then(move |addr| { - TcpStream::connect(&addr).map_err(ErrBox::from).and_then( - move |tcp_stream| { + TcpStream::connect(&addr) + .map_err(ErrBox::from) + .and_then(move |tcp_stream| { + let local_addr = tcp_stream.local_addr()?; + let remote_addr = tcp_stream.peer_addr()?; let tcp_stream_resource = resources::add_tcp_stream(tcp_stream); + Ok((tcp_stream_resource, local_addr, remote_addr)) + }) + .map_err(ErrBox::from) + .and_then(move |(tcp_stream_resource, local_addr, remote_addr)| { futures::future::ok(json!({ - "rid": tcp_stream_resource.rid + "rid": tcp_stream_resource.rid, + "localAddr": local_addr.to_string(), + "remoteAddr": remote_addr.to_string(), })) - }, - ) + }) }); Ok(JsonOp::Async(Box::new(op))) @@ -129,7 +144,11 @@ pub fn op_listen( let addr = resolve_addr(&address).wait()?; let listener = TcpListener::bind(&addr)?; + let local_addr = listener.local_addr()?; let resource = resources::add_tcp_listener(listener); - Ok(JsonOp::Sync(json!(resource.rid))) + Ok(JsonOp::Sync(json!({ + "rid": resource.rid, + "localAddr": local_addr.to_string() + }))) } diff --git a/js/net.ts b/js/net.ts index b478ae6132..809dd40929 100644 --- a/js/net.ts +++ b/js/net.ts @@ -9,8 +9,10 @@ export type Network = "tcp"; // TODO support other types: // export type Network = "tcp" | "tcp4" | "tcp6" | "unix" | "unixpacket"; -// TODO Support finding network from Addr, see https://golang.org/pkg/net/#Addr -export type Addr = string; +export interface Addr { + network: Network; + address: string; +} /** A Listener is a generic network listener for stream-oriented protocols. */ export interface Listener extends AsyncIterator { @@ -75,12 +77,15 @@ class ConnImpl implements Conn { } class ListenerImpl implements Listener { - constructor(readonly rid: number) {} + constructor( + readonly rid: number, + private network: Network, + private localAddr: string + ) {} async accept(): Promise { const res = await sendAsync(dispatch.OP_ACCEPT, { rid: this.rid }); - // TODO(bartlomieju): add remoteAddr and localAddr on Rust side - return new ConnImpl(res.rid, res.remoteAddr!, res.localAddr!); + return new ConnImpl(res.rid, res.remoteAddr, res.localAddr); } close(): void { @@ -88,7 +93,10 @@ class ListenerImpl implements Listener { } addr(): Addr { - return notImplemented(); + return { + network: this.network, + address: this.localAddr + }; } async next(): Promise> { @@ -136,8 +144,8 @@ export interface Conn extends Reader, Writer, Closer { * See `dial()` for a description of the network and address parameters. */ export function listen(network: Network, address: string): Listener { - const rid = sendSync(dispatch.OP_LISTEN, { network, address }); - return new ListenerImpl(rid); + const res = sendSync(dispatch.OP_LISTEN, { network, address }); + return new ListenerImpl(res.rid, network, res.localAddr); } /** Dial connects to the address on the named network. diff --git a/js/net_test.ts b/js/net_test.ts index 10652136c1..23387001fb 100644 --- a/js/net_test.ts +++ b/js/net_test.ts @@ -3,6 +3,9 @@ import { testPerm, assert, assertEquals } from "./test_util.ts"; testPerm({ net: true }, function netListenClose(): void { const listener = Deno.listen("tcp", "127.0.0.1:4500"); + const addr = listener.addr(); + assertEquals(addr.network, "tcp"); + assertEquals(addr.address, "127.0.0.1:4500"); listener.close(); }); @@ -46,11 +49,15 @@ testPerm({ net: true }, async function netDialListen(): Promise { const listener = Deno.listen("tcp", ":4500"); listener.accept().then( async (conn): Promise => { + assert(conn.remoteAddr != null); + assertEquals(conn.localAddr, "127.0.0.1:4500"); await conn.write(new Uint8Array([1, 2, 3])); conn.close(); } ); const conn = await Deno.dial("tcp", "127.0.0.1:4500"); + assertEquals(conn.remoteAddr, "127.0.0.1:4500"); + assert(conn.localAddr != null); const buf = new Uint8Array(1024); const readResult = await conn.read(buf); assertEquals(3, readResult);