mirror of
https://github.com/binwiederhier/ntfy.git
synced 2026-11-10 14:44:04 +08:00
The server uses the topic package instead of the limit service for the rate visitors: a subscribe sets the rate visitor (user ID and masked IP), a poll, a stream close and the manager's keepalive record subscriber activity, a refused push notes an unknown topic (insert-only). The limiter key is derived on every push from the rate visitor's current tier (refreshed in the background every minute, never on the publish path), so a downgrade or a deleted account charges the IP. Before a 404 for an abandoned topic the row is read from the database. The Matrix gateway rejects the pushkey under the same 7-day rule; its 12 h per-node rule is gone.
50 lines
1.9 KiB
Go
50 lines
1.9 KiB
Go
package visitor
|
|
|
|
import (
|
|
"net/netip"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
"heckel.io/ntfy/v2/user"
|
|
)
|
|
|
|
func TestKey(t *testing.T) {
|
|
confWithDefaults := &Config{
|
|
PrefixBitsIPv4: 32,
|
|
PrefixBitsIPv6: 64,
|
|
}
|
|
confWithShortenedPrefixes := &Config{
|
|
PrefixBitsIPv4: 16,
|
|
PrefixBitsIPv6: 56,
|
|
}
|
|
userWithTier := &user.User{
|
|
ID: "u_123",
|
|
Tier: &user.Tier{},
|
|
}
|
|
userWithoutTier := &user.User{
|
|
ID: "u_456",
|
|
}
|
|
require.Equal(t, "ip:1.2.3.4", Key(netip.MustParseAddr("1.2.3.4"), nil, confWithDefaults))
|
|
require.Equal(t, "ip:2a01:599:b26:2397::", Key(netip.MustParseAddr("2a01:599:b26:2397:dbe7:5aa2:95ce:1e83"), nil, confWithDefaults))
|
|
require.Equal(t, "ip:2001:db8:25:86::", Key(netip.MustParseAddr("2001:db8:25:86:1::1"), nil, confWithDefaults))
|
|
require.Equal(t, "ip:2001:db8:25:86::", Key(netip.MustParseAddr("2001:db8:25:86:2::1"), nil, confWithDefaults))
|
|
|
|
// Only a tier promotes a visitor to a user key; a user without one shares the IP key
|
|
require.Equal(t, "user:u_123", Key(netip.MustParseAddr("1.2.3.4"), userWithTier, confWithDefaults))
|
|
require.Equal(t, "user:u_123", Key(netip.MustParseAddr("2a01:599:b26:2397:dbe7:5aa2:95ce:1e83"), userWithTier, confWithDefaults))
|
|
require.Equal(t, "ip:1.2.3.4", Key(netip.MustParseAddr("1.2.3.4"), userWithoutTier, confWithDefaults))
|
|
|
|
require.Equal(t, "ip:1.2.0.0", Key(netip.MustParseAddr("1.2.3.4"), nil, confWithShortenedPrefixes))
|
|
require.Equal(t, "ip:2a01:599:b26:2300::", Key(netip.MustParseAddr("2a01:599:b26:2397:dbe7:5aa2:95ce:1e83"), nil, confWithShortenedPrefixes))
|
|
}
|
|
|
|
func TestMask(t *testing.T) {
|
|
conf := &Config{
|
|
PrefixBitsIPv4: 16,
|
|
PrefixBitsIPv6: 56,
|
|
}
|
|
require.Equal(t, "1.2.0.0", Mask(netip.MustParseAddr("1.2.3.4"), conf).String())
|
|
require.Equal(t, "2a01:599:b26:2300::", Mask(netip.MustParseAddr("2a01:599:b26:2397:dbe7:5aa2:95ce:1e83"), conf).String())
|
|
require.Equal(t, "1.2.0.0", Mask(netip.MustParseAddr("1.2.0.0"), conf).String()) // Masking again changes nothing
|
|
}
|