Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: split out UDP association handling from serving (#221)
* refactor: split UDP serving from handling of packets This will allow us to re-use the handling of packets in the Caddy server where serving is handled separately. * Use a read channel. * Rename `wrappedPacketConn` to just `packetConn`. * Update `shadowsocks_handler`. * Change the `HandlePacket` API to not require the `pkt`. * Remove the NAT map from the `Handle()` function. 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. * Rework the metrics now that the NAT is no longer done by the handler. * Move the metrics `AddClosed()` call to after the `timedCopy` returns. * Remove the explicit `targetConn.Close()` and let it expire on its own. * Add the NAT metrics back in at the server level. * Use buffer pool on `packetHandler` instead of global. * Revert wrapping the `clientConn` with shadowsocks encryption/decryption. * Rename `packet` to `association`. * Fix tests. * Update the docstring for `PacketServe`. * Fix comment to refer to "associations". * Fix metrics test. * Use `slicepool`. * Let the assocation handler provide the buffer. * Remove the `packetConnWrapper` and move the logic into `natconn` instead. * Fix close while reading of `natconn`. * Simplify the natmap a little. * Refactor `PacketServe` to use events (close and read). * Use correct logger. * Keep `readCh` and `closeCh` unbuffered. * Catch panics in the `ReadFrom` go routine. * Close the `readCh` instead of sending the error on the `readCh`. * Wrap a logger with the association's client address so we can simplify the `timedCopy()` signature. * Reference GitHub issue for supporting multiple IPs. * Simplify packet handling with a new `association` struct. * Add some comments to the `Association` interface. * Consolidate debug logging. * Rename some vars. * Update doc. * Format. * Do not unpack first packets twice. * Move handling into the association. * Add some comments to the timeout value. * Don't set the stream dialer in the old config flow. * Rename `AddAuthentication` and `AddClose`. * Update comment to reflect it handles packets from both directions. * Separate the interfaces for `Handle()` and `HandlePacket()`. * Fix typo in `UDPAssocationMetrics`. * Don't pass `conn` to `Handle()`. * Update comment. * Add comments. * Remove ConnAssociation in favor of a `HandleAssociation(Conn, PacketAssociation)`. * Split `Service` interface into outline-ss-server and Caddy interfaces. * Remove unused const. * Don't require `conn` in `HandleAssociation()`. * Exit the loop if the connection is closed. * Re-use global buffer pool. * Remove app-specific interfaces. Instead move the `HandleAssociation` logic into the Caddy Shadowsocks handler. * Decouple the association and the packet handling. * Move timedCopy handling to the packet handler. * Remove unused property. * Decouple shadowsocks from association. * Move authentication into its own function. * Remove the `packetMetrics` struct. We'll need to decouple metrics from handling in some future change. * Update tests. * Remove the `Metrics()` method. * Move variables into the anonymous functions. * Rename stream and packet handlers `Handle()` methods. * More `handle` naming clarification. * Refactor to make packet handler an association handler. * Only handle the association if it was new. * Fix the metric race condition in tests. * Move `AddNATEntry()` call to new entry only. * Format. * Address review comments. * Make `clientConn` an `io.Writer`. * Handle the `EOF` case and stop reading from the connection. * Address review comments. * Remove debug log.
- Loading branch information