Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #2654 #2941

Merged
merged 2 commits into from
Jan 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions transport/internet/udp/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,44 +28,43 @@ type connEntry struct {

type Dispatcher struct {
sync.RWMutex
conns map[net.Destination]*connEntry
conn *connEntry
dispatcher routing.Dispatcher
callback ResponseCallback
callClose func() error
}

func NewDispatcher(dispatcher routing.Dispatcher, callback ResponseCallback) *Dispatcher {
return &Dispatcher{
conns: make(map[net.Destination]*connEntry),
dispatcher: dispatcher,
callback: callback,
}
}

func (v *Dispatcher) RemoveRay(dest net.Destination) {
func (v *Dispatcher) RemoveRay() {
v.Lock()
defer v.Unlock()
if conn, found := v.conns[dest]; found {
common.Close(conn.link.Reader)
common.Close(conn.link.Writer)
delete(v.conns, dest)
if v.conn != nil {
common.Close(v.conn.link.Reader)
common.Close(v.conn.link.Writer)
v.conn = nil
}
}

func (v *Dispatcher) getInboundRay(ctx context.Context, dest net.Destination) (*connEntry, error) {
v.Lock()
defer v.Unlock()

if entry, found := v.conns[dest]; found {
return entry, nil
if v.conn != nil {
return v.conn, nil
}

newError("establishing new connection for ", dest).WriteToLog()

ctx, cancel := context.WithCancel(ctx)
removeRay := func() {
cancel()
v.RemoveRay(dest)
v.RemoveRay()
}
timer := signal.CancelAfterInactivity(ctx, removeRay, time.Minute)

Expand All @@ -79,7 +78,7 @@ func (v *Dispatcher) getInboundRay(ctx context.Context, dest net.Destination) (*
timer: timer,
cancel: removeRay,
}
v.conns[dest] = entry
v.conn = entry
go handleInput(ctx, entry, dest, v.callback, v.callClose)
return entry, nil
}
Expand Down Expand Up @@ -130,6 +129,9 @@ func handleInput(ctx context.Context, conn *connEntry, dest net.Destination, cal
}
timer.Update()
for _, b := range mb {
if b.UDP != nil {
dest = *b.UDP
}
callback(ctx, &udp.Packet{
Payload: b,
Source: dest,
Expand All @@ -153,7 +155,6 @@ func DialDispatcher(ctx context.Context, dispatcher routing.Dispatcher) (net.Pac
}

d := &Dispatcher{
conns: make(map[net.Destination]*connEntry),
dispatcher: dispatcher,
callback: c.callback,
callClose: c.Close,
Expand Down Expand Up @@ -199,7 +200,9 @@ func (c *dispatcherConn) WriteTo(p []byte, addr net.Addr) (int, error) {
n := copy(raw, p)
buffer.Resize(0, int32(n))

c.dispatcher.Dispatch(c.ctx, net.DestinationFromAddr(addr), buffer)
destination := net.DestinationFromAddr(addr)
buffer.UDP = &destination
c.dispatcher.Dispatch(c.ctx, destination, buffer)
return n, nil
}

Expand Down
Loading