koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 1 | import re |
koder aka kdanilov | 3a6633e | 2015-03-26 18:20:00 +0200 | [diff] [blame] | 2 | import getpass |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 3 | from typing import List |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 4 | |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 5 | |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 6 | from .common_types import IPAddr |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 7 | |
| 8 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 9 | class URIsNamespace: |
| 10 | class ReParts: |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 11 | user_rr = "[^:]*?" |
koder aka kdanilov | 7e0f7cf | 2015-05-01 17:24:35 +0300 | [diff] [blame] | 12 | host_rr = "[^:@]*?" |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 13 | port_rr = "\\d+" |
| 14 | key_file_rr = "[^:@]*" |
| 15 | passwd_rr = ".*?" |
| 16 | |
| 17 | re_dct = ReParts.__dict__ |
| 18 | |
| 19 | for attr_name, val in re_dct.items(): |
| 20 | if attr_name.endswith('_rr'): |
| 21 | new_rr = "(?P<{0}>{1})".format(attr_name[:-3], val) |
| 22 | setattr(ReParts, attr_name, new_rr) |
| 23 | |
| 24 | re_dct = ReParts.__dict__ |
| 25 | |
| 26 | templs = [ |
| 27 | "^{host_rr}$", |
koder aka kdanilov | 7e0f7cf | 2015-05-01 17:24:35 +0300 | [diff] [blame] | 28 | "^{host_rr}:{port_rr}$", |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 29 | "^{host_rr}::{key_file_rr}$", |
| 30 | "^{host_rr}:{port_rr}:{key_file_rr}$", |
koder aka kdanilov | 7e0f7cf | 2015-05-01 17:24:35 +0300 | [diff] [blame] | 31 | "^{user_rr}@{host_rr}$", |
| 32 | "^{user_rr}@{host_rr}:{port_rr}$", |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 33 | "^{user_rr}@{host_rr}::{key_file_rr}$", |
| 34 | "^{user_rr}@{host_rr}:{port_rr}:{key_file_rr}$", |
koder aka kdanilov | 7e0f7cf | 2015-05-01 17:24:35 +0300 | [diff] [blame] | 35 | "^{user_rr}:{passwd_rr}@{host_rr}$", |
| 36 | "^{user_rr}:{passwd_rr}@{host_rr}:{port_rr}$", |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 37 | ] |
| 38 | |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 39 | uri_reg_exprs = [] # type: List[str] |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 40 | for templ in templs: |
| 41 | uri_reg_exprs.append(templ.format(**re_dct)) |
| 42 | |
| 43 | |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 44 | class ConnCreds: |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 45 | def __init__(self, host: str, user: str, passwd: str = None, port: int = 22, |
| 46 | key_file: str = None, key: bytes = None) -> None: |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 47 | self.user = user |
| 48 | self.passwd = passwd |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 49 | self.addr = IPAddr(host, port) |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 50 | self.key_file = key_file |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 51 | self.key = key |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 52 | |
| 53 | def __str__(self) -> str: |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 54 | return "{}@{}:{}".format(self.user, self.addr.host, self.addr.port) |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 55 | |
| 56 | |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 57 | def parse_ssh_uri(uri: str) -> ConnCreds: |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 58 | """Parse ssh connection URL from one of following form |
| 59 | [ssh://]user:passwd@host[:port] |
| 60 | [ssh://][user@]host[:port][:key_file] |
| 61 | """ |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 62 | |
koder aka kdanilov | 4d4771c | 2015-04-23 01:32:02 +0300 | [diff] [blame] | 63 | if uri.startswith("ssh://"): |
| 64 | uri = uri[len("ssh://"):] |
| 65 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 66 | res = ConnCreds("", getpass.getuser()) |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 67 | |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 68 | for rr in URIsNamespace.uri_reg_exprs: |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 69 | rrm = re.match(rr, uri) |
| 70 | if rrm is not None: |
| 71 | res.__dict__.update(rrm.groupdict()) |
| 72 | return res |
koder aka kdanilov | 652cd80 | 2015-04-13 12:21:07 +0300 | [diff] [blame] | 73 | |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 74 | raise ValueError("Can't parse {0!r} as ssh uri value".format(uri)) |
| 75 | |
| 76 | |