Added a new `--session-id option to pio home` // Resolve #3397

This commit is contained in:
Ivan Kravets
2021-01-25 23:44:26 +02:00
parent 2c389ae11e
commit 0ed99b7687
5 changed files with 39 additions and 11 deletions

View File

@ -28,9 +28,13 @@ PlatformIO Core 5
- Disabled automatic removal of unnecessary development platform packages (`issue #3708 <https://github.com/platformio/platformio-core/issues/3708>`_, `issue #3770 <https://github.com/platformio/platformio-core/issues/3770>`_) - Disabled automatic removal of unnecessary development platform packages (`issue #3708 <https://github.com/platformio/platformio-core/issues/3708>`_, `issue #3770 <https://github.com/platformio/platformio-core/issues/3770>`_)
- Fixed an issue when unnecessary packages were removed in ``update --dry-run`` mode (`issue #3809 <https://github.com/platformio/platformio-core/issues/3809>`_) - Fixed an issue when unnecessary packages were removed in ``update --dry-run`` mode (`issue #3809 <https://github.com/platformio/platformio-core/issues/3809>`_)
* **Miscellaneous** * **PlatformIO Home**
- Significantly speedup PlatformIO Home loading time by migrating to native Python 3 Asynchronous I/O - Significantly speedup PlatformIO Home loading time by migrating to native Python 3 Asynchronous I/O
- Added a new ``--session-id`` option to `pio home <https://docs.platformio.org/page/core/userguide/cmd_home.html>`__ command that helps to keep PlatformIO Home isolated from other instances and protect from 3rd party access (`issue #3397 <https://github.com/platformio/platformio-core/issues/3397>`_)
* **Miscellaneous**
- Improved listing of `multicast DNS services <https://docs.platformio.org/page/core/userguide/device/cmd_list.html>`_ - Improved listing of `multicast DNS services <https://docs.platformio.org/page/core/userguide/device/cmd_list.html>`_
- Check for debugging server's "ready_pattern" in "stderr" - Check for debugging server's "ready_pattern" in "stderr"
- Fixed a "UnicodeDecodeError: 'utf-8' codec can't decode byte" when using J-Link for firmware uploading on Linux (`issue #3804 <https://github.com/platformio/platformio-core/issues/3804>`_) - Fixed a "UnicodeDecodeError: 'utf-8' codec can't decode byte" when using J-Link for firmware uploading on Linux (`issue #3804 <https://github.com/platformio/platformio-core/issues/3804>`_)

2
docs

Submodule docs updated: ee815b1b42...24f5766660

View File

@ -47,7 +47,7 @@ __pioremote_endpoint__ = "ssl:host=remote.platformio.org:port=4413"
__default_requests_timeout__ = (10, None) # (connect, read) __default_requests_timeout__ = (10, None) # (connect, read)
__core_packages__ = { __core_packages__ = {
"contrib-piohome": "~3.3.1", "contrib-piohome": "~3.3.2",
"contrib-pysite": "~2.%d%d.0" % (sys.version_info.major, sys.version_info.minor), "contrib-pysite": "~2.%d%d.0" % (sys.version_info.major, sys.version_info.minor),
"tool-unity": "~1.20500.0", "tool-unity": "~1.20500.0",
"tool-scons": "~2.20501.7" if sys.version_info.major == 2 else "~4.40100.0", "tool-scons": "~2.20501.7" if sys.version_info.major == 2 else "~4.40100.0",

View File

@ -40,7 +40,14 @@ from platformio.compat import ensure_python3
"are connected. Default is 0 which means never auto shutdown" "are connected. Default is 0 which means never auto shutdown"
), ),
) )
def cli(port, host, no_open, shutdown_timeout): @click.option(
"--session-id",
help=(
"A unique session identifier to keep PIO Home isolated from other instances "
"and protect from 3rd party access"
),
)
def cli(port, host, no_open, shutdown_timeout, session_id):
ensure_python3() ensure_python3()
# Ensure PIO Home mimetypes are known # Ensure PIO Home mimetypes are known
@ -52,7 +59,11 @@ def cli(port, host, no_open, shutdown_timeout):
if host == "__do_not_start__": if host == "__do_not_start__":
return return
home_url = "http://%s:%d" % (host, port) home_url = "http://%s:%d%s" % (
host,
port,
("/session/%s/" % session_id) if session_id else "/",
)
click.echo( click.echo(
"\n".join( "\n".join(
[ [
@ -61,7 +72,7 @@ def cli(port, host, no_open, shutdown_timeout):
" /\\-_--\\ PlatformIO Home", " /\\-_--\\ PlatformIO Home",
"/ \\_-__\\", "/ \\_-__\\",
"|[]| [] | %s" % home_url, "|[]| [] | %s" % home_url,
"|__|____|______________%s" % ("_" * len(host)), "|__|____|__%s" % ("_" * len(home_url)),
] ]
) )
) )

View File

@ -13,6 +13,7 @@
# limitations under the License. # limitations under the License.
import os import os
from urllib.parse import urlparse
import click import click
import uvicorn import uvicorn
@ -21,6 +22,7 @@ from starlette.middleware import Middleware
from starlette.responses import PlainTextResponse from starlette.responses import PlainTextResponse
from starlette.routing import Mount, Route, WebSocketRoute from starlette.routing import Mount, Route, WebSocketRoute
from starlette.staticfiles import StaticFiles from starlette.staticfiles import StaticFiles
from starlette.status import HTTP_403_FORBIDDEN
from platformio.commands.home.rpc.handlers.account import AccountRPC from platformio.commands.home.rpc.handlers.account import AccountRPC
from platformio.commands.home.rpc.handlers.app import AppRPC from platformio.commands.home.rpc.handlers.app import AppRPC
@ -51,6 +53,12 @@ async def shutdown_server(_=None):
return PlainTextResponse("Server has been shutdown!") return PlainTextResponse("Server has been shutdown!")
async def protected_page(_):
return PlainTextResponse(
"Protected PlatformIO Home session", status_code=HTTP_403_FORBIDDEN
)
def run_server(host, port, no_open, shutdown_timeout, home_url): def run_server(host, port, no_open, shutdown_timeout, home_url):
contrib_dir = get_core_package_dir("contrib-piohome") contrib_dir = get_core_package_dir("contrib-piohome")
if not os.path.isdir(contrib_dir): if not os.path.isdir(contrib_dir):
@ -65,14 +73,19 @@ def run_server(host, port, no_open, shutdown_timeout, home_url):
ws_rpc_factory.addHandler(PIOCoreRPC(), namespace="core") ws_rpc_factory.addHandler(PIOCoreRPC(), namespace="core")
ws_rpc_factory.addHandler(ProjectRPC(), namespace="project") ws_rpc_factory.addHandler(ProjectRPC(), namespace="project")
path = urlparse(home_url).path
routes = [
WebSocketRoute(path + "wsrpc", ws_rpc_factory, name="wsrpc"),
Route(path + "__shutdown__", shutdown_server, methods=["POST"]),
Mount(path, StaticFiles(directory=contrib_dir, html=True), name="static"),
]
if path != "/":
routes.append(Route("/", protected_page))
uvicorn.run( uvicorn.run(
Starlette( Starlette(
middleware=[Middleware(ShutdownMiddleware)], middleware=[Middleware(ShutdownMiddleware)],
routes=[ routes=routes,
WebSocketRoute("/wsrpc", ws_rpc_factory, name="wsrpc"),
Route("/__shutdown__", shutdown_server, methods=["POST"]),
Mount("/", StaticFiles(directory=contrib_dir, html=True)),
],
on_startup=[ on_startup=[
lambda: click.echo( lambda: click.echo(
"PIO Home has been started. Press Ctrl+C to shutdown." "PIO Home has been started. Press Ctrl+C to shutdown."