hashicorp / hashicorp/go-plugin
Use custom listener when serving plugin
- Dominant language
- Go
- Stars
- 6.1k
- Forks
- 511
- Avg merge
- 22h 47m
- Merged PRs (30d)
- 4
Description
I really enjoy using this package, but I encountered a limitation when trying to serve plugins that listen on TCP instead than with UNIX sockets due to security limitations.
The `Serve` function, as shown in the examples, seems only to create UNIX sockets listeners when running on non-windows system.
I tried a few things, first with a manually created server like this :
```go
listener, err := net.Listen("tcp", ":12345")
if err != nil { panic(err) }
server := plugin.GRPCServer{
// ...plugin config
}
err = server.Init()
if err != nil { panic(err) }
server.Serve(listener)
```
But I encountered an issue with the logger, as it is not initialized in the `server.Init` function, so the program crashes anytime, it tries to log with a segmentation error.
I made it work by adding a field for a custom listener in the `ServeConfig` struct (server.go:58) and preventing calling the `serverListener()` function in the `Serve` function. This way I can use the TCP listener like this :
```go
listener, err := net.Listen("tcp", ":12345")
if err != nil {
panic(err)
}
plugin.Serve(&plugin.ServeConfig{
// ...plugin config
Listener: listener,
})
```
And this seems to be working fine.
I am not sure to fully understand this library that's why I didn't make a PR about this now. Is there another planned way of doing that? I can do a PR about it if needed.
If it helps, you can find my fork here : [https://github.com/Anthony-Jhoiro/go-plugin/tree/feat/serve_custom_listener](https://github.com/Anthony-Jhoiro/go-plugin/tree/feat/serve_custom_listener)
I am using the latest package version and go 1.18.3 btw.
Thanks a lot !
Contributor guide
Assessment
This issue has not been assessed yet.