Skip to content

Commit 0a3e7e7

Browse files
sharpninjaCopilot
andcommitted
Fix: generate QR code server-side (embedded PNG) instead of CDN JS
The CDN qrcode.js script was blocked/unavailable so no QR was shown. Switch to QRCoder (server-side) which generates the QR as a PNG and embeds it as a data:image/png;base64 <img> tag — no external requests needed, works in air-gapped / firewalled environments. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 324f229 commit 0a3e7e7

2 files changed

Lines changed: 18 additions & 17 deletions

File tree

src/RemoteAgent.Service/RemoteAgent.Service.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
<PackageReference Include="Grpc.AspNetCore" Version="2.64.0" />
2020
<PackageReference Include="LiteDB" Version="5.0.21" />
2121
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="10.0.3" />
22+
<PackageReference Include="QRCoder" Version="1.6.0" />
2223
</ItemGroup>
2324

2425
</Project>

src/RemoteAgent.Service/Web/PairingHtml.cs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Net;
2+
using QRCoder;
23

34
namespace RemoteAgent.Service.Web;
45

@@ -49,7 +50,7 @@ internal static class PairingHtml
4950
h2 { margin-bottom: 4px; }
5051
p { color: #555; margin-top: 0; }
5152
#qr { margin: 16px auto; }
52-
#qr canvas { border-radius: 8px; }
53+
#qr img { border-radius: 8px; display: block; margin: 0 auto; }
5354
.key { font-family: monospace; background: #f5f5f5; padding: 12px; border-radius: 6px; word-break: break-all; margin: 16px 0; font-size: 13px; border: 1px solid #ddd; text-align: left; }
5455
.btn { display: block; padding: 14px; background: #0078d4; color: white; border-radius: 6px; text-decoration: none; font-size: 16px; margin-top: 8px; }
5556
.btn:hover { background: #005a9e; }
@@ -59,17 +60,7 @@ internal static class PairingHtml
5960
<body>
6061
<h2>Your API Key</h2>
6162
%%BODY%%
62-
<div id="qr"></div>
63-
<script src="https://cdn.jsdelivr.net/npm/qrcode@1.5.4/build/qrcode.min.js"></script>
64-
<script>
65-
(function() {
66-
var data = %%QR_DATA%%;
67-
if (!data) return;
68-
QRCode.toCanvas(document.createElement('canvas'), data, { width: 256 }, function(err, c) {
69-
if (!err) { document.getElementById('qr').appendChild(c); }
70-
});
71-
})();
72-
</script>
63+
<div id="qr">%%QR_IMG%%</div>
7364
</body>
7465
</html>
7566
""";
@@ -96,13 +87,13 @@ public static string LoginPage(bool error = false, bool noPairingUsers = false)
9687

9788
public static string KeyPage(string apiKey, string deepLink)
9889
{
99-
string body, qrData;
90+
string body, qrImg;
10091

10192
if (string.IsNullOrEmpty(apiKey))
10293
{
10394
body = "<div class=\"warn\">No API key is configured on this server. " +
10495
"Set <code>Agent:ApiKey</code> in <code>appsettings.json</code>.</div>";
105-
qrData = "null";
96+
qrImg = "";
10697
}
10798
else
10899
{
@@ -111,10 +102,19 @@ public static string KeyPage(string apiKey, string deepLink)
111102
body = $"<p>Scan the QR code with your Remote Agent app, or tap <strong>Open in App</strong>.</p>" +
112103
$"<div class=\"key\">{encodedKey}</div>" +
113104
$"<a class=\"btn\" href=\"{encodedLink}\">Open in Remote Agent App</a>";
114-
// Serialize as JSON string — safe to embed directly in <script>.
115-
qrData = System.Text.Json.JsonSerializer.Serialize(deepLink);
105+
qrImg = GenerateQrPngImg(deepLink);
116106
}
117107

118-
return KeyTemplate.Replace("%%BODY%%", body).Replace("%%QR_DATA%%", qrData);
108+
return KeyTemplate.Replace("%%BODY%%", body).Replace("%%QR_IMG%%", qrImg);
109+
}
110+
111+
private static string GenerateQrPngImg(string data)
112+
{
113+
using var generator = new QRCodeGenerator();
114+
var qrData = generator.CreateQrCode(data, QRCodeGenerator.ECCLevel.M);
115+
using var code = new PngByteQRCode(qrData);
116+
var png = code.GetGraphic(6);
117+
var b64 = Convert.ToBase64String(png);
118+
return $"<img src=\"data:image/png;base64,{b64}\" width=\"256\" height=\"256\" alt=\"Pairing QR code\" />";
119119
}
120120
}

0 commit comments

Comments
 (0)