Highly-opinionated (ex-bullshit-free) MTPROTO proxy for Telegram. If you use v1.0 or upgrade broke you proxy, please read the chapter Version 2
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dialer.go 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package telegram
  2. import (
  3. "io"
  4. "net"
  5. "time"
  6. "github.com/juju/errors"
  7. "github.com/9seconds/mtg/config"
  8. "github.com/9seconds/mtg/wrappers"
  9. )
  10. const telegramKeepAlive = 30 * time.Second
  11. type tgDialer struct {
  12. net.Dialer
  13. conf *config.Config
  14. }
  15. func (t *tgDialer) dial(addr string) (net.Conn, error) {
  16. connRaw, err := t.Dialer.Dial("tcp", addr)
  17. if err != nil {
  18. return nil, errors.Annotate(err, "Cannot connect to Telegram")
  19. }
  20. conn := connRaw.(*net.TCPConn)
  21. if err = conn.SetKeepAlive(true); err != nil {
  22. return nil, errors.Annotate(err, "Cannot establish keepalive connection")
  23. }
  24. if err = conn.SetKeepAlivePeriod(telegramKeepAlive); err != nil {
  25. return nil, errors.Annotate(err, "Cannot set keepalive timeout")
  26. }
  27. return conn, nil
  28. }
  29. func (t *tgDialer) dialRWC(addr string) (io.ReadWriteCloser, error) {
  30. conn, err := t.dial(addr)
  31. if err != nil {
  32. return nil, err
  33. }
  34. return wrappers.NewTimeoutRWC(conn, t.conf.TimeoutRead, t.conf.TimeoutWrite), nil
  35. }
  36. func newDialer(conf *config.Config) *tgDialer {
  37. return &tgDialer{
  38. Dialer: net.Dialer{Timeout: conf.TimeoutRead},
  39. conf: conf,
  40. }
  41. }