1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-21 17:46:58 -05:00

fix lint errors

This commit is contained in:
Darien Raymond 2018-04-11 16:15:29 +02:00
parent 044c641d7b
commit 9d7f43a299
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
11 changed files with 87 additions and 66 deletions

View File

@ -3,6 +3,7 @@ package buf
import ( import (
"io" "io"
"v2ray.com/core/common"
"v2ray.com/core/common/errors" "v2ray.com/core/common/errors"
) )
@ -23,8 +24,7 @@ func (w *BufferToBytesWriter) WriteMultiBuffer(mb MultiBuffer) error {
defer mb.Release() defer mb.Release()
bs := mb.ToNetBuffers() bs := mb.ToNetBuffers()
_, err := bs.WriteTo(w.Writer) return common.Error2(bs.WriteTo(w.Writer))
return err
} }
// ReadFrom implements io.ReaderFrom. // ReadFrom implements io.ReaderFrom.
@ -52,8 +52,7 @@ func NewBufferedWriter(writer Writer) *BufferedWriter {
// WriteByte implements io.ByteWriter. // WriteByte implements io.ByteWriter.
func (w *BufferedWriter) WriteByte(c byte) error { func (w *BufferedWriter) WriteByte(c byte) error {
_, err := w.Write([]byte{c}) return common.Error2(w.Write([]byte{c}))
return err
} }
// Write implements io.Writer. // Write implements io.Writer.

View File

@ -16,3 +16,8 @@ func Must2(v interface{}, err error) interface{} {
Must(err) Must(err)
return v return v
} }
// Error2 returns the err from the 2nd parameter.
func Error2(v interface{}, err error) error {
return err
}

View File

@ -113,8 +113,7 @@ func (r *AuthenticationReader) readSize() (int32, error) {
return s, nil return s, nil
} }
sizeBytes := make([]byte, r.sizeParser.SizeBytes()) sizeBytes := make([]byte, r.sizeParser.SizeBytes())
_, err := io.ReadFull(r.reader, sizeBytes) if _, err := io.ReadFull(r.reader, sizeBytes); err != nil {
if err != nil {
return 0, err return 0, err
} }
size, err := r.sizeParser.Decode(sizeBytes) size, err := r.sizeParser.Decode(sizeBytes)

View File

@ -4,6 +4,7 @@ import (
"crypto/cipher" "crypto/cipher"
"io" "io"
"v2ray.com/core/common"
"v2ray.com/core/common/buf" "v2ray.com/core/common/buf"
) )
@ -50,6 +51,7 @@ func (w *CryptionWriter) Write(data []byte) (int, error) {
return w.writer.Write(data) return w.writer.Write(data)
} }
// WriteMultiBuffer implements buf.Writer.
func (w *CryptionWriter) WriteMultiBuffer(mb buf.MultiBuffer) error { func (w *CryptionWriter) WriteMultiBuffer(mb buf.MultiBuffer) error {
defer mb.Release() defer mb.Release()
@ -57,6 +59,5 @@ func (w *CryptionWriter) WriteMultiBuffer(mb buf.MultiBuffer) error {
for _, b := range bs { for _, b := range bs {
w.stream.XORKeyStream(b, b) w.stream.XORKeyStream(b, b)
} }
_, err := bs.WriteTo(w.writer) return common.Error2(bs.WriteTo(w.writer))
return err
} }

View File

@ -3,8 +3,10 @@ package protocol
import ( import (
"io" "io"
"v2ray.com/core/common"
"v2ray.com/core/common/buf" "v2ray.com/core/common/buf"
"v2ray.com/core/common/net" "v2ray.com/core/common/net"
"v2ray.com/core/common/signal"
) )
type AddressOption func(*AddressParser) type AddressOption func(*AddressParser)
@ -30,6 +32,7 @@ func WithAddressTypeParser(atp AddressTypeParser) AddressOption {
} }
} }
// AddressParser is a utility for reading and writer addresses.
type AddressParser struct { type AddressParser struct {
addrTypeMap map[byte]net.AddressFamily addrTypeMap map[byte]net.AddressFamily
addrByteMap map[net.AddressFamily]byte addrByteMap map[net.AddressFamily]byte
@ -37,6 +40,7 @@ type AddressParser struct {
typeParser AddressTypeParser typeParser AddressTypeParser
} }
// NewAddressParser creates a new AddressParser
func NewAddressParser(options ...AddressOption) *AddressParser { func NewAddressParser(options ...AddressOption) *AddressParser {
p := &AddressParser{ p := &AddressParser{
addrTypeMap: make(map[byte]net.AddressFamily, 8), addrTypeMap: make(map[byte]net.AddressFamily, 8),
@ -118,30 +122,42 @@ func (p *AddressParser) readAddress(b *buf.Buffer, reader io.Reader) (net.Addres
} }
} }
// ReadAddressPort reads address and port from the given input.
func (p *AddressParser) ReadAddressPort(buffer *buf.Buffer, input io.Reader) (net.Address, net.Port, error) { func (p *AddressParser) ReadAddressPort(buffer *buf.Buffer, input io.Reader) (net.Address, net.Port, error) {
if buffer == nil { if buffer == nil {
buffer = buf.New() buffer = buf.New()
defer buffer.Release() defer buffer.Release()
} }
var addr net.Address
var port net.Port
pTask := func() error {
lp, err := p.readPort(buffer, input)
if err != nil {
return err
}
port = lp
return nil
}
aTask := func() error {
a, err := p.readAddress(buffer, input)
if err != nil {
return err
}
addr = a
return nil
}
var err error
if p.portFirst { if p.portFirst {
port, err := p.readPort(buffer, input) err = signal.Execute(pTask, aTask)
if err != nil { } else {
return nil, 0, err err = signal.Execute(aTask, pTask)
}
addr, err := p.readAddress(buffer, input)
if err != nil {
return nil, 0, err
}
return addr, port, nil
} }
addr, err := p.readAddress(buffer, input)
if err != nil {
return nil, 0, err
}
port, err := p.readPort(buffer, input)
if err != nil { if err != nil {
return nil, 0, err return nil, 0, err
} }
@ -150,8 +166,7 @@ func (p *AddressParser) ReadAddressPort(buffer *buf.Buffer, input io.Reader) (ne
} }
func (p *AddressParser) writePort(writer io.Writer, port net.Port) error { func (p *AddressParser) writePort(writer io.Writer, port net.Port) error {
_, err := writer.Write(port.Bytes(nil)) return common.Error2(writer.Write(port.Bytes(nil)))
return err
} }
func (p *AddressParser) writeAddress(writer io.Writer, address net.Address) error { func (p *AddressParser) writeAddress(writer io.Writer, address net.Address) error {
@ -162,43 +177,38 @@ func (p *AddressParser) writeAddress(writer io.Writer, address net.Address) erro
switch address.Family() { switch address.Family() {
case net.AddressFamilyIPv4, net.AddressFamilyIPv6: case net.AddressFamilyIPv4, net.AddressFamilyIPv6:
if _, err := writer.Write([]byte{tb}); err != nil { return signal.Execute(func() error {
return err return common.Error2(writer.Write([]byte{tb}))
} }, func() error {
if _, err := writer.Write(address.IP()); err != nil { return common.Error2(writer.Write(address.IP()))
return err })
}
case net.AddressFamilyDomain: case net.AddressFamilyDomain:
domain := address.Domain() domain := address.Domain()
if isDomainTooLong(domain) { if isDomainTooLong(domain) {
return newError("Super long domain is not supported: ", domain) return newError("Super long domain is not supported: ", domain)
} }
if _, err := writer.Write([]byte{tb, byte(len(domain))}); err != nil { return signal.Execute(func() error {
return err return common.Error2(writer.Write([]byte{tb, byte(len(domain))}))
} }, func() error {
if _, err := writer.Write([]byte(domain)); err != nil { return common.Error2(writer.Write([]byte(domain)))
return err })
} default:
panic("Unknown family type.")
} }
return nil
} }
// WriteAddressPort writes address and port into the given writer.
func (p *AddressParser) WriteAddressPort(writer io.Writer, addr net.Address, port net.Port) error { func (p *AddressParser) WriteAddressPort(writer io.Writer, addr net.Address, port net.Port) error {
if p.portFirst { pTask := func() error {
if err := p.writePort(writer, port); err != nil { return p.writePort(writer, port)
return err }
} aTask := func() error {
if err := p.writeAddress(writer, addr); err != nil { return p.writeAddress(writer, addr)
return err
}
return nil
} }
if err := p.writeAddress(writer, addr); err != nil { if p.portFirst {
return err return signal.Execute(pTask, aTask)
} }
if err := p.writePort(writer, port); err != nil {
return err return signal.Execute(aTask, pTask)
}
return nil
} }

View File

@ -12,6 +12,16 @@ func executeAndFulfill(f func() error, done chan<- error) {
close(done) close(done)
} }
// Execute runs a list of tasks sequentially, returns the first error encountered or nil if all tasks pass.
func Execute(tasks ...func() error) error {
for _, task := range tasks {
if err := task(); err != nil {
return err
}
}
return nil
}
// ExecuteAsync executes a function asynchronously and return its result. // ExecuteAsync executes a function asynchronously and return its result.
func ExecuteAsync(f func() error) <-chan error { func ExecuteAsync(f func() error) <-chan error {
done := make(chan error, 1) done := make(chan error, 1)

View File

@ -11,6 +11,8 @@ type PeriodicTask struct {
Interval time.Duration Interval time.Duration
// Execute is the task function // Execute is the task function
Execute func() error Execute func() error
// OnFailure will be called when Execute returns non-nil error
OnError func(error)
access sync.Mutex access sync.Mutex
timer *time.Timer timer *time.Timer
@ -30,7 +32,9 @@ func (t *PeriodicTask) checkedExecute() error {
} }
t.timer = time.AfterFunc(t.Interval, func() { t.timer = time.AfterFunc(t.Interval, func() {
t.checkedExecute() if err := t.checkedExecute(); err != nil && t.OnError != nil {
t.OnError(err)
}
}) })
return nil return nil

View File

@ -94,9 +94,7 @@ func ParseString(str string) (UUID, error) {
text = text[1:] text = text[1:]
} }
_, err := hex.Decode(b[:byteGroup/2], text[:byteGroup]) if _, err := hex.Decode(b[:byteGroup/2], text[:byteGroup]); err != nil {
if err != nil {
return uuid, err return uuid, err
} }

View File

@ -116,8 +116,7 @@ Start:
if len(s.config.Accounts) > 0 { if len(s.config.Accounts) > 0 {
user, pass, ok := parseBasicAuth(request.Header.Get("Proxy-Authorization")) user, pass, ok := parseBasicAuth(request.Header.Get("Proxy-Authorization"))
if !ok || !s.config.HasAccount(user, pass) { if !ok || !s.config.HasAccount(user, pass) {
_, err := conn.Write([]byte("HTTP/1.1 407 Proxy Authentication Required\r\nProxy-Authenticate: Basic realm=\"proxy\"\r\n\r\n")) return common.Error2(conn.Write([]byte("HTTP/1.1 407 Proxy Authentication Required\r\nProxy-Authenticate: Basic realm=\"proxy\"\r\n\r\n")))
return err
} }
} }

View File

@ -138,8 +138,7 @@ func WriteTCPRequest(request *protocol.RequestHeader, writer io.Writer) (buf.Wri
if account.Cipher.IVSize() > 0 { if account.Cipher.IVSize() > 0 {
iv = make([]byte, account.Cipher.IVSize()) iv = make([]byte, account.Cipher.IVSize())
common.Must2(rand.Read(iv)) common.Must2(rand.Read(iv))
_, err = writer.Write(iv) if _, err = writer.Write(iv); err != nil {
if err != nil {
return nil, newError("failed to write IV") return nil, newError("failed to write IV")
} }
} }
@ -186,8 +185,7 @@ func ReadTCPResponse(user *protocol.User, reader io.Reader) (buf.Reader, error)
var iv []byte var iv []byte
if account.Cipher.IVSize() > 0 { if account.Cipher.IVSize() > 0 {
iv = make([]byte, account.Cipher.IVSize()) iv = make([]byte, account.Cipher.IVSize())
_, err = io.ReadFull(reader, iv) if _, err = io.ReadFull(reader, iv); err != nil {
if err != nil {
return nil, newError("failed to read IV").Base(err) return nil, newError("failed to read IV").Base(err)
} }
} }
@ -207,8 +205,7 @@ func WriteTCPResponse(request *protocol.RequestHeader, writer io.Writer) (buf.Wr
if account.Cipher.IVSize() > 0 { if account.Cipher.IVSize() > 0 {
iv = make([]byte, account.Cipher.IVSize()) iv = make([]byte, account.Cipher.IVSize())
common.Must2(rand.Read(iv)) common.Must2(rand.Read(iv))
_, err = writer.Write(iv) if _, err = writer.Write(iv); err != nil {
if err != nil {
return nil, newError("failed to write IV.").Base(err) return nil, newError("failed to write IV.").Base(err)
} }
} }

View File

@ -122,8 +122,7 @@ func (s *Server) processTCP(ctx context.Context, conn internet.Connection, dispa
func (*Server) handleUDP(c io.Reader) error { func (*Server) handleUDP(c io.Reader) error {
// The TCP connection closes after this method returns. We need to wait until // The TCP connection closes after this method returns. We need to wait until
// the client closes it. // the client closes it.
_, err := io.Copy(buf.DiscardBytes, c) return common.Error2(io.Copy(buf.DiscardBytes, c))
return err
} }
func (s *Server) transport(ctx context.Context, reader io.Reader, writer io.Writer, dest net.Destination, dispatcher core.Dispatcher) error { func (s *Server) transport(ctx context.Context, reader io.Reader, writer io.Writer, dest net.Destination, dispatcher core.Dispatcher) error {