chore codes
Kim committed
Mar 31, 2026 at 14:37 UTC
84a233085527f40d8cbdf62562a864b1149a5865
2 files changed
+71
-79
cmd/relay-server/frontend.go
+71
@@ -1,9 +1,12 @@
1
package main
2
3
import (
4
+ "crypto/sha256"
5
"embed"
6
+ "encoding/hex"
7
"encoding/json"
8
"errors"
9
+ "fmt"
10
"html"
11
"io/fs"
12
"mime"
@@ -15,6 +18,7 @@ import (
18
"sync/atomic"
19
"time"
20
21
+ "github.com/gosuda/portal/v2/cmd/portal-tunnel/installer"
22
"github.com/gosuda/portal/v2/portal"
23
"github.com/gosuda/portal/v2/types"
24
"github.com/gosuda/portal/v2/utils"
@@ -343,3 +347,70 @@ func frontendRootAssetPaths() []string {
347
"/web-app-manifest-512x512.png",
348
}
349
}
350
+
351
+func serveInstallBinary(w http.ResponseWriter, r *http.Request) {
352
+ if r.Method != http.MethodGet && r.Method != http.MethodHead {
353
+ w.Header().Set("Allow", http.MethodGet+", "+http.MethodHead)
354
+ http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
355
+ return
356
+ }
357
+
358
+ slug := strings.Trim(strings.TrimPrefix(r.URL.Path, types.PathInstallBinPrefix), "/")
359
+ checksumRequest := strings.HasSuffix(slug, ".sha256")
360
+ if checksumRequest {
361
+ slug = strings.TrimSuffix(slug, ".sha256")
362
+ }
363
+
364
+ filename, ok := installer.AssetFilename(slug)
365
+ if !ok {
366
+ http.NotFound(w, r)
367
+ return
368
+ }
369
+ data, err := embeddedDistFS.ReadFile("dist/tunnel/" + filename)
370
+ if err != nil {
371
+ redirectURL, ok := installer.OfficialAssetURL(slug, checksumRequest)
372
+ if !ok {
373
+ http.NotFound(w, r)
374
+ return
375
+ }
376
+ http.Redirect(w, r, redirectURL, http.StatusTemporaryRedirect)
377
+ return
378
+ }
379
+ sum := sha256.Sum256(data)
380
+ checksumHex := hex.EncodeToString(sum[:])
381
+
382
+ if checksumRequest {
383
+ w.Header().Set("Content-Type", "text/plain; charset=utf-8")
384
+ if r.Method == http.MethodGet {
385
+ _, _ = fmt.Fprintf(w, "%s %s\n", checksumHex, filename)
386
+ }
387
+ return
388
+ }
389
+
390
+ w.Header().Set("Content-Type", "application/octet-stream")
391
+ w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%q", filename))
392
+ w.Header().Set("X-Checksum-Sha256", checksumHex)
393
+ if r.Method == http.MethodGet {
394
+ _, _ = w.Write(data)
395
+ }
396
+}
397
+
398
+func serveInstallScript(w http.ResponseWriter, r *http.Request, portalURL string, isWindows bool) {
399
+ if r.Method != http.MethodGet && r.Method != http.MethodHead {
400
+ w.Header().Set("Allow", http.MethodGet+", "+http.MethodHead)
401
+ http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
402
+ return
403
+ }
404
+
405
+ script, filename, contentType, err := installer.RelayScript(portalURL, isWindows)
406
+ if err != nil {
407
+ http.Error(w, "failed to render install script", http.StatusInternalServerError)
408
+ return
409
+ }
410
+
411
+ w.Header().Set("Content-Type", contentType)
412
+ w.Header().Set("Content-Disposition", fmt.Sprintf("inline; filename=%q", filename))
413
+ if r.Method == http.MethodGet {
414
+ _, _ = w.Write([]byte(script))
415
+ }
416
+}
cmd/relay-server/tunnel.go
deleted
-79
@@ -1,79 +0,0 @@
1
-package main
2
-
3
-import (
4
- "crypto/sha256"
5
- "encoding/hex"
6
- "fmt"
7
- "net/http"
8
- "strings"
9
-
10
- "github.com/gosuda/portal/v2/cmd/portal-tunnel/installer"
11
- "github.com/gosuda/portal/v2/types"
12
-)
13
-
14
-func serveInstallBinary(w http.ResponseWriter, r *http.Request) {
15
- if r.Method != http.MethodGet && r.Method != http.MethodHead {
16
- w.Header().Set("Allow", http.MethodGet+", "+http.MethodHead)
17
- http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
18
- return
19
- }
20
-
21
- slug := strings.Trim(strings.TrimPrefix(r.URL.Path, types.PathInstallBinPrefix), "/")
22
- checksumRequest := strings.HasSuffix(slug, ".sha256")
23
- if checksumRequest {
24
- slug = strings.TrimSuffix(slug, ".sha256")
25
- }
26
-
27
- filename, ok := installer.AssetFilename(slug)
28
- if !ok {
29
- http.NotFound(w, r)
30
- return
31
- }
32
- data, err := embeddedDistFS.ReadFile("dist/tunnel/" + filename)
33
- if err != nil {
34
- redirectURL, ok := installer.OfficialAssetURL(slug, checksumRequest)
35
- if !ok {
36
- http.NotFound(w, r)
37
- return
38
- }
39
- http.Redirect(w, r, redirectURL, http.StatusTemporaryRedirect)
40
- return
41
- }
42
- sum := sha256.Sum256(data)
43
- checksumHex := hex.EncodeToString(sum[:])
44
-
45
- if checksumRequest {
46
- w.Header().Set("Content-Type", "text/plain; charset=utf-8")
47
- if r.Method == http.MethodGet {
48
- _, _ = fmt.Fprintf(w, "%s %s\n", checksumHex, filename)
49
- }
50
- return
51
- }
52
-
53
- w.Header().Set("Content-Type", "application/octet-stream")
54
- w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%q", filename))
55
- w.Header().Set("X-Checksum-Sha256", checksumHex)
56
- if r.Method == http.MethodGet {
57
- _, _ = w.Write(data)
58
- }
59
-}
60
-
61
-func serveInstallScript(w http.ResponseWriter, r *http.Request, portalURL string, isWindows bool) {
62
- if r.Method != http.MethodGet && r.Method != http.MethodHead {
63
- w.Header().Set("Allow", http.MethodGet+", "+http.MethodHead)
64
- http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
65
- return
66
- }
67
-
68
- script, filename, contentType, err := installer.RelayScript(portalURL, isWindows)
69
- if err != nil {
70
- http.Error(w, "failed to render install script", http.StatusInternalServerError)
71
- return
72
- }
73
-
74
- w.Header().Set("Content-Type", contentType)
75
- w.Header().Set("Content-Disposition", fmt.Sprintf("inline; filename=%q", filename))
76
- if r.Method == http.MethodGet {
77
- _, _ = w.Write([]byte(script))
78
- }
79
-}