proxyapi 0.4.0

Core library for the Proxelar MITM proxy
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
use bytes::Bytes;
use hyper::{Request, Response};

use crate::body::{self, ProxyBody};

const CERT_PAGE_HTML: &str = r#"<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Proxelar - Certificate Installation</title>
    <style>
        :root {
            --bg: #0f172a;
            --surface: #1e293b;
            --border: #334155;
            --text: #e2e8f0;
            --text-muted: #94a3b8;
            --accent: #e94560;
            --accent-hover: #d6336c;
            --green: #22c55e;
        }

        * { margin: 0; padding: 0; box-sizing: border-box; }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', sans-serif;
            background: var(--bg);
            color: var(--text);
            min-height: 100vh;
        }

        .container {
            max-width: 720px;
            margin: 0 auto;
            padding: 60px 24px;
        }

        .header {
            text-align: center;
            margin-bottom: 48px;
        }

        .header h1 {
            font-size: 42px;
            font-weight: 700;
            letter-spacing: -1px;
            margin-bottom: 8px;
        }

        .header h1 span { color: var(--accent); }

        .header p {
            color: var(--text-muted);
            font-size: 16px;
            line-height: 1.6;
        }

        .status {
            display: inline-flex;
            align-items: center;
            gap: 8px;
            background: rgba(34, 197, 94, 0.1);
            border: 1px solid rgba(34, 197, 94, 0.3);
            color: var(--green);
            padding: 6px 16px;
            border-radius: 20px;
            font-size: 13px;
            font-weight: 500;
            margin-bottom: 24px;
        }

        .status::before {
            content: '';
            width: 8px;
            height: 8px;
            background: var(--green);
            border-radius: 50%;
        }

        .platforms {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 16px;
            margin-bottom: 48px;
        }

        .platform-card {
            background: var(--surface);
            border: 1px solid var(--border);
            border-radius: 12px;
            padding: 24px;
            text-align: center;
            transition: border-color 0.2s, transform 0.2s;
        }

        .platform-card:hover {
            border-color: var(--accent);
            transform: translateY(-2px);
        }

        .platform-icon {
            font-size: 36px;
            margin-bottom: 12px;
        }

        .platform-card h3 {
            font-size: 16px;
            margin-bottom: 4px;
        }

        .platform-card .ext {
            color: var(--text-muted);
            font-size: 13px;
            margin-bottom: 16px;
        }

        .platform-card a {
            display: inline-block;
            padding: 8px 20px;
            background: var(--accent);
            color: white;
            text-decoration: none;
            border-radius: 6px;
            font-size: 14px;
            font-weight: 500;
            transition: background 0.2s;
        }

        .platform-card a:hover {
            background: var(--accent-hover);
        }

        .instructions {
            background: var(--surface);
            border: 1px solid var(--border);
            border-radius: 12px;
            overflow: hidden;
        }

        .instructions summary {
            padding: 16px 24px;
            cursor: pointer;
            font-weight: 600;
            font-size: 15px;
            list-style: none;
            display: flex;
            align-items: center;
            gap: 8px;
            border-bottom: 1px solid var(--border);
        }

        .instructions summary::-webkit-details-marker { display: none; }

        .instructions summary::before {
            content: '\25B6';
            font-size: 10px;
            transition: transform 0.2s;
        }

        .instructions[open] summary::before {
            transform: rotate(90deg);
        }

        .instructions .content {
            padding: 20px 24px;
        }

        .instructions h4 {
            font-size: 14px;
            color: var(--accent);
            margin: 16px 0 8px;
        }

        .instructions h4:first-child { margin-top: 0; }

        .instructions ol, .instructions ul {
            margin-left: 20px;
            color: var(--text-muted);
            line-height: 1.8;
            font-size: 14px;
        }

        .instructions code {
            background: var(--bg);
            padding: 2px 8px;
            border-radius: 4px;
            font-size: 13px;
            color: var(--green);
        }

        .instructions pre {
            background: var(--bg);
            padding: 12px 16px;
            border-radius: 6px;
            margin: 8px 0;
            overflow-x: auto;
            font-size: 13px;
            color: var(--green);
        }

        .note {
            background: var(--surface);
            border: 1px solid var(--border);
            border-radius: 12px;
            padding: 20px 24px;
            margin-bottom: 32px;
            font-size: 14px;
            color: var(--text-muted);
            line-height: 1.6;
        }

        .note strong { color: var(--text); }
        .note code {
            background: var(--bg);
            padding: 2px 8px;
            border-radius: 4px;
            font-size: 13px;
            color: var(--green);
        }

        .footer {
            text-align: center;
            margin-top: 48px;
            color: var(--text-muted);
            font-size: 13px;
        }

        .footer a {
            color: var(--accent);
            text-decoration: none;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <div class="status">Proxy is running</div>
            <h1>Proxe<span>lar</span></h1>
            <p>
                To intercept HTTPS traffic, you need to install the Proxelar
                Certificate Authority (CA) certificate on your device.
            </p>
        </div>

        <div class="note">
            <strong>Tip:</strong> If you reached this page through the proxy
            (<code>http://proxel.ar</code>), open
            <code>http://localhost:PORT</code> directly in your browser
            (replacing PORT with your proxy port, e.g. 8080) for reliable
            certificate downloads.
        </div>

        <div class="platforms">
            <div class="platform-card">
                <div class="platform-icon">&#127823;</div>
                <h3>macOS</h3>
                <div class="ext">proxelar-ca.cer</div>
                <a href="/cert/cer">Download</a>
            </div>
            <div class="platform-card">
                <div class="platform-icon">&#128039;</div>
                <h3>Linux</h3>
                <div class="ext">proxelar-ca.pem</div>
                <a href="/cert/pem">Download</a>
            </div>
            <div class="platform-card">
                <div class="platform-icon">&#128187;</div>
                <h3>Windows</h3>
                <div class="ext">proxelar-ca.cer</div>
                <a href="/cert/cer">Download</a>
            </div>
            <div class="platform-card">
                <div class="platform-icon">&#128241;</div>
                <h3>iOS</h3>
                <div class="ext">proxelar-ca.pem</div>
                <a href="/cert/pem">Download</a>
            </div>
            <div class="platform-card">
                <div class="platform-icon">&#129302;</div>
                <h3>Android</h3>
                <div class="ext">proxelar-ca.cer</div>
                <a href="/cert/cer">Download</a>
            </div>
            <div class="platform-card">
                <div class="platform-icon">&#128272;</div>
                <h3>Other</h3>
                <div class="ext">PEM format</div>
                <a href="/cert/pem">Download</a>
            </div>
        </div>

        <details class="instructions">
            <summary>macOS Installation</summary>
            <div class="content">
                <ol>
                    <li>Download the certificate above</li>
                    <li>Double-click the downloaded <code>.pem</code> file &mdash; it opens in Keychain Access</li>
                    <li>The certificate is added to your <strong>login</strong> keychain</li>
                    <li>Find <strong>proxelar</strong> in the list, double-click it</li>
                    <li>Expand <strong>Trust</strong>, set <em>When using this certificate</em> to <strong>Always Trust</strong></li>
                    <li>Close the dialog and enter your password to confirm</li>
                </ol>
                <h4>Or via command line:</h4>
                <pre>curl http://localhost:8080/cert/pem -o /tmp/proxelar-ca.pem
sudo security add-trusted-cert -d -r trustRoot \
  -k /Library/Keychains/System.keychain /tmp/proxelar-ca.pem</pre>
            </div>
        </details>

        <details class="instructions" style="margin-top: 8px">
            <summary>Linux Installation</summary>
            <div class="content">
                <h4>Debian / Ubuntu</h4>
                <pre>curl http://localhost:8080/cert/pem | sudo tee /usr/local/share/ca-certificates/proxelar-ca.crt
sudo update-ca-certificates</pre>
                <h4>Fedora / RHEL</h4>
                <pre>curl http://localhost:8080/cert/pem | sudo tee /etc/pki/ca-trust/source/anchors/proxelar-ca.pem
sudo update-ca-trust</pre>
                <h4>Firefox (all distros)</h4>
                <ol>
                    <li>Open <code>about:preferences#privacy</code></li>
                    <li>Scroll to <strong>Certificates</strong> &rarr; <strong>View Certificates</strong></li>
                    <li><strong>Import</strong> the <code>.pem</code> file</li>
                    <li>Check <em>Trust this CA to identify websites</em></li>
                </ol>
            </div>
        </details>

        <details class="instructions" style="margin-top: 8px">
            <summary>Windows Installation</summary>
            <div class="content">
                <ol>
                    <li>Download the <code>.cer</code> certificate above</li>
                    <li>Double-click the file</li>
                    <li>Click <strong>Install Certificate</strong></li>
                    <li>Select <strong>Local Machine</strong>, click Next</li>
                    <li>Select <strong>Place all certificates in the following store</strong></li>
                    <li>Click Browse, select <strong>Trusted Root Certification Authorities</strong></li>
                    <li>Click Next, then Finish</li>
                </ol>
            </div>
        </details>

        <details class="instructions" style="margin-top: 8px">
            <summary>iOS Installation</summary>
            <div class="content">
                <ol>
                    <li>Download the certificate on your iOS device</li>
                    <li>Go to <strong>Settings</strong> &rarr; <strong>General</strong> &rarr; <strong>VPN & Device Management</strong></li>
                    <li>Tap the <strong>proxelar</strong> profile and install it</li>
                    <li>Go to <strong>Settings</strong> &rarr; <strong>General</strong> &rarr; <strong>About</strong> &rarr; <strong>Certificate Trust Settings</strong></li>
                    <li>Enable full trust for <strong>proxelar</strong></li>
                </ol>
            </div>
        </details>

        <details class="instructions" style="margin-top: 8px">
            <summary>Android Installation</summary>
            <div class="content">
                <ol>
                    <li>Download the certificate on your Android device</li>
                    <li>Go to <strong>Settings</strong> &rarr; <strong>Security</strong> &rarr; <strong>Encryption & credentials</strong></li>
                    <li>Tap <strong>Install a certificate</strong> &rarr; <strong>CA certificate</strong></li>
                    <li>Select the downloaded file</li>
                </ol>
                <p style="color: var(--text-muted); margin-top: 12px; font-size: 13px;">
                    Note: On Android 7+, user-installed CAs are only trusted by apps that explicitly opt in.
                    For system-wide trust, a rooted device is required.
                </p>
            </div>
        </details>

        <div class="footer">
            <p>
                <a href="https://github.com/emanuele-em/proxelar">Proxelar</a>
                &mdash; Man in the Middle proxy
            </p>
        </div>
    </div>
</body>
</html>"#;

pub fn is_cert_request<T>(req: &Request<T>) -> bool {
    req.uri().host().is_some_and(|h| h == "proxel.ar")
        || req
            .headers()
            .get("host")
            .and_then(|h| h.to_str().ok())
            .is_some_and(|h| h == "proxel.ar" || h.starts_with("proxel.ar:"))
}

pub fn handle<T>(
    req: &Request<T>,
    ca_cert_pem: &[u8],
    proxy_addr: Option<std::net::SocketAddr>,
) -> Response<ProxyBody> {
    match req.uri().path() {
        "/cert/pem" => {
            let len = ca_cert_pem.len();
            Response::builder()
                .header("content-type", "application/x-x509-ca-cert")
                .header("content-length", len.to_string())
                .header(
                    "content-disposition",
                    "attachment; filename=\"proxelar-ca-cert.pem\"",
                )
                .body(body::full(Bytes::from(ca_cert_pem.to_vec())))
                .unwrap_or_else(|e| {
                    tracing::error!("Failed to build PEM response: {e}");
                    Response::new(body::empty())
                })
        }
        "/cert/cer" => {
            let der = pem_to_der(ca_cert_pem);
            let len = der.len();
            Response::builder()
                .header("content-type", "application/x-x509-ca-cert")
                .header("content-length", len.to_string())
                .header(
                    "content-disposition",
                    "attachment; filename=\"proxelar-ca-cert.cer\"",
                )
                .body(body::full(Bytes::from(der)))
                .unwrap_or_else(|e| {
                    tracing::error!("Failed to build DER response: {e}");
                    Response::new(body::empty())
                })
        }
        _ => {
            // When accessed via proxel.ar (proxied), rewrite download links
            // to point directly at the proxy server (http://localhost:PORT/cert/...)
            // so that downloads bypass the proxy and work in all browsers.
            let html = if let Some(addr) = proxy_addr {
                let base = format!("http://{addr}");
                CERT_PAGE_HTML
                    .replace("href=\"/cert/pem\"", &format!("href=\"{base}/cert/pem\""))
                    .replace("href=\"/cert/cer\"", &format!("href=\"{base}/cert/cer\""))
            } else {
                CERT_PAGE_HTML.to_string()
            };

            let len = html.len();
            Response::builder()
                .header("content-type", "text/html; charset=utf-8")
                .header("content-length", len.to_string())
                .body(body::full(Bytes::from(html)))
                .unwrap_or_else(|e| {
                    tracing::error!("Failed to build cert page response: {e}");
                    Response::new(body::empty())
                })
        }
    }
}

fn pem_to_der(pem: &[u8]) -> Vec<u8> {
    match openssl::x509::X509::from_pem(pem).and_then(|cert| cert.to_der()) {
        Ok(der) => der,
        Err(e) => {
            tracing::error!("Failed to convert PEM to DER: {e}");
            // Return empty rather than corrupt data
            Vec::new()
        }
    }
}