Files
mirror-3x-ui/internal/sub/hwid_block_test.go
T
animesha3 6bc7e81c90 feat(clients): add per-subscription HWID device bans (#7133)
An operator could remove a device from a subscription, but it simply registered again on its next refresh; there was no way to keep one device out.

Add Block and Unblock to the device list. A banned device is refused on every subscription route whether or not the client has a device limit, does not hold a registration slot, and survives trimming and clearing the list. Unblocking needs a free slot, so registered devices never exceed the limit, and a banned device whose configs are replaced is told it is blocked.
2026-11-24 21:26:44 +03:30

76 lines
3.1 KiB
Go

package sub
import (
"encoding/base64"
"net/http"
"strings"
"testing"
"github.com/mhsanaei/3x-ui/v3/internal/database"
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
"github.com/mhsanaei/3x-ui/v3/internal/web/service"
)
func TestSubscriptionBannedHwidWithUnlimitedDevices(t *testing.T) {
router, subID := initHwidSubRouter(t, 0)
if r := requestSub(t, router, http.MethodGet, "/sub/"+subID, "banned-device", ""); r.Code != http.StatusOK {
t.Fatalf("register: %d", r.Code)
}
svc := &service.ClientService{}
list, err := svc.ListClientHwids("route@example.com")
if err != nil || len(list) != 1 {
t.Fatalf("list: %+v, %v", list, err)
}
if err := svc.SetClientHwidBlocked("route@example.com", list[0].Id, true); err != nil {
t.Fatal(err)
}
for _, path := range []string{"/sub/", "/json/", "/clash/", "/mihomo/", "/clash-legacy/"} {
for _, method := range []string{http.MethodGet, http.MethodHead} {
r := requestSub(t, router, method, path+subID, "banned-device", "")
if r.Code != http.StatusNotFound || r.Header().Get("X-Hwid-Blocked") != "true" || r.Header().Get("X-Hwid-Active") != "true" || r.Header().Get("X-Hwid-Max-Devices-Reached") != "" || r.Body.Len() != 0 {
t.Fatalf("%s %s: banned response: %d %v %s", method, path, r.Code, r.Header(), r.Body)
}
}
}
assertRealConfig := func(hwid string) {
t.Helper()
r := requestSub(t, router, http.MethodGet, "/sub/"+subID, hwid, "")
body, err := base64.StdEncoding.DecodeString(r.Body.String())
if err != nil || r.Code != http.StatusOK || !strings.Contains(string(body), "11111111-2222-4333-8444-555555555555") || r.Header().Get("X-Hwid-Blocked") != "" {
t.Fatalf("allowed response: %d %s, %v", r.Code, r.Body, err)
}
}
assertRealConfig("another-device")
if r := requestSub(t, router, http.MethodGet, "/sub/unknown", "banned-device", ""); r.Code != http.StatusNotFound {
t.Fatalf("unknown subscription: %d", r.Code)
}
if err := svc.SetClientHwidBlocked("route@example.com", list[0].Id, false); err != nil {
t.Fatal(err)
}
assertRealConfig("banned-device")
}
// A banned device that has its configs replaced must read that it is blocked: the limit
// is not full, and freeing a slot would not let it back in.
func TestSubscriptionBannedHwidReplacementNamesTheBan(t *testing.T) {
router, subID := initHwidSubRouter(t, 0, WithSUBEncryption(false))
if err := database.GetDB().Create(&model.Setting{Key: "subHwidRejectWithConfig", Value: "true"}).Error; err != nil {
t.Fatal(err)
}
if r := requestSub(t, router, http.MethodGet, "/sub/"+subID, "banned-device", ""); r.Code != http.StatusOK {
t.Fatalf("register: %d", r.Code)
}
svc := &service.ClientService{}
list, err := svc.ListClientHwids("route@example.com")
if err != nil || len(list) != 1 {
t.Fatalf("list: %+v, %v", list, err)
}
if err := svc.SetClientHwidBlocked("route@example.com", list[0].Id, true); err != nil {
t.Fatal(err)
}
r := requestSub(t, router, http.MethodGet, "/sub/"+subID, "banned-device", "")
if want := "socks://192.0.2.1:1#Device%20blocked\n"; r.Code != http.StatusOK || r.Body.String() != want {
t.Fatalf("banned replacement = %d %q, want 200 %q", r.Code, r.Body.String(), want)
}
}