2019-12-08 12:17:24 +08:00
|
|
|
package outbound
|
2018-06-10 22:50:03 +08:00
|
|
|
|
|
|
|
import (
|
2019-10-12 23:55:39 +08:00
|
|
|
"context"
|
2018-06-10 22:50:03 +08:00
|
|
|
"io"
|
2018-06-14 01:00:58 +08:00
|
|
|
"net"
|
2018-07-31 17:53:39 +08:00
|
|
|
"time"
|
2018-06-10 22:50:03 +08:00
|
|
|
|
2023-11-03 21:01:45 +08:00
|
|
|
"github.com/metacubex/mihomo/common/buf"
|
|
|
|
"github.com/metacubex/mihomo/component/dialer"
|
|
|
|
C "github.com/metacubex/mihomo/constant"
|
2018-06-10 22:50:03 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type Reject struct {
|
2018-12-22 23:56:42 +08:00
|
|
|
*Base
|
2023-11-18 13:17:15 +08:00
|
|
|
drop bool
|
2018-06-10 22:50:03 +08:00
|
|
|
}
|
|
|
|
|
2023-10-11 13:01:14 +08:00
|
|
|
type RejectOption struct {
|
|
|
|
Name string `proxy:"name"`
|
|
|
|
}
|
|
|
|
|
2021-04-29 11:23:14 +08:00
|
|
|
// DialContext implements C.ProxyAdapter
|
2021-11-07 16:48:51 +08:00
|
|
|
func (r *Reject) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) {
|
2023-11-18 13:17:15 +08:00
|
|
|
if r.drop {
|
|
|
|
return NewConn(dropConn{}, r), nil
|
|
|
|
}
|
2023-03-04 18:33:05 +08:00
|
|
|
return NewConn(nopConn{}, r), nil
|
2018-11-21 13:47:46 +08:00
|
|
|
}
|
|
|
|
|
2021-10-15 21:44:53 +08:00
|
|
|
// ListenPacketContext implements C.ProxyAdapter
|
2021-11-07 16:48:51 +08:00
|
|
|
func (r *Reject) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
|
2023-11-18 13:17:15 +08:00
|
|
|
if r.drop {
|
|
|
|
return newPacketConn(&dropPacketConn{}, r), nil
|
|
|
|
}
|
|
|
|
return newPacketConn(&nopPacketConn{}, r), nil
|
2019-12-30 10:51:35 +08:00
|
|
|
}
|
|
|
|
|
2023-10-11 13:01:14 +08:00
|
|
|
func NewRejectWithOption(option RejectOption) *Reject {
|
|
|
|
return &Reject{
|
|
|
|
Base: &Base{
|
|
|
|
name: option.Name,
|
|
|
|
tp: C.Direct,
|
|
|
|
udp: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-10 22:50:03 +08:00
|
|
|
func NewReject() *Reject {
|
2018-12-22 23:56:42 +08:00
|
|
|
return &Reject{
|
|
|
|
Base: &Base{
|
2022-08-28 13:41:19 +08:00
|
|
|
name: "REJECT",
|
|
|
|
tp: C.Reject,
|
|
|
|
udp: true,
|
|
|
|
prefer: C.DualStack,
|
2018-12-22 23:56:42 +08:00
|
|
|
},
|
|
|
|
}
|
2018-06-10 22:50:03 +08:00
|
|
|
}
|
|
|
|
|
2023-11-18 13:17:15 +08:00
|
|
|
func NewRejectDrop() *Reject {
|
|
|
|
return &Reject{
|
|
|
|
Base: &Base{
|
|
|
|
name: "REJECT-DROP",
|
|
|
|
tp: C.RejectDrop,
|
|
|
|
udp: true,
|
|
|
|
prefer: C.DualStack,
|
|
|
|
},
|
|
|
|
drop: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-02 09:16:47 +08:00
|
|
|
func NewPass() *Reject {
|
|
|
|
return &Reject{
|
|
|
|
Base: &Base{
|
2022-08-28 13:41:19 +08:00
|
|
|
name: "PASS",
|
|
|
|
tp: C.Pass,
|
|
|
|
udp: true,
|
|
|
|
prefer: C.DualStack,
|
2022-05-02 09:16:47 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-20 12:34:14 +08:00
|
|
|
type nopConn struct{}
|
2018-06-10 22:50:03 +08:00
|
|
|
|
2023-11-18 13:17:15 +08:00
|
|
|
func (rw nopConn) Read(b []byte) (int, error) { return 0, io.EOF }
|
2018-07-31 17:53:39 +08:00
|
|
|
|
2023-11-18 13:17:15 +08:00
|
|
|
func (rw nopConn) ReadBuffer(buffer *buf.Buffer) error { return io.EOF }
|
2023-03-04 18:33:05 +08:00
|
|
|
|
2023-11-18 13:17:15 +08:00
|
|
|
func (rw nopConn) Write(b []byte) (int, error) { return 0, io.EOF }
|
|
|
|
func (rw nopConn) WriteBuffer(buffer *buf.Buffer) error { return io.EOF }
|
|
|
|
func (rw nopConn) Close() error { return nil }
|
|
|
|
func (rw nopConn) LocalAddr() net.Addr { return nil }
|
|
|
|
func (rw nopConn) RemoteAddr() net.Addr { return nil }
|
|
|
|
func (rw nopConn) SetDeadline(time.Time) error { return nil }
|
|
|
|
func (rw nopConn) SetReadDeadline(time.Time) error { return nil }
|
|
|
|
func (rw nopConn) SetWriteDeadline(time.Time) error { return nil }
|
2023-03-04 18:33:05 +08:00
|
|
|
|
|
|
|
var udpAddrIPv4Unspecified = &net.UDPAddr{IP: net.IPv4zero, Port: 0}
|
2018-07-31 17:53:39 +08:00
|
|
|
|
2021-11-20 12:44:31 +08:00
|
|
|
type nopPacketConn struct{}
|
2018-07-31 17:53:39 +08:00
|
|
|
|
2023-11-18 13:17:15 +08:00
|
|
|
func (npc nopPacketConn) WriteTo(b []byte, addr net.Addr) (n int, err error) {
|
|
|
|
return len(b), nil
|
|
|
|
}
|
|
|
|
func (npc nopPacketConn) ReadFrom(b []byte) (int, net.Addr, error) {
|
|
|
|
return 0, nil, io.EOF
|
|
|
|
}
|
2023-06-02 22:58:33 +08:00
|
|
|
func (npc nopPacketConn) WaitReadFrom() ([]byte, func(), net.Addr, error) {
|
|
|
|
return nil, nil, nil, io.EOF
|
|
|
|
}
|
|
|
|
func (npc nopPacketConn) Close() error { return nil }
|
|
|
|
func (npc nopPacketConn) LocalAddr() net.Addr { return udpAddrIPv4Unspecified }
|
|
|
|
func (npc nopPacketConn) SetDeadline(time.Time) error { return nil }
|
|
|
|
func (npc nopPacketConn) SetReadDeadline(time.Time) error { return nil }
|
|
|
|
func (npc nopPacketConn) SetWriteDeadline(time.Time) error { return nil }
|
2023-11-18 13:17:15 +08:00
|
|
|
|
|
|
|
type dropConn struct{}
|
|
|
|
|
|
|
|
func (rw dropConn) Read(b []byte) (int, error) { return 0, io.EOF }
|
|
|
|
func (rw dropConn) ReadBuffer(buffer *buf.Buffer) error {
|
|
|
|
time.Sleep(C.DefaultDropTime)
|
|
|
|
return io.EOF
|
|
|
|
}
|
|
|
|
func (rw dropConn) Write(b []byte) (int, error) { return 0, io.EOF }
|
|
|
|
func (rw dropConn) WriteBuffer(buffer *buf.Buffer) error { return io.EOF }
|
|
|
|
func (rw dropConn) Close() error { return nil }
|
|
|
|
func (rw dropConn) LocalAddr() net.Addr { return nil }
|
|
|
|
func (rw dropConn) RemoteAddr() net.Addr { return nil }
|
|
|
|
func (rw dropConn) SetDeadline(time.Time) error { return nil }
|
|
|
|
func (rw dropConn) SetReadDeadline(time.Time) error { return nil }
|
|
|
|
func (rw dropConn) SetWriteDeadline(time.Time) error { return nil }
|
|
|
|
|
|
|
|
type dropPacketConn struct{}
|
|
|
|
|
|
|
|
func (npc dropPacketConn) WriteTo(b []byte, addr net.Addr) (n int, err error) {
|
|
|
|
time.Sleep(C.DefaultDropTime)
|
|
|
|
return len(b), nil
|
|
|
|
}
|
|
|
|
func (npc dropPacketConn) ReadFrom(b []byte) (int, net.Addr, error) {
|
|
|
|
time.Sleep(C.DefaultDropTime)
|
|
|
|
return 0, nil, io.EOF
|
|
|
|
}
|
|
|
|
func (npc dropPacketConn) WaitReadFrom() ([]byte, func(), net.Addr, error) {
|
|
|
|
return nil, nil, nil, io.EOF
|
|
|
|
}
|
|
|
|
func (npc dropPacketConn) Close() error { return nil }
|
|
|
|
func (npc dropPacketConn) LocalAddr() net.Addr { return udpAddrIPv4Unspecified }
|
|
|
|
func (npc dropPacketConn) SetDeadline(time.Time) error { return nil }
|
|
|
|
func (npc dropPacketConn) SetReadDeadline(time.Time) error { return nil }
|
|
|
|
func (npc dropPacketConn) SetWriteDeadline(time.Time) error { return nil }
|