reject.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package outbound
  2. import (
  3. "context"
  4. "io"
  5. "net"
  6. "time"
  7. "github.com/metacubex/mihomo/common/buf"
  8. "github.com/metacubex/mihomo/component/dialer"
  9. C "github.com/metacubex/mihomo/constant"
  10. )
  11. type Reject struct {
  12. *Base
  13. drop bool
  14. }
  15. type RejectOption struct {
  16. Name string `proxy:"name"`
  17. }
  18. // DialContext implements C.ProxyAdapter
  19. func (r *Reject) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) {
  20. if r.drop {
  21. return NewConn(dropConn{}, r), nil
  22. }
  23. return NewConn(nopConn{}, r), nil
  24. }
  25. // ListenPacketContext implements C.ProxyAdapter
  26. func (r *Reject) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
  27. return newPacketConn(&nopPacketConn{}, r), nil
  28. }
  29. func NewRejectWithOption(option RejectOption) *Reject {
  30. return &Reject{
  31. Base: &Base{
  32. name: option.Name,
  33. tp: C.Direct,
  34. udp: true,
  35. },
  36. }
  37. }
  38. func NewReject() *Reject {
  39. return &Reject{
  40. Base: &Base{
  41. name: "REJECT",
  42. tp: C.Reject,
  43. udp: true,
  44. prefer: C.DualStack,
  45. },
  46. }
  47. }
  48. func NewRejectDrop() *Reject {
  49. return &Reject{
  50. Base: &Base{
  51. name: "REJECT-DROP",
  52. tp: C.RejectDrop,
  53. udp: true,
  54. prefer: C.DualStack,
  55. },
  56. drop: true,
  57. }
  58. }
  59. func NewPass() *Reject {
  60. return &Reject{
  61. Base: &Base{
  62. name: "PASS",
  63. tp: C.Pass,
  64. udp: true,
  65. prefer: C.DualStack,
  66. },
  67. }
  68. }
  69. type nopConn struct{}
  70. func (rw nopConn) Read(b []byte) (int, error) { return 0, io.EOF }
  71. func (rw nopConn) ReadBuffer(buffer *buf.Buffer) error { return io.EOF }
  72. func (rw nopConn) Write(b []byte) (int, error) { return 0, io.EOF }
  73. func (rw nopConn) WriteBuffer(buffer *buf.Buffer) error { return io.EOF }
  74. func (rw nopConn) Close() error { return nil }
  75. func (rw nopConn) LocalAddr() net.Addr { return nil }
  76. func (rw nopConn) RemoteAddr() net.Addr { return nil }
  77. func (rw nopConn) SetDeadline(time.Time) error { return nil }
  78. func (rw nopConn) SetReadDeadline(time.Time) error { return nil }
  79. func (rw nopConn) SetWriteDeadline(time.Time) error { return nil }
  80. var udpAddrIPv4Unspecified = &net.UDPAddr{IP: net.IPv4zero, Port: 0}
  81. type nopPacketConn struct{}
  82. func (npc nopPacketConn) WriteTo(b []byte, addr net.Addr) (n int, err error) {
  83. return len(b), nil
  84. }
  85. func (npc nopPacketConn) ReadFrom(b []byte) (int, net.Addr, error) {
  86. return 0, nil, io.EOF
  87. }
  88. func (npc nopPacketConn) WaitReadFrom() ([]byte, func(), net.Addr, error) {
  89. return nil, nil, nil, io.EOF
  90. }
  91. func (npc nopPacketConn) Close() error { return nil }
  92. func (npc nopPacketConn) LocalAddr() net.Addr { return udpAddrIPv4Unspecified }
  93. func (npc nopPacketConn) SetDeadline(time.Time) error { return nil }
  94. func (npc nopPacketConn) SetReadDeadline(time.Time) error { return nil }
  95. func (npc nopPacketConn) SetWriteDeadline(time.Time) error { return nil }
  96. type dropConn struct{}
  97. func (rw dropConn) Read(b []byte) (int, error) { return 0, io.EOF }
  98. func (rw dropConn) ReadBuffer(buffer *buf.Buffer) error {
  99. time.Sleep(C.DefaultDropTime)
  100. return io.EOF
  101. }
  102. func (rw dropConn) Write(b []byte) (int, error) { return 0, io.EOF }
  103. func (rw dropConn) WriteBuffer(buffer *buf.Buffer) error { return io.EOF }
  104. func (rw dropConn) Close() error { return nil }
  105. func (rw dropConn) LocalAddr() net.Addr { return nil }
  106. func (rw dropConn) RemoteAddr() net.Addr { return nil }
  107. func (rw dropConn) SetDeadline(time.Time) error { return nil }
  108. func (rw dropConn) SetReadDeadline(time.Time) error { return nil }
  109. func (rw dropConn) SetWriteDeadline(time.Time) error { return nil }