magic-wormhole / magic-wormhole-transit-relay

Transit Relay server for Magic-Wormhole
MIT License
169 stars 42 forks source link

use line receiver to simplify handshake logic #13

Closed sigwinch28 closed 4 years ago

sigwinch28 commented 4 years ago

This is just a suggestion.

I noticed the handshake logic was quite complicated because the transit server is getting raw data as opposed to lines.

Twisted has a LineReceiver protocol which seems to make the job easier: the lineReceived method will be called by Twisted for each \n-terminated line which arrives (up to a default maximum length of 16384). As soon as the handshake is complete can call setRawMode() which immediately turns off line-based processing and diverts all incoming data to the rawDataReceived method which we can use when we're glued to a buddy connection.

All the tests still pass: impatience behaviour is maintained, and bad handshakes are detected unless a newline is never sent to the server.

codecov-io commented 4 years ago

Codecov Report

Merging #13 into master will decrease coverage by 0.05%. The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #13      +/-   ##
==========================================
- Coverage   99.43%   99.38%   -0.06%     
==========================================
  Files           5        5              
  Lines         356      324      -32     
  Branches       57       45      -12     
==========================================
- Hits          354      322      -32     
  Misses          1        1              
  Partials        1        1
Impacted Files Coverage Δ
src/wormhole_transit_relay/transit_server.py 100% <100%> (ø) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update c644532...912cfa6. Read the comment docs.

warner commented 4 years ago

Oh, this is awesome. Thank you!