diff --git a/app/dispatcher/dispatcher.go b/app/dispatcher/dispatcher.go index eb73ee1db..a9cbf8dcf 100644 --- a/app/dispatcher/dispatcher.go +++ b/app/dispatcher/dispatcher.go @@ -14,3 +14,6 @@ const ( type PacketDispatcher interface { DispatchToOutbound(session *proxy.SessionInfo) ray.InboundRay } + +type Inspector interface { +} diff --git a/common/common.go b/common/common.go index 551bdb965..a33bf813b 100644 --- a/common/common.go +++ b/common/common.go @@ -25,3 +25,10 @@ func Release(v interface{}) { releasable.Release() } } + +// Must panics if err is not nil. +func Must(err error) { + if err != nil { + panic(err) + } +} diff --git a/common/crypto/auth.go b/common/crypto/auth.go index 64d9465e4..54cad60c6 100644 --- a/common/crypto/auth.go +++ b/common/crypto/auth.go @@ -5,6 +5,7 @@ import ( "errors" "io" + "v2ray.com/core/common" "v2ray.com/core/common/buf" "v2ray.com/core/common/serial" ) @@ -135,9 +136,9 @@ func (v *AuthenticationReader) EnsureChunk() error { v.buffer.Clear() } else { 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 - }) + })) } err = v.buffer.AppendSupplier(buf.ReadFrom(v.reader)) if err == nil { diff --git a/common/crypto/crypto.go b/common/crypto/crypto.go index 3a4e4751c..ac85837da 100644 --- a/common/crypto/crypto.go +++ b/common/crypto/crypto.go @@ -1,3 +1,2 @@ -// Provides common crypto libraries for V2Ray. - +// Package crypto provides common crypto libraries for V2Ray. package crypto diff --git a/common/crypto/io.go b/common/crypto/io.go index 1f7bc7420..22d958282 100644 --- a/common/crypto/io.go +++ b/common/crypto/io.go @@ -20,9 +20,6 @@ func NewCryptionReader(stream cipher.Stream, reader io.Reader) *CryptionReader { } func (v *CryptionReader) Read(data []byte) (int, error) { - if v.reader == nil { - return 0, common.ErrObjectReleased - } nBytes, err := v.reader.Read(data) if nBytes > 0 { v.stream.XORKeyStream(data[:nBytes], data[:nBytes]) @@ -40,6 +37,7 @@ type CryptionWriter struct { writer io.Writer } +// NewCryptionWriter creates a new CryptionWriter. func NewCryptionWriter(stream cipher.Stream, writer io.Writer) *CryptionWriter { return &CryptionWriter{ 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) { - if v.writer == nil { - return 0, common.ErrObjectReleased - } v.stream.XORKeyStream(data, data) return v.writer.Write(data) } +// Release implements common.Releasable.Release(). func (v *CryptionWriter) Release() { common.Release(v.writer) common.Release(v.stream) diff --git a/proxy/blackhole/init.go b/proxy/blackhole/init.go index df356a3ea..d0f54372e 100644 --- a/proxy/blackhole/init.go +++ b/proxy/blackhole/init.go @@ -1,11 +1,12 @@ package blackhole import ( + "v2ray.com/core/common" "v2ray.com/core/common/serial" "v2ray.com/core/proxy" ) func init() { // Must listed after config.pb.go - proxy.MustRegisterOutboundHandlerCreator(serial.GetMessageType(new(Config)), new(Factory)) + common.Must(proxy.RegisterOutboundHandlerCreator(serial.GetMessageType(new(Config)), new(Factory))) } diff --git a/proxy/dokodemo/dokodemo.go b/proxy/dokodemo/dokodemo.go index a8a93dfc8..5b08f9016 100644 --- a/proxy/dokodemo/dokodemo.go +++ b/proxy/dokodemo/dokodemo.go @@ -5,6 +5,7 @@ import ( "v2ray.com/core/app" "v2ray.com/core/app/dispatcher" + "v2ray.com/core/common" "v2ray.com/core/common/buf" "v2ray.com/core/common/errors" "v2ray.com/core/common/log" @@ -211,5 +212,5 @@ func (v *Factory) Create(space app.Space, rawConfig interface{}, meta *proxy.Inb } func init() { - proxy.MustRegisterInboundHandlerCreator(serial.GetMessageType(new(Config)), new(Factory)) + common.Must(proxy.RegisterInboundHandlerCreator(serial.GetMessageType(new(Config)), new(Factory))) } diff --git a/proxy/freedom/freedom.go b/proxy/freedom/freedom.go index dd9796204..e5290e3f4 100644 --- a/proxy/freedom/freedom.go +++ b/proxy/freedom/freedom.go @@ -5,6 +5,7 @@ import ( "v2ray.com/core/app" "v2ray.com/core/app/dns" + "v2ray.com/core/common" "v2ray.com/core/common/buf" "v2ray.com/core/common/dice" "v2ray.com/core/common/errors" @@ -141,5 +142,5 @@ func (v *Factory) Create(space app.Space, config interface{}, meta *proxy.Outbou } func init() { - proxy.MustRegisterOutboundHandlerCreator(serial.GetMessageType(new(Config)), new(Factory)) + common.Must(proxy.RegisterOutboundHandlerCreator(serial.GetMessageType(new(Config)), new(Factory))) } diff --git a/proxy/handler_cache.go b/proxy/handler_cache.go index 5e4d6c5f0..774e62da6 100644 --- a/proxy/handler_cache.go +++ b/proxy/handler_cache.go @@ -20,12 +20,6 @@ func RegisterInboundHandlerCreator(name string, creator InboundHandlerFactory) e return nil } -func MustRegisterInboundHandlerCreator(name string, creator InboundHandlerFactory) { - if err := RegisterInboundHandlerCreator(name, creator); err != nil { - panic(err) - } -} - func RegisterOutboundHandlerCreator(name string, creator OutboundHandlerFactory) error { if _, found := outboundFactories[name]; found { return common.ErrDuplicatedName @@ -34,12 +28,6 @@ func RegisterOutboundHandlerCreator(name string, creator OutboundHandlerFactory) 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) { creator, found := inboundFactories[name] if !found { diff --git a/proxy/http/server.go b/proxy/http/server.go index 7ced64fc4..49f409fce 100644 --- a/proxy/http/server.go +++ b/proxy/http/server.go @@ -297,5 +297,5 @@ func (v *ServerFactory) Create(space app.Space, rawConfig interface{}, meta *pro } func init() { - proxy.MustRegisterInboundHandlerCreator(serial.GetMessageType(new(ServerConfig)), new(ServerFactory)) + common.Must(proxy.RegisterInboundHandlerCreator(serial.GetMessageType(new(ServerConfig)), new(ServerFactory))) } diff --git a/proxy/shadowsocks/init.go b/proxy/shadowsocks/init.go index 13b25e098..240174a4b 100644 --- a/proxy/shadowsocks/init.go +++ b/proxy/shadowsocks/init.go @@ -1,12 +1,13 @@ package shadowsocks import ( + "v2ray.com/core/common" "v2ray.com/core/common/serial" "v2ray.com/core/proxy" ) func init() { // Must happen after config is initialized - proxy.MustRegisterOutboundHandlerCreator(serial.GetMessageType(new(ClientConfig)), new(ClientFactory)) - proxy.MustRegisterInboundHandlerCreator(serial.GetMessageType(new(ServerConfig)), new(ServerFactory)) + common.Must(proxy.RegisterOutboundHandlerCreator(serial.GetMessageType(new(ClientConfig)), new(ClientFactory))) + common.Must(proxy.RegisterInboundHandlerCreator(serial.GetMessageType(new(ServerConfig)), new(ServerFactory))) } diff --git a/proxy/socks/server.go b/proxy/socks/server.go index 1d28ed2c5..6ba3f6292 100644 --- a/proxy/socks/server.go +++ b/proxy/socks/server.go @@ -7,6 +7,7 @@ import ( "v2ray.com/core/app" "v2ray.com/core/app/dispatcher" + "v2ray.com/core/common" "v2ray.com/core/common/buf" "v2ray.com/core/common/bufio" "v2ray.com/core/common/crypto" @@ -333,5 +334,5 @@ func (v *ServerFactory) Create(space app.Space, rawConfig interface{}, meta *pro } func init() { - proxy.MustRegisterInboundHandlerCreator(serial.GetMessageType(new(ServerConfig)), new(ServerFactory)) + common.Must(proxy.RegisterInboundHandlerCreator(serial.GetMessageType(new(ServerConfig)), new(ServerFactory))) } diff --git a/proxy/vmess/inbound/inbound.go b/proxy/vmess/inbound/inbound.go index b8ab3fc65..9809b882b 100644 --- a/proxy/vmess/inbound/inbound.go +++ b/proxy/vmess/inbound/inbound.go @@ -271,5 +271,5 @@ func (v *Factory) Create(space app.Space, rawConfig interface{}, meta *proxy.Inb } func init() { - proxy.MustRegisterInboundHandlerCreator(serial.GetMessageType(new(Config)), new(Factory)) + common.Must(proxy.RegisterInboundHandlerCreator(serial.GetMessageType(new(Config)), new(Factory))) } diff --git a/proxy/vmess/outbound/outbound.go b/proxy/vmess/outbound/outbound.go index 22a0f3e62..5bc96cbcf 100644 --- a/proxy/vmess/outbound/outbound.go +++ b/proxy/vmess/outbound/outbound.go @@ -4,6 +4,7 @@ import ( "sync" "v2ray.com/core/app" + "v2ray.com/core/common" "v2ray.com/core/common/buf" "v2ray.com/core/common/bufio" "v2ray.com/core/common/log" @@ -178,5 +179,5 @@ func (v *Factory) Create(space app.Space, rawConfig interface{}, meta *proxy.Out } func init() { - proxy.MustRegisterOutboundHandlerCreator(serial.GetMessageType(new(Config)), new(Factory)) + common.Must(proxy.RegisterOutboundHandlerCreator(serial.GetMessageType(new(Config)), new(Factory))) }