From 7c1b3ac1f09b03e39e35ad3cf80aa9d361313f9f Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Sun, 9 Apr 2017 13:30:46 +0200 Subject: [PATCH] cleanup error messages --- proxy/shadowsocks/protocol.go | 34 ++++++++++++++--------------- proxy/socks/protocol.go | 24 ++++++++++---------- transport/internet/kcp/dialer.go | 8 +++---- transport/internet/kcp/listener.go | 8 +++---- transport/internet/tcp/dialer.go | 6 ++--- transport/internet/tcp/hub.go | 8 +++---- transport/internet/udp/hub.go | 4 ++-- transport/internet/websocket/hub.go | 2 +- 8 files changed, 47 insertions(+), 47 deletions(-) diff --git a/proxy/shadowsocks/protocol.go b/proxy/shadowsocks/protocol.go index 8316d9c8..0269c1a0 100644 --- a/proxy/shadowsocks/protocol.go +++ b/proxy/shadowsocks/protocol.go @@ -168,7 +168,7 @@ func WriteTCPRequest(request *protocol.RequestHeader, writer io.Writer) (buf.Wri header.AppendBytes(AddrTypeDomain, byte(len(request.Address.Domain()))) header.Append([]byte(request.Address.Domain())) default: - return nil, newError("Shadowsocks|TCP: Unsupported address type: ", request.Address.Family()) + return nil, newError("unsupported address type: ", request.Address.Family()) } header.AppendSupplier(serial.WriteUint16(uint16(request.Port))) @@ -182,7 +182,7 @@ func WriteTCPRequest(request *protocol.RequestHeader, writer io.Writer) (buf.Wri _, err = writer.Write(header.Bytes()) if err != nil { - return nil, newError("Shadowsocks|TCP: Failed to write header.").Base(err) + return nil, newError("failed to write header").Base(err) } var chunkWriter buf.Writer @@ -198,19 +198,19 @@ func WriteTCPRequest(request *protocol.RequestHeader, writer io.Writer) (buf.Wri func ReadTCPResponse(user *protocol.User, reader io.Reader) (buf.Reader, error) { rawAccount, err := user.GetTypedAccount() if err != nil { - return nil, newError("Shadowsocks|TCP: Failed to parse account.").Base(err) + return nil, newError("failed to parse account").Base(err).AtError() } account := rawAccount.(*ShadowsocksAccount) iv := make([]byte, account.Cipher.IVSize()) _, err = io.ReadFull(reader, iv) if err != nil { - return nil, newError("Shadowsocks|TCP: Failed to read IV.").Base(err) + return nil, newError("failed to read IV").Base(err) } stream, err := account.Cipher.NewDecodingStream(account.Key, iv) if err != nil { - return nil, newError("Shadowsocks|TCP: Failed to initialize decoding stream.").Base(err) + return nil, newError("failed to initialize decoding stream").Base(err).AtError() } return buf.NewReader(crypto.NewCryptionReader(stream, reader)), nil } @@ -219,7 +219,7 @@ func WriteTCPResponse(request *protocol.RequestHeader, writer io.Writer) (buf.Wr user := request.User rawAccount, err := user.GetTypedAccount() if err != nil { - return nil, newError("Shadowsocks|TCP: Failed to parse account.").Base(err) + return nil, newError("failed to parse account.").Base(err).AtError() } account := rawAccount.(*ShadowsocksAccount) @@ -227,12 +227,12 @@ func WriteTCPResponse(request *protocol.RequestHeader, writer io.Writer) (buf.Wr rand.Read(iv) _, err = writer.Write(iv) if err != nil { - return nil, newError("Shadowsocks|TCP: Failed to write IV.").Base(err) + return nil, newError("failed to write IV.").Base(err) } stream, err := account.Cipher.NewEncodingStream(account.Key, iv) if err != nil { - return nil, newError("Shadowsocks|TCP: Failed to create encoding stream.").Base(err) + return nil, newError("failed to create encoding stream.").Base(err).AtError() } return buf.NewWriter(crypto.NewCryptionWriter(stream, writer)), nil @@ -242,7 +242,7 @@ func EncodeUDPPacket(request *protocol.RequestHeader, payload *buf.Buffer) (*buf user := request.User rawAccount, err := user.GetTypedAccount() if err != nil { - return nil, newError("Shadowsocks|UDP: Failed to parse account.").Base(err) + return nil, newError("failed to parse account.").Base(err).AtError() } account := rawAccount.(*ShadowsocksAccount) @@ -262,7 +262,7 @@ func EncodeUDPPacket(request *protocol.RequestHeader, payload *buf.Buffer) (*buf buffer.AppendBytes(AddrTypeDomain, byte(len(request.Address.Domain()))) buffer.Append([]byte(request.Address.Domain())) default: - return nil, newError("Shadowsocks|UDP: Unsupported address type: ", request.Address.Family()) + return nil, newError("unsupported address type: ", request.Address.Family()) } buffer.AppendSupplier(serial.WriteUint16(uint16(request.Port))) @@ -277,7 +277,7 @@ func EncodeUDPPacket(request *protocol.RequestHeader, payload *buf.Buffer) (*buf stream, err := account.Cipher.NewEncodingStream(account.Key, iv) if err != nil { - return nil, newError("Shadowsocks|TCP: Failed to create encoding stream.").Base(err) + return nil, newError("failed to create encoding stream").Base(err).AtError() } stream.XORKeyStream(buffer.BytesFrom(ivLen), buffer.BytesFrom(ivLen)) @@ -287,7 +287,7 @@ func EncodeUDPPacket(request *protocol.RequestHeader, payload *buf.Buffer) (*buf func DecodeUDPPacket(user *protocol.User, payload *buf.Buffer) (*protocol.RequestHeader, *buf.Buffer, error) { rawAccount, err := user.GetTypedAccount() if err != nil { - return nil, nil, newError("Shadowsocks|UDP: Failed to parse account.").Base(err) + return nil, nil, newError("failed to parse account").Base(err).AtError() } account := rawAccount.(*ShadowsocksAccount) @@ -297,7 +297,7 @@ func DecodeUDPPacket(user *protocol.User, payload *buf.Buffer) (*protocol.Reques stream, err := account.Cipher.NewDecodingStream(account.Key, iv) if err != nil { - return nil, nil, newError("Shadowsocks|UDP: Failed to initialize decoding stream.").Base(err) + return nil, nil, newError("failed to initialize decoding stream").Base(err).AtError() } stream.XORKeyStream(payload.Bytes(), payload.Bytes()) @@ -314,11 +314,11 @@ func DecodeUDPPacket(user *protocol.User, payload *buf.Buffer) (*protocol.Reques } if request.Option.Has(RequestOptionOneTimeAuth) && account.OneTimeAuth == Account_Disabled { - return nil, nil, newError("Shadowsocks|UDP: Rejecting packet with OTA enabled, while server disables OTA.") + return nil, nil, newError("rejecting packet with OTA enabled, while server disables OTA") } if !request.Option.Has(RequestOptionOneTimeAuth) && account.OneTimeAuth == Account_Enabled { - return nil, nil, newError("Shadowsocks|UDP: Rejecting packet with OTA disabled, while server enables OTA.") + return nil, nil, newError("rejecting packet with OTA disabled, while server enables OTA") } if request.Option.Has(RequestOptionOneTimeAuth) { @@ -328,7 +328,7 @@ func DecodeUDPPacket(user *protocol.User, payload *buf.Buffer) (*protocol.Reques actualAuth := make([]byte, AuthSize) authenticator.Authenticate(payload.BytesTo(payloadLen))(actualAuth) if !bytes.Equal(actualAuth, authBytes) { - return nil, nil, newError("Shadowsocks|UDP: Invalid OTA.") + return nil, nil, newError("invalid OTA") } payload.Slice(0, payloadLen) @@ -348,7 +348,7 @@ func DecodeUDPPacket(user *protocol.User, payload *buf.Buffer) (*protocol.Reques request.Address = v2net.DomainAddress(string(payload.BytesRange(1, 1+domainLength))) payload.SliceFrom(1 + domainLength) default: - return nil, nil, newError("Shadowsocks|UDP: Unknown address type: ", addrType) + return nil, nil, newError("unknown address type: ", addrType) } request.Port = v2net.PortFromBytes(payload.BytesTo(2)) diff --git a/proxy/socks/protocol.go b/proxy/socks/protocol.go index 2233ce14..37274a64 100644 --- a/proxy/socks/protocol.go +++ b/proxy/socks/protocol.go @@ -77,7 +77,7 @@ func (s *ServerSession) Handshake(reader io.Reader, writer io.Writer) (*protocol return request, nil default: writeSocks4Response(writer, socks4RequestRejected, v2net.AnyIP, v2net.Port(0)) - return nil, newError("Socks|Server: Unsupported command: ", buffer.Byte(1)) + return nil, newError("unsupported command: ", buffer.Byte(1)) } } @@ -283,7 +283,7 @@ func writeSocks4Response(writer io.Writer, errCode byte, address v2net.Address, func DecodeUDPPacket(packet []byte) (*protocol.RequestHeader, []byte, error) { if len(packet) < 5 { - return nil, nil, newError("Socks|UDP: Insufficient length of packet.") + return nil, nil, newError("insufficient length of packet.") } request := &protocol.RequestHeader{ Version: socks5Version, @@ -292,7 +292,7 @@ func DecodeUDPPacket(packet []byte) (*protocol.RequestHeader, []byte, error) { // packet[0] and packet[1] are reserved if packet[2] != 0 /* fragments */ { - return nil, nil, newError("Socks|UDP: Fragmented payload.") + return nil, nil, newError("discarding fragmented payload.") } addrType := packet[3] @@ -301,7 +301,7 @@ func DecodeUDPPacket(packet []byte) (*protocol.RequestHeader, []byte, error) { switch addrType { case addrTypeIPv4: if len(packet) < 10 { - return nil, nil, newError("Socks|UDP: Insufficient length of packet.") + return nil, nil, newError("insufficient length of packet") } ip := packet[4:8] request.Port = v2net.PortFromBytes(packet[8:10]) @@ -309,7 +309,7 @@ func DecodeUDPPacket(packet []byte) (*protocol.RequestHeader, []byte, error) { dataBegin = 10 case addrTypeIPv6: if len(packet) < 22 { - return nil, nil, newError("Socks|UDP: Insufficient length of packet.") + return nil, nil, newError("insufficient length of packet") } ip := packet[4:20] request.Port = v2net.PortFromBytes(packet[20:22]) @@ -318,14 +318,14 @@ func DecodeUDPPacket(packet []byte) (*protocol.RequestHeader, []byte, error) { case addrTypeDomain: domainLength := int(packet[4]) if len(packet) < 5+domainLength+2 { - return nil, nil, newError("Socks|UDP: Insufficient length of packet.") + return nil, nil, newError("insufficient length of packet") } domain := string(packet[5 : 5+domainLength]) request.Port = v2net.PortFromBytes(packet[5+domainLength : 5+domainLength+2]) request.Address = v2net.ParseAddress(domain) dataBegin = 5 + domainLength + 2 default: - return nil, nil, newError("Socks|UDP: Unknown address type ", addrType) + return nil, nil, newError("unknown address type ", addrType) } return request, packet[dataBegin:], nil @@ -399,10 +399,10 @@ func ClientHandshake(request *protocol.RequestHeader, reader io.Reader, writer i } if b.Byte(0) != socks5Version { - return nil, newError("Socks|Client: Unexpected server version: ", b.Byte(0)).AtWarning() + return nil, newError("unexpected server version: ", b.Byte(0)).AtWarning() } if b.Byte(1) != authByte { - return nil, newError("Socks|Client: auth method not supported.").AtWarning() + return nil, newError("auth method not supported.").AtWarning() } if authByte == authPassword { @@ -425,7 +425,7 @@ func ClientHandshake(request *protocol.RequestHeader, reader io.Reader, writer i return nil, err } if b.Byte(1) != 0x00 { - return nil, newError("Socks|Client: Server rejects account: ", b.Byte(1)) + return nil, newError("server rejects account: ", b.Byte(1)) } } @@ -448,7 +448,7 @@ func ClientHandshake(request *protocol.RequestHeader, reader io.Reader, writer i resp := b.Byte(1) if resp != 0x00 { - return nil, newError("Socks|Client: Server rejects request: ", resp) + return nil, newError("server rejects request: ", resp) } addrType := b.Byte(3) @@ -477,7 +477,7 @@ func ClientHandshake(request *protocol.RequestHeader, reader io.Reader, writer i } address = v2net.DomainAddress(string(b.BytesFrom(-domainLength))) default: - return nil, newError("Socks|Server: Unknown address type: ", addrType) + return nil, newError("unknown address type: ", addrType) } if err := b.AppendSupplier(buf.ReadFullFrom(reader, 2)); err != nil { diff --git a/transport/internet/kcp/dialer.go b/transport/internet/kcp/dialer.go index c3436b21..2934d4ee 100644 --- a/transport/internet/kcp/dialer.go +++ b/transport/internet/kcp/dialer.go @@ -103,12 +103,12 @@ func (o *ClientConnection) Run() { func DialKCP(ctx context.Context, dest v2net.Destination) (internet.Connection, error) { dest.Network = v2net.Network_UDP - log.Trace(newError("KCP|Dialer: Dialing KCP to ", dest)) + log.Trace(newError("dialing mKCP to ", dest)) src := internet.DialerSourceFromContext(ctx) rawConn, err := internet.DialSystem(ctx, src, dest) if err != nil { - log.Trace(newError("KCP|Dialer: Failed to dial to dest: ", err).AtError()) + log.Trace(newError("failed to dial to dest: ", err).AtError()) return nil, err } conn := &ClientConnection{ @@ -120,11 +120,11 @@ func DialKCP(ctx context.Context, dest v2net.Destination) (internet.Connection, header, err := kcpSettings.GetPackerHeader() if err != nil { - return nil, newError("KCP|Dialer: Failed to create packet header.").Base(err) + return nil, newError("failed to create packet header").Base(err) } security, err := kcpSettings.GetSecurity() if err != nil { - return nil, newError("KCP|Dialer: Failed to create security.").Base(err) + return nil, newError("failed to create security").Base(err) } conn.ResetSecurity(header, security) conv := uint16(atomic.AddUint32(&globalConv, 1)) diff --git a/transport/internet/kcp/listener.go b/transport/internet/kcp/listener.go index 7b61ec3c..36cf402b 100644 --- a/transport/internet/kcp/listener.go +++ b/transport/internet/kcp/listener.go @@ -90,11 +90,11 @@ func NewListener(ctx context.Context, address v2net.Address, port v2net.Port, co header, err := kcpSettings.GetPackerHeader() if err != nil { - return nil, newError("KCP|Listener: Failed to create packet header.").Base(err) + return nil, newError("failed to create packet header").Base(err).AtError() } security, err := kcpSettings.GetSecurity() if err != nil { - return nil, newError("KCP|Listener: Failed to create security.").Base(err) + return nil, newError("failed to create security").Base(err).AtError() } l := &Listener{ header: header, @@ -122,7 +122,7 @@ func NewListener(ctx context.Context, address v2net.Address, port v2net.Port, co l.Lock() l.hub = hub l.Unlock() - log.Trace(newError("KCP|Listener: listening on ", address, ":", port)) + log.Trace(newError("listening on ", address, ":", port)) return l, nil } @@ -131,7 +131,7 @@ func (v *Listener) OnReceive(payload *buf.Buffer, src v2net.Destination, origina segments := v.reader.Read(payload.Bytes()) if len(segments) == 0 { - log.Trace(newError("KCP|Listener: discarding invalid payload from ", src)) + log.Trace(newError("discarding invalid payload from ", src)) return } diff --git a/transport/internet/tcp/dialer.go b/transport/internet/tcp/dialer.go index d08b92af..072ad13f 100644 --- a/transport/internet/tcp/dialer.go +++ b/transport/internet/tcp/dialer.go @@ -12,7 +12,7 @@ import ( ) func Dial(ctx context.Context, dest v2net.Destination) (internet.Connection, error) { - log.Trace(newError("Internet|TCP: Dailing TCP to ", dest)) + log.Trace(newError("dailing TCP to ", dest)) src := internet.DialerSourceFromContext(ctx) tcpSettings := internet.TransportSettingsFromContext(ctx).(*Config) @@ -34,11 +34,11 @@ func Dial(ctx context.Context, dest v2net.Destination) (internet.Connection, err if tcpSettings.HeaderSettings != nil { headerConfig, err := tcpSettings.HeaderSettings.GetInstance() if err != nil { - return nil, newError("Internet|TCP: Failed to get header settings.").Base(err) + return nil, newError("failed to get header settings").Base(err).AtError() } auth, err := internet.CreateConnectionAuthenticator(headerConfig) if err != nil { - return nil, newError("Internet|TCP: Failed to create header authenticator.").Base(err) + return nil, newError("failed to create header authenticator").Base(err).AtError() } conn = auth.Client(conn) } diff --git a/transport/internet/tcp/hub.go b/transport/internet/tcp/hub.go index eb69d673..a1203ad8 100644 --- a/transport/internet/tcp/hub.go +++ b/transport/internet/tcp/hub.go @@ -35,7 +35,7 @@ func ListenTCP(ctx context.Context, address v2net.Address, port v2net.Port, conn if err != nil { return nil, err } - log.Trace(newError("TCP|Listener: Listening on ", address, ":", port)) + log.Trace(newError("listening TCP on ", address, ":", port)) networkSettings := internet.TransportSettingsFromContext(ctx) tcpSettings := networkSettings.(*Config) @@ -54,11 +54,11 @@ func ListenTCP(ctx context.Context, address v2net.Address, port v2net.Port, conn if tcpSettings.HeaderSettings != nil { headerConfig, err := tcpSettings.HeaderSettings.GetInstance() if err != nil { - return nil, newError("Internet|TCP: Invalid header settings.").Base(err) + return nil, newError("invalid header settings").Base(err).AtError() } auth, err := internet.CreateConnectionAuthenticator(headerConfig) if err != nil { - return nil, newError("Internet|TCP: Invalid header settings.").Base(err) + return nil, newError("invalid header settings.").Base(err).AtError() } l.authConfig = auth } @@ -83,7 +83,7 @@ func (v *TCPListener) KeepAccepting() { return nil }) if err != nil { - log.Trace(newError("TCP|Listener: Failed to accepted raw connections: ", err).AtWarning()) + log.Trace(newError("failed to accepted raw connections").Base(err).AtWarning()) continue } diff --git a/transport/internet/udp/hub.go b/transport/internet/udp/hub.go index a0f5c7bb..c3271f8a 100644 --- a/transport/internet/udp/hub.go +++ b/transport/internet/udp/hub.go @@ -92,7 +92,7 @@ func ListenUDP(address v2net.Address, port v2net.Port, option ListenOption) (*Hu if err != nil { return nil, err } - log.Trace(newError("UDP|Hub: Listening on ", address, ":", port)) + log.Trace(newError("listening UDP on ", address, ":", port)) if option.ReceiveOriginalDest { fd, err := internal.GetSysFd(udpConn) if err != nil { @@ -147,7 +147,7 @@ L: }) if err != nil { - log.Trace(newError("UDP|Hub: Failed to read UDP msg: ", err)) + log.Trace(newError("failed to read UDP msg").Base(err)) buffer.Release() continue } diff --git a/transport/internet/websocket/hub.go b/transport/internet/websocket/hub.go index 6a8a6d4f..a966ebb8 100644 --- a/transport/internet/websocket/hub.go +++ b/transport/internet/websocket/hub.go @@ -33,7 +33,7 @@ func (h *requestHandler) ServeHTTP(writer http.ResponseWriter, request *http.Req } conn, err := converttovws(writer, request) if err != nil { - log.Trace(newError("WebSocket|Listener: Failed to convert to WebSocket connection: ", err)) + log.Trace(newError("failed to convert to WebSocket connection").Base(err)) return }