mirror of
https://github.com/eycorsican/leaf.git
synced 2026-12-05 08:01:26 +00:00
Writing a plugin in Go means writing the vtables and const-pointer shims cgo cannot express, handle allocation, panic recovery, the two-pass get_last_error protocol and address encoding -- once per plugin, by hand. leaf-abi-go holds all of it once, so a plugin is a package that registers engines and implements them over byte slices: tls-cabi-go is 143 lines, trojan-cabi-go 321. ConnEngine is the other half. crypto/tls insists on a blocking net.Conn, which the ABI forbids an engine call from being, so it runs the implementation on goroutines over an in-memory pipe and reports BLOCKED with a wake while those are what it waits on. A codec needing none of that implements StreamEngine directly, as trojan does. The SDK revokes its host handle once Destroy returns, so a goroutine outliving its engine drops a log line rather than calling into memory the host has reclaimed, and it masks BLOCKED off when the host offered no wake callback. Register runs from Go's init and calls out to C; it cannot go the other way, since entering Go binds a thread to the Go runtime for good and the thread that loads a plugin is the host's. The descriptor declares that these plugins embed a language runtime, so the host never unmaps them.
51 lines
1.6 KiB
Go
51 lines
1.6 KiB
Go
package leafabi
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
)
|
|
|
|
// Error carries a status code alongside a message, so an engine can say
|
|
// exactly which of the ABI's outcomes it means. Any other error an engine
|
|
// returns is reported to the host as StatusPluginFailure.
|
|
type Error struct {
|
|
Status Status
|
|
Message string
|
|
}
|
|
|
|
func (e *Error) Error() string { return e.Message }
|
|
|
|
// Errorf builds an Error with the given status.
|
|
func Errorf(status Status, format string, args ...any) error {
|
|
return &Error{Status: status, Message: fmt.Sprintf(format, args...)}
|
|
}
|
|
|
|
// ErrBufferTooSmall tells the host the output buffer could not hold the result.
|
|
// An engine returning it must have consumed nothing, so the host can grow the
|
|
// buffer and offer the same input again.
|
|
var ErrBufferTooSmall = &Error{Status: StatusBufferTooSmall, Message: "output buffer too small"}
|
|
|
|
// ErrUnsupported tells the host the engine does not implement the operation at
|
|
// all, so retrying cannot help.
|
|
var ErrUnsupported = &Error{Status: StatusUnsupported, Message: "unsupported"}
|
|
|
|
// ErrIncomplete says the input stops in the middle of a frame.
|
|
//
|
|
// On a reliable transport that is an ordinary outcome rather than a failure:
|
|
// DecodePacket returning it makes the SDK report success having consumed
|
|
// nothing, which is how the host is told to read more bytes and call again.
|
|
// Address parsing uses it for the same reason.
|
|
var ErrIncomplete = errors.New("incomplete frame")
|
|
|
|
// statusOf is the code the host is told about err.
|
|
func statusOf(err error) Status {
|
|
if err == nil {
|
|
return StatusOK
|
|
}
|
|
var typed *Error
|
|
if errors.As(err, &typed) {
|
|
return typed.Status
|
|
}
|
|
return StatusPluginFailure
|
|
}
|