Skip to content

PasteFerry (隔空贴): clipboard and file sync across macOS, Windows and Linux. Direct-first, end-to-end encrypted. Optional official access: no server to host. Independent self-hosting remains available.

test(core): verify the allowance and relay charges in envcheck

baiyuzed408f1b2a14b
diff --git a/client-core/cmd/envcheck/main.go b/client-core/cmd/envcheck/main.go
index db78ee3..11f23ce 100644
--- a/client-core/cmd/envcheck/main.go
+++ b/client-core/cmd/envcheck/main.go
@@ -288,9 +288,25 @@ func run() (resultErr error) {
}
connectedAfter := time.Since(started)
fmt.Fprintln(os.Stderr, "envcheck: connected", want.String(), "after", connectedAfter.Round(time.Millisecond))
- if *mode == "direct" && (client.Usage().UpdatedAt != 0 || client.Usage().Reason != "") {
+ // The allowance is visible without relay; only relay authorization reads
+ // the machine digest, and a direct path must never request it.
+ if *mode == "direct" && (client.RelayRequested() || client.Usage().Reason != "") {
return errors.New("direct connection requested gateway authorization")
}
+ var atConnect gateway.Usage
+ if official {
+ for client.Usage().UpdatedAt == 0 {
+ select {
+ case <-time.After(100 * time.Millisecond):
+ case <-ctx.Done():
+ return errors.New("allowance was not fetched before transfer")
+ }
+ }
+ atConnect = client.Usage()
+ if *mode == "direct" && (atConnect.Group != "" || atConnect.Limit <= 0 || atConnect.Remaining != atConnect.Limit) {
+ return fmt.Errorf("fresh identity without relay should see the untouched default allowance: %+v", atConnect)
+ }
+ }
if *mode == "official-relay" && connectedAfter < 8*time.Second {
return errors.New("relay started before direct attempt window")
}
@@ -346,7 +362,23 @@ func run() (resultErr error) {
}
result := map[string]any{"platform": runtime.GOOS, "arch": runtime.GOARCH, "mode": *mode, "self": *selfLabel, "device_id": self.DeviceID, "peer_id": peer.DeviceID, "connection": want.String(), "connected_ms": connectedAfter.Milliseconds(), "transfer_ms": time.Since(transferStarted).Milliseconds(), "text_verified": true, "image_sha256": sum(fixture("image")), "file_sha256": sum(fixture("file")), "file_bytes": len(fixture("file"))}
if official {
- result["gateway_authorized"] = client.Usage().UpdatedAt > 0
+ result["gateway_authorized"] = client.RelayRequested()
+ // Read the allowance again after the gateway has metered the transfer.
+ client.RefreshUsage()
+ time.Sleep(3 * time.Second)
+ after := client.Usage()
+ charged := after.Upload + after.Download - atConnect.Upload - atConnect.Download
+ result["usage_at_connect"] = atConnect
+ result["usage_after"] = after
+ result["relay_bytes_charged"] = charged
+ switch {
+ case *mode == "direct" && (charged != 0 || after.Group != "" || client.RelayRequested()):
+ return fmt.Errorf("direct transfer was charged or bound a quota group: %+v", after)
+ case *mode == "official-relay" && (after.Group == "" || charged < int64(len(fixture("file")))):
+ return fmt.Errorf("relay transfer was not charged to the machine group: charged %d, %+v", charged, after)
+ case *mode == "official-relay" && after.Remaining > atConnect.Remaining-charged+int64(len(fixture("file"))):
+ return fmt.Errorf("remaining allowance did not shrink with the charge: %d -> %d", atConnect.Remaining, after.Remaining)
+ }
}
encoded, _ := json.MarshalIndent(result, "", " ")
if err = os.WriteFile(filepath.Join(*out, "result.json"), append(encoded, '\n'), 0600); err != nil {
diff --git a/client-core/internal/gateway/client.go b/client-core/internal/gateway/client.go
index 4d409fd..66f60c0 100644
--- a/client-core/internal/gateway/client.go
+++ b/client-core/internal/gateway/client.go
@@ -10,6 +10,7 @@ import (
"net/url"
"strings"
"sync"
+ "sync/atomic"
"time"
"github.com/baiyuze/pasteferry/client-core/internal/identity"
@@ -30,6 +31,7 @@ type Client struct {
root context.Context
cancel context.CancelFunc
kick chan struct{}
+ relayAsked atomic.Bool
}
// Without relay sessions the allowance only changes through policy, so it is
@@ -149,7 +151,13 @@ func (c *Client) update(u Usage) {
}
}
func (c *Client) Usage() Usage { c.mu.Lock(); defer c.mu.Unlock(); return c.last }
+
+// RelayRequested reports whether relay authorization, the only request that
+// carries a machine digest, has been attempted since the client was created.
+func (c *Client) RelayRequested() bool { return c.relayAsked.Load() }
+
func (c *Client) Authorize(ctx context.Context, peer, session string) (Access, error) {
+ c.relayAsked.Store(true)
var nonce struct {
Nonce string `json:"nonce"`
}
@@ -243,9 +251,6 @@ func (c *Client) Run(ctx context.Context) {
c.ReportError(err)
}
}
- if force {
- continue
- }
for session, a := range sessions {
var err error
if time.Now().Unix() >= a.ExpiresAt-120 {
diff --git a/client-core/internal/gateway/gateway_test.go b/client-core/internal/gateway/gateway_test.go
index 630378c..f8e4c30 100644
--- a/client-core/internal/gateway/gateway_test.go
+++ b/client-core/internal/gateway/gateway_test.go
@@ -316,6 +316,9 @@ func TestRunFetchesOwnerUsageWithoutRelay(t *testing.T) {
if authorizes.Load() != 2 || usages.Load() != 1 {
t.Fatalf("authorize=%d usage=%d", authorizes.Load(), usages.Load())
}
+ if c.RelayRequested() {
+ t.Fatal("owner allowance queries were counted as relay requests")
+ }
// Disabled mode makes no official request, even when asked to refresh.
c.SetEnabled(false)
before := authorizes.Load() + usages.Load()