mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-10-13 01:14:59 +03:00
feat(xray): add the MASQUE protocol and transport
xray-core v26.10.10 serves MASQUE: CONNECT-IP over HTTP/3 or HTTP/2, always behind TLS, as an inbound and an outbound protocol riding a dedicated "masque" transport. The panel could not create either. Inbound: MASQUE is a multi-user protocol whose users log in with their email and password over Basic auth. The panel keeps the client's "password" like trojan and GenXrayInboundConfig hands it to the core as "pass", so the full-config path, the live AddInbound path and node pushes all emit what the core reads; one user with an empty pass makes the core refuse the whole inbound. Live user adds build a masque.Account from either key, client-only changes diff per user so a new client does not drop every connected tunnel, and client creation, copy, validation and edits key on the password. The address pool and MTU get form fields, TLS is mandatory, and the port check follows the core's listener choice: HTTP/2 on TCP when the ALPN offers h2, HTTP/3 on UDP when it offers h3 or not h2. Nodes may host MASQUE from v3.9.1, the first panel release that knows to rename the password. MASQUE has no share-link format, so links and Clash skip it; the JSON subscription builds the client's masque outbound with its email and password, and the subscription allowlist now lets a subId see MASQUE inbounds at all. The IP-limit job, client pages and traffic tracking list the protocol. Outbound: the form edits the server, remote DNS, and the transport's host, path, Basic credentials, headers and WARP block (enrolled key, endpoint public key, tunnel addresses), which xray-core refuses next to user/pass. The outbound test probes it through the core, since its default HTTP/3 dial is UDP.
This commit is contained in:
@@ -663,6 +663,8 @@ func (s *SubJsonService) getConfig(subReq *SubService, inbound *model.Inbound, c
|
||||
newOutbounds = append(newOutbounds, s.genServer(subReq, inbound, streamSettings, client, jsonMux(mux, hostMux)))
|
||||
case "hysteria":
|
||||
newOutbounds = append(newOutbounds, s.genHy(inbound, newStream, client, jsonMux(mux, hostMux)))
|
||||
case "masque":
|
||||
newOutbounds = append(newOutbounds, s.genMasque(inbound, newStream, client))
|
||||
case "wireguard":
|
||||
wgOutbound := s.genWireguard(inbound, client)
|
||||
if wgOutbound == nil {
|
||||
@@ -1009,6 +1011,32 @@ func (s *SubJsonService) genHy(inbound *model.Inbound, newStream map[string]any,
|
||||
return result
|
||||
}
|
||||
|
||||
// genMasque builds the client side of a MASQUE inbound: the server reads only the
|
||||
// path, and authenticates the CONNECT-IP request by the client's email and password.
|
||||
func (s *SubJsonService) genMasque(inbound *model.Inbound, newStream map[string]any, client model.Client) json_util.RawMessage {
|
||||
outbound := Outbound{
|
||||
Protocol: string(inbound.Protocol),
|
||||
Tag: "proxy",
|
||||
Settings: map[string]any{
|
||||
"address": inbound.Listen,
|
||||
"port": inbound.Port,
|
||||
},
|
||||
}
|
||||
masqueSettings := map[string]any{"user": client.Email, "pass": client.Password}
|
||||
if server, ok := newStream["masqueSettings"].(map[string]any); ok {
|
||||
if path, ok := server["path"].(string); ok && path != "" {
|
||||
masqueSettings["path"] = path
|
||||
}
|
||||
}
|
||||
newStream["masqueSettings"] = masqueSettings
|
||||
newStream["network"] = "masque"
|
||||
newStream["security"] = "tls"
|
||||
outbound.StreamSettings, _ = json.MarshalIndent(newStream, "", " ")
|
||||
|
||||
result, _ := json.MarshalIndent(outbound, "", " ")
|
||||
return result
|
||||
}
|
||||
|
||||
// genWireguard builds an Xray wireguard outbound for a native WireGuard inbound,
|
||||
// mirroring genWireguardLink: the peer public key is derived from the inbound
|
||||
// secretKey, the client owns the private key / tunnel address / pre-shared key,
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
package sub
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/database"
|
||||
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
|
||||
)
|
||||
|
||||
// MASQUE has no share-link format, so the JSON subscription is the only way a client
|
||||
// gets one: a masque outbound authenticating with the client's email and password.
|
||||
func TestSubJsonServiceMasqueClientConfig(t *testing.T) {
|
||||
inbound := &model.Inbound{
|
||||
Listen: "203.0.113.9", Port: 8443, Protocol: model.MASQUE, Remark: "masque",
|
||||
Settings: `{"clients":[{"email":"ivy@example.test","password":"pw-ivy"}],"address":["10.14.0.1/24"]}`,
|
||||
StreamSettings: `{"network":"masque","security":"tls",
|
||||
"masqueSettings":{"path":"/.well-known/masque/ip/*/*/"},
|
||||
"tlsSettings":{"serverName":"masque.example.test","alpn":["h3"],
|
||||
"certificates":[{"certificateFile":"/etc/ssl/m.crt","keyFile":"/etc/ssl/m.key"}],
|
||||
"settings":{"fingerprint":"chrome"}}}`,
|
||||
}
|
||||
client := model.Client{Email: "ivy@example.test", Password: "pw-ivy"}
|
||||
|
||||
configs := NewSubJsonService("", "", "", "", nil).getConfig(&SubService{address: "sub.example.com"}, inbound, client, "sub.example.com")
|
||||
if len(configs) != 1 {
|
||||
t.Fatalf("got %d configs, want one", len(configs))
|
||||
}
|
||||
var config struct {
|
||||
Outbounds []struct {
|
||||
Protocol string `json:"protocol"`
|
||||
Settings map[string]any `json:"settings"`
|
||||
StreamSettings map[string]any `json:"streamSettings"`
|
||||
} `json:"outbounds"`
|
||||
}
|
||||
if err := json.Unmarshal(configs[0], &config); err != nil {
|
||||
t.Fatalf("decode config: %v", err)
|
||||
}
|
||||
proxy := config.Outbounds[0]
|
||||
if proxy.Protocol != "masque" {
|
||||
t.Fatalf("first outbound protocol = %q, want masque", proxy.Protocol)
|
||||
}
|
||||
if proxy.Settings["address"] != "203.0.113.9" || proxy.Settings["port"] != float64(8443) {
|
||||
t.Fatalf("masque settings = %v, want the inbound address and port", proxy.Settings)
|
||||
}
|
||||
wantMasque := map[string]any{"path": "/.well-known/masque/ip/*/*/", "user": "ivy@example.test", "pass": "pw-ivy"}
|
||||
if got := proxy.StreamSettings["masqueSettings"]; !reflect.DeepEqual(got, wantMasque) {
|
||||
t.Fatalf("masqueSettings = %v, want %v", got, wantMasque)
|
||||
}
|
||||
tls, _ := proxy.StreamSettings["tlsSettings"].(map[string]any)
|
||||
if proxy.StreamSettings["network"] != "masque" || proxy.StreamSettings["security"] != "tls" ||
|
||||
tls["serverName"] != "masque.example.test" || tls["certificates"] != nil {
|
||||
t.Fatalf("stream = %v, want masque over client-side TLS to masque.example.test", proxy.StreamSettings)
|
||||
}
|
||||
}
|
||||
|
||||
// The subscription SQL allowlist decides which inbounds a subId can see at all; left
|
||||
// off it, a MASQUE client's JSON subscription would come back empty.
|
||||
func TestGetInboundsBySubIdIncludesMasque(t *testing.T) {
|
||||
initSubDB(t)
|
||||
db := database.GetDB()
|
||||
|
||||
in := &model.Inbound{Port: 8443, Protocol: model.MASQUE, Enable: true, Tag: "masque-sub", Settings: `{"clients":[],"address":["10.14.0.1/24"]}`}
|
||||
if err := db.Create(in).Error; err != nil {
|
||||
t.Fatalf("create inbound: %v", err)
|
||||
}
|
||||
rec := &model.ClientRecord{Email: "u@masque", SubID: "submasque", Enable: true}
|
||||
if err := db.Create(rec).Error; err != nil {
|
||||
t.Fatalf("create client: %v", err)
|
||||
}
|
||||
if err := db.Create(&model.ClientInbound{ClientId: rec.Id, InboundId: in.Id}).Error; err != nil {
|
||||
t.Fatalf("create link: %v", err)
|
||||
}
|
||||
|
||||
inbounds, err := (&SubService{}).getInboundsBySubId("submasque")
|
||||
if err != nil {
|
||||
t.Fatalf("getInboundsBySubId: %v", err)
|
||||
}
|
||||
if len(inbounds) != 1 || inbounds[0].Id != in.Id {
|
||||
t.Fatalf("masque inbound not returned for subId: %+v", inbounds)
|
||||
}
|
||||
}
|
||||
@@ -685,7 +685,7 @@ func (s *SubService) getInboundsBySubId(subId string) ([]*model.Inbound, error)
|
||||
JOIN client_inbounds ON client_inbounds.inbound_id = inbounds.id
|
||||
JOIN clients ON clients.id = client_inbounds.client_id
|
||||
WHERE
|
||||
inbounds.protocol in ('vmess','vless','trojan','shadowsocks','hysteria','wireguard','amneziawg','mtproto','tuic')
|
||||
inbounds.protocol in ('vmess','vless','trojan','shadowsocks','hysteria','wireguard','amneziawg','mtproto','tuic','masque')
|
||||
AND clients.sub_id = ? AND inbounds.enable = ?
|
||||
)`, subId, true).Order("sub_sort_index ASC").Order("id ASC").Find(&inbounds).Error
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user