diff --git a/app/proxy/proxy_test.go b/app/proxy/proxy_test.go index c72612130..6a8c95f9f 100644 --- a/app/proxy/proxy_test.go +++ b/app/proxy/proxy_test.go @@ -20,7 +20,7 @@ func TestProxyDial(t *testing.T) { space := app.NewSpace() outboundManager := outbound.New() - outboundManager.SetHandler("tag", freedom.NewFreedomConnection(&freedom.Config{}, space, &proxy.OutboundHandlerMeta{ + outboundManager.SetHandler("tag", freedom.New(&freedom.Config{}, space, &proxy.OutboundHandlerMeta{ Tag: "tag", StreamSettings: &internet.StreamConfig{ Network: v2net.Network_RawTCP, diff --git a/proxy/blackhole/blackhole.go b/proxy/blackhole/blackhole.go index df8298835..2243fbb23 100644 --- a/proxy/blackhole/blackhole.go +++ b/proxy/blackhole/blackhole.go @@ -1,3 +1,4 @@ +// Package blackhole is an outbound handler that blocks all connections. package blackhole import ( diff --git a/proxy/blackhole/config.go b/proxy/blackhole/config.go index 147a3fc32..b87e9e405 100644 --- a/proxy/blackhole/config.go +++ b/proxy/blackhole/config.go @@ -15,18 +15,23 @@ Content-Length: 0 ` ) +// ResponseConfig is the configuration for blackhole responses. type ResponseConfig interface { + // WriteTo writes predefined response to the give buffer. WriteTo(buf.Writer) } +// WriteTo implements ResponseConfig.WriteTo(). func (v *NoneResponse) WriteTo(buf.Writer) {} +// WriteTo implements ResponseConfig.WriteTo(). func (v *HTTPResponse) WriteTo(writer buf.Writer) { b := buf.NewLocal(512) b.AppendSupplier(serial.WriteString(http403response)) writer.Write(b) } +// GetInternalResponse converts response settings from proto to internal data structure. func (v *Config) GetInternalResponse() (ResponseConfig, error) { if v.GetResponse() == nil { return new(NoneResponse), nil diff --git a/proxy/dokodemo/config.go b/proxy/dokodemo/config.go index 45bf84ba8..33bd6b85e 100644 --- a/proxy/dokodemo/config.go +++ b/proxy/dokodemo/config.go @@ -4,6 +4,7 @@ import ( v2net "v2ray.com/core/common/net" ) +// GetPredefinedAddress returns the defined address from proto config. Null if address is not valid. func (v *Config) GetPredefinedAddress() v2net.Address { addr := v.Address.AsAddress() if addr == nil { diff --git a/proxy/dokodemo/dokodemo_test.go b/proxy/dokodemo/dokodemo_test.go index c8146711a..9e90f2f02 100644 --- a/proxy/dokodemo/dokodemo_test.go +++ b/proxy/dokodemo/dokodemo_test.go @@ -40,7 +40,7 @@ func TestDokodemoTCP(t *testing.T) { space.BindApp(dispatcher.APP_ID, dispatchers.NewDefaultDispatcher(space)) ohm := outbound.New() ohm.SetDefaultHandler( - freedom.NewFreedomConnection( + freedom.New( &freedom.Config{}, space, &proxy.OutboundHandlerMeta{ @@ -111,7 +111,7 @@ func TestDokodemoUDP(t *testing.T) { space.BindApp(dispatcher.APP_ID, dispatchers.NewDefaultDispatcher(space)) ohm := outbound.New() ohm.SetDefaultHandler( - freedom.NewFreedomConnection( + freedom.New( &freedom.Config{}, space, &proxy.OutboundHandlerMeta{ diff --git a/proxy/freedom/freedom.go b/proxy/freedom/freedom.go index 3cfb31825..dd9796204 100644 --- a/proxy/freedom/freedom.go +++ b/proxy/freedom/freedom.go @@ -18,15 +18,15 @@ import ( "v2ray.com/core/transport/ray" ) -type FreedomConnection struct { +type Handler struct { domainStrategy Config_DomainStrategy timeout uint32 dns dns.Server meta *proxy.OutboundHandlerMeta } -func NewFreedomConnection(config *Config, space app.Space, meta *proxy.OutboundHandlerMeta) *FreedomConnection { - f := &FreedomConnection{ +func New(config *Config, space app.Space, meta *proxy.OutboundHandlerMeta) *Handler { + f := &Handler{ domainStrategy: config.DomainStrategy, timeout: config.Timeout, meta: meta, @@ -44,7 +44,7 @@ func NewFreedomConnection(config *Config, space app.Space, meta *proxy.OutboundH } // Private: Visible for testing. -func (v *FreedomConnection) ResolveIP(destination v2net.Destination) v2net.Destination { +func (v *Handler) ResolveIP(destination v2net.Destination) v2net.Destination { if !destination.Address.Family().IsDomain() { return destination } @@ -66,7 +66,7 @@ func (v *FreedomConnection) ResolveIP(destination v2net.Destination) v2net.Desti return newDest } -func (v *FreedomConnection) Dispatch(destination v2net.Destination, payload *buf.Buffer, ray ray.OutboundRay) { +func (v *Handler) Dispatch(destination v2net.Destination, payload *buf.Buffer, ray ray.OutboundRay) { log.Info("Freedom: Opening connection to ", destination) defer payload.Release() @@ -128,18 +128,18 @@ func (v *FreedomConnection) Dispatch(destination v2net.Destination, payload *buf ray.OutboundOutput().Close() } -type FreedomFactory struct{} +type Factory struct{} -func (v *FreedomFactory) StreamCapability() v2net.NetworkList { +func (v *Factory) StreamCapability() v2net.NetworkList { return v2net.NetworkList{ Network: []v2net.Network{v2net.Network_RawTCP}, } } -func (v *FreedomFactory) Create(space app.Space, config interface{}, meta *proxy.OutboundHandlerMeta) (proxy.OutboundHandler, error) { - return NewFreedomConnection(config.(*Config), space, meta), nil +func (v *Factory) Create(space app.Space, config interface{}, meta *proxy.OutboundHandlerMeta) (proxy.OutboundHandler, error) { + return New(config.(*Config), space, meta), nil } func init() { - proxy.MustRegisterOutboundHandlerCreator(serial.GetMessageType(new(Config)), new(FreedomFactory)) + proxy.MustRegisterOutboundHandlerCreator(serial.GetMessageType(new(Config)), new(Factory)) } diff --git a/proxy/freedom/freedom_test.go b/proxy/freedom/freedom_test.go index 7b829cee5..980fb1c1e 100644 --- a/proxy/freedom/freedom_test.go +++ b/proxy/freedom/freedom_test.go @@ -36,7 +36,7 @@ func TestSinglePacket(t *testing.T) { assert.Error(err).IsNil() space := app.NewSpace() - freedom := NewFreedomConnection( + freedom := New( &Config{}, space, &proxy.OutboundHandlerMeta{ @@ -45,7 +45,7 @@ func TestSinglePacket(t *testing.T) { Network: v2net.Network_RawTCP, }, }) - space.Initialize() + assert.Error(space.Initialize()).IsNil() traffic := ray.NewRay() data2Send := "Data to be sent to remote" @@ -77,7 +77,7 @@ func TestIPResolution(t *testing.T) { }) space.BindApp(dns.APP_ID, dnsServer) - freedom := NewFreedomConnection( + freedom := New( &Config{DomainStrategy: Config_USE_IP}, space, &proxy.OutboundHandlerMeta{ @@ -87,7 +87,7 @@ func TestIPResolution(t *testing.T) { }, }) - space.Initialize() + assert.Error(space.Initialize()).IsNil() ipDest := freedom.ResolveIP(v2net.TCPDestination(v2net.DomainAddress("v2ray.com"), v2net.Port(80))) assert.Destination(ipDest).IsTCP()