//go:build cshared // Command billing is the thin C ABI entry point for CLIProxyAPI. package main /* #include #include typedef struct { void* ptr; size_t len; } cliproxy_buffer; typedef int (*cliproxy_host_call_fn)(void*, const char*, const uint8_t*, size_t, cliproxy_buffer*); typedef void (*cliproxy_host_free_fn)(void*, size_t); typedef struct { uint32_t abi_version; void* host_ctx; cliproxy_host_call_fn call; cliproxy_host_free_fn free_buffer; } cliproxy_host_api; typedef int (*cliproxy_plugin_call_fn)(char*, uint8_t*, size_t, cliproxy_buffer*); typedef void (*cliproxy_plugin_free_fn)(void*, size_t); typedef void (*cliproxy_plugin_shutdown_fn)(void); typedef struct { uint32_t abi_version; cliproxy_plugin_call_fn call; cliproxy_plugin_free_fn free_buffer; cliproxy_plugin_shutdown_fn shutdown; } cliproxy_plugin_api; extern int cliproxyPluginCall(char*, uint8_t*, size_t, cliproxy_buffer*); extern void cliproxyPluginFree(void*, size_t); extern void cliproxyPluginShutdown(void); static const cliproxy_host_api* stored_host; static void store_host_api(const cliproxy_host_api* host) { stored_host = host; } static int call_host_api(const char* method, const uint8_t* request, size_t request_len, cliproxy_buffer* response) { if (stored_host == NULL || stored_host->call == NULL) { return 1; } return stored_host->call(stored_host->host_ctx, method, request, request_len, response); } static void free_host_buffer(void* ptr, size_t len) { if (stored_host != NULL && stored_host->free_buffer != NULL && ptr != NULL) { stored_host->free_buffer(ptr, len); } } */ import "C" import ( "fmt" "net/http" "unsafe" "billing/internal/plugin" ) var app = plugin.NewApp() func main() {} //export cliproxy_plugin_init func cliproxy_plugin_init(host *C.cliproxy_host_api, api *C.cliproxy_plugin_api) C.int { if api == nil { return 1 } C.store_host_api(host) app.SetHostCaller(cHostCaller{}) api.abi_version = C.uint32_t(plugin.ABIVersion) api.call = C.cliproxy_plugin_call_fn(C.cliproxyPluginCall) api.free_buffer = C.cliproxy_plugin_free_fn(C.cliproxyPluginFree) api.shutdown = C.cliproxy_plugin_shutdown_fn(C.cliproxyPluginShutdown) return 0 } type cHostCaller struct{} func (cHostCaller) Call(method string, request []byte) ([]byte, error) { cMethod := C.CString(method) defer C.free(unsafe.Pointer(cMethod)) var response C.cliproxy_buffer var requestPtr *C.uint8_t if len(request) > 0 { requestPtr = (*C.uint8_t)(C.CBytes(request)) defer C.free(unsafe.Pointer(requestPtr)) } if status := C.call_host_api(cMethod, requestPtr, C.size_t(len(request)), &response); status != 0 { return nil, fmt.Errorf("host callback %s failed with status %d", method, int(status)) } if response.ptr == nil || response.len == 0 { return nil, nil } defer C.free_host_buffer(response.ptr, response.len) return C.GoBytes(response.ptr, C.int(response.len)), nil } //export cliproxyPluginCall func cliproxyPluginCall(method *C.char, request *C.uint8_t, requestLen C.size_t, response *C.cliproxy_buffer) C.int { if response != nil { response.ptr = nil response.len = 0 } if method == nil { writeResponse(response, plugin.ErrorEnvelope("invalid_method", "缺少插件方法", http.StatusBadRequest)) return 1 } var requestBytes []byte if request != nil && requestLen > 0 { requestBytes = C.GoBytes(unsafe.Pointer(request), C.int(requestLen)) } raw, err := app.HandleMethod(C.GoString(method), requestBytes) if err != nil { writeResponse(response, plugin.ErrorEnvelope("plugin_error", err.Error(), http.StatusInternalServerError)) return 1 } writeResponse(response, raw) return 0 } //export cliproxyPluginFree func cliproxyPluginFree(ptr unsafe.Pointer, _ C.size_t) { if ptr != nil { C.free(ptr) } } //export cliproxyPluginShutdown func cliproxyPluginShutdown() { app.Shutdown() } func writeResponse(response *C.cliproxy_buffer, raw []byte) { if response == nil || len(raw) == 0 { return } ptr := C.CBytes(raw) if ptr == nil { return } response.ptr = ptr response.len = C.size_t(len(raw)) }