transport

package
v0.26.1 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2021 License: MIT Imports: 14 Imported by: 14

Documentation

Overview

Package transport contains different MTProto transport implementations.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Codec

type Codec interface {
	// WriteHeader sends protocol tag if needed.
	WriteHeader(w io.Writer) error
	// ReadHeader reads protocol tag if needed.
	ReadHeader(r io.Reader) error
	// Write encode to writer message from given buffer.
	Write(w io.Writer, b *bin.Buffer) error
	// Read fills buffer with received message.
	Read(r io.Reader, b *bin.Buffer) error
}

Codec is MTProto transport protocol encoding abstraction.

type Conn added in v0.12.0

type Conn interface {
	Send(ctx context.Context, b *bin.Buffer) error
	Recv(ctx context.Context, b *bin.Buffer) error
	Close() error
}

Conn is transport connection.

type DialFunc

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

DialFunc is functional helper for Dialer.

Example
package main

import (
	"context"
	"fmt"
	"time"

	"golang.org/x/net/proxy"

	"github.com/gotd/td/telegram"
	"github.com/gotd/td/transport"
)

func main() {
	trp := transport.Intermediate(transport.DialFunc(proxy.Dial))

	// Creating connection.
	ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
	defer cancel()
	client := telegram.NewClient(1, "appHash", telegram.Options{
		Transport: trp,
	})

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

func (DialFunc) DialContext

func (d DialFunc) DialContext(ctx context.Context, network, address string) (net.Conn, error)

DialContext implements Dialer.

type Dialer

type Dialer interface {
	DialContext(ctx context.Context, network, address string) (net.Conn, error)
}

Dialer dials using a context.

Example

ExampleDialer for socks5. Methods form https://stackoverflow.com/questions/59456936/socks5-proxy-client-with-context-support

package main

import (
	"context"
	"fmt"
	"net"
	"time"

	"golang.org/x/net/proxy"

	"github.com/gotd/td/telegram"
	"github.com/gotd/td/transport"
)

func main() {
	// No error would be return.
	sock5, _ := proxy.SOCKS5("tcp", "IP:PORT", &proxy.Auth{
		User:     "YOURUSERNAME",
		Password: "YOURPASSWORD",
	}, proxy.Direct)

	dc := sock5.(interface {
		DialContext(ctx context.Context, network, addr string) (net.Conn, error)
	})

	// Creating connection.
	ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
	defer cancel()
	client := telegram.NewClient(1, "appHash", telegram.Options{
		Transport: transport.Intermediate(transport.DialFunc(dc.DialContext)),
	})

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

type Handler

type Handler func(ctx context.Context, conn Conn) error

Handler is MTProto server connection handler.

type Server

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

Server is a simple MTProto server.

func NewCustomServer

func NewCustomServer(c func() Codec, listener net.Listener) *Server

NewCustomServer creates new MTProto server with custom transport codec.

func NewFullServer added in v0.12.0

func NewFullServer(listener net.Listener) *Server

NewFullServer creates new MTProto server with Full transport codec.

func NewIntermediateServer

func NewIntermediateServer(listener net.Listener) *Server

NewIntermediateServer creates new MTProto server with Intermediate transport codec.

func (*Server) Addr

func (s *Server) Addr() net.Addr

Addr returns server address.

func (*Server) Close

func (s *Server) Close() error

Close stops server and closes given listener.

func (*Server) Serve

func (s *Server) Serve(serveCtx context.Context, handler Handler) error

Serve runs server using given listener.

type Transport

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

Transport is MTProto connection creator.

func Abridged added in v0.14.0

func Abridged(d Dialer) *Transport

Abridged creates Abridged transport.

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

func Full

func Full(d Dialer) *Transport

Full creates Full transport.

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

func Intermediate

func Intermediate(d Dialer) *Transport

Intermediate creates Intermediate transport.

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

func MTProxy added in v0.14.0

func MTProxy(d Dialer, addr string, secret []byte) (*Transport, error)

MTProxy creates MTProxy obfuscated transport.

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

Example
package main

import (
	"context"
	"encoding/hex"
	"fmt"
	"os"
	"time"

	"github.com/gotd/td/telegram"
	"github.com/gotd/td/transport"
)

func main() {
	addr, ok := os.LookupEnv("MTPROXY_ADDR")
	if !ok {
		fmt.Println("MTPROXY_ADDR is not set")
		return
	}

	secret, err := hex.DecodeString(os.Getenv("MTPROXY_SECRET"))
	if err != nil {
		panic(err)
	}

	trp, err := transport.MTProxy(nil, addr, secret)
	if err != nil {
		panic(err)
	}

	// Creating connection.
	ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
	defer cancel()
	client := telegram.NewClient(1, "appHash", telegram.Options{
		Transport: trp,
	})

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

func NewTransport added in v0.12.0

func NewTransport(dialer Dialer, getCodec func() Codec) *Transport

NewTransport creates transport using user Codec constructor.

func PaddedIntermediate added in v0.14.0

func PaddedIntermediate(d Dialer) *Transport

PaddedIntermediate creates Padded intermediate transport.

See https://core.telegram.org/mtproto/mtproto-transports#padded-intermediate

func (*Transport) Codec

func (t *Transport) Codec() Codec

Codec creates new codec using transport settings.

func (*Transport) DialContext

func (t *Transport) DialContext(ctx context.Context, network, address string) (Conn, error)

DialContext creates new MTProto connection.

func (*Transport) Pipe added in v0.15.1

func (t *Transport) Pipe() (a, b Conn)

Pipe creates a in-memory MTProto connection.

Jump to

Keyboard shortcuts

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