base

package
v3.15.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 17, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package base provides common infrastructure for protocol proxies

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DatabaseProxyConfig

type DatabaseProxyConfig struct {
	DatabaseName string // Configurable database name
}

DatabaseProxyConfig holds database name configuration for any database proxy This provides a common base for all database types (MySQL, PostgreSQL, MongoDB, etc.)

func NewDatabaseProxyConfig

func NewDatabaseProxyConfig(databaseName string) *DatabaseProxyConfig

NewDatabaseProxyConfig creates a new config with the given database name If databaseName is empty, it uses a sensible default

func (*DatabaseProxyConfig) GetDatabaseName

func (c *DatabaseProxyConfig) GetDatabaseName() string

GetDatabaseName returns the current database name

func (*DatabaseProxyConfig) SetDatabaseName

func (c *DatabaseProxyConfig) SetDatabaseName(name string)

SetDatabaseName sets the database name

type Message

type Message struct {
	Type    byte
	Payload []byte
}

Message represents a generic protocol message

type ProtocolHandler

type ProtocolHandler interface {
	// Name returns the name of this protocol handler (e.g., "mysql", "postgresql")
	Name() string

	// HandleConnection handles the main proxying loop for a connection pair
	// This is the primary method that implementations should override
	HandleConnection(clientConn, upstreamConn net.Conn)

	// HandleClientStartup performs the initial handshake with a client connection
	// Returns connection parameters and any error
	HandleClientStartup(clientConn net.Conn) (map[string]string, error)

	// HandleUpstreamStartup performs the initial handshake with the upstream server
	// Returns any error
	HandleUpstreamStartup(upstreamConn net.Conn) error

	// ReadClientMessage reads a message from the client
	// Returns nil when connection is closed
	ReadClientMessage(clientConn net.Conn) (*Message, error)

	// ReadUpstreamMessage reads a message from the upstream server
	ReadUpstreamMessage(upstreamConn net.Conn) (*Message, error)

	// WriteClientMessage writes a message to the client
	WriteClientMessage(clientConn net.Conn, msg *Message) error

	// WriteUpstreamMessage writes a message to the upstream server
	WriteUpstreamMessage(upstreamConn net.Conn, msg *Message) error

	// ExtractQuery extracts SQL and metadata from a message
	// Returns nil if message is not a query
	ExtractQuery(msg *Message) *QueryInfo

	// IsExtendedProtocolMessage returns true if message is part of extended query protocol
	// (e.g., Parse, Bind, Execute, Describe in PostgreSQL)
	IsExtendedProtocolMessage(msg *Message) bool

	// HandleExtendedProtocol handles extended protocol message
	// Returns true if message was handled, false to forward to upstream
	HandleExtendedProtocol(msg *Message, upstreamConn, clientConn net.Conn) (bool, error)

	// CreateMockQueryResponse creates a mock response for a simple query
	CreateMockQueryResponse(query *QueryInfo, columns []string, rows []map[string]interface{}) ([]*Message, error)

	// CreateMockExtendedResponse creates mock responses for extended protocol
	// (e.g., ParseComplete, BindComplete, RowDescription, DataRow, CommandComplete)
	CreateMockExtendedResponse(query *QueryInfo, columns []string, rows []map[string]interface{}) ([]*Message, error)

	// CreateEmptyResultSet creates an empty result set response
	CreateEmptyResultSet(columns []string) ([]*Message, error)

	// CreateOKResponse creates a success response for write operations
	CreateOKResponse(query *QueryInfo) ([]*Message, error)

	// CreateErrorResponse creates an error response message
	CreateErrorResponse(errorMessage string) ([]*Message, error)

	// IsWhitelisted checks if a query should bypass mocking
	IsWhitelisted(query *QueryInfo) bool
}

ProtocolHandler defines the interface for database protocol handlers Implementations must handle the wire protocol specifics for each database type

type Proxy

type Proxy struct {
	Registry      *registry.MockRegistry
	IsWhitelisted func(query string) bool
	ExtractTable  func(query string) string
}

Proxy manages database proxy connections using the MySQL pattern: 1. Connect to upstream FIRST 2. Start goroutine piping upstream->client transparently (includes startup) 3. In main goroutine, read client->upstream with selective interception

type ProxyServer

type ProxyServer struct {
}

ProxyServer is a generic protocol proxy server

type QueryInfo

type QueryInfo struct {
	SQL       string
	TableName string
	IsWrite   bool
}

QueryInfo holds information about a SQL query

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL