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
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package client
  2. import (
  3. "net"
  4. "time"
  5. "github.com/juju/errors"
  6. "github.com/9seconds/mtg/config"
  7. "github.com/9seconds/mtg/mtproto"
  8. "github.com/9seconds/mtg/obfuscated2"
  9. "github.com/9seconds/mtg/wrappers"
  10. )
  11. const handshakeTimeout = 10 * time.Second
  12. // DirectInit initializes client connection for proxy which connects to
  13. // Telegram directly.
  14. func DirectInit(socket net.Conn, connID string, conf *config.Config) (wrappers.Wrap, *mtproto.ConnectionOpts, error) {
  15. tcpSocket := socket.(*net.TCPConn)
  16. if err := tcpSocket.SetNoDelay(false); err != nil {
  17. return nil, nil, errors.Annotate(err, "Cannot disable NO_DELAY to client socket")
  18. }
  19. if err := tcpSocket.SetReadBuffer(conf.ReadBufferSize); err != nil {
  20. return nil, nil, errors.Annotate(err, "Cannot set read buffer size of client socket")
  21. }
  22. if err := tcpSocket.SetWriteBuffer(conf.WriteBufferSize); err != nil {
  23. return nil, nil, errors.Annotate(err, "Cannot set write buffer size of client socket")
  24. }
  25. socket.SetReadDeadline(time.Now().Add(handshakeTimeout)) // nolint: errcheck
  26. frame, err := obfuscated2.ExtractFrame(socket)
  27. if err != nil {
  28. return nil, nil, errors.Annotate(err, "Cannot extract frame")
  29. }
  30. socket.SetReadDeadline(time.Time{}) // nolint: errcheck
  31. conn := wrappers.NewConn(socket, connID, wrappers.ConnPurposeClient, conf.PublicIPv4, conf.PublicIPv6)
  32. obfs2, connOpts, err := obfuscated2.ParseObfuscated2ClientFrame(conf.Secret, frame)
  33. if err != nil {
  34. return nil, nil, errors.Annotate(err, "Cannot parse obfuscated frame")
  35. }
  36. connOpts.ConnectionProto = mtproto.ConnectionProtocolAny
  37. connOpts.ClientAddr = conn.RemoteAddr()
  38. conn = wrappers.NewStreamCipher(conn, obfs2.Encryptor, obfs2.Decryptor)
  39. conn.Logger().Infow("Client connection initialized")
  40. return conn, connOpts, nil
  41. }