Files
drip/internal/shared/protocol/error_sender.go
zhiqing 307cf8e6cc feat: Add Bearer Token authentication support and optimize code structure
- Add Bearer Token authentication, supporting tunnel access control via the --auth-bearer parameter
- Refactor large modules into smaller, more focused components to improve code maintainability
- Update dependency versions, including golang.org/x/crypto, golang.org/x/net, etc.
- Add SilenceUsage and SilenceErrors configuration for all CLI commands
- Modify connector configuration structure to support the new authentication method
- Update recent change log in README with new feature descriptions

BREAKING CHANGE: Authentication via Bearer Token is now supported, requiring the new --auth-bearer parameter
2026-01-29 14:40:53 +08:00

69 lines
1.8 KiB
Go

package protocol
import (
"fmt"
"net"
json "github.com/goccy/go-json"
"go.uber.org/zap"
)
// ErrorSender handles sending error frames over connections.
type ErrorSender struct {
conn net.Conn
frameWriter *FrameWriter
logger *zap.Logger
}
// NewErrorSender creates a new error sender.
func NewErrorSender(conn net.Conn, frameWriter *FrameWriter, logger *zap.Logger) *ErrorSender {
return &ErrorSender{
conn: conn,
frameWriter: frameWriter,
logger: logger,
}
}
// SendError sends an error frame with the given code and message.
func (e *ErrorSender) SendError(code, message string) error {
errMsg := ErrorMessage{
Code: code,
Message: message,
}
data, err := json.Marshal(errMsg)
if err != nil {
e.logger.Error("Failed to marshal error message", zap.Error(err))
return fmt.Errorf("failed to marshal error: %w", err)
}
errFrame := NewFrame(FrameTypeError, data)
if e.frameWriter == nil {
return WriteFrame(e.conn, errFrame)
}
return e.frameWriter.WriteFrame(errFrame)
}
// SendAuthenticationError sends an authentication failed error.
func (e *ErrorSender) SendAuthenticationError() error {
return e.SendError("authentication_failed", "Invalid authentication token")
}
// SendRegistrationError sends a registration failed error.
func (e *ErrorSender) SendRegistrationError(message string) error {
return e.SendError("registration_failed", message)
}
// SendPortAllocationError sends a port allocation failed error.
func (e *ErrorSender) SendPortAllocationError(message string) error {
return e.SendError("port_allocation_failed", message)
}
// SendTunnelTypeNotAllowedError sends a tunnel type not allowed error.
func (e *ErrorSender) SendTunnelTypeNotAllowedError(tunnelType string) error {
return e.SendError("tunnel_type_not_allowed",
fmt.Sprintf("Tunnel type '%s' is not allowed on this server", tunnelType))
}