-
Notifications
You must be signed in to change notification settings - Fork 189
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
base: master
Are you sure you want to change the base?
Conversation
56aa501
to
37414f9
Compare
This will allow us to re-use the handling of packets in the Caddy server where serving is handled separately.
37414f9
to
b2aa859
Compare
c830973
to
8d95309
Compare
There was a problem hiding this 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.
Instead, make the connection an association by wrapping it with the client address and pass that to the `Handle()` function. Each connection will call `Handle()` only once.
afcdb28
to
07984ed
Compare
07984ed
to
d14ea20
Compare
There was a problem hiding this 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?
There was a problem hiding this 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.
// released after the packet is processed. | ||
HandlePacket(pkt []byte, lazySlice slicepool.LazySlice) | ||
|
||
// Read reads data from the association. |
There was a problem hiding this comment.
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).
There was a problem hiding this 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
// 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) |
There was a problem hiding this comment.
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.
} | ||
s.sh.Handle(ctx, conn, connMetrics) | ||
HandleAssociation(assoc) |
There was a problem hiding this comment.
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.
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 handlePacketConn
types) that is similar to ourStreamServe
function (to handleStreamConn
types).