mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-11-25 12:04:44 +03:00
feat(wireguard): support multiple devices per client (#7143)
A WireGuard client had one key pair and one tunnel address, so an operator who gave the same config to a phone and a laptop saw the two fight over the peer endpoint. One client can now own extra devices, up to a per-client limit: each has its own keys and address, shares the client's quota, expiry and switch, and gets its own config, QR code and subscription. Devices become extra peers under the client's email, built by the same core-user map the other paths use, and a device change is applied to the running inbound in place instead of rebuilding it. Nodes advertise a wg-devices capability; the master refuses a device for a node that would ignore it, before storing it and again when pushing the inbound.
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/web/service"
|
||||
)
|
||||
|
||||
func (a *ClientController) wireguardDevices(c *gin.Context) {
|
||||
devices, err := a.clientService.WireguardDevices(c.Query("email"))
|
||||
jsonObj(c, devices, err)
|
||||
}
|
||||
|
||||
func (a *ClientController) finishWireguardDeviceWrite(c *gin.Context, err error) {
|
||||
if err != nil {
|
||||
jsonObj(c, nil, err)
|
||||
return
|
||||
}
|
||||
a.xrayService.SetToNeedReconcile()
|
||||
notifyClientsChanged()
|
||||
a.wireguardDevices(c)
|
||||
}
|
||||
|
||||
func (a *ClientController) addWireguardDevice(c *gin.Context) {
|
||||
var body struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&body); err != nil {
|
||||
jsonObj(c, nil, err)
|
||||
return
|
||||
}
|
||||
a.finishWireguardDeviceWrite(c, a.clientService.AddWireguardDevice(c.Query("email"), body.Name))
|
||||
}
|
||||
|
||||
func (a *ClientController) updateWireguardDevice(c *gin.Context) {
|
||||
var patch service.WireguardDevicePatch
|
||||
if err := c.ShouldBindJSON(&patch); err != nil {
|
||||
jsonObj(c, nil, err)
|
||||
return
|
||||
}
|
||||
a.finishWireguardDeviceWrite(c, a.clientService.UpdateWireguardDevice(c.Query("email"), c.Param("deviceId"), patch))
|
||||
}
|
||||
|
||||
func (a *ClientController) deleteWireguardDevice(c *gin.Context) {
|
||||
a.finishWireguardDeviceWrite(c, a.clientService.DeleteWireguardDevice(c.Query("email"), c.Param("deviceId")))
|
||||
}
|
||||
|
||||
func (a *ClientController) setWireguardDeviceLimit(c *gin.Context) {
|
||||
var body struct {
|
||||
Limit int `json:"limit"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&body); err != nil {
|
||||
jsonObj(c, nil, err)
|
||||
return
|
||||
}
|
||||
a.finishWireguardDeviceWrite(c, a.clientService.SetWireguardDeviceLimit(c.Query("email"), body.Limit))
|
||||
}
|
||||
@@ -71,6 +71,11 @@ func (a *ClientController) initRouter(g *gin.RouterGroup) {
|
||||
g.POST("/:email/attach", a.attach)
|
||||
g.POST("/:email/detach", a.detach)
|
||||
g.POST("/:email/externalLinks", a.setExternalLinks)
|
||||
g.GET("/wireguardDevices", a.wireguardDevices)
|
||||
g.POST("/wireguardDevices", a.addWireguardDevice)
|
||||
g.PUT("/wireguardDevices/limit", a.setWireguardDeviceLimit)
|
||||
g.PATCH("/wireguardDevices/:deviceId", a.updateWireguardDevice)
|
||||
g.DELETE("/wireguardDevices/:deviceId", a.deleteWireguardDevice)
|
||||
g.GET("/export", a.export)
|
||||
g.POST("/import", a.importClients)
|
||||
g.POST("/delOrphans", a.delOrphans)
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"testing"
|
||||
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/database"
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
|
||||
wgutil "github.com/mhsanaei/3x-ui/v3/internal/util/wireguard"
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/web/service"
|
||||
)
|
||||
|
||||
func TestWireguardDevicesQueryEmailRoundTrip(t *testing.T) {
|
||||
for _, email := range []string{".", "..", "alice@example.com", "legacy/name"} {
|
||||
t.Run(email, func(t *testing.T) {
|
||||
router, record, inbound := seedLegacyDotClient(t, email)
|
||||
privateKey, publicKey, err := wgutil.GenerateWireguardKeypair()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
client := *record.ToClient()
|
||||
client.PrivateKey, client.PublicKey = privateKey, publicKey
|
||||
client.AllowedIPs = []string{"10.0.0.2/32"}
|
||||
settings, err := json.Marshal(map[string]any{"clients": []model.Client{client}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := database.GetDB().Model(&model.Inbound{}).Where("id = ?", inbound.Id).Updates(map[string]any{"protocol": model.WireGuard, "settings": string(settings)}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
query := "?email=" + url.QueryEscape(email)
|
||||
read := func() service.WireguardDeviceList {
|
||||
t.Helper()
|
||||
response := dotClientRequest(t, router, http.MethodGet, "wireguardDevices"+query, nil)
|
||||
assertDotClientSuccess(t, response)
|
||||
var message struct {
|
||||
Obj service.WireguardDeviceList `json:"obj"`
|
||||
}
|
||||
if err := json.Unmarshal(response.Body.Bytes(), &message); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return message.Obj
|
||||
}
|
||||
initial := read()
|
||||
if len(initial.Inbounds) != 1 || len(initial.Inbounds[0].Devices) != 1 {
|
||||
t.Fatal("primary device is missing")
|
||||
}
|
||||
response := dotClientRequest(t, router, http.MethodPut, "wireguardDevices/limit"+query, map[string]any{"limit": 2})
|
||||
assertDotClientSuccess(t, response)
|
||||
response = dotClientRequest(t, router, http.MethodPost, "wireguardDevices"+query, map[string]any{"name": "Phone"})
|
||||
assertDotClientSuccess(t, response)
|
||||
created := read()
|
||||
if created.Limit != 2 || len(created.Inbounds[0].Devices) != 2 {
|
||||
t.Fatal("device creation did not persist")
|
||||
}
|
||||
id := created.Inbounds[0].Devices[1].Id
|
||||
response = dotClientRequest(t, router, http.MethodPatch, "wireguardDevices/"+id+query, map[string]any{"name": "Laptop", "enable": false})
|
||||
assertDotClientSuccess(t, response)
|
||||
patched := read().Inbounds[0].Devices[1]
|
||||
if patched.Enable || patched.Name != "Laptop" {
|
||||
t.Fatal("device patch did not persist")
|
||||
}
|
||||
response = dotClientRequest(t, router, http.MethodDelete, "wireguardDevices/"+id+query, nil)
|
||||
assertDotClientSuccess(t, response)
|
||||
if len(read().Inbounds[0].Devices) != 1 {
|
||||
t.Fatal("device deletion did not persist")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user