Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ Configuration is done via environment variables. The following environment varia
| `MU_NODE_ID` | Required stable node identity; bound to the state file | `node-1` |
| `MU_STATE_FILE` | Private persistent state file (default `data/mu-state.json`) | `/var/lib/mu/state.json` |
| `MU_TROJAN_ADDRS` | Comma-separated list of Trojan server addresses | `trojan1.example.com,trojan2.example.com` |
| `MU_V2FLY_ADDRS` | Comma-separated list of V2Fly server addresses | `v2fly1.example.com,v2fly2.example.com` |
| `MU_V2FLY_ADDRS` | Comma-separated list of V2Fly server addresses (VMess inbound, tag `proxy`) | `v2fly1.example.com,v2fly2.example.com` |
| `MU_VLESS_ADDRS` | Comma-separated list of V2Fly server addresses with a VLESS inbound | `vless1.example.com,vless2.example.com` |
| `MU_VLESS_INBOUND_TAG` | Tag of the VLESS inbound on those servers (default `vless`) | `vless` |

### API failure diagnostics

Expand Down Expand Up @@ -68,6 +70,10 @@ export MU_TROJAN_ADDRS="trojan1.example.com,trojan2.example.com"

# Set V2Fly server addresses
export MU_V2FLY_ADDRS="v2fly1.example.com,v2fly2.example.com"

# Set V2Fly servers hosting a VLESS inbound (optional)
export MU_VLESS_ADDRS="vless1.example.com"
export MU_VLESS_INBOUND_TAG="vless"
```

## Docker Compose
Expand All @@ -89,6 +95,7 @@ services:
- MU_STATE_FILE=/var/lib/mu/state.json
- MU_TROJAN_ADDRS=trojan1.example.com,trojan2.example.com
- MU_V2FLY_ADDRS=v2fly1.example.com,v2fly2.example.com
- MU_VLESS_ADDRS=vless1.example.com
volumes:
- mu-state:/var/lib/mu
networks:
Expand Down Expand Up @@ -127,6 +134,24 @@ identities reject the affected snapshot before authorization changes. `enable` m
MU must be the sole user manager for each configured Trojan instance and V2Ray `proxy` inbound.
The APIs use plaintext gRPC: keep them on a trusted private network and do not expose them publicly.

### VLESS support

`MU_VLESS_ADDRS` manages a **VLESS** inbound (tag `MU_VLESS_INBOUND_TAG`, default `vless`) on a
v2fly (`v2ray-core` v5) instance through the same gRPC `HandlerService`/`StatsService` API as V2Fly.
Xray is not supported. Users are the same `GetUsers` snapshot and the same UUID as for VMess;
`alter_id` is ignored and the account uses empty flow and `none` encryption.

- **Backend email suffix.** V2Ray statistics are keyed by email only, so VLESS users are installed
with the backend email `<email>#vless`. This keeps VLESS and VMess counters of one V2Ray instance
disjoint, so a VMess and a VLESS inbound can share an instance without double billing. Each syncer
ignores the other protocol's counters. Emails ending in `#vless` (case-insensitive) are reserved and
reject the whole snapshot for the V2Fly and VLESS backends.
- **Inbound requirements.** The VLESS inbound must have its own tag (do not reuse `proxy`), MU must be
its sole user manager, and it must start with an empty user list, as for VMess. Enable per-user
statistics (`statsUserUplink`/`statsUserDownlink`) for the user's policy level.
- **Traffic type.** VLESS traffic is uploaded as `TRAFFIC_TYPE_V2FLY`; use the `vless` sync label in
metrics to tell it apart. State is kept under a separate `vless:<addr>:<tag>` key.

### Persistent state is required

- Put `MU_STATE_FILE` on a persistent volume. Its parent must be writable by the MU process.
Expand Down
7 changes: 7 additions & 0 deletions internal/mu/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ func (a *App) Init() (err error) {
}
a.userSync = append(a.userSync, syncer)
}
for _, addr := range a.config.VlessAddrs {
syncer, err := newVlessSync(addr, a.config.VlessInboundTag, a.state)
if err != nil {
return err
}
a.userSync = append(a.userSync, syncer)
}
return nil
}
func (a *App) initTracing() error {
Expand Down
26 changes: 19 additions & 7 deletions internal/mu/app/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ type Config struct {

TrojanAddrs []string
V2flyAddrs []string
VlessAddrs []string
// VlessInboundTag is the V2Ray inbound tag of the VLESS inbound (default "vless").
VlessInboundTag string

// Metrics 配置
MetricsEnabled bool
Expand All @@ -32,6 +35,8 @@ type Config struct {
// - MU_NODE_ID: Node identifier
// - MU_TROJAN_ADDRS: Comma-separated list of Trojan addresses
// - MU_V2FLY_ADDRS: Comma-separated list of V2Fly addresses
// - MU_VLESS_ADDRS: Comma-separated list of V2Fly addresses hosting a VLESS inbound
// - MU_VLESS_INBOUND_TAG: Tag of the VLESS inbound (default "vless")
func ConfigFromEnv() *Config {
v := viper.New()

Expand All @@ -42,6 +47,8 @@ func ConfigFromEnv() *Config {
v.SetDefault("NODE_ID", "")
v.SetDefault("TROJAN_ADDRS", "")
v.SetDefault("V2FLY_ADDRS", "")
v.SetDefault("VLESS_ADDRS", "")
v.SetDefault("VLESS_INBOUND_TAG", "vless")
v.SetDefault("STATE_FILE", "data/mu-state.json")

// Metrics defaults
Expand All @@ -66,13 +73,14 @@ func ConfigFromEnv() *Config {

// Create a config struct with values from environment variables
config := &Config{
MuAPIBaseURL: v.GetString("API_BASE_URL"),
MuAPIToken: v.GetString("API_TOKEN"),
MuAPIDebug: v.GetBool("API_DEBUG"),
MuNodeID: v.GetString("NODE_ID"),
StateFile: v.GetString("STATE_FILE"),
MetricsEnabled: v.GetBool("METRICS_ENABLED"),
MetricsPort: v.GetString("METRICS_PORT"),
MuAPIBaseURL: v.GetString("API_BASE_URL"),
MuAPIToken: v.GetString("API_TOKEN"),
MuAPIDebug: v.GetBool("API_DEBUG"),
MuNodeID: v.GetString("NODE_ID"),
StateFile: v.GetString("STATE_FILE"),
VlessInboundTag: v.GetString("VLESS_INBOUND_TAG"),
MetricsEnabled: v.GetBool("METRICS_ENABLED"),
MetricsPort: v.GetString("METRICS_PORT"),
Tracing: tracing.Config{
Enabled: v.GetBool("TRACING_ENABLED"),
ServiceName: v.GetString("TRACING_SERVICE_NAME"),
Expand All @@ -91,6 +99,10 @@ func ConfigFromEnv() *Config {
config.V2flyAddrs = parseAddresses(v2flyAddrsStr)
}

if vlessAddrsStr := v.GetString("VLESS_ADDRS"); vlessAddrsStr != "" {
config.VlessAddrs = parseAddresses(vlessAddrsStr)
}

return config
}

Expand Down
106 changes: 59 additions & 47 deletions internal/mu/app/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ func defaultTracing() tracing.Config {
func TestConfigFromEnv(t *testing.T) {
// Save original environment variables to restore later
originalEnvVars := map[string]string{
"MU_API_BASE_URL": os.Getenv("MU_API_BASE_URL"),
"MU_API_TOKEN": os.Getenv("MU_API_TOKEN"),
"MU_NODE_ID": os.Getenv("MU_NODE_ID"),
"MU_TROJAN_ADDRS": os.Getenv("MU_TROJAN_ADDRS"),
"MU_V2FLY_ADDRS": os.Getenv("MU_V2FLY_ADDRS"),
"MU_API_BASE_URL": os.Getenv("MU_API_BASE_URL"),
"MU_API_TOKEN": os.Getenv("MU_API_TOKEN"),
"MU_NODE_ID": os.Getenv("MU_NODE_ID"),
"MU_TROJAN_ADDRS": os.Getenv("MU_TROJAN_ADDRS"),
"MU_V2FLY_ADDRS": os.Getenv("MU_V2FLY_ADDRS"),
"MU_VLESS_ADDRS": os.Getenv("MU_VLESS_ADDRS"),
"MU_VLESS_INBOUND_TAG": os.Getenv("MU_VLESS_INBOUND_TAG"),
}

// Restore environment variables after test
Expand All @@ -48,64 +50,74 @@ func TestConfigFromEnv(t *testing.T) {
{
name: "Empty environment",
envVars: map[string]string{
"MU_API_BASE_URL": "",
"MU_API_TOKEN": "",
"MU_NODE_ID": "",
"MU_TROJAN_ADDRS": "",
"MU_V2FLY_ADDRS": "",
"MU_API_BASE_URL": "",
"MU_API_TOKEN": "",
"MU_NODE_ID": "",
"MU_TROJAN_ADDRS": "",
"MU_V2FLY_ADDRS": "",
"MU_VLESS_ADDRS": "",
"MU_VLESS_INBOUND_TAG": "",
},
expected: &Config{
MuAPIBaseURL: "",
MuAPIToken: "",
MuNodeID: "",
StateFile: "data/mu-state.json",
TrojanAddrs: nil,
V2flyAddrs: nil,
MetricsEnabled: true,
MetricsPort: "9090",
Tracing: defaultTracing(),
MuAPIBaseURL: "",
MuAPIToken: "",
MuNodeID: "",
StateFile: "data/mu-state.json",
TrojanAddrs: nil,
V2flyAddrs: nil,
VlessInboundTag: "vless",
MetricsEnabled: true,
MetricsPort: "9090",
Tracing: defaultTracing(),
},
},
{
name: "Full configuration",
envVars: map[string]string{
"MU_API_BASE_URL": "https://api.example.com",
"MU_API_TOKEN": "test-token",
"MU_NODE_ID": "node-1",
"MU_TROJAN_ADDRS": "trojan1.example.com,trojan2.example.com",
"MU_V2FLY_ADDRS": "v2fly1.example.com,v2fly2.example.com",
"MU_API_BASE_URL": "https://api.example.com",
"MU_API_TOKEN": "test-token",
"MU_NODE_ID": "node-1",
"MU_TROJAN_ADDRS": "trojan1.example.com,trojan2.example.com",
"MU_V2FLY_ADDRS": "v2fly1.example.com,v2fly2.example.com",
"MU_VLESS_ADDRS": "vless1.example.com, vless2.example.com,vless1.example.com",
"MU_VLESS_INBOUND_TAG": "edge-vless",
},
expected: &Config{
MuAPIBaseURL: "https://api.example.com",
MuAPIToken: "test-token",
MuNodeID: "node-1",
StateFile: "data/mu-state.json",
TrojanAddrs: []string{"trojan1.example.com", "trojan2.example.com"},
V2flyAddrs: []string{"v2fly1.example.com", "v2fly2.example.com"},
MetricsEnabled: true,
MetricsPort: "9090",
Tracing: defaultTracing(),
MuAPIBaseURL: "https://api.example.com",
MuAPIToken: "test-token",
MuNodeID: "node-1",
StateFile: "data/mu-state.json",
TrojanAddrs: []string{"trojan1.example.com", "trojan2.example.com"},
V2flyAddrs: []string{"v2fly1.example.com", "v2fly2.example.com"},
VlessAddrs: []string{"vless1.example.com", "vless2.example.com"},
VlessInboundTag: "edge-vless",
MetricsEnabled: true,
MetricsPort: "9090",
Tracing: defaultTracing(),
},
},
{
name: "Partial configuration",
envVars: map[string]string{
"MU_API_BASE_URL": "https://api.example.com",
"MU_API_TOKEN": "test-token",
"MU_NODE_ID": "node-2",
"MU_TROJAN_ADDRS": "",
"MU_V2FLY_ADDRS": "v2fly.example.com",
"MU_API_BASE_URL": "https://api.example.com",
"MU_API_TOKEN": "test-token",
"MU_NODE_ID": "node-2",
"MU_TROJAN_ADDRS": "",
"MU_V2FLY_ADDRS": "v2fly.example.com",
"MU_VLESS_ADDRS": "",
"MU_VLESS_INBOUND_TAG": "",
},
expected: &Config{
MuAPIBaseURL: "https://api.example.com",
MuAPIToken: "test-token",
MuNodeID: "node-2",
StateFile: "data/mu-state.json",
TrojanAddrs: nil,
V2flyAddrs: []string{"v2fly.example.com"},
MetricsEnabled: true,
MetricsPort: "9090",
Tracing: defaultTracing(),
MuAPIBaseURL: "https://api.example.com",
MuAPIToken: "test-token",
MuNodeID: "node-2",
StateFile: "data/mu-state.json",
TrojanAddrs: nil,
V2flyAddrs: []string{"v2fly.example.com"},
VlessInboundTag: "vless",
MetricsEnabled: true,
MetricsPort: "9090",
Tracing: defaultTracing(),
},
},
}
Expand Down
15 changes: 12 additions & 3 deletions internal/mu/app/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

const protocolTrojan = "trojan"
const protocolV2Fly = "v2fly"
const protocolVLESS = "vless"

// GetTraffic durably queues deltas in the shared state before returning them for metrics.
// Sync also takes a final snapshot before changing authorization. Neither operation resets counters.
Expand All @@ -35,7 +36,8 @@ func desiredUsers(users []*pb.User, protocol string) (map[string]managedUser, ma
v := user.V2RayUser
uuidKey := v.Uuid
id := common.SHA224String(v.Uuid)
if protocol == protocolV2Fly {
backendEmail := v.Email
if protocol == protocolV2Fly || protocol == protocolVLESS {
parsed, err := uuid.ParseString(v.Uuid)
if err != nil {
return nil, nil, fmt.Errorf("invalid V2Ray UUID at index %d", index)
Expand All @@ -44,7 +46,14 @@ func desiredUsers(users []*pb.User, protocol string) (map[string]managedUser, ma
if strings.TrimSpace(v.Email) == "" || strings.TrimSpace(v.Email) != v.Email || strings.Contains(v.Email, ">>>") {
return nil, nil, fmt.Errorf("invalid V2Ray email at index %d", index)
}
id = strings.ToLower(v.Email)
// The suffix is reserved for VLESS backend identities; reject it so protocols cannot collide.
if hasVlessSuffix(v.Email) {
return nil, nil, fmt.Errorf("V2Ray email at index %d uses reserved suffix %q", index, vlessEmailSuffix)
}
if protocol == protocolVLESS {
backendEmail += vlessEmailSuffix
}
id = strings.ToLower(backendEmail)
if emails[id] {
return nil, nil, errors.New("duplicate V2Ray email in snapshot")
}
Expand All @@ -54,7 +63,7 @@ func desiredUsers(users []*pb.User, protocol string) (map[string]managedUser, ma
return nil, nil, errors.New("duplicate user UUID in snapshot")
}
uuids[uuidKey] = true
record := managedUser{ID: user.Id, UUID: v.Uuid, Email: v.Email, Level: v.Level, AlterID: v.AlterId}
record := managedUser{ID: user.Id, UUID: v.Uuid, Email: backendEmail, Level: v.Level, AlterID: v.AlterId}
all[id] = record
if user.Enable == 1 {
enabled[id] = record
Expand Down
2 changes: 1 addition & 1 deletion internal/mu/app/sync_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestInvalidSnapshotsAreRejectedBeforeBackendCalls(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
backends := []UserSync{&v2flySync{}}
backends := []UserSync{&v2flySync{}, &v2flySync{name: protocolVLESS}}
if !tt.v2Only {
backends = append(backends, &trojanSync{})
}
Expand Down
Loading
Loading