Skip to content

Use custom listener when serving plugin #213

@Anthony-Jhoiro

Description

@Anthony-Jhoiro

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 :

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 :

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

I am using the latest package version and go 1.18.3 btw.
Thanks a lot !

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions