dcs

package
v0.0.0-...-87e9d67 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2023 License: MIT Imports: 24 Imported by: 0

Documentation

Overview

Package dcs contains Telegram DCs list and some helpers.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func FindDCs

func FindDCs(opts []tg.DCOption, dcID int, preferIPv6 bool) []tg.DCOption

FindDCs searches DCs candidates from given config.

func FindPrimaryDCs

func FindPrimaryDCs(opts []tg.DCOption, dcID int, preferIPv6 bool) []tg.DCOption

FindPrimaryDCs searches new primary DC from given config. Unlike FindDC, it filters CDNs and MediaOnly servers, returns error if not found.

Types

type DNSConfig

type DNSConfig struct {
	// Date field of HelpConfigSimple.
	Date int
	// Expires field of HelpConfigSimple.
	Expires int
	// Rules field of HelpConfigSimple.
	Rules []tg.AccessPointRule
}

DNSConfig is DC connection config obtained from DNS.

func ParseDNSConfig

func ParseDNSConfig(txt []string) (DNSConfig, error)

ParseDNSConfig parses tg.HelpConfigSimple from TXT response.

func (DNSConfig) Options

func (d DNSConfig) Options() (r []tg.DCOption)

Options returns DC options from this config.

type DialFunc

type DialFunc func(ctx context.Context, network, addr string) (net.Conn, error)

DialFunc connects to the address on the named network.

Example
package main

import (
	"context"
	"fmt"
	"time"

	"golang.org/x/net/proxy"

	"bitbucket.org/hokego/hokego-td/telegram"
	"bitbucket.org/hokego/hokego-td/telegram/dcs"
)

func main() {
	// Dial using proxy from environment.

	// Creating connection.
	ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
	defer cancel()
	client := telegram.NewClient(1, "appHash", telegram.Options{
		Resolver: dcs.Plain(dcs.PlainOptions{Dial: proxy.Dial}),
	})

	_ = client.Run(ctx, func(ctx context.Context) error {
		fmt.Println("Started")
		return nil
	})
}
Output:

Example (Dialer)
package main

import (
	"context"
	"fmt"
	"time"

	"golang.org/x/net/proxy"

	"bitbucket.org/hokego/hokego-td/telegram"
	"bitbucket.org/hokego/hokego-td/telegram/dcs"
)

func main() {
	// Dial using SOCKS5 proxy.

	sock5, _ := proxy.SOCKS5("tcp", "IP:PORT", &proxy.Auth{
		User:     "YOURUSERNAME",
		Password: "YOURPASSWORD",
	}, proxy.Direct)
	dc := sock5.(proxy.ContextDialer)

	// Creating connection.
	ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
	defer cancel()
	client := telegram.NewClient(1, "appHash", telegram.Options{
		Resolver: dcs.Plain(dcs.PlainOptions{
			Dial: dc.DialContext,
		}),
	})

	_ = client.Run(ctx, func(ctx context.Context) error {
		fmt.Println("Started")
		return nil
	})
}
Output:

type List

type List struct {
	Options []tg.DCOption
	Domains map[int]string
	Test    bool
}

List is a list of Telegram DC addresses and domains.

func Prod

func Prod() List

Prod returns production DC list.

func Staging deprecated

func Staging() List

Staging returns staging DC list.

Deprecated: Use Test().

func Test

func Test() List

Test returns test DC list.

func (List) Zero

func (d List) Zero() bool

Zero returns true if this List is zero value.

type MTProxyOptions

type MTProxyOptions struct {
	// Dial specifies the dial function for creating unencrypted TCP connections.
	// If Dial is nil, then the resolver dials using package net.
	Dial DialFunc
	// Network to use. Defaults to "tcp"
	Network string
	// Random source for MTProxy obfuscator.
	Rand io.Reader
}

MTProxyOptions is MTProxy resolver creation options.

type PlainOptions

type PlainOptions struct {
	// Protocol is the transport protocol to use. Defaults to intermediate.
	Protocol Protocol
	// Dial specifies the dial function for creating unencrypted TCP connections.
	// If Dial is nil, then the resolver dials using package net.
	Dial DialFunc
	// Random source for TCPObfuscated DCs.
	Rand io.Reader
	// Network to use. Defaults to "tcp".
	Network string
	// NoObfuscated denotes to filter out TCP Obfuscated Only DCs.
	NoObfuscated bool
	// PreferIPv6 gives IPv6 DCs higher precedence.
	// Default is to prefer IPv4 DCs over IPv6.
	PreferIPv6 bool
}

PlainOptions is plain resolver creation options.

type Protocol

type Protocol interface {
	Codec() transport.Codec
	Handshake(conn net.Conn) (transport.Conn, error)
}

Protocol is MTProto transport protocol.

See https://core.telegram.org/mtproto/mtproto-transports

type Resolver

type Resolver interface {
	Primary(ctx context.Context, dc int, list List) (transport.Conn, error)
	MediaOnly(ctx context.Context, dc int, list List) (transport.Conn, error)
	CDN(ctx context.Context, dc int, list List) (transport.Conn, error)
}

Resolver resolves DC and creates transport MTProto connection.

func DefaultResolver

func DefaultResolver() Resolver

DefaultResolver returns default DC resolver for current platform.

func MTProxy

func MTProxy(addr string, secret []byte, opts MTProxyOptions) (Resolver, error)

MTProxy creates MTProxy obfuscated DC resolver.

See https://core.telegram.org/mtproto/mtproto-transports#transport-obfuscation.

func Plain

func Plain(opts PlainOptions) Resolver

Plain creates plain DC resolver.

func Websocket

func Websocket(opts WebsocketOptions) Resolver

Websocket creates Websocket DC resolver.

See https://core.telegram.org/mtproto/transports#websocket.

type WebsocketOptions

type WebsocketOptions struct {
	// Dialer specifies the websocket dialer.
	// If Dialer is nil, then the resolver dials using websocket.DefaultDialer.
	DialOptions *websocket.DialOptions
	// Random source for MTProxy obfuscator.
	Rand io.Reader
}

WebsocketOptions is Websocket resolver creation options.

Jump to

Keyboard shortcuts

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