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.

init_tcp.go 846B

12345678910111213141516171819202122232425262728293031323334353637
  1. package utils
  2. import (
  3. "fmt"
  4. "net"
  5. "time"
  6. "github.com/9seconds/mtg/config"
  7. )
  8. const tcpKeepAlivePingPeriod = 2 * time.Second
  9. func InitTCP(conn net.Conn) error {
  10. tcpConn := conn.(*net.TCPConn)
  11. if err := tcpConn.SetNoDelay(true); err != nil {
  12. return fmt.Errorf("cannot set TCP_NO_DELAY: %w", err)
  13. }
  14. if err := tcpConn.SetReadBuffer(config.C.ReadBuffer); err != nil {
  15. return fmt.Errorf("cannot set read buffer size: %w", err)
  16. }
  17. if err := tcpConn.SetWriteBuffer(config.C.WriteBuffer); err != nil {
  18. return fmt.Errorf("cannot set write buffer size: %w", err)
  19. }
  20. if err := tcpConn.SetKeepAlive(true); err != nil {
  21. return fmt.Errorf("cannot enable keep-alive: %w", err)
  22. }
  23. if err := tcpConn.SetKeepAlivePeriod(tcpKeepAlivePingPeriod); err != nil {
  24. return fmt.Errorf("cannot set keep-alive period: %w", err)
  25. }
  26. return nil
  27. }