cleanup error messages

pull/432/head
Darien Raymond 2017-04-09 13:30:46 +02:00
parent a8586eace6
commit 7c1b3ac1f0
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
8 changed files with 47 additions and 47 deletions

View File

@ -168,7 +168,7 @@ func WriteTCPRequest(request *protocol.RequestHeader, writer io.Writer) (buf.Wri
header.AppendBytes(AddrTypeDomain, byte(len(request.Address.Domain()))) header.AppendBytes(AddrTypeDomain, byte(len(request.Address.Domain())))
header.Append([]byte(request.Address.Domain())) header.Append([]byte(request.Address.Domain()))
default: 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))) 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()) _, err = writer.Write(header.Bytes())
if err != nil { 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 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) { func ReadTCPResponse(user *protocol.User, reader io.Reader) (buf.Reader, error) {
rawAccount, err := user.GetTypedAccount() rawAccount, err := user.GetTypedAccount()
if err != nil { 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) account := rawAccount.(*ShadowsocksAccount)
iv := make([]byte, account.Cipher.IVSize()) iv := make([]byte, account.Cipher.IVSize())
_, err = io.ReadFull(reader, iv) _, err = io.ReadFull(reader, iv)
if err != nil { 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) stream, err := account.Cipher.NewDecodingStream(account.Key, iv)
if err != nil { 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 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 user := request.User
rawAccount, err := user.GetTypedAccount() rawAccount, err := user.GetTypedAccount()
if err != nil { 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) account := rawAccount.(*ShadowsocksAccount)
@ -227,12 +227,12 @@ func WriteTCPResponse(request *protocol.RequestHeader, writer io.Writer) (buf.Wr
rand.Read(iv) rand.Read(iv)
_, err = writer.Write(iv) _, err = writer.Write(iv)
if err != nil { 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) stream, err := account.Cipher.NewEncodingStream(account.Key, iv)
if err != nil { 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 return buf.NewWriter(crypto.NewCryptionWriter(stream, writer)), nil
@ -242,7 +242,7 @@ func EncodeUDPPacket(request *protocol.RequestHeader, payload *buf.Buffer) (*buf
user := request.User user := request.User
rawAccount, err := user.GetTypedAccount() rawAccount, err := user.GetTypedAccount()
if err != nil { 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) 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.AppendBytes(AddrTypeDomain, byte(len(request.Address.Domain())))
buffer.Append([]byte(request.Address.Domain())) buffer.Append([]byte(request.Address.Domain()))
default: 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))) 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) stream, err := account.Cipher.NewEncodingStream(account.Key, iv)
if err != nil { 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)) 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) { func DecodeUDPPacket(user *protocol.User, payload *buf.Buffer) (*protocol.RequestHeader, *buf.Buffer, error) {
rawAccount, err := user.GetTypedAccount() rawAccount, err := user.GetTypedAccount()
if err != nil { 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) 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) stream, err := account.Cipher.NewDecodingStream(account.Key, iv)
if err != nil { 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()) 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 { 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 { 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) { if request.Option.Has(RequestOptionOneTimeAuth) {
@ -328,7 +328,7 @@ func DecodeUDPPacket(user *protocol.User, payload *buf.Buffer) (*protocol.Reques
actualAuth := make([]byte, AuthSize) actualAuth := make([]byte, AuthSize)
authenticator.Authenticate(payload.BytesTo(payloadLen))(actualAuth) authenticator.Authenticate(payload.BytesTo(payloadLen))(actualAuth)
if !bytes.Equal(actualAuth, authBytes) { if !bytes.Equal(actualAuth, authBytes) {
return nil, nil, newError("Shadowsocks|UDP: Invalid OTA.") return nil, nil, newError("invalid OTA")
} }
payload.Slice(0, payloadLen) 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))) request.Address = v2net.DomainAddress(string(payload.BytesRange(1, 1+domainLength)))
payload.SliceFrom(1 + domainLength) payload.SliceFrom(1 + domainLength)
default: 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)) request.Port = v2net.PortFromBytes(payload.BytesTo(2))

View File

@ -77,7 +77,7 @@ func (s *ServerSession) Handshake(reader io.Reader, writer io.Writer) (*protocol
return request, nil return request, nil
default: default:
writeSocks4Response(writer, socks4RequestRejected, v2net.AnyIP, v2net.Port(0)) 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) { func DecodeUDPPacket(packet []byte) (*protocol.RequestHeader, []byte, error) {
if len(packet) < 5 { 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{ request := &protocol.RequestHeader{
Version: socks5Version, Version: socks5Version,
@ -292,7 +292,7 @@ func DecodeUDPPacket(packet []byte) (*protocol.RequestHeader, []byte, error) {
// packet[0] and packet[1] are reserved // packet[0] and packet[1] are reserved
if packet[2] != 0 /* fragments */ { if packet[2] != 0 /* fragments */ {
return nil, nil, newError("Socks|UDP: Fragmented payload.") return nil, nil, newError("discarding fragmented payload.")
} }
addrType := packet[3] addrType := packet[3]
@ -301,7 +301,7 @@ func DecodeUDPPacket(packet []byte) (*protocol.RequestHeader, []byte, error) {
switch addrType { switch addrType {
case addrTypeIPv4: case addrTypeIPv4:
if len(packet) < 10 { 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] ip := packet[4:8]
request.Port = v2net.PortFromBytes(packet[8:10]) request.Port = v2net.PortFromBytes(packet[8:10])
@ -309,7 +309,7 @@ func DecodeUDPPacket(packet []byte) (*protocol.RequestHeader, []byte, error) {
dataBegin = 10 dataBegin = 10
case addrTypeIPv6: case addrTypeIPv6:
if len(packet) < 22 { 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] ip := packet[4:20]
request.Port = v2net.PortFromBytes(packet[20:22]) request.Port = v2net.PortFromBytes(packet[20:22])
@ -318,14 +318,14 @@ func DecodeUDPPacket(packet []byte) (*protocol.RequestHeader, []byte, error) {
case addrTypeDomain: case addrTypeDomain:
domainLength := int(packet[4]) domainLength := int(packet[4])
if len(packet) < 5+domainLength+2 { 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]) domain := string(packet[5 : 5+domainLength])
request.Port = v2net.PortFromBytes(packet[5+domainLength : 5+domainLength+2]) request.Port = v2net.PortFromBytes(packet[5+domainLength : 5+domainLength+2])
request.Address = v2net.ParseAddress(domain) request.Address = v2net.ParseAddress(domain)
dataBegin = 5 + domainLength + 2 dataBegin = 5 + domainLength + 2
default: default:
return nil, nil, newError("Socks|UDP: Unknown address type ", addrType) return nil, nil, newError("unknown address type ", addrType)
} }
return request, packet[dataBegin:], nil return request, packet[dataBegin:], nil
@ -399,10 +399,10 @@ func ClientHandshake(request *protocol.RequestHeader, reader io.Reader, writer i
} }
if b.Byte(0) != socks5Version { 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 { 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 { if authByte == authPassword {
@ -425,7 +425,7 @@ func ClientHandshake(request *protocol.RequestHeader, reader io.Reader, writer i
return nil, err return nil, err
} }
if b.Byte(1) != 0x00 { 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) resp := b.Byte(1)
if resp != 0x00 { if resp != 0x00 {
return nil, newError("Socks|Client: Server rejects request: ", resp) return nil, newError("server rejects request: ", resp)
} }
addrType := b.Byte(3) 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))) address = v2net.DomainAddress(string(b.BytesFrom(-domainLength)))
default: 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 { if err := b.AppendSupplier(buf.ReadFullFrom(reader, 2)); err != nil {

View File

@ -103,12 +103,12 @@ func (o *ClientConnection) Run() {
func DialKCP(ctx context.Context, dest v2net.Destination) (internet.Connection, error) { func DialKCP(ctx context.Context, dest v2net.Destination) (internet.Connection, error) {
dest.Network = v2net.Network_UDP 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) src := internet.DialerSourceFromContext(ctx)
rawConn, err := internet.DialSystem(ctx, src, dest) rawConn, err := internet.DialSystem(ctx, src, dest)
if err != nil { 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 return nil, err
} }
conn := &ClientConnection{ conn := &ClientConnection{
@ -120,11 +120,11 @@ func DialKCP(ctx context.Context, dest v2net.Destination) (internet.Connection,
header, err := kcpSettings.GetPackerHeader() header, err := kcpSettings.GetPackerHeader()
if err != nil { 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() security, err := kcpSettings.GetSecurity()
if err != nil { 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) conn.ResetSecurity(header, security)
conv := uint16(atomic.AddUint32(&globalConv, 1)) conv := uint16(atomic.AddUint32(&globalConv, 1))

View File

@ -90,11 +90,11 @@ func NewListener(ctx context.Context, address v2net.Address, port v2net.Port, co
header, err := kcpSettings.GetPackerHeader() header, err := kcpSettings.GetPackerHeader()
if err != nil { 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() security, err := kcpSettings.GetSecurity()
if err != nil { 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{ l := &Listener{
header: header, header: header,
@ -122,7 +122,7 @@ func NewListener(ctx context.Context, address v2net.Address, port v2net.Port, co
l.Lock() l.Lock()
l.hub = hub l.hub = hub
l.Unlock() l.Unlock()
log.Trace(newError("KCP|Listener: listening on ", address, ":", port)) log.Trace(newError("listening on ", address, ":", port))
return l, nil return l, nil
} }
@ -131,7 +131,7 @@ func (v *Listener) OnReceive(payload *buf.Buffer, src v2net.Destination, origina
segments := v.reader.Read(payload.Bytes()) segments := v.reader.Read(payload.Bytes())
if len(segments) == 0 { if len(segments) == 0 {
log.Trace(newError("KCP|Listener: discarding invalid payload from ", src)) log.Trace(newError("discarding invalid payload from ", src))
return return
} }

View File

@ -12,7 +12,7 @@ import (
) )
func Dial(ctx context.Context, dest v2net.Destination) (internet.Connection, error) { 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) src := internet.DialerSourceFromContext(ctx)
tcpSettings := internet.TransportSettingsFromContext(ctx).(*Config) tcpSettings := internet.TransportSettingsFromContext(ctx).(*Config)
@ -34,11 +34,11 @@ func Dial(ctx context.Context, dest v2net.Destination) (internet.Connection, err
if tcpSettings.HeaderSettings != nil { if tcpSettings.HeaderSettings != nil {
headerConfig, err := tcpSettings.HeaderSettings.GetInstance() headerConfig, err := tcpSettings.HeaderSettings.GetInstance()
if err != nil { 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) auth, err := internet.CreateConnectionAuthenticator(headerConfig)
if err != nil { 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) conn = auth.Client(conn)
} }

View File

@ -35,7 +35,7 @@ func ListenTCP(ctx context.Context, address v2net.Address, port v2net.Port, conn
if err != nil { if err != nil {
return nil, err return nil, err
} }
log.Trace(newError("TCP|Listener: Listening on ", address, ":", port)) log.Trace(newError("listening TCP on ", address, ":", port))
networkSettings := internet.TransportSettingsFromContext(ctx) networkSettings := internet.TransportSettingsFromContext(ctx)
tcpSettings := networkSettings.(*Config) tcpSettings := networkSettings.(*Config)
@ -54,11 +54,11 @@ func ListenTCP(ctx context.Context, address v2net.Address, port v2net.Port, conn
if tcpSettings.HeaderSettings != nil { if tcpSettings.HeaderSettings != nil {
headerConfig, err := tcpSettings.HeaderSettings.GetInstance() headerConfig, err := tcpSettings.HeaderSettings.GetInstance()
if err != nil { 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) auth, err := internet.CreateConnectionAuthenticator(headerConfig)
if err != nil { 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 l.authConfig = auth
} }
@ -83,7 +83,7 @@ func (v *TCPListener) KeepAccepting() {
return nil return nil
}) })
if err != 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 continue
} }

View File

@ -92,7 +92,7 @@ func ListenUDP(address v2net.Address, port v2net.Port, option ListenOption) (*Hu
if err != nil { if err != nil {
return nil, err return nil, err
} }
log.Trace(newError("UDP|Hub: Listening on ", address, ":", port)) log.Trace(newError("listening UDP on ", address, ":", port))
if option.ReceiveOriginalDest { if option.ReceiveOriginalDest {
fd, err := internal.GetSysFd(udpConn) fd, err := internal.GetSysFd(udpConn)
if err != nil { if err != nil {
@ -147,7 +147,7 @@ L:
}) })
if err != nil { 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() buffer.Release()
continue continue
} }

View File

@ -33,7 +33,7 @@ func (h *requestHandler) ServeHTTP(writer http.ResponseWriter, request *http.Req
} }
conn, err := converttovws(writer, request) conn, err := converttovws(writer, request)
if err != nil { 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 return
} }