Commit 7489ee16700e57bcef0a2e6aae34fd3bfd9b3461

Parents: a853d559316fc60ca325b06a6af9f5af2a6ef84b

From: Moritz Poldrack <git@moritz.sh>
Date: Mon Sep 18 13:52:31 2023 +0700

client: fix CORS header
Signed-off-by: Moritz Poldrack <git@moritz.sh>

Stats

internal/client/client.go +19/-11
internal/client/restapi.go +0/-2

Changeset

 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
diff --git a/internal/client/client.go b/internal/client/client.go
index 64ff1ab90baa9c685675e29f7461cf54c0d684b3..206f9f2174306977fc70aa9d3dd713e0c2966562 100644
--- a/internal/client/client.go
+++ b/internal/client/client.go
@@ -45,6 +45,19 @@ 		return fmt.Errorf("failed to start mpv: %w", err)
 	}
 	defer p.Close()
 
+	var opts []grpc.DialOption
+	opts = append(opts, grpc.WithBlock())
+	if u.Query().Has("insecure") || insecureByDefault(u.Hostname()) {
+		opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))
+		if insecureByDefault(u.Hostname()) {
+			q := u.Query()
+			q.Add("insecure", "")
+			u.RawQuery = q.Encode()
+		}
+	} else {
+		opts = append(opts, grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{})))
+	}
+
 	status := api.StatusConnectionConnecting
 	go StartRestServer(context.Background(), p, &status, u)
 
@@ -56,14 +69,6 @@ 	}
 
 	if u.Port() == "" {
 		u.Host += ":443"
-	}
-
-	var opts []grpc.DialOption
-	opts = append(opts, grpc.WithBlock())
-	if u.Query().Has("insecure") || insecureByDefault(u.Hostname()) {
-		opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))
-	} else {
-		opts = append(opts, grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{})))
 	}
 
 	slog.Debug("connecting to remote", "host", u.Host, "insecure_explicit", u.Query().Has("insecure"), "insecure_ip", insecureByDefault(u.Hostname()))
@@ -265,9 +270,12 @@ 		}
 		return ip[0].IsLoopback() || ip[0].IsPrivate()
 	}
 
-	ip, err := net.ResolveIPAddr("ip", host)
+	ipaddr, err := net.ResolveIPAddr("ip", host)
+	slog.Debug("resolving host using system resolver", "ip", ipaddr.IP, "error", err)
+	ip := ipaddr.IP
 	if err != nil {
-		return false
+		ip = net.ParseIP(host)
+		slog.Debug("parsing as literal IP", "host", host, "ip", ip)
 	}
-	return ip.IP.IsLoopback()
+	return ip.IsLoopback() || ip.IsPrivate()
 }
diff --git a/internal/client/restapi.go b/internal/client/restapi.go
index 5076c6104d772fb14ba53d2b3cdedb9901368773..1d8951eb121cba26cbcf5f6c85cc0519529ec33c 100644
--- a/internal/client/restapi.go
+++ b/internal/client/restapi.go
@@ -16,8 +16,6 @@ 	"strconv"
 	"strings"
 	"time"
 
-	_ "embed"
-
 	"git.sr.ht/~mpldr/uniview/internal/buildinfo"
 	"git.sr.ht/~mpldr/uniview/internal/client/api"
 	"git.sr.ht/~mpldr/uniview/internal/config"