1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-28 06:46:14 -04:00

common.Must

This commit is contained in:
Darien Raymond 2016-12-28 00:53:29 +01:00
parent 22fa151391
commit 7c751fcca0
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
14 changed files with 32 additions and 31 deletions

View File

@ -14,3 +14,6 @@ const (
type PacketDispatcher interface { type PacketDispatcher interface {
DispatchToOutbound(session *proxy.SessionInfo) ray.InboundRay DispatchToOutbound(session *proxy.SessionInfo) ray.InboundRay
} }
type Inspector interface {
}

View File

@ -25,3 +25,10 @@ func Release(v interface{}) {
releasable.Release() releasable.Release()
} }
} }
// Must panics if err is not nil.
func Must(err error) {
if err != nil {
panic(err)
}
}

View File

@ -5,6 +5,7 @@ import (
"errors" "errors"
"io" "io"
"v2ray.com/core/common"
"v2ray.com/core/common/buf" "v2ray.com/core/common/buf"
"v2ray.com/core/common/serial" "v2ray.com/core/common/serial"
) )
@ -135,9 +136,9 @@ func (v *AuthenticationReader) EnsureChunk() error {
v.buffer.Clear() v.buffer.Clear()
} else { } else {
leftover := v.buffer.Bytes() leftover := v.buffer.Bytes()
v.buffer.Reset(func(b []byte) (int, error) { common.Must(v.buffer.Reset(func(b []byte) (int, error) {
return copy(b, leftover), nil return copy(b, leftover), nil
}) }))
} }
err = v.buffer.AppendSupplier(buf.ReadFrom(v.reader)) err = v.buffer.AppendSupplier(buf.ReadFrom(v.reader))
if err == nil { if err == nil {

View File

@ -1,3 +1,2 @@
// Provides common crypto libraries for V2Ray. // Package crypto provides common crypto libraries for V2Ray.
package crypto package crypto

View File

@ -20,9 +20,6 @@ func NewCryptionReader(stream cipher.Stream, reader io.Reader) *CryptionReader {
} }
func (v *CryptionReader) Read(data []byte) (int, error) { func (v *CryptionReader) Read(data []byte) (int, error) {
if v.reader == nil {
return 0, common.ErrObjectReleased
}
nBytes, err := v.reader.Read(data) nBytes, err := v.reader.Read(data)
if nBytes > 0 { if nBytes > 0 {
v.stream.XORKeyStream(data[:nBytes], data[:nBytes]) v.stream.XORKeyStream(data[:nBytes], data[:nBytes])
@ -40,6 +37,7 @@ type CryptionWriter struct {
writer io.Writer writer io.Writer
} }
// NewCryptionWriter creates a new CryptionWriter.
func NewCryptionWriter(stream cipher.Stream, writer io.Writer) *CryptionWriter { func NewCryptionWriter(stream cipher.Stream, writer io.Writer) *CryptionWriter {
return &CryptionWriter{ return &CryptionWriter{
stream: stream, stream: stream,
@ -47,14 +45,13 @@ func NewCryptionWriter(stream cipher.Stream, writer io.Writer) *CryptionWriter {
} }
} }
// Write implements io.Writer.Write().
func (v *CryptionWriter) Write(data []byte) (int, error) { func (v *CryptionWriter) Write(data []byte) (int, error) {
if v.writer == nil {
return 0, common.ErrObjectReleased
}
v.stream.XORKeyStream(data, data) v.stream.XORKeyStream(data, data)
return v.writer.Write(data) return v.writer.Write(data)
} }
// Release implements common.Releasable.Release().
func (v *CryptionWriter) Release() { func (v *CryptionWriter) Release() {
common.Release(v.writer) common.Release(v.writer)
common.Release(v.stream) common.Release(v.stream)

View File

@ -1,11 +1,12 @@
package blackhole package blackhole
import ( import (
"v2ray.com/core/common"
"v2ray.com/core/common/serial" "v2ray.com/core/common/serial"
"v2ray.com/core/proxy" "v2ray.com/core/proxy"
) )
func init() { func init() {
// Must listed after config.pb.go // Must listed after config.pb.go
proxy.MustRegisterOutboundHandlerCreator(serial.GetMessageType(new(Config)), new(Factory)) common.Must(proxy.RegisterOutboundHandlerCreator(serial.GetMessageType(new(Config)), new(Factory)))
} }

View File

@ -5,6 +5,7 @@ import (
"v2ray.com/core/app" "v2ray.com/core/app"
"v2ray.com/core/app/dispatcher" "v2ray.com/core/app/dispatcher"
"v2ray.com/core/common"
"v2ray.com/core/common/buf" "v2ray.com/core/common/buf"
"v2ray.com/core/common/errors" "v2ray.com/core/common/errors"
"v2ray.com/core/common/log" "v2ray.com/core/common/log"
@ -211,5 +212,5 @@ func (v *Factory) Create(space app.Space, rawConfig interface{}, meta *proxy.Inb
} }
func init() { func init() {
proxy.MustRegisterInboundHandlerCreator(serial.GetMessageType(new(Config)), new(Factory)) common.Must(proxy.RegisterInboundHandlerCreator(serial.GetMessageType(new(Config)), new(Factory)))
} }

View File

@ -5,6 +5,7 @@ import (
"v2ray.com/core/app" "v2ray.com/core/app"
"v2ray.com/core/app/dns" "v2ray.com/core/app/dns"
"v2ray.com/core/common"
"v2ray.com/core/common/buf" "v2ray.com/core/common/buf"
"v2ray.com/core/common/dice" "v2ray.com/core/common/dice"
"v2ray.com/core/common/errors" "v2ray.com/core/common/errors"
@ -141,5 +142,5 @@ func (v *Factory) Create(space app.Space, config interface{}, meta *proxy.Outbou
} }
func init() { func init() {
proxy.MustRegisterOutboundHandlerCreator(serial.GetMessageType(new(Config)), new(Factory)) common.Must(proxy.RegisterOutboundHandlerCreator(serial.GetMessageType(new(Config)), new(Factory)))
} }

View File

@ -20,12 +20,6 @@ func RegisterInboundHandlerCreator(name string, creator InboundHandlerFactory) e
return nil return nil
} }
func MustRegisterInboundHandlerCreator(name string, creator InboundHandlerFactory) {
if err := RegisterInboundHandlerCreator(name, creator); err != nil {
panic(err)
}
}
func RegisterOutboundHandlerCreator(name string, creator OutboundHandlerFactory) error { func RegisterOutboundHandlerCreator(name string, creator OutboundHandlerFactory) error {
if _, found := outboundFactories[name]; found { if _, found := outboundFactories[name]; found {
return common.ErrDuplicatedName return common.ErrDuplicatedName
@ -34,12 +28,6 @@ func RegisterOutboundHandlerCreator(name string, creator OutboundHandlerFactory)
return nil return nil
} }
func MustRegisterOutboundHandlerCreator(name string, creator OutboundHandlerFactory) {
if err := RegisterOutboundHandlerCreator(name, creator); err != nil {
panic(err)
}
}
func CreateInboundHandler(name string, space app.Space, config interface{}, meta *InboundHandlerMeta) (InboundHandler, error) { func CreateInboundHandler(name string, space app.Space, config interface{}, meta *InboundHandlerMeta) (InboundHandler, error) {
creator, found := inboundFactories[name] creator, found := inboundFactories[name]
if !found { if !found {

View File

@ -297,5 +297,5 @@ func (v *ServerFactory) Create(space app.Space, rawConfig interface{}, meta *pro
} }
func init() { func init() {
proxy.MustRegisterInboundHandlerCreator(serial.GetMessageType(new(ServerConfig)), new(ServerFactory)) common.Must(proxy.RegisterInboundHandlerCreator(serial.GetMessageType(new(ServerConfig)), new(ServerFactory)))
} }

View File

@ -1,12 +1,13 @@
package shadowsocks package shadowsocks
import ( import (
"v2ray.com/core/common"
"v2ray.com/core/common/serial" "v2ray.com/core/common/serial"
"v2ray.com/core/proxy" "v2ray.com/core/proxy"
) )
func init() { func init() {
// Must happen after config is initialized // Must happen after config is initialized
proxy.MustRegisterOutboundHandlerCreator(serial.GetMessageType(new(ClientConfig)), new(ClientFactory)) common.Must(proxy.RegisterOutboundHandlerCreator(serial.GetMessageType(new(ClientConfig)), new(ClientFactory)))
proxy.MustRegisterInboundHandlerCreator(serial.GetMessageType(new(ServerConfig)), new(ServerFactory)) common.Must(proxy.RegisterInboundHandlerCreator(serial.GetMessageType(new(ServerConfig)), new(ServerFactory)))
} }

View File

@ -7,6 +7,7 @@ import (
"v2ray.com/core/app" "v2ray.com/core/app"
"v2ray.com/core/app/dispatcher" "v2ray.com/core/app/dispatcher"
"v2ray.com/core/common"
"v2ray.com/core/common/buf" "v2ray.com/core/common/buf"
"v2ray.com/core/common/bufio" "v2ray.com/core/common/bufio"
"v2ray.com/core/common/crypto" "v2ray.com/core/common/crypto"
@ -333,5 +334,5 @@ func (v *ServerFactory) Create(space app.Space, rawConfig interface{}, meta *pro
} }
func init() { func init() {
proxy.MustRegisterInboundHandlerCreator(serial.GetMessageType(new(ServerConfig)), new(ServerFactory)) common.Must(proxy.RegisterInboundHandlerCreator(serial.GetMessageType(new(ServerConfig)), new(ServerFactory)))
} }

View File

@ -271,5 +271,5 @@ func (v *Factory) Create(space app.Space, rawConfig interface{}, meta *proxy.Inb
} }
func init() { func init() {
proxy.MustRegisterInboundHandlerCreator(serial.GetMessageType(new(Config)), new(Factory)) common.Must(proxy.RegisterInboundHandlerCreator(serial.GetMessageType(new(Config)), new(Factory)))
} }

View File

@ -4,6 +4,7 @@ import (
"sync" "sync"
"v2ray.com/core/app" "v2ray.com/core/app"
"v2ray.com/core/common"
"v2ray.com/core/common/buf" "v2ray.com/core/common/buf"
"v2ray.com/core/common/bufio" "v2ray.com/core/common/bufio"
"v2ray.com/core/common/log" "v2ray.com/core/common/log"
@ -178,5 +179,5 @@ func (v *Factory) Create(space app.Space, rawConfig interface{}, meta *proxy.Out
} }
func init() { func init() {
proxy.MustRegisterOutboundHandlerCreator(serial.GetMessageType(new(Config)), new(Factory)) common.Must(proxy.RegisterOutboundHandlerCreator(serial.GetMessageType(new(Config)), new(Factory)))
} }