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

View File

@ -16,3 +16,8 @@ func Must2(v interface{}, err error) interface{} {
Must(err)
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
}
sizeBytes := make([]byte, r.sizeParser.SizeBytes())
_, err := io.ReadFull(r.reader, sizeBytes)
if err != nil {
if _, err := io.ReadFull(r.reader, sizeBytes); err != nil {
return 0, err
}
size, err := r.sizeParser.Decode(sizeBytes)

View File

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

View File

@ -3,8 +3,10 @@ package protocol
import (
"io"
"v2ray.com/core/common"
"v2ray.com/core/common/buf"
"v2ray.com/core/common/net"
"v2ray.com/core/common/signal"
)
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 {
addrTypeMap map[byte]net.AddressFamily
addrByteMap map[net.AddressFamily]byte
@ -37,6 +40,7 @@ type AddressParser struct {
typeParser AddressTypeParser
}
// NewAddressParser creates a new AddressParser
func NewAddressParser(options ...AddressOption) *AddressParser {
p := &AddressParser{
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) {
if buffer == nil {
buffer = buf.New()
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 {
port, err := p.readPort(buffer, input)
if err != nil {
return nil, 0, err
}
addr, err := p.readAddress(buffer, input)
if err != nil {
return nil, 0, err
}
return addr, port, nil
err = signal.Execute(pTask, aTask)
} else {
err = signal.Execute(aTask, pTask)
}
addr, err := p.readAddress(buffer, input)
if err != nil {
return nil, 0, err
}
port, err := p.readPort(buffer, input)
if err != nil {
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 {
_, err := writer.Write(port.Bytes(nil))
return err
return common.Error2(writer.Write(port.Bytes(nil)))
}
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() {
case net.AddressFamilyIPv4, net.AddressFamilyIPv6:
if _, err := writer.Write([]byte{tb}); err != nil {
return err
}
if _, err := writer.Write(address.IP()); err != nil {
return err
}
return signal.Execute(func() error {
return common.Error2(writer.Write([]byte{tb}))
}, func() error {
return common.Error2(writer.Write(address.IP()))
})
case net.AddressFamilyDomain:
domain := address.Domain()
if isDomainTooLong(domain) {
return newError("Super long domain is not supported: ", domain)
}
if _, err := writer.Write([]byte{tb, byte(len(domain))}); err != nil {
return err
}
if _, err := writer.Write([]byte(domain)); err != nil {
return err
}
return signal.Execute(func() error {
return common.Error2(writer.Write([]byte{tb, byte(len(domain))}))
}, func() error {
return common.Error2(writer.Write([]byte(domain)))
})
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 {
if p.portFirst {
if err := p.writePort(writer, port); err != nil {
return err
}
if err := p.writeAddress(writer, addr); err != nil {
return err
}
return nil
pTask := func() error {
return p.writePort(writer, port)
}
aTask := func() error {
return p.writeAddress(writer, addr)
}
if err := p.writeAddress(writer, addr); err != nil {
return err
if p.portFirst {
return signal.Execute(pTask, aTask)
}
if err := p.writePort(writer, port); err != nil {
return err
}
return nil
return signal.Execute(aTask, pTask)
}

View File

@ -12,6 +12,16 @@ func executeAndFulfill(f func() error, done chan<- error) {
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.
func ExecuteAsync(f func() error) <-chan error {
done := make(chan error, 1)

View File

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

View File

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

View File

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

View File

@ -138,8 +138,7 @@ func WriteTCPRequest(request *protocol.RequestHeader, writer io.Writer) (buf.Wri
if account.Cipher.IVSize() > 0 {
iv = make([]byte, account.Cipher.IVSize())
common.Must2(rand.Read(iv))
_, err = writer.Write(iv)
if err != nil {
if _, err = writer.Write(iv); err != nil {
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
if account.Cipher.IVSize() > 0 {
iv = make([]byte, account.Cipher.IVSize())
_, err = io.ReadFull(reader, iv)
if err != nil {
if _, err = io.ReadFull(reader, iv); err != nil {
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 {
iv = make([]byte, account.Cipher.IVSize())
common.Must2(rand.Read(iv))
_, err = writer.Write(iv)
if err != nil {
if _, err = writer.Write(iv); err != nil {
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 {
// The TCP connection closes after this method returns. We need to wait until
// the client closes it.
_, err := io.Copy(buf.DiscardBytes, c)
return err
return common.Error2(io.Copy(buf.DiscardBytes, c))
}
func (s *Server) transport(ctx context.Context, reader io.Reader, writer io.Writer, dest net.Destination, dispatcher core.Dispatcher) error {