func (this *DefaultDispatcher) DispatchToOutbound(meta *proxy.InboundHandlerMeta, session *proxy.SessionInfo) ray.InboundRay { direct := ray.NewRay() dispatcher := this.ohm.GetDefaultHandler() destination := session.Destination if this.router != nil { if tag, err := this.router.TakeDetour(destination); err == nil { if handler := this.ohm.GetHandler(tag); handler != nil { log.Info("DefaultDispatcher: Taking detour [", tag, "] for [", destination, "].") dispatcher = handler } else { log.Warning("DefaultDispatcher: Nonexisting tag: ", tag) } } else { log.Info("DefaultDispatcher: Default route for ", destination) } } if meta.AllowPassiveConnection { go dispatcher.Dispatch(destination, alloc.NewLocalBuffer(32).Clear(), direct) } else { go this.FilterPacketAndDispatch(destination, direct, dispatcher) } return direct }
func (this *TestPacketDispatcher) DispatchToOutbound(meta *proxy.InboundHandlerMeta, session *proxy.SessionInfo) ray.InboundRay { traffic := ray.NewRay() this.Destination <- session.Destination go this.Handler(session.Destination, traffic) return traffic }
func (v *TestPacketDispatcher) DispatchToOutbound(session *proxy.SessionInfo) ray.InboundRay { traffic := ray.NewRay() v.Destination <- session.Destination go v.Handler(session.Destination, traffic) return traffic }
func (h *Handler) Dial(ctx context.Context, dest v2net.Destination) (internet.Connection, error) { if h.senderSettings != nil { if h.senderSettings.ProxySettings.HasTag() { tag := h.senderSettings.ProxySettings.Tag handler := h.outboundManager.GetHandler(tag) if handler != nil { log.Info("Proxyman|OutboundHandler: Proxying to ", tag) ctx = proxy.ContextWithDestination(ctx, dest) stream := ray.NewRay(ctx) go handler.Dispatch(ctx, stream) return NewConnection(stream), nil } log.Warning("Proxyman|OutboundHandler: Failed to get outbound handler with tag: ", tag) } if h.senderSettings.Via != nil { ctx = internet.ContextWithDialerSource(ctx, h.senderSettings.Via.AsAddress()) } if h.senderSettings != nil { ctx = internet.ContextWithStreamSettings(ctx, h.senderSettings.StreamSettings) } } return internet.Dial(ctx, dest) }
func (v *DefaultDispatcher) DispatchToOutbound(session *proxy.SessionInfo) ray.InboundRay { direct := ray.NewRay() dispatcher := v.ohm.GetDefaultHandler() destination := session.Destination if v.router != nil { if tag, err := v.router.TakeDetour(session); err == nil { if handler := v.ohm.GetHandler(tag); handler != nil { log.Info("DefaultDispatcher: Taking detour [", tag, "] for [", destination, "].") dispatcher = handler } else { log.Warning("DefaultDispatcher: Nonexisting tag: ", tag) } } else { log.Info("DefaultDispatcher: Default route for ", destination) } } if session.Inbound != nil && session.Inbound.AllowPassiveConnection { go dispatcher.Dispatch(destination, buf.NewLocal(32), direct) } else { go v.FilterPacketAndDispatch(destination, direct, dispatcher) } return direct }
func (v *OutboundProxy) Dial(src v2net.Address, dest v2net.Destination, options internet.DialerOptions) (internet.Connection, error) { handler := v.outboundManager.GetHandler(options.Proxy.Tag) if handler == nil { log.Warning("Proxy: Failed to get outbound handler with tag: ", options.Proxy.Tag) return internet.Dial(src, dest, internet.DialerOptions{ Stream: options.Stream, }) } log.Info("Proxy: Dialing to ", dest) stream := ray.NewRay() go handler.Dispatch(dest, nil, stream) return NewProxyConnection(src, dest, stream), nil }
func TestUnreachableDestination(t *testing.T) { assert := assert.On(t) freedom := NewFreedomConnection( &Config{}, app.NewSpace(), &proxy.OutboundHandlerMeta{ Address: v2net.AnyIP, StreamSettings: &internet.StreamConfig{ Network: v2net.Network_RawTCP, }, }) traffic := ray.NewRay() data2Send := "Data to be sent to remote" payload := alloc.NewLocalBuffer(2048).Clear().Append([]byte(data2Send)) err := freedom.Dispatch(v2net.TCPDestination(v2net.IPAddress([]byte{127, 0, 0, 1}), 128), payload, traffic) assert.Error(err).IsNotNil() }
func TestSinglePacket(t *testing.T) { assert := assert.On(t) tcpServer := &tcp.Server{ MsgProcessor: func(data []byte) []byte { buffer := make([]byte, 0, 2048) buffer = append(buffer, []byte("Processed: ")...) buffer = append(buffer, data...) return buffer }, } _, err := tcpServer.Start() assert.Error(err).IsNil() space := app.NewSpace() freedom := NewFreedomConnection( &Config{}, space, &proxy.OutboundHandlerMeta{ Address: v2net.AnyIP, StreamSettings: &internet.StreamConfig{ Network: v2net.Network_RawTCP, }, }) space.Initialize() traffic := ray.NewRay() data2Send := "Data to be sent to remote" payload := buf.NewLocal(2048) payload.Append([]byte(data2Send)) go freedom.Dispatch(v2net.TCPDestination(v2net.LocalHostIP, tcpServer.Port), payload, traffic) traffic.InboundInput().Close() respPayload, err := traffic.InboundOutput().Read() assert.Error(err).IsNil() assert.String(respPayload.String()).Equals("Processed: Data to be sent to remote") tcpServer.Close() }
func (v *DefaultDispatcher) DispatchToOutbound(ctx context.Context) ray.InboundRay { dispatcher := v.ohm.GetDefaultHandler() destination := proxy.DestinationFromContext(ctx) if !destination.IsValid() { panic("Dispatcher: Invalid destination.") } if v.router != nil { if tag, err := v.router.TakeDetour(ctx); err == nil { if handler := v.ohm.GetHandler(tag); handler != nil { log.Info("DefaultDispatcher: Taking detour [", tag, "] for [", destination, "].") dispatcher = handler } else { log.Warning("DefaultDispatcher: Nonexisting tag: ", tag) } } else { log.Info("DefaultDispatcher: Default route for ", destination) } } direct := ray.NewRay(ctx) var waitFunc func() error if allowPassiveConnection, ok := proxy.AllowPassiveConnectionFromContext(ctx); ok && allowPassiveConnection { waitFunc = noOpWait() } else { wdi := &waitDataInspector{ hasData: make(chan bool, 1), } direct.AddInspector(wdi) waitFunc = waitForData(wdi) } go v.waitAndDispatch(ctx, waitFunc, direct, dispatcher) return direct }