From f884d7cf76fc75b1d0a544533de03bc8e03b49b9 Mon Sep 17 00:00:00 2001 From: NI Date: Fri, 15 Apr 2022 18:42:16 +0800 Subject: [PATCH] Code clean up --- application/command/commands.go | 2 +- application/command/handler.go | 11 +++------- application/command/streams.go | 8 ++++---- application/commands/address.go | 12 +++++------ application/commands/integer.go | 4 ++-- application/commands/ssh.go | 20 +++++++++---------- application/commands/string.go | 4 ++-- application/commands/telnet.go | 2 +- application/configuration/config.go | 8 ++++---- application/configuration/loader_enviro.go | 16 +++------------ application/configuration/loader_file.go | 2 +- application/configuration/loader_redundant.go | 2 +- application/configuration/string.go | 6 +++--- application/configuration/string_test.go | 2 +- application/network/dial_ac.go | 2 +- application/rw/fetch.go | 2 +- application/rw/limited.go | 2 +- application/server/conn.go | 4 ---- application/server/server.go | 3 +-- 19 files changed, 46 insertions(+), 66 deletions(-) diff --git a/application/command/commands.go b/application/command/commands.go index 09691b5..f38dc7d 100644 --- a/application/command/commands.go +++ b/application/command/commands.go @@ -33,7 +33,7 @@ const ( // Errors var ( ErrCommandRunUndefinedCommand = errors.New( - "Undefined Command") + "undefined Command") ) // Command represents a command handler machine builder diff --git a/application/command/handler.go b/application/command/handler.go index e808714..85bc46a 100644 --- a/application/command/handler.go +++ b/application/command/handler.go @@ -31,13 +31,13 @@ import ( // Errors var ( ErrHandlerUnknownHeaderType = errors.New( - "Unknown command header type") + "unknown command header type") ErrHandlerControlMessageTooLong = errors.New( - "Control message was too long") + "control message was too long") ErrHandlerInvalidControlMessage = errors.New( - "Invalid control message") + "invalid control message") ) // HandlerCancelSignal signals the cancel of the entire handling proccess @@ -113,11 +113,6 @@ type streamHandlerSender struct { sendDelay time.Duration } -// signal sends handler signal -func (h streamHandlerSender) signal(hd Header, d []byte, buf []byte) error { - return h.handlerSender.signal(hd, d, buf) -} - // Write sends data func (h streamHandlerSender) Write(b []byte) (int, error) { defer time.Sleep(h.sendDelay) diff --git a/application/command/streams.go b/application/command/streams.go index abc4c55..3d1465b 100644 --- a/application/command/streams.go +++ b/application/command/streams.go @@ -28,16 +28,16 @@ import ( // Errors var ( ErrStreamsInvalidStreamID = errors.New( - "Stream ID is invalid") + "stream ID is invalid") ErrStreamsStreamOperateInactiveStream = errors.New( - "Specified stream was inactive for operation") + "specified stream was inactive for operation") ErrStreamsStreamClosingInactiveStream = errors.New( - "Closing an inactive stream is not allowed") + "closing an inactive stream is not allowed") ErrStreamsStreamReleasingInactiveStream = errors.New( - "Releasing an inactive stream is not allowed") + "releasing an inactive stream is not allowed") ) // StreamError Stream Error signal diff --git a/application/commands/address.go b/application/commands/address.go index dcab6f0..b7b7255 100644 --- a/application/commands/address.go +++ b/application/commands/address.go @@ -28,22 +28,22 @@ import ( //Errors var ( ErrAddressParseBufferTooSmallForHeader = errors.New( - "Buffer space was too small to parse the address header") + "buffer space was too small to parse the address header") ErrAddressParseBufferTooSmallForIPv4 = errors.New( - "Buffer space was too small to parse the IPv4 address") + "buffer space was too small to parse the IPv4 address") ErrAddressParseBufferTooSmallForIPv6 = errors.New( - "Buffer space was too small to parse the IPv6 address") + "buffer space was too small to parse the IPv6 address") ErrAddressParseBufferTooSmallForHostName = errors.New( - "Buffer space was too small to parse the hostname address") + "buffer space was too small to parse the hostname address") ErrAddressMarshalBufferTooSmall = errors.New( - "Buffer space was too small to marshal the address") + "buffer space was too small to marshal the address") ErrAddressInvalidAddressType = errors.New( - "Invalid address type") + "invalid address type") ) // AddressType Type of the address diff --git a/application/commands/integer.go b/application/commands/integer.go index c743254..bb67264 100644 --- a/application/commands/integer.go +++ b/application/commands/integer.go @@ -26,10 +26,10 @@ import ( // Errors var ( ErrIntegerMarshalNotEnoughBuffer = errors.New( - "Not enough buffer to marshal the integer") + "not enough buffer to marshal the integer") ErrIntegerMarshalTooLarge = errors.New( - "Integer cannot be marshalled, because the vaule was too large") + "integer cannot be marshalled, because the vaule was too large") ) // Integer is a 16bit unsigned integer data diff --git a/application/commands/ssh.go b/application/commands/ssh.go index 781c376..76923de 100644 --- a/application/commands/ssh.go +++ b/application/commands/ssh.go @@ -74,34 +74,34 @@ type sshAuthMethodBuilder func(b []byte) []ssh.AuthMethod // Errors var ( ErrSSHAuthCancelled = errors.New( - "Authentication has been cancelled") + "authentication has been cancelled") ErrSSHInvalidAuthMethod = errors.New( - "Invalid auth method") + "invalid auth method") ErrSSHInvalidAddress = errors.New( - "Invalid address") + "invalid address") ErrSSHRemoteFingerprintVerificationCancelled = errors.New( - "Server Fingerprint verification process has been cancelled") + "server Fingerprint verification process has been cancelled") ErrSSHRemoteFingerprintRefused = errors.New( - "Server Fingerprint has been refused") + "server Fingerprint has been refused") ErrSSHRemoteConnUnavailable = errors.New( - "Remote SSH connection is unavailable") + "remote SSH connection is unavailable") ErrSSHUnexpectedFingerprintVerificationRespond = errors.New( - "Unexpected fingerprint verification respond") + "unexpected fingerprint verification respond") ErrSSHUnexpectedCredentialDataRespond = errors.New( - "Unexpected credential data respond") + "unexpected credential data respond") ErrSSHCredentialDataTooLarge = errors.New( - "Credential was too large") + "credential was too large") ErrSSHUnknownClientSignal = errors.New( - "Unknown client signal") + "unknown client signal") ) var ( diff --git a/application/commands/string.go b/application/commands/string.go index 2b86e98..a6cb94c 100644 --- a/application/commands/string.go +++ b/application/commands/string.go @@ -26,10 +26,10 @@ import ( // Errors var ( ErrStringParseBufferTooSmall = errors.New( - "Not enough buffer space to parse given string") + "not enough buffer space to parse given string") ErrStringMarshalBufferTooSmall = errors.New( - "Not enough buffer space to marshal given string") + "not enough buffer space to marshal given string") ) // String data diff --git a/application/commands/telnet.go b/application/commands/telnet.go index 6bb69ff..2758f58 100644 --- a/application/commands/telnet.go +++ b/application/commands/telnet.go @@ -33,7 +33,7 @@ import ( // Errors var ( ErrTelnetUnableToReceiveRemoteConn = errors.New( - "Unable to acquire remote connection handle") + "unable to acquire remote connection handle") ) // Error codes diff --git a/application/configuration/config.go b/application/configuration/config.go index 321cd80..cf09052 100644 --- a/application/configuration/config.go +++ b/application/configuration/config.go @@ -108,7 +108,7 @@ func (s Server) IsTLS() bool { // Verify verifies current configuration func (s Server) Verify() error { if net.ParseIP(s.ListenInterface) == nil { - return fmt.Errorf("Invalid IP address \"%s\"", s.ListenInterface) + return fmt.Errorf("invalid IP address \"%s\"", s.ListenInterface) } if (len(s.TLSCertificateFile) > 0 && len(s.TLSCertificateKeyFile) <= 0) || @@ -132,7 +132,7 @@ func (m Meta) Concretize() (map[string]string, error) { result, err := v.Parse() if err != nil { - return nil, fmt.Errorf("Unable to parse Meta \"%s\": %s", k, err) + return nil, fmt.Errorf("unable to parse Meta \"%s\": %s", k, err) } mm[k] = result @@ -175,7 +175,7 @@ type Common struct { // Verify verifies current setting func (c Configuration) Verify() error { if len(c.Servers) <= 0 { - return errors.New("Must specify at least one server") + return errors.New("must specify at least one server") } for i, c := range c.Servers { @@ -185,7 +185,7 @@ func (c Configuration) Verify() error { continue } - return fmt.Errorf("Invalid setting for server %d: %s", i, vErr) + return fmt.Errorf("invalid setting for server %d: %s", i, vErr) } return nil diff --git a/application/configuration/loader_enviro.go b/application/configuration/loader_enviro.go index d4093c8..d984c66 100644 --- a/application/configuration/loader_enviro.go +++ b/application/configuration/loader_enviro.go @@ -42,16 +42,6 @@ func parseEnv(name string) string { return os.Getenv(v[21:]) } -func parseEnvDef(name string, def string) string { - v := parseEnv(name) - - if len(v) > 0 { - return v - } - - return def -} - // Enviro creates an environment variable based configuration loader func Enviro() Loader { return func(log log.Logger) (string, Configuration, error) { @@ -75,7 +65,7 @@ func Enviro() Loader { if cfgErr != nil { return enviroTypeName, Configuration{}, fmt.Errorf( - "Failed to build the configuration: %s", cfgErr) + "failed to build the configuration: %s", cfgErr) } listenIface := parseEnv("SSHWIFTY_LISTENINTERFACE") @@ -122,7 +112,7 @@ func Enviro() Loader { if jErr != nil { return enviroTypeName, Configuration{}, fmt.Errorf( - "Invalid \"SSHWIFTY_PRESETS\": %s", jErr) + "invalid \"SSHWIFTY_PRESETS\": %s", jErr) } } @@ -130,7 +120,7 @@ func Enviro() Loader { if err != nil { return enviroTypeName, Configuration{}, fmt.Errorf( - "Unable to parse Preset data: %s", err) + "unable to parse Preset data: %s", err) } return enviroTypeName, Configuration{ diff --git a/application/configuration/loader_file.go b/application/configuration/loader_file.go index e7ab3b4..eb73034 100644 --- a/application/configuration/loader_file.go +++ b/application/configuration/loader_file.go @@ -106,7 +106,7 @@ func (f fileCfgPresets) concretize() ([]Preset, error) { if err != nil { return nil, fmt.Errorf( - "Unable to concretize Preset %d (titled \"%s\"): %s", + "unable to concretize Preset %d (titled \"%s\"): %s", i+1, p.Title, err) } diff --git a/application/configuration/loader_redundant.go b/application/configuration/loader_redundant.go index 180e504..5a40e17 100644 --- a/application/configuration/loader_redundant.go +++ b/application/configuration/loader_redundant.go @@ -47,6 +47,6 @@ func Redundant(loaders ...Loader) Loader { } return redundantTypeName, Configuration{}, fmt.Errorf( - "All existing redundant loader has failed") + "all existing redundant loader has failed") } } diff --git a/application/configuration/string.go b/application/configuration/string.go index afb03e6..343ae9c 100644 --- a/application/configuration/string.go +++ b/application/configuration/string.go @@ -51,7 +51,7 @@ func (s String) Parse() (string, error) { f, e := os.Open(fPath) if e != nil { - return "", fmt.Errorf("Unable to open %s: %s", fPath, e) + return "", fmt.Errorf("unable to open %s: %s", fPath, e) } defer f.Close() @@ -59,7 +59,7 @@ func (s String) Parse() (string, error) { fData, e := ioutil.ReadAll(f) if e != nil { - return "", fmt.Errorf("Unable to read from %s: %s", fPath, e) + return "", fmt.Errorf("unable to read from %s: %s", fPath, e) } return string(fData), nil @@ -74,6 +74,6 @@ func (s String) Parse() (string, error) { default: return "", fmt.Errorf( - "Scheme \"%s\" was unsupported", ss[:sSchemeLeadIdx]) + "scheme \"%s\" was unsupported", ss[:sSchemeLeadIdx]) } } diff --git a/application/configuration/string_test.go b/application/configuration/string_test.go index 7deafa6..1eda5cb 100644 --- a/application/configuration/string_test.go +++ b/application/configuration/string_test.go @@ -84,7 +84,7 @@ func TestStringFile(t *testing.T) { ss = String("file://" + filePath + ".notexist") - result, err = ss.Parse() + _, err = ss.Parse() if err == nil { t.Error("Parsing an non-existing file should result an error") diff --git a/application/network/dial_ac.go b/application/network/dial_ac.go index 201fe3d..ea1f7f0 100644 --- a/application/network/dial_ac.go +++ b/application/network/dial_ac.go @@ -26,7 +26,7 @@ import ( // Errors var ( ErrAccessControlDialTargetHostNotAllowed = errors.New( - "Unable to dial to the specified remote host due to restriction") + "unable to dial to the specified remote host due to restriction") ) // AllowedHosts contains a map of allowed remote hosts diff --git a/application/rw/fetch.go b/application/rw/fetch.go index 13d1e2f..d6161c8 100644 --- a/application/rw/fetch.go +++ b/application/rw/fetch.go @@ -22,7 +22,7 @@ import "errors" // Errors var ( ErrFetchReaderNotEnoughBuffer = errors.New( - "Not enough buffer") + "not enough buffer") ) // FetchReaderFetcher generates data for SourceReader diff --git a/application/rw/limited.go b/application/rw/limited.go index eb30427..4103308 100644 --- a/application/rw/limited.go +++ b/application/rw/limited.go @@ -25,7 +25,7 @@ import ( // Errors var ( ErrReadUntilCompletedBufferFull = errors.New( - "Cannot read more, not enough data buffer") + "cannot read more, not enough data buffer") ) // LimitedReader reads only n bytes of data diff --git a/application/server/conn.go b/application/server/conn.go index 1230426..35bc54a 100644 --- a/application/server/conn.go +++ b/application/server/conn.go @@ -24,10 +24,6 @@ import ( "github.com/nirui/sshwifty/application/network" ) -var ( - emptyTime = time.Time{} -) - type listener struct { *net.TCPListener diff --git a/application/server/server.go b/application/server/server.go index d770119..b6c50d6 100644 --- a/application/server/server.go +++ b/application/server/server.go @@ -42,7 +42,7 @@ func (d dumpWrite) Write(b []byte) (int, error) { // Errors var ( ErrInvalidIPAddress = errors.New( - "Invalid IP address") + "invalid IP address") ) // HandlerBuilder builds a HTTP handler @@ -60,7 +60,6 @@ type CloseCallback func(error) // Server represents a server type Server struct { logger log.Logger - cfg configuration.Common shutdownWait *sync.WaitGroup }