diff --git a/poc/bin/request b/poc/bin/request index 3268445..0ebf854 100755 --- a/poc/bin/request +++ b/poc/bin/request @@ -16,4 +16,4 @@ # limitations under the License. # -curl -sf ${KAPOW_URL}/connections/${KAPOW_CONNECTION}/request$1 +curl -sf "${KAPOW_URL}/handlers/${KAPOW_HANDLER_ID}/request$1" diff --git a/poc/bin/response b/poc/bin/response index c0961e4..75dc091 100755 --- a/poc/bin/response +++ b/poc/bin/response @@ -24,17 +24,17 @@ import requests @click.command() @click.option("--url", envvar='KAPOW_URL') -@click.option("--connection", envvar='KAPOW_CONNECTION') +@click.option("--handler-id", envvar='KAPOW_HANDLER_ID') @click.argument("path", nargs=1) @click.argument("value", required=False) -def response(url, connection, path, value): +def response(url, handler_id, path, value): if value is None: data = sys.stdin.buffer else: data = value.encode('utf-8') try: - response = requests.put(f"{url}/connections/{connection}/response{path}", + response = requests.put(f"{url}/handlers/{handler_id}/response{path}", data=data) except requests.exceptions.ConnectionError: return False diff --git a/poc/examples/nmap/nmap.pow b/poc/examples/nmap/nmap.pow index 6df7789..b493921 100644 --- a/poc/examples/nmap/nmap.pow +++ b/poc/examples/nmap/nmap.pow @@ -16,4 +16,4 @@ # limitations under the License. # -kroute add -X GET '/list/{ip}' -c 'nmap -sL $(request /match/ip) | response /body' +kroute add -X GET '/list/{ip}' -c 'nmap -sL $(request /matches/ip) | response /body' diff --git a/poc/examples/operator.pow b/poc/examples/operator.pow index 6e86640..926d27e 100755 --- a/poc/examples/operator.pow +++ b/poc/examples/operator.pow @@ -16,7 +16,7 @@ # limitations under the License. # -kroute add /list/files -c 'ls -la $(request /param/path) | response /body' +kroute add /list/files -c 'ls -la $(request /params/path) | response /body' kroute add /list/processes -c 'ps -aux | response /body' @@ -31,11 +31,11 @@ kroute add /show/connections -c 'ss -pluton | response /body' kroute add /show/mounts -c 'mount | response /body' kroute add /tail/dmesg - <<-'EOF' - response /header/Content-Type text/plain + response /headers/Content-Type text/plain dmesg -w | response /stream EOF kroute add /tail/journal - <<-'EOF' - response /header/Content-Type text/plain + response /headers/Content-Type text/plain journalctl -f | response /stream EOF diff --git a/poc/examples/pandoc/pandoc.pow b/poc/examples/pandoc/pandoc.pow index 4bd2bbf..67d308d 100755 --- a/poc/examples/pandoc/pandoc.pow +++ b/poc/examples/pandoc/pandoc.pow @@ -17,8 +17,8 @@ # kroute add -X POST --entrypoint '/bin/zsh -c' '/convert/{from}/{to}' - <<-'EOF' - pandoc --from=$(request /match/from) \ - --to=$(request /match/to) \ + pandoc --from=$(request /matches/from) \ + --to=$(request /matches/to) \ --output=>(response /body) \ =(request /body) EOF diff --git a/poc/examples/pdfeditor/pdfeditor.pow b/poc/examples/pdfeditor/pdfeditor.pow index 7b26f7e..e1749db 100644 --- a/poc/examples/pdfeditor/pdfeditor.pow +++ b/poc/examples/pdfeditor/pdfeditor.pow @@ -17,4 +17,4 @@ # kroute add -X POST --entrypoint ./topdf '/editor/pdf' -kroute add / -c 'response /header/Content-Type text/html && response /body < pdfeditor.html' +kroute add / -c 'response /headers/Content-Type text/html && response /body < pdfeditor.html' diff --git a/poc/examples/pdfeditor/topdf b/poc/examples/pdfeditor/topdf index 722cd20..d05b2dc 100755 --- a/poc/examples/pdfeditor/topdf +++ b/poc/examples/pdfeditor/topdf @@ -19,7 +19,7 @@ tmpfile=$(mktemp --suffix=.pdf) pandoc --from=$(request /form/from) --to=pdf --output=${tmpfile} -t latex =(request /form/content) if [ $? -eq 0 ]; then - response /header/Content-Type application/pdf + response /headers/Content-Type application/pdf response /body < ${tmpfile} response /status 200 else diff --git a/poc/examples/torrent.pow b/poc/examples/torrent.pow index b6b4a39..2813641 100644 --- a/poc/examples/torrent.pow +++ b/poc/examples/torrent.pow @@ -17,7 +17,7 @@ # kroute add / - <<-'EOF' - response /header/Content-Type text/html + response /headers/Content-Type text/html response /body <<-HTML @@ -28,7 +28,7 @@ kroute add / - <<-'EOF' EOF kroute add /save/magnet -e '/bin/bash -c' - <<-'EOF' - link=$(request /param/link) + link=$(request /params/link) [ -z $link ] && response /status 400 && exit 0 watch_folder=/tmp @@ -37,7 +37,7 @@ kroute add /save/magnet -e '/bin/bash -c' - <<-'EOF' echo "d10:magnet-uri${#link}:${link}e" > "meta-${BASH_REMATCH[1]}.torrent" response /status 302 - response /header/Location /torrent/list + response /headers/Location /torrent/list EOF kroute add /torrent/list -c 'response /body "Not Implemented Yet"' diff --git a/poc/kapow b/poc/kapow index c2183c0..6832c9e 100755 --- a/poc/kapow +++ b/poc/kapow @@ -69,17 +69,17 @@ class Connection: return self.request.content elif res.path == 'request/path': return self.request.path.encode('utf-8') - elif res.path.startswith('request/match/'): + elif res.path.startswith('request/matches/'): return self.request.match_info[nth(2)].encode('utf-8') - elif res.path.startswith('request/param/'): + elif res.path.startswith('request/params/'): return self.request.rel_url.query[nth(2)].encode('utf-8') - elif res.path.startswith('request/header/'): + elif res.path.startswith('request/headers/'): return self.request.headers[nth(2)].encode('utf-8') - elif res.path.startswith('request/cookie/'): + elif res.path.startswith('request/cookies/'): return self.request.cookies[nth(2)].encode('utf-8') elif res.path.startswith('request/form/'): return (await self.request.post())[nth(2)].encode('utf-8') - elif res.path.startswith('request/file/'): + elif res.path.startswith('request/files/'): name = nth(2) content = nth(3) # filename / content field = (await self.request.post())[name] @@ -109,10 +109,10 @@ class Connection: self._status = int((await content.read()).decode('utf-8')) elif res.path == 'response/body': self._body.write(await content.read()) - elif res.path.startswith('response/header/'): + elif res.path.startswith('response/headers/'): clean = (await content.read()).rstrip(b'\n').decode('utf-8') self._headers[nth(2)] = clean - elif res.path.startswith('response/cookie/'): + elif res.path.startswith('response/cookies/'): clean = (await content.read()).rstrip(b'\n').decode('utf-8') self._cookies[nth(2)] = clean elif res.path == 'response/stream': @@ -227,8 +227,8 @@ def handle_route(entrypoint, command): shell_task = await asyncio.create_subprocess_shell( args, env={**os.environ, - "KAPOW_URL": "http://localhost:8080/kapow", - "KAPOW_CONNECTION": id + "KAPOW_URL": "http://localhost:8080", + "KAPOW_HANDLER_ID": id }, stdin=asyncio.subprocess.DEVNULL) @@ -253,7 +253,7 @@ async def get_routes(request): return web.json_response(list(request.app.router)) -async def create_route(request): +async def append_route(request): """Create a new Kapow! route.""" request.app.router._frozen = False content = await request.json() @@ -298,7 +298,7 @@ async def run_init_script(app): shell_task = await asyncio.create_subprocess_shell( cmd, env={**os.environ, - "KAPOW_URL": "http://localhost:8080/kapow" + "KAPOW_URL": "http://localhost:8080" }) await shell_task.wait() @@ -315,12 +315,15 @@ def kapow(): """Start aiohttp app.""" app = web.Application(client_max_size=1024**3) app.add_routes([ - web.get('/kapow/routes', get_routes), - web.post('/kapow/routes', create_route), - web.delete('/kapow/routes/{id}', delete_route), - web.get('/kapow/connections/{id}/{field:.*}', get_field), - # web.post('/kapow/connections/{id}/{field:.*}', append_field), - web.put('/kapow/connections/{id}/{field:.*}', set_field), + # Control API + web.get('/routes', get_routes), + web.post('/routes', append_route), # TODO: return route index + # web.put('/routes', insert_route), # TODO: return route index + web.delete('/routes/{id}', delete_route), + + # Data API + web.get('/handlers/{id}/{field:.*}', get_field), + web.put('/handlers/{id}/{field:.*}', set_field), ]) app.on_startup.append(start_background_tasks) web.run_app(app)