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
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

default.go 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package network
  2. import (
  3. "context"
  4. "fmt"
  5. "net"
  6. "time"
  7. "github.com/libp2p/go-reuseport"
  8. )
  9. type defaultDialer struct {
  10. net.Dialer
  11. bufferSize int
  12. }
  13. func (d *defaultDialer) Dial(network, address string) (net.Conn, error) {
  14. return d.DialContext(context.Background(), network, address)
  15. }
  16. func (d *defaultDialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
  17. switch network {
  18. case "tcp", "tcp4", "tcp6":
  19. default:
  20. return nil, fmt.Errorf("unsupported network %s", network)
  21. }
  22. conn, err := d.Dialer.DialContext(ctx, network, address)
  23. if err != nil {
  24. return nil, fmt.Errorf("cannot dial to %s: %w", address, err)
  25. }
  26. tcpConn := conn.(*net.TCPConn)
  27. if err := tcpConn.SetNoDelay(true); err != nil {
  28. conn.Close()
  29. return nil, fmt.Errorf("cannot set TCP_NO_DELAY: %w", err)
  30. }
  31. if err := tcpConn.SetReadBuffer(d.bufferSize); err != nil {
  32. tcpConn.Close()
  33. return nil, fmt.Errorf("cannot set read buffer size: %w", err)
  34. }
  35. if err := tcpConn.SetWriteBuffer(d.bufferSize); err != nil {
  36. tcpConn.Close()
  37. return nil, fmt.Errorf("cannot set write buffer size: %w", err)
  38. }
  39. if err := tcpConn.SetKeepAlive(true); err != nil {
  40. tcpConn.Close()
  41. return nil, fmt.Errorf("cannot enable keep-alive: %w", err)
  42. }
  43. return tcpConn, nil
  44. }
  45. func NewDefaultDialer(timeout time.Duration, bufferSize int) (Dialer, error) {
  46. switch {
  47. case timeout < 0:
  48. return nil, fmt.Errorf("timeout %v should be positive number", timeout)
  49. case bufferSize < 0:
  50. return nil, fmt.Errorf("buffer size %s should be positive number", bufferSize)
  51. }
  52. if timeout == 0 {
  53. timeout = DefaultTimeout
  54. }
  55. if bufferSize == 0 {
  56. bufferSize = DefaultBufferSize
  57. }
  58. return &defaultDialer{
  59. Dialer: net.Dialer{
  60. Timeout: timeout,
  61. Control: reuseport.Control,
  62. },
  63. bufferSize: bufferSize,
  64. }, nil
  65. }