XTLS/Xray-core · error

len(item.Packet) > 0 && item.Rand.To > 0

Error message

len(item.Packet) > 0 && item.Rand.To > 0

What it means

NoiseMask.Build (infra/conf/transport_finalmask.go:311) enforces mutual exclusivity per noise item: an item may carry a fixed "packet" OR generate "rand" random bytes (rand.to>0), never both. Violating this returns the literal condition string "len(item.Packet) > 0 && item.Rand.To > 0" as the error.

Source

Thrown at infra/conf/transport_finalmask.go:311

}

type NoiseItem struct {
	Rand      Int32Range      `json:"rand"`
	RandRange *Int32Range     `json:"randRange"`
	Type      string          `json:"type"`
	Packet    json.RawMessage `json:"packet"`
	Delay     Int32Range      `json:"delay"`
}

type NoiseMask struct {
	Reset Int32Range  `json:"reset"`
	Noise []NoiseItem `json:"noise"`
}

func (c *NoiseMask) Build() (proto.Message, error) {
	for _, item := range c.Noise {
		if len(item.Packet) > 0 && item.Rand.To > 0 {
			return nil, errors.New("len(item.Packet) > 0 && item.Rand.To > 0")
		}
	}

	noiseSlice := make([]*noise.Item, 0, len(c.Noise))
	for _, item := range c.Noise {
		if item.RandRange == nil {
			item.RandRange = &Int32Range{From: 0, To: 255}
		}
		if item.RandRange.From < 0 || item.RandRange.To > 255 {
			return nil, errors.New("invalid randRange")
		}
		var err error
		if item.Packet, err = PraseByteSlice(item.Packet, item.Type); err != nil {
			return nil, err
		}
		noiseSlice = append(noiseSlice, &noise.Item{
			RandMin:      int64(item.Rand.From),
			RandMax:      int64(item.Rand.To),

View on GitHub (pinned to 7d214f8b09)

Solutions

  1. Split into two items: one with packet only, one with rand only
  2. Delete "packet" (and "type") when using rand, or set "rand":{"from":0,"to":0} / omit rand when using packet

Example fix

// before
"noise": [ { "type":"str", "packet":"hi", "rand": {"to":16} } ]
// after
"noise": [ { "type":"str", "packet":"hi" }, { "rand": {"to":16} } ]
Defensive patterns

Strategy: validation

Validate before calling

for (const it of cfg.noise ?? []) {
  const hasPacket = it.packet != null && it.packet !== '' && !(Array.isArray(it.packet) && it.packet.length === 0);
  if (hasPacket && (it.rand?.to ?? 0) > 0) throw new Error('noise item cannot set both packet and rand.to>0');
}

Type guard

const isExclusiveNoiseItem = (it:any) => !((it.packet != null && it.packet !== '' && !(Array.isArray(it.packet)&&it.packet.length===0)) && ((it.rand?.to ?? 0) > 0));

Prevention

When it happens

Trigger: A noise item like {"packet":"abc","type":"str","rand":{"to":16}}. rand.to defaults to 0, so "rand":{} or omitting rand keeps a fixed packet legal; any positive upper bound alongside a non-empty packet triggers it.

Common situations: Template items that set all fields for 'flexibility'; merging a fixed-padding item with a random-padding item into one; editing configs where rand was left from a previous random item.

Related errors


AI-assisted analysis of XTLS/Xray-core@7d214f8b09 (2026-08-15). Data as JSON: /api/errors/01ffe6322a12b46e. Report an issue: GitHub.