mirror of
https://github.com/XTLS/Xray-core.git
synced 2026-11-21 00:59:12 +00:00
https://github.com/XTLS/Xray-core/pull/6769#issuecomment-6117945421 https://github.com/XTLS/Xray-core/issues/7394#issuecomment-6855385193 https://github.com/XTLS/Xray-core/pull/6769#issuecomment-6880324101 https://github.com/XTLS/Xray-core/pull/7402#issuecomment-6887468010 https://github.com/XTLS/Xray-core/pull/7402#issuecomment-6890345279 https://github.com/XTLS/Xray-core/pull/7402#issuecomment-6896090317 https://github.com/XTLS/Xray-core/issues/7394#issuecomment-6897315492 https://github.com/XTLS/Xray-core/pull/7402#issuecomment-6912788851
104 lines
3.4 KiB
Go
104 lines
3.4 KiB
Go
package xmc
|
|
|
|
import (
|
|
"fmt"
|
|
"math"
|
|
"time"
|
|
|
|
"github.com/xtls/xray-core/common/net"
|
|
"github.com/xtls/xray-core/transport/internet/finalmask"
|
|
)
|
|
|
|
// maxPaddingDelayMilliseconds keeps the millisecond value convertible to a
|
|
// time.Duration on every platform.
|
|
const maxPaddingDelayMilliseconds = int64(math.MaxInt64 / int64(time.Millisecond))
|
|
|
|
// MaxLegacyPasswordLength is the longest pre-shared password the login can
|
|
// carry. The verify token (4 random bytes) and the password travel together in
|
|
// one RSA-1024 PKCS#1 v1.5 block, which holds at most 128-11 bytes.
|
|
const MaxLegacyPasswordLength = 128 - 11 - 4
|
|
|
|
func (c *Config) WrapConnClient(conn net.Conn, dest *net.Destination, dialer *finalmask.Dialer) (net.Conn, error) {
|
|
profiles, err := profilesFromConfig(c.Profiles)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("minecraft finalmask: %w", err)
|
|
}
|
|
cc, err := newClientConn(conn, profiles, c.Password, c.RsaPublicKey, c.Hostname, c.Paddings)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("minecraft finalmask: %w", err)
|
|
}
|
|
|
|
return cc, nil
|
|
}
|
|
|
|
func (c *Config) WrapConnServer(conn net.Conn) (net.Conn, error) {
|
|
profiles, err := profilesFromConfig(c.Profiles)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("minecraft finalmask: %w", err)
|
|
}
|
|
cc, err := wrapConnServer(conn, profiles, c.Password, c.RsaPrivateKey, c.RsaPublicKey, c.Paddings)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("minecraft finalmask: %w", err)
|
|
}
|
|
|
|
return cc, nil
|
|
}
|
|
|
|
// ValidatePadding checks custom startup turns without selecting a built-in preset.
|
|
func (c *Config) ValidatePadding() error {
|
|
_, err := paddingScheduleFromConfig(c.Paddings)
|
|
return err
|
|
}
|
|
|
|
func paddingScheduleFromConfig(paddings []*Padding) ([]paddingTurn, error) {
|
|
if len(paddings) == 0 {
|
|
return nil, nil
|
|
}
|
|
schedule := make([]paddingTurn, len(paddings))
|
|
for i, turn := range paddings {
|
|
if turn == nil || turn.LengthMin < 1 || turn.LengthMax < turn.LengthMin || turn.LengthMax > maxPaddingTurnLength {
|
|
return nil, fmt.Errorf("invalid padding length range at turn %d", i)
|
|
}
|
|
if turn.DelayMin < 0 || turn.DelayMax < turn.DelayMin {
|
|
return nil, fmt.Errorf("invalid padding delay range %d-%d at turn %d (must be non-negative and ordered)", turn.DelayMin, turn.DelayMax, i)
|
|
}
|
|
if turn.DelayMax > maxPaddingDelayMilliseconds {
|
|
return nil, fmt.Errorf("padding delay range %d-%d at turn %d is too large", turn.DelayMin, turn.DelayMax, i)
|
|
}
|
|
var direction paddingDirection
|
|
switch turn.Direction {
|
|
case 1:
|
|
direction = paddingClientToServer
|
|
case 2:
|
|
direction = paddingServerToClient
|
|
default:
|
|
return nil, fmt.Errorf("invalid padding direction %d at turn %d (1=client-to-server, 2=server-to-client)", turn.Direction, i)
|
|
}
|
|
schedule[i] = paddingTurn{
|
|
direction: direction,
|
|
minLength: int(turn.LengthMin),
|
|
maxLength: int(turn.LengthMax),
|
|
delay: paddingDelayRange{
|
|
min: time.Duration(turn.DelayMin) * time.Millisecond,
|
|
max: time.Duration(turn.DelayMax) * time.Millisecond,
|
|
},
|
|
}
|
|
}
|
|
// The first turn includes the two-byte Login Acknowledged packet.
|
|
if err := validatePaddingSchedule(schedule, 2); err != nil {
|
|
return nil, err
|
|
}
|
|
return schedule, nil
|
|
}
|
|
|
|
func newPaddingSchedule(paddings []*Padding, isClient bool) ([]paddingTurn, error) {
|
|
schedule, err := paddingScheduleFromConfig(paddings)
|
|
if err != nil || schedule != nil {
|
|
return schedule, err
|
|
}
|
|
if isClient {
|
|
return newClientPaddingSchedule2612()
|
|
}
|
|
return newServerPaddingSchedule2612()
|
|
}
|