mihomo/adapter/outbound/reject.go

82 lines
2 KiB
Go
Raw Normal View History

2019-12-08 12:17:24 +08:00
package outbound
2018-06-10 22:50:03 +08:00
import (
"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
"github.com/Dreamacro/clash/component/dialer"
2018-06-10 22:50:03 +08:00
C "github.com/Dreamacro/clash/constant"
)
type Reject struct {
2018-12-22 23:56:42 +08:00
*Base
2018-06-10 22:50:03 +08:00
}
2021-04-29 11:23:14 +08:00
// DialContext implements C.ProxyAdapter
func (r *Reject) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) {
2021-11-20 12:34:14 +08:00
return NewConn(&nopConn{}, r), nil
2018-11-21 13:47:46 +08:00
}
// ListenPacketContext implements C.ProxyAdapter
func (r *Reject) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
2021-11-20 12:34:14 +08:00
return newPacketConn(newNopPacketConn(), r), nil
}
2018-06-10 22:50:03 +08:00
func NewReject() *Reject {
2018-12-22 23:56:42 +08:00
return &Reject{
Base: &Base{
name: "REJECT",
tp: C.Reject,
udp: true,
2018-12-22 23:56:42 +08:00
},
}
2018-06-10 22:50:03 +08:00
}
2021-11-20 12:34:14 +08:00
type nopConn struct{}
2018-06-10 22:50:03 +08:00
2021-11-20 12:34:14 +08:00
func (rw *nopConn) Read(b []byte) (int, error) {
return 0, io.EOF
2018-06-10 22:50:03 +08:00
}
2021-11-20 12:34:14 +08:00
func (rw *nopConn) Write(b []byte) (int, error) {
2018-06-10 22:50:03 +08:00
return 0, io.EOF
}
2018-07-31 17:53:39 +08:00
2021-11-20 12:34:14 +08:00
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 }
2018-07-31 17:53:39 +08:00
2021-11-20 12:34:14 +08:00
type nopPacketConn struct {
ch chan struct{}
}
2018-07-31 17:53:39 +08:00
2021-11-20 12:34:14 +08:00
func (npc *nopPacketConn) WriteTo(b []byte, addr net.Addr) (n int, err error) {
return len(b), nil
}
2018-07-31 17:53:39 +08:00
2021-11-20 12:34:14 +08:00
func (npc *nopPacketConn) ReadFrom(b []byte) (int, net.Addr, error) {
<-npc.ch
return 0, nil, io.EOF
}
2018-07-31 17:53:39 +08:00
2021-11-20 12:34:14 +08:00
func (npc *nopPacketConn) Close() error {
close(npc.ch)
return nil
}
2018-07-31 17:53:39 +08:00
2021-11-20 12:34:14 +08:00
func (npc *nopPacketConn) LocalAddr() net.Addr { return &net.UDPAddr{IP: net.IPv4zero, Port: 0} }
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 }
func newNopPacketConn() *nopPacketConn {
return &nopPacketConn{
ch: make(chan struct{}),
}
}