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.

default.go 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package network
  2. import (
  3. "context"
  4. "fmt"
  5. "net"
  6. "time"
  7. )
  8. type defaultDialer struct {
  9. net.Dialer
  10. bufferSize int
  11. }
  12. func (d *defaultDialer) Dial(network, address string) (net.Conn, error) {
  13. return d.DialContext(context.Background(), network, address)
  14. }
  15. func (d *defaultDialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
  16. switch network {
  17. case "tcp", "tcp4", "tcp6": // nolint: goconst
  18. default:
  19. return nil, fmt.Errorf("unsupported network %s", network)
  20. }
  21. conn, err := d.Dialer.DialContext(ctx, network, address)
  22. if err != nil {
  23. return nil, fmt.Errorf("cannot dial to %s: %w", address, err)
  24. }
  25. // we do not need to call to end user. End users call us.
  26. if err := SetServerSocketOptions(conn, d.bufferSize); err != nil {
  27. conn.Close()
  28. return nil, fmt.Errorf("cannot set socket options: %w", err)
  29. }
  30. return conn, nil
  31. }
  32. // NewDefaultDialer build a new dialer which dials bypassing proxies
  33. // etc.
  34. //
  35. // The most default one you can imagine. But it has tunes TCP
  36. // connections and setups SO_REUSEPORT.
  37. func NewDefaultDialer(timeout time.Duration, bufferSize int) (Dialer, error) {
  38. switch {
  39. case timeout < 0:
  40. return nil, fmt.Errorf("timeout %v should be positive number", timeout)
  41. case bufferSize < 0:
  42. return nil, fmt.Errorf("buffer size %d should be positive number", bufferSize)
  43. }
  44. if timeout == 0 {
  45. timeout = DefaultTimeout
  46. }
  47. if bufferSize == 0 {
  48. bufferSize = DefaultBufferSize
  49. }
  50. return &defaultDialer{
  51. Dialer: net.Dialer{
  52. Timeout: timeout,
  53. },
  54. bufferSize: bufferSize,
  55. }, nil
  56. }