1
0
mirror of https://github.com/falk-werner/webfuse synced 2024-10-27 20:34:10 +00:00

allow to specify path, url and authentication token via command line

This commit is contained in:
Falk Werner 2023-02-04 09:36:37 +01:00
parent a83fcd7260
commit b55ae45018
2 changed files with 22 additions and 6 deletions

View File

@ -2,7 +2,7 @@
AUTH_TOKEN="$1" AUTH_TOKEN="$1"
if [[ "$AUTH_TOKEN" == "user:bob;token=foo" ]] if [[ "$AUTH_TOKEN" == "simple_token" ]]
then then
echo "$(date): webfuse: auth granted: $AUTH_TOKEN" >> /tmp/webfuse_auth.log echo "$(date): webfuse: auth granted: $AUTH_TOKEN" >> /tmp/webfuse_auth.log
else else

View File

@ -7,6 +7,7 @@ import os
import stat import stat
import errno import errno
import getpass import getpass
import argparse
import websockets import websockets
INVALID_FD = 0xffffffffffffffff INVALID_FD = 0xffffffffffffffff
@ -252,9 +253,10 @@ class MessageWriter:
class FilesystemProvider: class FilesystemProvider:
"""Webfuse filesystem provider.""" """Webfuse filesystem provider."""
def __init__(self, path, url): def __init__(self, path, url, token):
self.root = os.path.abspath(path) self.root = os.path.abspath(path)
self.url = url self.url = url
self.token = token
self.commands = { self.commands = {
0x01: FilesystemProvider.access, 0x01: FilesystemProvider.access,
0x02: FilesystemProvider.getattr, 0x02: FilesystemProvider.getattr,
@ -282,7 +284,7 @@ class FilesystemProvider:
} }
async def run(self): async def run(self):
extra_headers = [("X-Auth-Token", "user:bob;token=foo")] extra_headers = [("X-Auth-Token", self.token)] if self.token != "" else []
# pylint: disable-next=no-member # pylint: disable-next=no-member
async with websockets.connect(self.url, extra_headers=extra_headers) as connection: async with websockets.connect(self.url, extra_headers=extra_headers) as connection:
while True: while True:
@ -575,10 +577,24 @@ class FilesystemProvider:
writer.write_u64(buffer.f_namemax) writer.write_u64(buffer.f_namemax)
def getcreds(self, _, writer): def getcreds(self, _, writer):
credentials = getpass.getpass(prompt="credentials: ") credentials = self.token if self.token == "" else getpass.getpass(prompt="credentials: ")
writer.write_str(credentials) writer.write_str(credentials)
def main():
parser = argparse.ArgumentParser(prog='webfuse_provider')
parser.add_argument('-p', '--path', type=str, required=False, default='.',
help='path to provide (default: \".\")')
parser.add_argument('-u', '--url', type=str, required=True,
help='URL of webfuse server, e.g. \"ws://localhost:8081\"')
parser.add_argument('-t', '--token', type=str, required=False, default='',
help='authentication token (default: contents of environment variable WEBFUSE_TOKEN)')
args = parser.parse_args()
token = args.token if args.token != "" else os.environ.get('WEBFUSE_TOKEN')
provider = FilesystemProvider(args.path, args.url, token)
try:
asyncio.run(provider.run())
except KeyboardInterrupt as _:
pass
if __name__ == '__main__': if __name__ == '__main__':
provider = FilesystemProvider('.', 'ws://localhost:8081') main()
asyncio.run(provider.run())