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
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

default.go 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package network
  2. import (
  3. "context"
  4. "fmt"
  5. "net"
  6. "time"
  7. "github.com/9seconds/mtg/v2/essentials"
  8. )
  9. type defaultDialer struct {
  10. net.Dialer
  11. }
  12. func (d *defaultDialer) Dial(network, address string) (essentials.Conn, error) {
  13. return d.DialContext(context.Background(), network, address)
  14. }
  15. func (d *defaultDialer) DialContext(ctx context.Context, network, address string) (essentials.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, 0); err != nil {
  27. conn.Close() //nolint: errcheck
  28. return nil, fmt.Errorf("cannot set socket options: %w", err)
  29. }
  30. return conn.(essentials.Conn), nil //nolint: forcetypeassert
  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. //
  38. // bufferSize is deprecated and ignored. It is kept here for backward
  39. // compatibility.
  40. func NewDefaultDialer(timeout time.Duration, bufferSize int) (Dialer, error) {
  41. switch {
  42. case timeout < 0:
  43. return nil, fmt.Errorf("timeout %v should be positive number", timeout)
  44. case timeout == 0:
  45. timeout = DefaultTimeout
  46. }
  47. return &defaultDialer{
  48. Dialer: net.Dialer{
  49. Timeout: timeout,
  50. },
  51. }, nil
  52. }