Skip to content

A modern and elegant dashboard for network traffic visualization and analysis.

fix(surge): keep host requests on the LAN IP after the sourceAddress switch (#90)

luolei70c797eb00e0
diff --git a/apps/agent/internal/gateway/client.go b/apps/agent/internal/gateway/client.go
index ed82c61..35d926d 100644
--- a/apps/agent/internal/gateway/client.go
+++ b/apps/agent/internal/gateway/client.go
@@ -279,7 +279,7 @@ func (c *Client) collectSurge(ctx context.Context) ([]domain.FlowSnapshot, error
ip = extractHost(remoteAddress)
}
- sourceIP := extractHost(defaultString(strings.TrimSpace(reqItem.SourceAddress), strings.TrimSpace(reqItem.LocalAddress)))
+ sourceIP := surgeSourceIP(reqItem.SourceAddress, reqItem.LocalAddress)
chains := convertSurgeChains(reqItem.PolicyName, reqItem.OriginalPolicyName, []string(reqItem.Notes))
rule := defaultString(strings.TrimSpace(lastChain(chains)), defaultString(strings.TrimSpace(reqItem.OriginalPolicyName), "Match"))
rulePayload := strings.TrimSpace(reqItem.Rule)
@@ -385,6 +385,23 @@ func remoteAddressFirst(v string) string {
return strings.TrimSpace(parts[0])
}
+// surgeSourceIP returns the device IP of a Surge request. sourceAddress is the
+// client that sent the request (issue #90); requests made by the Surge host
+// itself report a loopback source, so those fall back to localAddress to keep
+// attributing them to the host's LAN IP as before.
+func surgeSourceIP(sourceAddress, localAddress string) string {
+ source := extractHost(sourceAddress)
+ if source != "" {
+ if ip := net.ParseIP(source); !strings.EqualFold(source, "localhost") && (ip == nil || !ip.IsLoopback()) {
+ return source
+ }
+ }
+ if local := extractHost(localAddress); local != "" {
+ return local
+ }
+ return source
+}
+
func extractHost(hostWithPort string) string {
hostWithPort = strings.TrimSpace(hostWithPort)
if hostWithPort == "" {
diff --git a/apps/agent/internal/gateway/surge_source_test.go b/apps/agent/internal/gateway/surge_source_test.go
new file mode 100644
index 0000000..508dc56
--- /dev/null
+++ b/apps/agent/internal/gateway/surge_source_test.go
@@ -0,0 +1,20 @@
+package gateway
+
+import "testing"
+
+func TestSurgeSourceIP(t *testing.T) {
+ cases := []struct{ source, local, want string }{
+ {"10.0.0.44", "10.0.0.88", "10.0.0.44"},
+ {"10.0.0.44:51234", "", "10.0.0.44"},
+ {"127.0.0.1", "10.0.0.88", "10.0.0.88"},
+ {"::1", "10.0.0.88:6152", "10.0.0.88"},
+ {"::ffff:127.0.0.1", "10.0.0.88", "10.0.0.88"},
+ {"", "192.168.1.2:56123", "192.168.1.2"},
+ {"127.0.0.1", "", "127.0.0.1"},
+ }
+ for _, c := range cases {
+ if got := surgeSourceIP(c.source, c.local); got != c.want {
+ t.Errorf("surgeSourceIP(%q, %q) = %q, want %q", c.source, c.local, got, c.want)
+ }
+ }
+}
diff --git a/apps/collector/src/modules/collector/surge-source-ip.test.ts b/apps/collector/src/modules/collector/surge-source-ip.test.ts
new file mode 100644
index 0000000..89a5722
--- /dev/null
+++ b/apps/collector/src/modules/collector/surge-source-ip.test.ts
@@ -0,0 +1,21 @@
+import { describe, expect, it } from "vitest";
+import { surgeSourceIP } from "./surge.collector.js";
+
+describe("surgeSourceIP (issue #90)", () => {
+ it("uses the requesting client from sourceAddress", () => {
+ expect(surgeSourceIP("10.0.0.44", "10.0.0.88")).toBe("10.0.0.44");
+ expect(surgeSourceIP("10.0.0.44:51234", "")).toBe("10.0.0.44");
+ });
+
+ it("falls back to localAddress for requests made by the Surge host itself", () => {
+ expect(surgeSourceIP("127.0.0.1", "10.0.0.88")).toBe("10.0.0.88");
+ expect(surgeSourceIP("::1", "10.0.0.88:6152")).toBe("10.0.0.88");
+ expect(surgeSourceIP("::ffff:127.0.0.1", "10.0.0.88")).toBe("10.0.0.88");
+ });
+
+ it("falls back to localAddress when sourceAddress is missing", () => {
+ expect(surgeSourceIP(undefined, "192.168.1.2:56123")).toBe("192.168.1.2");
+ expect(surgeSourceIP("127.0.0.1", undefined)).toBe("127.0.0.1");
+ expect(surgeSourceIP(undefined, undefined)).toBe("");
+ });
+});
diff --git a/apps/collector/src/modules/collector/surge.collector.ts b/apps/collector/src/modules/collector/surge.collector.ts
index 432bfb5..bebb30f 100644
--- a/apps/collector/src/modules/collector/surge.collector.ts
+++ b/apps/collector/src/modules/collector/surge.collector.ts
@@ -496,7 +496,7 @@ export function createSurgeCollector(
: isIP(remoteAddress)
? remoteAddress
: "";
- const sourceIP = req.sourceAddress || req.localAddress || "";
+ const sourceIP = surgeSourceIP(req.sourceAddress, req.localAddress);
// For Surge:
// - policyName = final proxy (e.g., "🇺🇸 US-SJC-IEPL")
@@ -871,6 +871,23 @@ export function createSurgeCollector(
return collectorWithReset;
}
+/**
+ * Device IP of a Surge request. `sourceAddress` is the client that sent the
+ * request (issue #90); requests made by the Surge host itself report a
+ * loopback source, so those fall back to `localAddress` to keep attributing
+ * them to the host's LAN IP as before.
+ */
+export function surgeSourceIP(sourceAddress?: string, localAddress?: string): string {
+ const source = extractHost((sourceAddress || "").trim());
+ if (source && !isLoopbackHost(source)) return source;
+ return extractHost((localAddress || "").trim()) || source;
+}
+
+function isLoopbackHost(host: string): boolean {
+ const h = host.toLowerCase();
+ return h === "::1" || h === "localhost" || /^(::ffff:)?127\./.test(h);
+}
+
/**
* Extract host without port from a host:port string
*/