Browse Source

get the responder side of things working, including COMPLETE! shutdown...

not fully tested on the various errors...
tags/v0.1.0
John-Mark Gurney 5 years ago
parent
commit
7f60466d35
1 changed files with 38 additions and 32 deletions
  1. +38
    -32
      ntunnel.py

+ 38
- 32
ntunnel.py View File

@@ -131,27 +131,24 @@ async def NoiseForwarder(mode, priv_key, rdrwrr, ptsockstr):
writer.write(proto.decrypt(tmsg)) writer.write(proto.decrypt(tmsg))
await writer.drain() await writer.drain()
finally: finally:
print('foo')
# XXX - how to test
#writer.write_eof()
writer.write_eof()


async def encses(): async def encses():
while True:
ptmsg = await reader.read(65535 - 16) # largest message
encmsg = proto.encrypt(ptmsg)
wrr.write(enclenfun(encmsg))
wrr.write(encmsg)
await wrr.drain()

done, pending = await asyncio.wait((decses(), encses()), return_when=asyncio.FIRST_COMPLETED)
for i in done:
print('v:', repr(await i))

done, pending = await asyncio.wait(pending, return_when=asyncio.FIRST_COMPLETED)
for i in done:
print('v:', repr(await i))
try:
while True:
ptmsg = await reader.read(65535 - 16) # largest message
if not ptmsg:
# eof
return 'enc'

encmsg = proto.encrypt(ptmsg)
wrr.write(enclenfun(encmsg))
wrr.write(encmsg)
await wrr.drain()
finally:
wrr.write_eof()


return done
return asyncio.gather(decses(), encses())


class TestListenSocket(unittest.TestCase): class TestListenSocket(unittest.TestCase):
def test_listensockstr(self): def test_listensockstr(self):
@@ -201,15 +198,19 @@ class Tests(unittest.TestCase):


@async_test @async_test
async def test_server(self): async def test_server(self):
# Test is plumbed:
# (reader, writer) -> servsock ->
# (rdr, wrr) NoiseForward (reader, writer) ->
# servptsock -> (ptsock[0], ptsock[1])
# Path that the server will sit on # Path that the server will sit on
servsockpath = os.path.join(self.tempdir, 'servsock') servsockpath = os.path.join(self.tempdir, 'servsock')
servarg = _makeunix(servsockpath) servarg = _makeunix(servsockpath)


# Path that the server will send pt data to # Path that the server will send pt data to
servsockpath = os.path.join(self.tempdir, 'servptsock')
servptpath = os.path.join(self.tempdir, 'servptsock')


# Setup pt target listener # Setup pt target listener
pttarg = _makeunix(servsockpath)
pttarg = _makeunix(servptpath)
ptsock = [] ptsock = []
def ptsockaccept(reader, writer, ptsock=ptsock): def ptsockaccept(reader, writer, ptsock=ptsock):
ptsock.append((reader, writer)) ptsock.append((reader, writer))
@@ -221,14 +222,10 @@ class Tests(unittest.TestCase):
event = asyncio.Event() event = asyncio.Event()


async def runnf(rdr, wrr): async def runnf(rdr, wrr):
print('a')
a = await NoiseForwarder('resp', self.server_key_pair[1], (rdr, wrr), pttarg) a = await NoiseForwarder('resp', self.server_key_pair[1], (rdr, wrr), pttarg)


print('b')
nfs.append(a) nfs.append(a)
print('c')
event.set() event.set()
print('d')


# Setup server listener # Setup server listener
ssock = await listensockstr(servarg, runnf) ssock = await listensockstr(servarg, runnf)
@@ -275,8 +272,9 @@ class Tests(unittest.TestCase):
# XXX - how to sync? # XXX - how to sync?
await asyncio.sleep(.1) await asyncio.sleep(.1)


ptreader, ptwriter = ptsock[0]
# read the test message # read the test message
rptmsg = await ptsock[0][0].readexactly(len(ptmsg))
rptmsg = await ptreader.readexactly(len(ptmsg))


self.assertEqual(rptmsg, ptmsg) self.assertEqual(rptmsg, ptmsg)


@@ -290,13 +288,13 @@ class Tests(unittest.TestCase):
await asyncio.sleep(.1) await asyncio.sleep(.1)


# read the test message # read the test message
rptmsg = await ptsock[0][0].readexactly(len(ptmsg))
rptmsg = await ptreader.readexactly(len(ptmsg))


self.assertEqual(rptmsg, ptmsg) self.assertEqual(rptmsg, ptmsg)


# now try the other way # now try the other way
ptmsg = os.urandom(912) ptmsg = os.urandom(912)
ptsock[0][1].write(ptmsg)
ptwriter.write(ptmsg)


# find out how much we need to read # find out how much we need to read
encmsg = await reader.readexactly(2 + 16) encmsg = await reader.readexactly(2 + 16)
@@ -309,12 +307,20 @@ class Tests(unittest.TestCase):


self.assertEqual(rptmsg, ptmsg) self.assertEqual(rptmsg, ptmsg)


# shut everything down
# shut down sending
writer.write_eof() writer.write_eof()
#ptsock[0][1].write_eof()


# XXX - how to sync?
await asyncio.sleep(.1)
# so pt reader should be shut down
r = await ptreader.read(1)
self.assertTrue(ptreader.at_eof())

# shut down pt
ptwriter.write_eof()

# make sure the enc reader is eof
r = await reader.read(1)
self.assertTrue(reader.at_eof())


await event.wait() await event.wait()
print(repr(nfs))

self.assertEqual(await nfs[0], [ 'dec', 'enc' ])

Loading…
Cancel
Save