exchangertool 0.1.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- exchangertool-0.1.1/LICENSE +21 -0
- exchangertool-0.1.1/PKG-INFO +60 -0
- exchangertool-0.1.1/README.md +49 -0
- exchangertool-0.1.1/exchanger/__init__.py +3 -0
- exchangertool-0.1.1/exchanger/__main__.py +6 -0
- exchangertool-0.1.1/exchanger/cli.py +105 -0
- exchangertool-0.1.1/exchanger/http_.py +219 -0
- exchangertool-0.1.1/exchanger/net_.py +172 -0
- exchangertool-0.1.1/exchanger/smb_.py +7 -0
- exchangertool-0.1.1/exchangertool.egg-info/PKG-INFO +60 -0
- exchangertool-0.1.1/exchangertool.egg-info/SOURCES.txt +15 -0
- exchangertool-0.1.1/exchangertool.egg-info/dependency_links.txt +1 -0
- exchangertool-0.1.1/exchangertool.egg-info/entry_points.txt +2 -0
- exchangertool-0.1.1/exchangertool.egg-info/requires.txt +1 -0
- exchangertool-0.1.1/exchangertool.egg-info/top_level.txt +1 -0
- exchangertool-0.1.1/pyproject.toml +19 -0
- exchangertool-0.1.1/setup.cfg +4 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 didntchooseaname
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: exchangertool
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary: Minimal CLI to send or receive files over HTTP or SMB.
|
|
5
|
+
License-Expression: MIT
|
|
6
|
+
Requires-Python: >=3.10
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Requires-Dist: smbprotocol
|
|
10
|
+
Dynamic: license-file
|
|
11
|
+
|
|
12
|
+
# exchanger
|
|
13
|
+
|
|
14
|
+
Serve files or listen to receive (target POSTs file to host). Default port 8080.
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
pipx install exchangertool
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
exchanger
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Help menu
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
___________ .__
|
|
32
|
+
\_ _____/__ ___ ____ | |__ _____ ____ ____ ___________
|
|
33
|
+
| __)_\ \/ // ___\| | \\__ \ / \ / ___\_/ __ \_ __ \
|
|
34
|
+
| \> <\ \___| Y \/ __ \| | \/ /_/ > ___/| | \/
|
|
35
|
+
/_______ /__/\_ \\___ >___| (____ /___| /\___ / \___ >__|
|
|
36
|
+
\/ \/ \/ \/ \/ \//_____/ \/
|
|
37
|
+
|
|
38
|
+
Serve files or listen to receive (target POSTs to host). Default port 8080.
|
|
39
|
+
|
|
40
|
+
positional arguments:
|
|
41
|
+
{serve,receive}
|
|
42
|
+
command (default:
|
|
43
|
+
serve)
|
|
44
|
+
serve serve current
|
|
45
|
+
directory; others
|
|
46
|
+
can send/receive
|
|
47
|
+
(default)
|
|
48
|
+
receive listen for target to
|
|
49
|
+
POST file to you
|
|
50
|
+
|
|
51
|
+
options:
|
|
52
|
+
-h, --help show this help
|
|
53
|
+
message and exit
|
|
54
|
+
|
|
55
|
+
examples:
|
|
56
|
+
exchanger (same as serve)
|
|
57
|
+
exchanger serve (target can GET or POST)
|
|
58
|
+
exchanger receive (host listens; target POSTs file to you)
|
|
59
|
+
exchanger receive --dir /tmp --port 8080
|
|
60
|
+
```
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# exchanger
|
|
2
|
+
|
|
3
|
+
Serve files or listen to receive (target POSTs file to host). Default port 8080.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pipx install exchangertool
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
exchanger
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Help menu
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
___________ .__
|
|
21
|
+
\_ _____/__ ___ ____ | |__ _____ ____ ____ ___________
|
|
22
|
+
| __)_\ \/ // ___\| | \\__ \ / \ / ___\_/ __ \_ __ \
|
|
23
|
+
| \> <\ \___| Y \/ __ \| | \/ /_/ > ___/| | \/
|
|
24
|
+
/_______ /__/\_ \\___ >___| (____ /___| /\___ / \___ >__|
|
|
25
|
+
\/ \/ \/ \/ \/ \//_____/ \/
|
|
26
|
+
|
|
27
|
+
Serve files or listen to receive (target POSTs to host). Default port 8080.
|
|
28
|
+
|
|
29
|
+
positional arguments:
|
|
30
|
+
{serve,receive}
|
|
31
|
+
command (default:
|
|
32
|
+
serve)
|
|
33
|
+
serve serve current
|
|
34
|
+
directory; others
|
|
35
|
+
can send/receive
|
|
36
|
+
(default)
|
|
37
|
+
receive listen for target to
|
|
38
|
+
POST file to you
|
|
39
|
+
|
|
40
|
+
options:
|
|
41
|
+
-h, --help show this help
|
|
42
|
+
message and exit
|
|
43
|
+
|
|
44
|
+
examples:
|
|
45
|
+
exchanger (same as serve)
|
|
46
|
+
exchanger serve (target can GET or POST)
|
|
47
|
+
exchanger receive (host listens; target POSTs file to you)
|
|
48
|
+
exchanger receive --dir /tmp --port 8080
|
|
49
|
+
```
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
"""CLI argument parsing and dispatch."""
|
|
2
|
+
|
|
3
|
+
import argparse
|
|
4
|
+
import os
|
|
5
|
+
import sys
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
_BANNER = r"""
|
|
9
|
+
___________ .__
|
|
10
|
+
\_ _____/__ ___ ____ | |__ _____ ____ ____ ___________
|
|
11
|
+
| __)_\ \/ // ___\| | \\__ \ / \ / ___\_/ __ \_ __ \
|
|
12
|
+
| \> <\ \___| Y \/ __ \| | \/ /_/ > ___/| | \/
|
|
13
|
+
/_______ /__/\_ \\___ >___| (____ /___| /\___ / \___ >__|
|
|
14
|
+
\/ \/ \/ \/ \/ \//_____/ \/
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def build_parser():
|
|
19
|
+
parser = argparse.ArgumentParser(
|
|
20
|
+
prog="exchanger",
|
|
21
|
+
description=_BANNER + "\nServe files or listen to receive (target POSTs to host). Default port 8080.",
|
|
22
|
+
formatter_class=argparse.RawDescriptionHelpFormatter,
|
|
23
|
+
epilog="""
|
|
24
|
+
examples:
|
|
25
|
+
%(prog)s (same as serve)
|
|
26
|
+
%(prog)s serve (target can GET or POST)
|
|
27
|
+
%(prog)s receive (host listens; target POSTs file to you)
|
|
28
|
+
%(prog)s receive --dir /tmp --port 8080
|
|
29
|
+
""",
|
|
30
|
+
)
|
|
31
|
+
def add_protocol_port(sp):
|
|
32
|
+
sp.add_argument(
|
|
33
|
+
"--protocol",
|
|
34
|
+
choices=("http", "smb"),
|
|
35
|
+
default="http",
|
|
36
|
+
help="protocol (default: http)",
|
|
37
|
+
)
|
|
38
|
+
sp.add_argument(
|
|
39
|
+
"-p", "--port",
|
|
40
|
+
type=int,
|
|
41
|
+
default=8080,
|
|
42
|
+
help="port (default: 8080)",
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
sub = parser.add_subparsers(dest="command", help="command (default: serve)")
|
|
46
|
+
|
|
47
|
+
# serve (default when no command)
|
|
48
|
+
serve_p = sub.add_parser("serve", help="serve current directory; others can send/receive (default)")
|
|
49
|
+
add_protocol_port(serve_p)
|
|
50
|
+
serve_p.add_argument(
|
|
51
|
+
"-d", "--dir",
|
|
52
|
+
default=".",
|
|
53
|
+
metavar="DIR",
|
|
54
|
+
help="directory to serve (default: current directory)",
|
|
55
|
+
)
|
|
56
|
+
serve_p.add_argument(
|
|
57
|
+
"--bind",
|
|
58
|
+
default="0.0.0.0",
|
|
59
|
+
metavar="ADDR",
|
|
60
|
+
help="address to bind (default: 0.0.0.0)",
|
|
61
|
+
)
|
|
62
|
+
serve_p.set_defaults(func=_cmd_serve)
|
|
63
|
+
|
|
64
|
+
# receive (host listens; target POSTs to host)
|
|
65
|
+
recv_p = sub.add_parser("receive", help="listen for target to POST file to you")
|
|
66
|
+
add_protocol_port(recv_p)
|
|
67
|
+
recv_p.add_argument(
|
|
68
|
+
"-d", "--dir",
|
|
69
|
+
default=".",
|
|
70
|
+
metavar="DIR",
|
|
71
|
+
help="directory to save received files (default: current directory)",
|
|
72
|
+
)
|
|
73
|
+
recv_p.add_argument(
|
|
74
|
+
"--bind",
|
|
75
|
+
default="0.0.0.0",
|
|
76
|
+
metavar="ADDR",
|
|
77
|
+
help="address to bind (default: 0.0.0.0)",
|
|
78
|
+
)
|
|
79
|
+
recv_p.set_defaults(func=_cmd_receive)
|
|
80
|
+
|
|
81
|
+
return parser
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def _cmd_serve(args):
|
|
85
|
+
if args.protocol == "smb":
|
|
86
|
+
from .smb_ import serve_smb
|
|
87
|
+
serve_smb(args)
|
|
88
|
+
else:
|
|
89
|
+
from .http_ import serve_http
|
|
90
|
+
serve_http(args)
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def _cmd_receive(args):
|
|
94
|
+
if args.protocol == "smb":
|
|
95
|
+
sys.exit("exchanger: receive is listen-only; use --protocol http.")
|
|
96
|
+
from .http_ import serve_http
|
|
97
|
+
serve_http(args, receive_only=True)
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def main():
|
|
101
|
+
parser = build_parser()
|
|
102
|
+
args = parser.parse_args()
|
|
103
|
+
if args.command is None:
|
|
104
|
+
args = parser.parse_args(["serve"] + [a for a in sys.argv[1:] if a not in ("-h", "--help")])
|
|
105
|
+
args.func(args)
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
"""HTTP server and client for file exchange."""
|
|
2
|
+
|
|
3
|
+
import http.server
|
|
4
|
+
import mimetypes
|
|
5
|
+
import os
|
|
6
|
+
import threading
|
|
7
|
+
import time
|
|
8
|
+
import urllib.parse
|
|
9
|
+
import urllib.request
|
|
10
|
+
import ssl
|
|
11
|
+
import sys
|
|
12
|
+
|
|
13
|
+
_SPINNER = (".", "\\", "/", "-")
|
|
14
|
+
_SPINNER_INTERVAL = 0.12
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def _spinner_loop(stop: threading.Event) -> None:
|
|
18
|
+
i = 0
|
|
19
|
+
while not stop.is_set():
|
|
20
|
+
sys.stderr.write(f"\r {_SPINNER[i % len(_SPINNER)]} ")
|
|
21
|
+
sys.stderr.flush()
|
|
22
|
+
i += 1
|
|
23
|
+
stop.wait(_SPINNER_INTERVAL)
|
|
24
|
+
sys.stderr.write("\r \r")
|
|
25
|
+
sys.stderr.flush()
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def _safe_join(base: str, path: str) -> str | None:
|
|
29
|
+
"""Resolve path under base; return None if path escapes base."""
|
|
30
|
+
base = os.path.abspath(base)
|
|
31
|
+
full = os.path.abspath(os.path.join(base, path.lstrip("/")))
|
|
32
|
+
if not full.startswith(base):
|
|
33
|
+
return None
|
|
34
|
+
return full
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class ExchangeHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
|
|
38
|
+
"""Serves directory (GET) and accepts file uploads (POST)."""
|
|
39
|
+
|
|
40
|
+
def __init__(self, *args, directory=None, **kwargs):
|
|
41
|
+
self.directory = directory or os.getcwd()
|
|
42
|
+
super().__init__(*args, directory=self.directory, **kwargs)
|
|
43
|
+
|
|
44
|
+
def do_GET(self):
|
|
45
|
+
path = urllib.parse.unquote(self.path)
|
|
46
|
+
if path == "/" or path == "":
|
|
47
|
+
path = "/"
|
|
48
|
+
self.send_response(200)
|
|
49
|
+
self.send_header("Content-type", "text/plain; charset=utf-8")
|
|
50
|
+
self.end_headers()
|
|
51
|
+
self.wfile.write(b"exchanger: GET /path/to/file to download, POST to upload\n")
|
|
52
|
+
return
|
|
53
|
+
if "?" in path:
|
|
54
|
+
path = path.split("?")[0]
|
|
55
|
+
local = _safe_join(self.directory, path)
|
|
56
|
+
if local is None:
|
|
57
|
+
self.send_error(403, "Forbidden")
|
|
58
|
+
return
|
|
59
|
+
if not os.path.isfile(local):
|
|
60
|
+
self.send_error(404, "Not Found")
|
|
61
|
+
return
|
|
62
|
+
size = os.path.getsize(local)
|
|
63
|
+
ctype, _ = mimetypes.guess_type(local)
|
|
64
|
+
ctype = ctype or "application/octet-stream"
|
|
65
|
+
self.send_response(200)
|
|
66
|
+
self.send_header("Content-type", ctype)
|
|
67
|
+
self.send_header("Content-Length", str(size))
|
|
68
|
+
self.end_headers()
|
|
69
|
+
with open(local, "rb") as f:
|
|
70
|
+
self.wfile.write(f.read())
|
|
71
|
+
|
|
72
|
+
def do_POST(self):
|
|
73
|
+
path = urllib.parse.unquote(self.path)
|
|
74
|
+
if path == "/" or path == "":
|
|
75
|
+
path = "/"
|
|
76
|
+
if "?" in path:
|
|
77
|
+
path = path.split("?")[0]
|
|
78
|
+
local = _safe_join(self.directory, path)
|
|
79
|
+
if local is None:
|
|
80
|
+
self.send_error(403, "Forbidden")
|
|
81
|
+
return
|
|
82
|
+
dirpath = os.path.dirname(local)
|
|
83
|
+
if dirpath and not os.path.isdir(dirpath):
|
|
84
|
+
try:
|
|
85
|
+
os.makedirs(dirpath, exist_ok=True)
|
|
86
|
+
except OSError:
|
|
87
|
+
self.send_error(500, "Cannot create directory")
|
|
88
|
+
return
|
|
89
|
+
content_type = self.headers.get("Content-Type", "")
|
|
90
|
+
length = self.headers.get("Content-Length")
|
|
91
|
+
if length is None:
|
|
92
|
+
self.send_error(411, "Content-Length required")
|
|
93
|
+
return
|
|
94
|
+
try:
|
|
95
|
+
length = int(length)
|
|
96
|
+
except ValueError:
|
|
97
|
+
self.send_error(400, "Invalid Content-Length")
|
|
98
|
+
return
|
|
99
|
+
if "multipart/form-data" in content_type:
|
|
100
|
+
boundary = None
|
|
101
|
+
for part in content_type.split(";"):
|
|
102
|
+
part = part.strip()
|
|
103
|
+
if part.startswith("boundary="):
|
|
104
|
+
boundary = part[9:].strip().strip('"')
|
|
105
|
+
break
|
|
106
|
+
if not boundary:
|
|
107
|
+
self.send_error(400, "Missing boundary")
|
|
108
|
+
return
|
|
109
|
+
data = self.rfile.read(length)
|
|
110
|
+
try:
|
|
111
|
+
body = _parse_multipart(data, boundary)
|
|
112
|
+
except ValueError:
|
|
113
|
+
self.send_error(400, "Invalid multipart")
|
|
114
|
+
return
|
|
115
|
+
if "file" in body:
|
|
116
|
+
payload = body["file"]
|
|
117
|
+
else:
|
|
118
|
+
keys = [k for k in body if k != "path"]
|
|
119
|
+
payload = body[keys[0]] if keys else b""
|
|
120
|
+
out_path = local if local != self.directory else os.path.join(self.directory, "upload")
|
|
121
|
+
if os.path.isdir(out_path):
|
|
122
|
+
out_path = os.path.join(out_path, "upload")
|
|
123
|
+
else:
|
|
124
|
+
payload = self.rfile.read(length)
|
|
125
|
+
if path == "/" or path == "" or (local and os.path.isdir(local)):
|
|
126
|
+
x_fn = self.headers.get("X-Filename", "").strip()
|
|
127
|
+
if x_fn:
|
|
128
|
+
safe = os.path.basename(x_fn.replace("\\", "/"))
|
|
129
|
+
if safe and "/" not in safe and ".." not in safe:
|
|
130
|
+
out_path = os.path.join(self.directory, safe)
|
|
131
|
+
else:
|
|
132
|
+
out_path = os.path.join(self.directory, "upload")
|
|
133
|
+
else:
|
|
134
|
+
out_path = os.path.join(self.directory, "upload")
|
|
135
|
+
else:
|
|
136
|
+
out_path = local
|
|
137
|
+
if os.path.isdir(out_path):
|
|
138
|
+
out_path = os.path.join(out_path, "upload")
|
|
139
|
+
try:
|
|
140
|
+
with open(out_path, "wb") as f:
|
|
141
|
+
f.write(payload)
|
|
142
|
+
except OSError as e:
|
|
143
|
+
self.send_error(500, str(e))
|
|
144
|
+
return
|
|
145
|
+
self.send_response(201)
|
|
146
|
+
self.send_header("Content-type", "text/plain; charset=utf-8")
|
|
147
|
+
self.end_headers()
|
|
148
|
+
self.wfile.write(f"created {out_path}\n".encode())
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def _parse_multipart(data: bytes, boundary: bytes) -> dict:
|
|
152
|
+
if isinstance(boundary, str):
|
|
153
|
+
boundary = boundary.encode()
|
|
154
|
+
parts = data.split(b"--" + boundary)
|
|
155
|
+
out = {}
|
|
156
|
+
for part in parts:
|
|
157
|
+
part = part.strip()
|
|
158
|
+
if not part or part == b"--":
|
|
159
|
+
continue
|
|
160
|
+
if b"\r\n\r\n" not in part:
|
|
161
|
+
continue
|
|
162
|
+
head, body = part.split(b"\r\n\r\n", 1)
|
|
163
|
+
if body.endswith(b"\r\n"):
|
|
164
|
+
body = body[:-2]
|
|
165
|
+
name = None
|
|
166
|
+
for line in head.split(b"\r\n"):
|
|
167
|
+
if line.lower().startswith(b"content-disposition:"):
|
|
168
|
+
for token in line.split(b";")[1:]:
|
|
169
|
+
token = token.strip()
|
|
170
|
+
if token.lower().startswith(b"name="):
|
|
171
|
+
name = token[5:].strip(b'"').decode("utf-8", "replace")
|
|
172
|
+
break
|
|
173
|
+
break
|
|
174
|
+
if name is not None:
|
|
175
|
+
out[name] = body
|
|
176
|
+
return out
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
def serve_http(args, receive_only: bool = False):
|
|
180
|
+
os.chdir(args.dir)
|
|
181
|
+
dir_abs = os.path.abspath(args.dir)
|
|
182
|
+
if not os.path.isdir(dir_abs):
|
|
183
|
+
sys.exit(f"exchanger: not a directory: {args.dir}")
|
|
184
|
+
handler = lambda *a, **k: ExchangeHTTPRequestHandler(*a, directory=dir_abs, **k)
|
|
185
|
+
server = http.server.HTTPServer((args.bind, args.port), handler)
|
|
186
|
+
if args.port == 443:
|
|
187
|
+
try:
|
|
188
|
+
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
|
|
189
|
+
ctx.load_cert_chain(
|
|
190
|
+
os.path.expanduser("~/.exchanger/cert.pem"),
|
|
191
|
+
os.path.expanduser("~/.exchanger/key.pem"),
|
|
192
|
+
)
|
|
193
|
+
server.socket = ctx.wrap_socket(server.socket, server_side=True)
|
|
194
|
+
except FileNotFoundError:
|
|
195
|
+
print("exchanger: port 443 requires TLS; no cert at ~/.exchanger/cert.pem", file=sys.stderr)
|
|
196
|
+
print("exchanger: use --port 8443 for plain HTTP or provide certs.", file=sys.stderr)
|
|
197
|
+
sys.exit(1)
|
|
198
|
+
if receive_only:
|
|
199
|
+
print(f"exchanger: listening to receive (target POSTs to you) on {args.bind}:{args.port}")
|
|
200
|
+
from .net_ import print_commands_receive_listen
|
|
201
|
+
print_commands_receive_listen(args.port, getattr(args, "protocol", "http"))
|
|
202
|
+
else:
|
|
203
|
+
print(f"exchanger: serving {dir_abs} on {args.bind}:{args.port} (protocol http)")
|
|
204
|
+
from .net_ import print_commands_serve
|
|
205
|
+
print_commands_serve(args.port, getattr(args, "protocol", "http"))
|
|
206
|
+
stop = threading.Event()
|
|
207
|
+
spinner = threading.Thread(target=_spinner_loop, args=(stop,), daemon=True)
|
|
208
|
+
spinner.start()
|
|
209
|
+
try:
|
|
210
|
+
server.serve_forever()
|
|
211
|
+
except KeyboardInterrupt:
|
|
212
|
+
pass
|
|
213
|
+
finally:
|
|
214
|
+
stop.set()
|
|
215
|
+
spinner.join(timeout=0.5)
|
|
216
|
+
sys.stderr.write("\r \r")
|
|
217
|
+
sys.stderr.flush()
|
|
218
|
+
|
|
219
|
+
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
"""Local IP detection and interface picker."""
|
|
2
|
+
|
|
3
|
+
import re
|
|
4
|
+
import subprocess
|
|
5
|
+
import sys
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def get_all_interfaces() -> list[tuple[str, str]]:
|
|
9
|
+
"""Return list of (interface_name, ipv4) excluding lo."""
|
|
10
|
+
out = subprocess.run(
|
|
11
|
+
["ip", "-4", "addr", "show"],
|
|
12
|
+
capture_output=True,
|
|
13
|
+
text=True,
|
|
14
|
+
timeout=2,
|
|
15
|
+
)
|
|
16
|
+
if out.returncode != 0:
|
|
17
|
+
return []
|
|
18
|
+
current_iface = None
|
|
19
|
+
result = []
|
|
20
|
+
for line in out.stdout.splitlines():
|
|
21
|
+
if line and not line[0].isspace():
|
|
22
|
+
parts = line.split(":", 2)
|
|
23
|
+
current_iface = parts[1].strip() if len(parts) > 1 else None
|
|
24
|
+
if current_iface and current_iface != "lo":
|
|
25
|
+
m = re.search(r"inet\s+(\d+\.\d+\.\d+\.\d+)", line)
|
|
26
|
+
if m:
|
|
27
|
+
result.append((current_iface, m.group(1)))
|
|
28
|
+
return result
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def _pick_interface_fzf(choices: list[tuple[str, str]]) -> str | None:
|
|
32
|
+
"""Use fzf to pick one; return selected ip or None."""
|
|
33
|
+
lines = [f"{name} {ip}" for name, ip in choices]
|
|
34
|
+
try:
|
|
35
|
+
p = subprocess.run(
|
|
36
|
+
["fzf", "--height", "10", "-1"],
|
|
37
|
+
input="\n".join(lines),
|
|
38
|
+
capture_output=True,
|
|
39
|
+
text=True,
|
|
40
|
+
timeout=30,
|
|
41
|
+
)
|
|
42
|
+
if p.returncode != 0 or not p.stdout.strip():
|
|
43
|
+
return None
|
|
44
|
+
selected = p.stdout.strip()
|
|
45
|
+
for name, ip in choices:
|
|
46
|
+
if f"{name} {ip}" == selected or ip == selected:
|
|
47
|
+
return ip
|
|
48
|
+
if re.match(r"^\d+\.\d+\.\d+\.\d+$", selected):
|
|
49
|
+
return selected
|
|
50
|
+
parts = selected.split()
|
|
51
|
+
return parts[-1] if parts and re.match(r"^\d+\.\d+\.\d+\.\d+$", parts[-1]) else None
|
|
52
|
+
except (FileNotFoundError, subprocess.TimeoutExpired):
|
|
53
|
+
return None
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def _pick_interface_menu(choices: list[tuple[str, str]]) -> str | None:
|
|
57
|
+
"""Numbered menu on stderr; return selected ip or None."""
|
|
58
|
+
for i, (name, ip) in enumerate(choices, 1):
|
|
59
|
+
sys.stderr.write(f" {i}) {name} {ip}\n")
|
|
60
|
+
sys.stderr.write(" choice (1-{}): ".format(len(choices)))
|
|
61
|
+
sys.stderr.flush()
|
|
62
|
+
try:
|
|
63
|
+
line = input().strip()
|
|
64
|
+
idx = int(line)
|
|
65
|
+
if 1 <= idx <= len(choices):
|
|
66
|
+
return choices[idx - 1][1]
|
|
67
|
+
except (ValueError, EOFError):
|
|
68
|
+
pass
|
|
69
|
+
return None
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def pick_interface() -> str | None:
|
|
73
|
+
"""Show fuzzy finder or menu to select interface; return its IPv4 or None."""
|
|
74
|
+
choices = get_all_interfaces()
|
|
75
|
+
if not choices:
|
|
76
|
+
return None
|
|
77
|
+
if len(choices) == 1:
|
|
78
|
+
return choices[0][1]
|
|
79
|
+
if not sys.stderr.isatty():
|
|
80
|
+
return get_local_ip()
|
|
81
|
+
sys.stderr.write("\n select interface (for command box):\n")
|
|
82
|
+
return _pick_interface_fzf(choices) or _pick_interface_menu(choices)
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def get_local_ip() -> str | None:
|
|
86
|
+
"""Return preferred local IPv4 (tun0 first, then first non-lo), or None."""
|
|
87
|
+
ifaces = get_all_interfaces()
|
|
88
|
+
for name, ip in ifaces:
|
|
89
|
+
if name == "tun0":
|
|
90
|
+
return ip
|
|
91
|
+
return ifaces[0][1] if ifaces else None
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def _port_proto(port: int, protocol: str) -> tuple[str, str]:
|
|
95
|
+
port_opt = "" if port == 8080 else f" -p {port}"
|
|
96
|
+
proto_opt = "" if protocol == "http" else f" --protocol {protocol}"
|
|
97
|
+
return port_opt, proto_opt
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def _base_url(ip: str, port: int) -> str:
|
|
101
|
+
scheme = "https" if port == 443 else "http"
|
|
102
|
+
if port in (80, 443):
|
|
103
|
+
return f"{scheme}://{ip}"
|
|
104
|
+
return f"{scheme}://{ip}:{port}"
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def _target_receive_linux(base: str, path: str = "/path/to/file", out: str = "out") -> str:
|
|
108
|
+
return f"curl -o {out} {base}{path}\nwget -O {out} {base}{path}"
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def _target_receive_win(base: str, path: str = "/path/to/file", out: str = "out") -> str:
|
|
112
|
+
return f"curl -o {out} {base}{path}\ncertutil -urlcache -split -f {base}{path} {out}"
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def _target_send_linux(base: str, path: str = "./file", name: str = "file") -> str:
|
|
116
|
+
return f"curl -X POST --data-binary @{path} {base}/{name}"
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def _target_send_win(base: str, path: str = "file", name: str = "file") -> str:
|
|
120
|
+
return f"curl -X POST --data-binary @{path} {base}/{name}"
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
_WIDTH = 56
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def _box_line(text: str) -> str:
|
|
127
|
+
return " | " + text.ljust(_WIDTH) + " |\n"
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def _section(title: str, lines: list[str]) -> str:
|
|
131
|
+
out = ["", f" {title}", " " + "-" * (len(title) + 2)]
|
|
132
|
+
for line in lines:
|
|
133
|
+
out.append(f" {line}")
|
|
134
|
+
return "\n".join(out) + "\n"
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def print_commands_serve(port: int, protocol: str = "http") -> None:
|
|
138
|
+
"""Print copy-paste commands for target (curl/wget/certutil) when we are serving."""
|
|
139
|
+
my_ip = pick_interface()
|
|
140
|
+
if not my_ip or protocol != "http":
|
|
141
|
+
return
|
|
142
|
+
base = _base_url(my_ip, port)
|
|
143
|
+
recv_linux = _target_receive_linux(base).split("\n")
|
|
144
|
+
send_linux = _target_send_linux(base).split("\n")
|
|
145
|
+
recv_win = _target_receive_win(base).split("\n")
|
|
146
|
+
send_win = _target_send_win(base).split("\n")
|
|
147
|
+
sys.stderr.write("\n")
|
|
148
|
+
sys.stderr.write(" +" + "-" * (_WIDTH + 4) + "+\n")
|
|
149
|
+
sys.stderr.write(_box_line("run on target (copy-paste)"))
|
|
150
|
+
sys.stderr.write(" +" + "-" * (_WIDTH + 4) + "+\n")
|
|
151
|
+
sys.stderr.write(_section("linux: receive from you", recv_linux))
|
|
152
|
+
sys.stderr.write(_section("linux: send to you", send_linux))
|
|
153
|
+
sys.stderr.write(_section("windows: receive from you", recv_win))
|
|
154
|
+
sys.stderr.write(_section("windows: send to you", send_win))
|
|
155
|
+
sys.stderr.write("\n")
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
def print_commands_receive_listen(port: int, protocol: str = "http") -> None:
|
|
159
|
+
"""Print copy-paste for target to POST file to you (host is listening)."""
|
|
160
|
+
my_ip = pick_interface()
|
|
161
|
+
if not my_ip or protocol != "http":
|
|
162
|
+
return
|
|
163
|
+
base = _base_url(my_ip, port)
|
|
164
|
+
send_linux = _target_send_linux(base).split("\n")
|
|
165
|
+
send_win = _target_send_win(base).split("\n")
|
|
166
|
+
sys.stderr.write("\n")
|
|
167
|
+
sys.stderr.write(" +" + "-" * (_WIDTH + 4) + "+\n")
|
|
168
|
+
sys.stderr.write(_box_line("run on target (POST file to you)"))
|
|
169
|
+
sys.stderr.write(" +" + "-" * (_WIDTH + 4) + "+\n")
|
|
170
|
+
sys.stderr.write(_section("linux", send_linux))
|
|
171
|
+
sys.stderr.write(_section("windows", send_win))
|
|
172
|
+
sys.stderr.write("\n")
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: exchangertool
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary: Minimal CLI to send or receive files over HTTP or SMB.
|
|
5
|
+
License-Expression: MIT
|
|
6
|
+
Requires-Python: >=3.10
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Requires-Dist: smbprotocol
|
|
10
|
+
Dynamic: license-file
|
|
11
|
+
|
|
12
|
+
# exchanger
|
|
13
|
+
|
|
14
|
+
Serve files or listen to receive (target POSTs file to host). Default port 8080.
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
pipx install exchangertool
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
exchanger
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Help menu
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
___________ .__
|
|
32
|
+
\_ _____/__ ___ ____ | |__ _____ ____ ____ ___________
|
|
33
|
+
| __)_\ \/ // ___\| | \\__ \ / \ / ___\_/ __ \_ __ \
|
|
34
|
+
| \> <\ \___| Y \/ __ \| | \/ /_/ > ___/| | \/
|
|
35
|
+
/_______ /__/\_ \\___ >___| (____ /___| /\___ / \___ >__|
|
|
36
|
+
\/ \/ \/ \/ \/ \//_____/ \/
|
|
37
|
+
|
|
38
|
+
Serve files or listen to receive (target POSTs to host). Default port 8080.
|
|
39
|
+
|
|
40
|
+
positional arguments:
|
|
41
|
+
{serve,receive}
|
|
42
|
+
command (default:
|
|
43
|
+
serve)
|
|
44
|
+
serve serve current
|
|
45
|
+
directory; others
|
|
46
|
+
can send/receive
|
|
47
|
+
(default)
|
|
48
|
+
receive listen for target to
|
|
49
|
+
POST file to you
|
|
50
|
+
|
|
51
|
+
options:
|
|
52
|
+
-h, --help show this help
|
|
53
|
+
message and exit
|
|
54
|
+
|
|
55
|
+
examples:
|
|
56
|
+
exchanger (same as serve)
|
|
57
|
+
exchanger serve (target can GET or POST)
|
|
58
|
+
exchanger receive (host listens; target POSTs file to you)
|
|
59
|
+
exchanger receive --dir /tmp --port 8080
|
|
60
|
+
```
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
exchanger/__init__.py
|
|
5
|
+
exchanger/__main__.py
|
|
6
|
+
exchanger/cli.py
|
|
7
|
+
exchanger/http_.py
|
|
8
|
+
exchanger/net_.py
|
|
9
|
+
exchanger/smb_.py
|
|
10
|
+
exchangertool.egg-info/PKG-INFO
|
|
11
|
+
exchangertool.egg-info/SOURCES.txt
|
|
12
|
+
exchangertool.egg-info/dependency_links.txt
|
|
13
|
+
exchangertool.egg-info/entry_points.txt
|
|
14
|
+
exchangertool.egg-info/requires.txt
|
|
15
|
+
exchangertool.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
smbprotocol
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
exchanger
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "exchangertool"
|
|
7
|
+
version = "0.1.1"
|
|
8
|
+
description = "Minimal CLI to send or receive files over HTTP or SMB."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
dependencies = ["smbprotocol"]
|
|
13
|
+
|
|
14
|
+
[project.scripts]
|
|
15
|
+
exchanger = "exchanger.cli:main"
|
|
16
|
+
|
|
17
|
+
[tool.setuptools.packages.find]
|
|
18
|
+
where = ["."]
|
|
19
|
+
include = ["exchanger*"]
|