Skip to content

Commit 3cf53a1

Browse files
committed
macOS: Eliminate security prompt: Allow "Crystal" to find devices on local networks?
1 parent 06329a6 commit 3cf53a1

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

RELEASE_NOTES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ and new `--port`/`--host` options for controlling how projects are served.
3838
* Serving improvements
3939
* The `--port` and `--host` CLI options can be used to control which port
4040
and address Crystal serves projects from.
41+
* No longer is an unnecessary
42+
'Allow "Crystal" to find devices on local networks?' security prompt
43+
shown on macOS when viewing a downloaded page after opening the app.
4144

4245
* Accessibility improvements
4346
* In the Open/Create Project Dialog you can now press Ctrl/Alt/Cmd+R key to toggle

src/crystal/server.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
from http.server import BaseHTTPRequestHandler, HTTPServer
2828
from io import TextIOBase
2929
import re
30+
import socket
31+
import socketserver
3032
from textwrap import dedent
3133
from typing import Literal, Optional
3234
from typing_extensions import override
@@ -343,6 +345,26 @@ def handle_error(self, request, client_address):
343345
# Print to stderr a message starting with
344346
# 'Exception occurred during processing of request from'
345347
return super().handle_error(request, client_address)
348+
349+
@override
350+
def server_bind(self):
351+
"""
352+
Overrides server_bind to assume the server name is "localhost"
353+
when bound to 127.0.0.1.
354+
355+
The default implementation of HTTPServer.server_bind() uses
356+
`socket.getfqdn(host)` to determine the server name, which shows an
357+
unwanted 'Allow "Crystal" to find devices on local networks?'
358+
security prompt on macOS.
359+
"""
360+
socketserver.TCPServer.server_bind(self)
361+
host, port = self.server_address[:2]
362+
if host == '127.0.0.1':
363+
# Avoid mDNS lookup on macOS
364+
self.server_name = 'localhost'
365+
else:
366+
self.server_name = socket.getfqdn(host)
367+
self.server_port = port
346368

347369

348370
class _RequestHandler(BaseHTTPRequestHandler):

0 commit comments

Comments
 (0)