Skip to content

Conversation

cfc4n
Copy link
Member

@cfc4n cfc4n commented Jul 4, 2025

This pull request introduces significant improvements to error handling across multiple command modules, adds WebSocket support for logging, and includes a new WebSocket client implementation with tests. The changes enhance the robustness of the codebase and expand functionality for capturing and logging events.

Error Handling Enhancements:

  • Updated all command functions (bashCommandFunc, gnuTlsCommandFunc, goTLSCommandFunc, mysqldCommandFunc, nssCommandFunc, postgresCommandFunc, openSSLCommandFunc, zshCommandFunc) to return errors instead of using implicit error handling. This improves error propagation and makes debugging easier. [1] [2] [3] [4] [5] [6] [7] [8]

WebSocket Support for Logging:

  • Added WebSocket support for logging by introducing a new logger type (loggerTypeWebsocket) and modifying the initLogger function to handle WebSocket connections. This enables logs to be sent to WebSocket servers. [1] [2] [3] [4]

WebSocket Client Implementation:

  • Introduced a new WebSocket client in pkg/util/ws/client.go with methods for connecting (Dial), writing (Write), and closing (Close) WebSocket connections. This client uses base64 encoding for transmitted data.
  • Added unit tests for the WebSocket client in pkg/util/ws/client_test.go to verify functionality, including data transmission and error handling during connection.

Command Module Updates:

  • Updated command definitions (Run to RunE) in multiple modules (cli/cmd/bash.go, cli/cmd/gnutls.go, cli/cmd/gotls.go, cli/cmd/mysqld.go, cli/cmd/nspr.go, cli/cmd/postgres.go, cli/cmd/tls.go, cli/cmd/zsh.go) to support error handling improvements. [1] [2] [3] [4] [5] [6] [7] [8]

Miscellaneous Improvements:

  • Refactored variable names for clarity in runModule function (e.g., url to upgradeUrl).
  • Improved error handling in runModule to return errors instead of logging fatal messages, ensuring better control over program flow.

Signed-off-by: CFC4N <cfc4n.cs@gmail.com>
@cfc4n cfc4n requested a review from Copilot July 4, 2025 13:57
@cfc4n cfc4n added the enhancement New feature or request label Jul 4, 2025
@cfc4n cfc4n added this to the v1.5.0 milestone Jul 4, 2025
@dosubot dosubot bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Jul 4, 2025
Copy link

github-actions bot commented Jul 4, 2025

Failed to generate code suggestions for PR

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds WebSocket-based logging support by implementing a WebSocket server and client (with tests), improves error propagation in command modules by switching to RunE and returning errors, and refactors initLogger/runModule to handle WebSocket log sinks.

  • Introduce pkg/util/ws client/server with base64‐encoded message handling and unit tests
  • Update CLI commands (bash, gnutls, gotls, mysqld, nspr, postgres, zsh, tls) to use RunE/error returns for better error propagation
  • Extend initLogger in cli/cmd/root.go to detect ws:///wss:// addresses and dial WebSocket clients

Reviewed Changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
pkg/util/ws/server_test.go New tests for WebSocket server message handling
pkg/util/ws/server.go WebSocket server implementation decoding base64 messages
pkg/util/ws/client_test.go Tests for WebSocket client Dial, Write
pkg/util/ws/client.go WebSocket client implementing io.Writer over base64
cli/cmd/root.go Refactor initLogger to support WebSocket addresses
cli/cmd/bash.go Switch bash command to RunE and return errors
cli/cmd/gnutls.go Switch gnutls command to RunE and return errors
cli/cmd/gotls.go Switch gotls command to RunE and return errors
cli/cmd/mysqld.go Switch mysqld command to RunE and return errors
cli/cmd/nspr.go Switch nspr command to RunE and return errors
cli/cmd/postgres.go Switch postgres command to RunE and return errors
cli/cmd/zsh.go Switch zsh command to RunE and return errors
cli/cmd/tls.go Switch tls command to RunE and return errors
Comments suppressed due to low confidence (6)

cli/cmd/root.go:205

  • After dialing the WebSocket connection, call modConfig.SetAddrType(loggerTypeWebsocket) so that downstream components know the address type is WebSocket.
		} else if strings.Contains(addr, "ws://") || strings.Contains(addr, "wss://") {

cli/cmd/tls.go:61

  • The comment above openSSLCommandFunc incorrectly references "bash"; update it to reflect the OpenSSL/TLS command (e.g., "executes the TLS module").
// openSSLCommandFunc executes the "bash" command.

cli/cmd/gnutls.go:58

  • The doc comment for gnuTlsCommandFunc mentions "bash"; please correct it to describe the GnuTLS command execution.
// gnuTlsCommandFunc executes the "bash" command.

pkg/util/ws/client.go:30

  • [nitpick] Consider adding an English version of this comment or making it bilingual to maintain consistency across the codebase.
// Write 实现 io.Writer 接口

pkg/util/ws/client.go:51

  • There is no test covering Close(), which may hide resource leaks or panics. Add a unit test to verify that Close always succeeds (or returns a predictable error).
func (w *Client) Close() error {

pkg/util/ws/server_test.go:64

  • Consider adding a test case that sends an invalid base64 message to handleWebSocket and verifies the server continues processing subsequent valid messages.
	select {

@dosubot dosubot bot added the test Tests and some Magic label Jul 4, 2025
cfc4n and others added 3 commits July 4, 2025 22:02
…Socket connections

Co-authored-by: Hugo <77865234+zenyanle@users.noreply.github.com>
Signed-off-by: CFC4N <cfc4n.cs@gmail.com>
@cfc4n cfc4n merged commit 5292be2 into master Jul 6, 2025
5 checks passed
@cfc4n cfc4n deleted the websocket_logger branch July 6, 2025 16:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request size:L This PR changes 100-499 lines, ignoring generated files. test Tests and some Magic
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant