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文字以内のものにしてください。

sockopts.go 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package network
  2. import (
  3. "fmt"
  4. "net"
  5. )
  6. // SetClientSocketOptions tunes a TCP socket that represents a connection to
  7. // end user (not Telegram service or fronting domain).
  8. //
  9. // bufferSize setting is deprecated and ignored.
  10. func SetClientSocketOptions(conn net.Conn, bufferSize int) error {
  11. return setCommonSocketOptions(conn.(*net.TCPConn)) //nolint: forcetypeassert
  12. }
  13. // SetServerSocketOptions tunes a TCP socket that represents a connection to
  14. // remote server like Telegram or fronting domain (but not end user).
  15. func SetServerSocketOptions(conn net.Conn, bufferSize int) error {
  16. return setCommonSocketOptions(conn.(*net.TCPConn)) //nolint: forcetypeassert
  17. }
  18. func setCommonSocketOptions(conn *net.TCPConn) error {
  19. if err := applyKeepAlive(conn, net.KeepAliveConfig{
  20. Enable: true,
  21. Idle: DefaultKeepAliveIdle,
  22. Interval: DefaultKeepAliveInterval,
  23. Count: DefaultKeepAliveCount,
  24. }); err != nil {
  25. return fmt.Errorf("cannot configure TCP keepalive: %w", err)
  26. }
  27. if err := conn.SetLinger(tcpLingerTimeout); err != nil {
  28. return fmt.Errorf("cannot set TCP linger timeout: %w", err)
  29. }
  30. rawConn, err := conn.SyscallConn()
  31. if err != nil {
  32. return fmt.Errorf("cannot get underlying raw connection: %w", err)
  33. }
  34. if err := setSocketReuseAddrPort(rawConn); err != nil {
  35. return fmt.Errorf("cannot setup SO_REUSEADDR/PORT: %w", err)
  36. }
  37. return nil
  38. }