network

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 15, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package network provides physical layer emulation boundaries and traffic tuning parameters to safely bypass DPI and mimic official Telegram clients.

Package network provides physical layer emulation boundaries and traffic tuning parameters to safely bypass DPI and mimic official Telegram clients.

Package network provides physical layer emulation boundaries and traffic tuning parameters to safely bypass DPI and mimic official Telegram clients.

Index

Constants

This section is empty.

Variables

View Source
var ErrDialerSuspended = errors.New("network: dialer is currently suspended")

ErrDialerSuspended is returned when a connection attempt is aborted due to transport suspension.

Functions

func BuildClientHelloSpec

func BuildClientHelloSpec(platform Platform) *utls.ClientHelloSpec

BuildClientHelloSpec generates a hand-crafted uTLS ClientHelloSpec to precisely mimic the TLS handshake signature (JA3/JA4) of the official Telegram clients. This avoids using browser presets which are easily detected by modern DPIs.

func EmulatedDialer

func EmulatedDialer(params SocketTuningParams, applySegmentation bool) func(ctx context.Context, network, addr string) (net.Conn, error)

EmulatedDialer builds a custom dialer function that applies OS-level socket tuning parameters (buffer sizes, Nagle's algorithm) and optional TCP segmentation.

func GenerateObfuscatedHeader

func GenerateObfuscatedHeader(dcID int16, randSource io.Reader) ([64]byte, error)

GenerateObfuscatedHeader creates the 64-byte initial payload required for MTProto Obfuscated2 protocol with the default Abridged transport signature (0xef).

func GenerateObfuscatedHeaderForTransport

func GenerateObfuscatedHeaderForTransport(transport TransportType, dcID int16, randSource io.Reader) ([64]byte, error)

GenerateObfuscatedHeaderForTransport creates the 64-byte initial payload required for MTProto Obfuscated2 protocol for a specified transport type (Abridged, Intermediate, or PaddedIntermediate). It safely avoids common HTTP method patterns to bypass DPI systems and injects the protocol signature and Target DC ID directly into the payload in accordance with official Telegram client standards.

Types

type AndroidFakeTLSProfile

type AndroidFakeTLSProfile struct {
	AndroidProfile
}

AndroidFakeTLSProfile extends AndroidProfile but enforces FakeTLS encapsulation.

func NewAndroidFakeTLSProfile

func NewAndroidFakeTLSProfile() *AndroidFakeTLSProfile

NewAndroidFakeTLSProfile constructs an Android profile wrapped inside FakeTLS boundaries.

func (*AndroidFakeTLSProfile) GetTransportType

func (*AndroidFakeTLSProfile) GetTransportType() TransportType

GetTransportType implements Profile.

type AndroidProfile

type AndroidProfile struct{}

AndroidProfile implements Profile optimized for emulation of mobile Android devices.

func NewAndroidProfile

func NewAndroidProfile() *AndroidProfile

NewAndroidProfile constructs an Android profile matching standard mobile properties.

func (*AndroidProfile) ApplySegmentation

func (*AndroidProfile) ApplySegmentation() bool

ApplySegmentation implements Profile.

func (*AndroidProfile) EnablePFS

func (*AndroidProfile) EnablePFS() bool

EnablePFS implements Profile.

func (*AndroidProfile) GetConnectionPoolLimits

func (*AndroidProfile) GetConnectionPoolLimits() ConnectionPoolLimits

GetConnectionPoolLimits implements Profile.

func (*AndroidProfile) GetKeepAliveSettings

func (*AndroidProfile) GetKeepAliveSettings() KeepAliveSettings

GetKeepAliveSettings implements Profile.

func (*AndroidProfile) GetSocketTuningParams

func (*AndroidProfile) GetSocketTuningParams() SocketTuningParams

GetSocketTuningParams implements Profile.

func (*AndroidProfile) GetTransportType

func (*AndroidProfile) GetTransportType() TransportType

GetTransportType implements Profile.

type BufferPool

type BufferPool struct {
	// contains filtered or unexported fields
}

BufferPool represents a bounded recycler of byte slices. It utilizes a bounded channel pool to enforce a strict memory ceiling, mitigating Garbage Collector (GC) heap pressure and preventing OOM kills under containerized environments (Kubernetes Cgroups).

func NewBufferPool

func NewBufferPool(size int, maxBuffers ...int) *BufferPool

NewBufferPool initializes a bounded zero-allocation memory pool for byte slices. It accepts an optional maxBuffers parameter to enforce a strict memory cap (defaults to 128).

func (*BufferPool) Get

func (p *BufferPool) Get() *[]byte

Get retrieves a pre-allocated byte slice from the bounded pool or allocates a new one if empty.

func (*BufferPool) PipeThrough

func (p *BufferPool) PipeThrough(dst io.Writer, src io.Reader) (int64, error)

PipeThrough streams data directly from an io.Reader to an io.Writer using a pre-allocated buffer from the pool. This direct pipe-through prevents unnecessary heap allocations and GC thrashing.

func (*BufferPool) Put

func (p *BufferPool) Put(b *[]byte)

Put returns the byte slice to the pool if capacity permits; otherwise it is safely dropped for GC.

type ConnectionPoolLimits

type ConnectionPoolLimits struct {
	// MaxUploadConnections defines the upload channel concurrency limit.
	MaxUploadConnections int64
	// MaxDownloadConnections defines the download channel concurrency limit.
	MaxDownloadConnections int64
}

ConnectionPoolLimits dictates the maximum number of parallel sockets permitted for high-throughput media operations.

type DesktopProfile

type DesktopProfile struct{}

DesktopProfile implements Profile optimized for Telegram Desktop (TDesktop).

func NewDesktopProfile

func NewDesktopProfile() *DesktopProfile

NewDesktopProfile constructs a desktop profile matching broadband client specs.

func (*DesktopProfile) ApplySegmentation

func (*DesktopProfile) ApplySegmentation() bool

ApplySegmentation implements Profile.

func (*DesktopProfile) EnablePFS

func (*DesktopProfile) EnablePFS() bool

EnablePFS implements Profile.

func (*DesktopProfile) GetConnectionPoolLimits

func (*DesktopProfile) GetConnectionPoolLimits() ConnectionPoolLimits

GetConnectionPoolLimits implements Profile.

func (*DesktopProfile) GetKeepAliveSettings

func (*DesktopProfile) GetKeepAliveSettings() KeepAliveSettings

GetKeepAliveSettings implements Profile.

func (*DesktopProfile) GetSocketTuningParams

func (*DesktopProfile) GetSocketTuningParams() SocketTuningParams

GetSocketTuningParams implements Profile.

func (*DesktopProfile) GetTransportType

func (*DesktopProfile) GetTransportType() TransportType

GetTransportType implements Profile.

type DialerState

type DialerState int

DialerState defines the operational phases of the gated transport layer.

const (
	// StateActive allows normal physical socket allocation.
	StateActive DialerState = iota
	// StateSuspended blocks socket allocation and buffers reconnect attempts.
	StateSuspended
)

type GatedDialer

type GatedDialer struct {
	// contains filtered or unexported fields
}

GatedDialer intercepts socket creation at the transport layer, enforcing absolute blockage during suspension phases.

func NewGatedDialer

func NewGatedDialer() *GatedDialer

NewGatedDialer instantiates a gated dialer in the Active state with an already unblocked resume channel.

func (*GatedDialer) ActiveConnectionsCount

func (g *GatedDialer) ActiveConnectionsCount() int

ActiveConnectionsCount returns the current count of monitored active connections.

func (*GatedDialer) DialContext

func (g *GatedDialer) DialContext(ctx context.Context, network, addr string, dialFunc func(ctx context.Context, network, addr string) (net.Conn, error)) (net.Conn, error)

DialContext handles physical TCP socket allocation, blocking safely if the gateway is suspended.

func (*GatedDialer) Resume

func (g *GatedDialer) Resume()

Resume transitions the gateway to Active state, signaling all blocked DialContext threads to proceed.

func (*GatedDialer) Suspend

func (g *GatedDialer) Suspend()

Suspend transitions the gateway to Suspended state and force-closes all active monitored connections.

type KeepAliveSettings

type KeepAliveSettings struct {
	// PingInterval defines the base frequency of application-level keep-alive pings.
	PingInterval time.Duration
	// JitterOffset defines the maximum random duration added/subtracted from the PingInterval.
	JitterOffset time.Duration
}

KeepAliveSettings contains timing configurations for background ping mechanisms. These parameters help mitigate DPI timing analysis attacks.

type Platform

type Platform string

Platform represents the target physical device platform to emulate.

const (
	// PlatformAndroid mimics the standard Telegram Android (OkHttp/BoringSSL) fingerprint.
	PlatformAndroid Platform = "android"

	// PlatformDesktop mimics the standard Telegram Desktop (Chrome/BoringSSL) fingerprint.
	PlatformDesktop Platform = "desktop"
)

type Profile

type Profile interface {
	// GetTransportType determines the base MTProto transport protocol.
	GetTransportType() TransportType

	// GetSocketTuningParams returns the OS-level TCP socket boundaries.
	GetSocketTuningParams() SocketTuningParams

	// GetConnectionPoolLimits returns the bounds for concurrent multi-DC media streams.
	GetConnectionPoolLimits() ConnectionPoolLimits

	// GetKeepAliveSettings returns the heartbeat interval and its jitter component.
	GetKeepAliveSettings() KeepAliveSettings

	// ApplySegmentation indicates whether TCP packet segmentation should be applied
	// to the initial connection packet to mimic specific client footprints.
	ApplySegmentation() bool

	// EnablePFS indicates whether Perfect Forward Secrecy (PFS) with temporary
	// authorization keys (authKeyTemp) should be enforced.
	EnablePFS() bool
}

Profile is the polymorphic interface contract representing a device's specific physical network fingerprint and connection behavior.

type SocketTuningParams

type SocketTuningParams struct {
	// ReadBufferSize specifies the SO_RCVBUF size in bytes.
	ReadBufferSize int
	// WriteBufferSize specifies the SO_SNDBUF size in bytes.
	WriteBufferSize int
	// TCPKeepAlive defines the interval between TCP keep-alive probes.
	TCPKeepAlive time.Duration
}

SocketTuningParams contains operating system level socket buffer sizes and TCP keep-alive configurations.

type TransportType

type TransportType uint8

TransportType represents the MTProto transport encoding strategy.

const (
	// TransportAbridged represents the Abridged protocol.
	TransportAbridged TransportType = iota
	// TransportIntermediate represents the Intermediate protocol.
	TransportIntermediate
	// TransportPaddedIntermediate represents the Padded Intermediate protocol.
	TransportPaddedIntermediate
	// TransportFakeTLS represents the FakeTLS protocol over an obfuscated connection.
	TransportFakeTLS
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL