Skip to content

Commit ac259ca

Browse files
committedApr 15, 2022
plugin: listen on IP address
Signed-off-by: Sam Batschelet <[email protected]>
1 parent 59fd1e1 commit ac259ca

File tree

1 file changed

+10
-28
lines changed

1 file changed

+10
-28
lines changed
 

‎src/plugin.rs

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
use std::{
2-
io::{self, Error, ErrorKind},
3-
path::Path,
4-
};
1+
use std::io::{self, Error, ErrorKind};
52

63
use crate::vm::vm_server::{Vm, VmServer};
74
use log::info;
8-
use tokio::{fs::create_dir_all, net::UnixListener};
9-
use tokio_stream::wrappers::UnixListenerStream;
5+
use tokio::net::TcpListener;
6+
use tokio_stream::wrappers::TcpListenerStream;
107
use tonic::transport::{server::NamedService, Server};
118
use tonic_health::server::health_reporter;
129

@@ -15,8 +12,6 @@ pub const PROTOCOL_VERSION: u8 = 12;
1512
pub const MAGIC_COOKIE_KEY: &str = "VM_PLUGIN";
1613
pub const MAGIC_COOKIE_VALUE: &str = "dynamic";
1714

18-
pub const UNIX_SOCKET_PATH: &str = "/var/run/mini-kvvm-rs.sock";
19-
2015
/// ref. https://github.com/ava-labs/avalanchego/blob/v1.7.10/vms/rpcchainvm/vm.go
2116
#[derive(Debug)]
2217
pub struct HandshakeConfig {
@@ -51,39 +46,26 @@ pub async fn serve<V>(vm: V, handshake_config: &HandshakeConfig) -> io::Result<(
5146
where
5247
V: Vm,
5348
{
54-
create_dir_all(Path::new(UNIX_SOCKET_PATH).parent().unwrap())
55-
.await
56-
.map_err(|e| {
57-
Error::new(
58-
ErrorKind::Other,
59-
format!("failed tokio::fs::create_dir_all '{}'", e),
60-
)
61-
})?;
62-
6349
// "go-plugin requires the gRPC Health Checking Service to be registered on your server"
6450
// ref. https://github.com/hashicorp/go-plugin/blob/master/docs/guide-plugin-write-non-go.md
6551
// ref. https://github.com/hyperium/tonic/blob/v0.7.1/examples/src/health/server.rs
6652
let (mut health_reporter, health_svc) = health_reporter();
6753
health_reporter.set_serving::<Plugin>().await;
6854

55+
// avalanchego currently only supports plugins listening on IP address.
56+
let listener = TcpListener::bind("127.0.0.1:0").await?;
57+
let addr = listener.local_addr()?;
58+
info!("plugin listening on address {:?}", addr);
59+
6960
// ref. https://github.com/hashicorp/go-plugin/blob/master/docs/guide-plugin-write-non-go.md#4-output-handshake-information
70-
let handshake_msg = format!(
71-
"1|{}|unix|{}|grpc|",
72-
handshake_config.protocol_version, UNIX_SOCKET_PATH,
73-
);
61+
let handshake_msg = format!("1|{}|tcp|{}|grpc|", handshake_config.protocol_version, addr);
7462
info!("handshake message: {}", handshake_msg);
7563
println!("{}", handshake_msg);
7664

77-
// ref. https://github.com/hyperium/tonic/blob/v0.7.1/examples/src/uds/server.rs
78-
let listener = UnixListener::bind(UNIX_SOCKET_PATH)?;
79-
80-
let socket_addr = listener.local_addr()?;
81-
info!("plugin listening on socket address {:?}", socket_addr);
82-
8365
Server::builder()
8466
.add_service(health_svc)
8567
.add_service(VmServer::new(vm))
86-
.serve_with_incoming(UnixListenerStream::new(listener))
68+
.serve_with_incoming(TcpListenerStream::new(listener))
8769
.await
8870
.map_err(|e| {
8971
Error::new(

0 commit comments

Comments
 (0)
Please sign in to comment.