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

refactor: split out association handling in packet handler #221

Open
wants to merge 52 commits into
base: master
Choose a base branch
from

Conversation

sbruens
Copy link

@sbruens sbruens commented Nov 15, 2024

This allows us to re-use the association handling logic in the Caddy server, where the NAT is handled by Caddy.

The NAT handling for the legacy server is split out into its own PacketServe function (to handle PacketConn types) that is similar to our StreamServe function (to handle StreamConn types).

@sbruens sbruens force-pushed the sbruens/udp-split-serving branch 5 times, most recently from 56aa501 to 37414f9 Compare November 19, 2024 15:18
This will allow us to re-use the handling of packets in the Caddy
server where serving is handled separately.
@sbruens sbruens force-pushed the sbruens/udp-split-serving branch from 37414f9 to b2aa859 Compare November 19, 2024 16:50
@sbruens sbruens force-pushed the sbruens/udp-split-serving branch from c830973 to 8d95309 Compare November 21, 2024 17:19
@sbruens sbruens marked this pull request as ready for review November 22, 2024 18:05
@sbruens sbruens requested a review from a team as a code owner November 22, 2024 18:05
@sbruens sbruens requested a review from fortuna November 22, 2024 18:05
service/udp.go Outdated Show resolved Hide resolved
Copy link

@fortuna fortuna left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this needs a redesign. We can make the shared abstraction be an association handler, not a packet handler. It significantly simplifies things because the handler has the association context, rather than having the context of an individual packet only.

And it will likely use less memory, since the handler can call read directly to the buffer it uses.

We will need to extract a NAT component for outline-ss-server.
Note that the handler is still the one to dictate the lifetime of an association, by closing the writes. You can't do that with a packet handler.

service/shadowsocks.go Outdated Show resolved Hide resolved
service/udp.go Outdated Show resolved Hide resolved
service/shadowsocks.go Outdated Show resolved Hide resolved
service/udp.go Outdated Show resolved Hide resolved
@sbruens sbruens changed the title refactor: split UDP serving from packet handling refactor: introduce a handler that handles a single association Nov 28, 2024
@sbruens sbruens changed the title refactor: introduce a handler that handles a single association refactor: create a handler that handles a single association Nov 28, 2024
@sbruens sbruens requested a review from fortuna December 19, 2024 22:02
@sbruens sbruens changed the title refactor: create a handler that handles a single association refactor: split out association handling in packet handler Dec 20, 2024
@sbruens sbruens force-pushed the sbruens/udp-split-serving branch from afcdb28 to 07984ed Compare December 20, 2024 20:23
@sbruens sbruens force-pushed the sbruens/udp-split-serving branch from 07984ed to d14ea20 Compare December 20, 2024 20:33
service/udp.go Outdated Show resolved Hide resolved
service/udp.go Outdated Show resolved Hide resolved
service/udp.go Outdated Show resolved Hide resolved
@sbruens sbruens requested a review from fortuna January 6, 2025 19:21
Copy link
Author

@sbruens sbruens left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Split the interface into a ConnAssociation and a PacketAssociation. Is that what you had in mind?

service/udp.go Outdated Show resolved Hide resolved
service/udp.go Outdated Show resolved Hide resolved
Copy link

@fortuna fortuna left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Association concept is a bit confusing and can be decoupled. Rather than two parallel concepts, make the Caddy handling be based on the PacketAssociation, so you have only one association type.

caddy/shadowsocks_handler.go Outdated Show resolved Hide resolved
service/shadowsocks.go Outdated Show resolved Hide resolved
service/udp.go Outdated Show resolved Hide resolved
service/udp.go Outdated Show resolved Hide resolved
service/udp.go Outdated Show resolved Hide resolved
service/udp.go Outdated Show resolved Hide resolved
service/shadowsocks.go Outdated Show resolved Hide resolved
service/udp.go Show resolved Hide resolved
// released after the packet is processed.
HandlePacket(pkt []byte, lazySlice slicepool.LazySlice)

// Read reads data from the association.
Copy link
Author

@sbruens sbruens Jan 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't feel great about adding a Read() here but otherwise we need HandleAssociation(net.Conn, PacketAssociation) despite PacketAssociation already wrapping the net.Conn (which is needed so that the natmap can close all associations when the natmap is closed).

@sbruens sbruens requested a review from fortuna January 9, 2025 20:32
Copy link

@fortuna fortuna left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit confused with some of the concepts. It may be purely naming. It needs some tweaks to clarify what is going on

service/udp.go Show resolved Hide resolved
service/shadowsocks.go Show resolved Hide resolved
cmd/outline-ss-server/main.go Show resolved Hide resolved
service/shadowsocks.go Show resolved Hide resolved
// pkt contains the raw packet data.
// lazySlice is the LazySlice that holds the pkt buffer, which should be
// released after the packet is processed.
HandlePacket(pkt []byte, lazySlice slicepool.LazySlice)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is confusing. The association and the packet handling should be decoupled. We should be able to use different implementations to handle an association.

service/shadowsocks.go Show resolved Hide resolved
}
s.sh.Handle(ctx, conn, connMetrics)
HandleAssociation(assoc)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sounds like you are using a default handler.
This should probably take an association and a handler.

I think the association is handling itself. That needs to be decoupled.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants