add support for icmpv6
This commit is contained in:
parent
2a75be91b0
commit
c0a80400d2
@ -19,9 +19,11 @@ import (
|
||||
|
||||
func init() {
|
||||
registry.DialerRegistry().Register("icmp", NewDialer)
|
||||
registry.DialerRegistry().Register("icmp6", NewDialer6)
|
||||
}
|
||||
|
||||
type icmpDialer struct {
|
||||
ip6 bool
|
||||
sessions map[string]*quicSession
|
||||
sessionMutex sync.Mutex
|
||||
logger logger.Logger
|
||||
@ -42,6 +44,19 @@ func NewDialer(opts ...dialer.Option) dialer.Dialer {
|
||||
}
|
||||
}
|
||||
|
||||
func NewDialer6(opts ...dialer.Option) dialer.Dialer {
|
||||
options := dialer.Options{}
|
||||
for _, opt := range opts {
|
||||
opt(&options)
|
||||
}
|
||||
|
||||
return &icmpDialer{
|
||||
ip6: true,
|
||||
sessions: make(map[string]*quicSession),
|
||||
logger: options.Logger,
|
||||
options: options,
|
||||
}
|
||||
}
|
||||
func (d *icmpDialer) Init(md md.Metadata) (err error) {
|
||||
if err = d.parseMetadata(md); err != nil {
|
||||
return
|
||||
@ -71,7 +86,11 @@ func (d *icmpDialer) Dial(ctx context.Context, addr string, opts ...dialer.DialO
|
||||
}
|
||||
|
||||
var pc net.PacketConn
|
||||
pc, err = icmp.ListenPacket("ip4:icmp", "")
|
||||
if d.ip6 {
|
||||
pc, err = icmp.ListenPacket("ip6:ipv6-icmp", "")
|
||||
} else {
|
||||
pc, err = icmp.ListenPacket("ip4:icmp", "")
|
||||
}
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@ -81,7 +100,7 @@ func (d *icmpDialer) Dial(ctx context.Context, addr string, opts ...dialer.DialO
|
||||
id = rand.New(rand.NewSource(time.Now().UnixNano())).Intn(math.MaxUint16) + 1
|
||||
raddr.Port = id
|
||||
}
|
||||
pc = icmp_pkg.ClientConn(pc, id)
|
||||
pc = icmp_pkg.ClientConn(d.ip6, pc, id)
|
||||
|
||||
session, err = d.initSession(ctx, raddr, pc)
|
||||
if err != nil {
|
||||
|
@ -14,21 +14,14 @@ type metadata struct {
|
||||
}
|
||||
|
||||
func (d *icmpDialer) parseMetadata(md mdata.Metadata) (err error) {
|
||||
const (
|
||||
keepAlive = "keepAlive"
|
||||
keepAlivePeriod = "ttl"
|
||||
handshakeTimeout = "handshakeTimeout"
|
||||
maxIdleTimeout = "maxIdleTimeout"
|
||||
)
|
||||
|
||||
if mdutil.GetBool(md, keepAlive) {
|
||||
d.md.keepAlivePeriod = mdutil.GetDuration(md, keepAlivePeriod)
|
||||
if mdutil.GetBool(md, "keepalive") {
|
||||
d.md.keepAlivePeriod = mdutil.GetDuration(md, "ttl")
|
||||
if d.md.keepAlivePeriod <= 0 {
|
||||
d.md.keepAlivePeriod = 10 * time.Second
|
||||
}
|
||||
}
|
||||
d.md.handshakeTimeout = mdutil.GetDuration(md, handshakeTimeout)
|
||||
d.md.maxIdleTimeout = mdutil.GetDuration(md, maxIdleTimeout)
|
||||
d.md.handshakeTimeout = mdutil.GetDuration(md, "handshakeTimeout")
|
||||
d.md.maxIdleTimeout = mdutil.GetDuration(md, "maxIdleTimeout")
|
||||
|
||||
return
|
||||
}
|
||||
|
@ -14,12 +14,8 @@ type metadata struct {
|
||||
}
|
||||
|
||||
func (h *redirectHandler) parseMetadata(md mdata.Metadata) (err error) {
|
||||
const (
|
||||
tproxy = "tproxy"
|
||||
sniffing = "sniffing"
|
||||
)
|
||||
h.md.tproxy = mdutil.GetBool(md, tproxy)
|
||||
h.md.sniffing = mdutil.GetBool(md, sniffing)
|
||||
h.md.tproxy = mdutil.GetBool(md, "tproxy")
|
||||
h.md.sniffing = mdutil.GetBool(md, "sniffing")
|
||||
h.md.sniffingTimeout = mdutil.GetDuration(md, "sniffing.timeout")
|
||||
return
|
||||
}
|
||||
|
@ -12,6 +12,12 @@ import (
|
||||
"github.com/go-gost/core/logger"
|
||||
"golang.org/x/net/icmp"
|
||||
"golang.org/x/net/ipv4"
|
||||
"golang.org/x/net/ipv6"
|
||||
)
|
||||
|
||||
const (
|
||||
ICMPv4 = 1
|
||||
ICMPv6 = 58
|
||||
)
|
||||
|
||||
const (
|
||||
@ -79,13 +85,15 @@ func (m *message) Decode(b []byte) (n int, err error) {
|
||||
}
|
||||
|
||||
type clientConn struct {
|
||||
ip6 bool
|
||||
net.PacketConn
|
||||
id int
|
||||
seq uint32
|
||||
}
|
||||
|
||||
func ClientConn(conn net.PacketConn, id int) net.PacketConn {
|
||||
func ClientConn(ip6 bool, conn net.PacketConn, id int) net.PacketConn {
|
||||
return &clientConn{
|
||||
ip6: ip6,
|
||||
PacketConn: conn,
|
||||
id: id,
|
||||
}
|
||||
@ -101,13 +109,17 @@ func (c *clientConn) ReadFrom(b []byte) (n int, addr net.Addr, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
m, err := icmp.ParseMessage(1, buf[:n])
|
||||
proto := ICMPv4
|
||||
if c.ip6 {
|
||||
proto = ICMPv6
|
||||
}
|
||||
m, err := icmp.ParseMessage(proto, buf[:n])
|
||||
if err != nil {
|
||||
// logger.Default().Error("icmp: parse message %v", err)
|
||||
return 0, addr, err
|
||||
}
|
||||
echo, ok := m.Body.(*icmp.Echo)
|
||||
if !ok || m.Type != ipv4.ICMPTypeEchoReply {
|
||||
if !ok || m.Type != ipv4.ICMPTypeEchoReply && m.Type != ipv6.ICMPTypeEchoReply {
|
||||
// logger.Default().Warnf("icmp: invalid type %s (discarded)", m.Type)
|
||||
continue // discard
|
||||
}
|
||||
@ -135,6 +147,7 @@ func (c *clientConn) ReadFrom(b []byte) (n int, addr net.Addr, err error) {
|
||||
addr = &net.UDPAddr{
|
||||
IP: v.IP,
|
||||
Port: c.id,
|
||||
Zone: v.Zone,
|
||||
}
|
||||
}
|
||||
// logger.Default().Infof("icmp: read from: %v %d", addr, n)
|
||||
@ -146,7 +159,7 @@ func (c *clientConn) WriteTo(b []byte, addr net.Addr) (n int, err error) {
|
||||
// logger.Default().Infof("icmp: write to: %v %d", addr, len(b))
|
||||
switch v := addr.(type) {
|
||||
case *net.UDPAddr:
|
||||
addr = &net.IPAddr{IP: v.IP}
|
||||
addr = &net.IPAddr{IP: v.IP, Zone: v.Zone}
|
||||
}
|
||||
|
||||
buf := bufpool.Get(writeBufferSize)
|
||||
@ -170,6 +183,10 @@ func (c *clientConn) WriteTo(b []byte, addr net.Addr) (n int, err error) {
|
||||
Code: 0,
|
||||
Body: &echo,
|
||||
}
|
||||
if c.ip6 {
|
||||
m.Type = ipv6.ICMPTypeEchoRequest
|
||||
}
|
||||
|
||||
wb, err := m.Marshal(nil)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
@ -180,12 +197,14 @@ func (c *clientConn) WriteTo(b []byte, addr net.Addr) (n int, err error) {
|
||||
}
|
||||
|
||||
type serverConn struct {
|
||||
ip6 bool
|
||||
net.PacketConn
|
||||
seqs [65535]uint32
|
||||
}
|
||||
|
||||
func ServerConn(conn net.PacketConn) net.PacketConn {
|
||||
func ServerConn(ip6 bool, conn net.PacketConn) net.PacketConn {
|
||||
return &serverConn{
|
||||
ip6: ip6,
|
||||
PacketConn: conn,
|
||||
}
|
||||
}
|
||||
@ -200,14 +219,18 @@ func (c *serverConn) ReadFrom(b []byte) (n int, addr net.Addr, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
m, err := icmp.ParseMessage(1, buf[:n])
|
||||
proto := ICMPv4
|
||||
if c.ip6 {
|
||||
proto = ICMPv6
|
||||
}
|
||||
m, err := icmp.ParseMessage(proto, buf[:n])
|
||||
if err != nil {
|
||||
// logger.Default().Error("icmp: parse message %v", err)
|
||||
return 0, addr, err
|
||||
}
|
||||
|
||||
echo, ok := m.Body.(*icmp.Echo)
|
||||
if !ok || m.Type != ipv4.ICMPTypeEcho || echo.ID <= 0 {
|
||||
if !ok || echo.ID <= 0 || m.Type != ipv4.ICMPTypeEcho && m.Type != ipv6.ICMPTypeEchoRequest {
|
||||
// logger.Default().Warnf("icmp: invalid type %s (discarded)", m.Type)
|
||||
continue
|
||||
}
|
||||
@ -229,6 +252,7 @@ func (c *serverConn) ReadFrom(b []byte) (n int, addr net.Addr, err error) {
|
||||
addr = &net.UDPAddr{
|
||||
IP: v.IP,
|
||||
Port: echo.ID,
|
||||
Zone: v.Zone,
|
||||
}
|
||||
}
|
||||
break
|
||||
@ -244,7 +268,7 @@ func (c *serverConn) WriteTo(b []byte, addr net.Addr) (n int, err error) {
|
||||
var id int
|
||||
switch v := addr.(type) {
|
||||
case *net.UDPAddr:
|
||||
addr = &net.IPAddr{IP: v.IP}
|
||||
addr = &net.IPAddr{IP: v.IP, Zone: v.Zone}
|
||||
id = v.Port
|
||||
}
|
||||
|
||||
@ -275,6 +299,10 @@ func (c *serverConn) WriteTo(b []byte, addr net.Addr) (n int, err error) {
|
||||
Code: 0,
|
||||
Body: &echo,
|
||||
}
|
||||
if c.ip6 {
|
||||
m.Type = ipv6.ICMPTypeEchoReply
|
||||
}
|
||||
|
||||
wb, err := m.Marshal(nil)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
|
@ -19,9 +19,11 @@ import (
|
||||
|
||||
func init() {
|
||||
registry.ListenerRegistry().Register("icmp", NewListener)
|
||||
registry.ListenerRegistry().Register("icmp6", NewListener6)
|
||||
}
|
||||
|
||||
type icmpListener struct {
|
||||
ip6 bool
|
||||
ln quic.EarlyListener
|
||||
cqueue chan net.Conn
|
||||
errChan chan error
|
||||
@ -41,6 +43,18 @@ func NewListener(opts ...listener.Option) listener.Listener {
|
||||
}
|
||||
}
|
||||
|
||||
func NewListener6(opts ...listener.Option) listener.Listener {
|
||||
options := listener.Options{}
|
||||
for _, opt := range opts {
|
||||
opt(&options)
|
||||
}
|
||||
return &icmpListener{
|
||||
ip6: true,
|
||||
logger: options.Logger,
|
||||
options: options,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *icmpListener) Init(md md.Metadata) (err error) {
|
||||
if err = l.parseMetadata(md); err != nil {
|
||||
return
|
||||
@ -52,11 +66,15 @@ func (l *icmpListener) Init(md md.Metadata) (err error) {
|
||||
}
|
||||
|
||||
var conn net.PacketConn
|
||||
conn, err = icmp.ListenPacket("ip4:icmp", addr)
|
||||
if l.ip6 {
|
||||
conn, err = icmp.ListenPacket("ip6:ipv6-icmp", addr)
|
||||
} else {
|
||||
conn, err = icmp.ListenPacket("ip4:icmp", addr)
|
||||
}
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
conn = icmp_pkg.ServerConn(conn)
|
||||
conn = icmp_pkg.ServerConn(l.ip6, conn)
|
||||
conn = metrics.WrapPacketConn(l.options.Service, conn)
|
||||
conn = stats.WrapPacketConn(conn, l.options.Stats)
|
||||
conn = admission.WrapPacketConn(l.options.Admission, conn)
|
||||
|
@ -20,28 +20,19 @@ type metadata struct {
|
||||
}
|
||||
|
||||
func (l *icmpListener) parseMetadata(md mdata.Metadata) (err error) {
|
||||
const (
|
||||
keepAlive = "keepAlive"
|
||||
keepAlivePeriod = "ttl"
|
||||
handshakeTimeout = "handshakeTimeout"
|
||||
maxIdleTimeout = "maxIdleTimeout"
|
||||
|
||||
backlog = "backlog"
|
||||
)
|
||||
|
||||
l.md.backlog = mdutil.GetInt(md, backlog)
|
||||
l.md.backlog = mdutil.GetInt(md, "backlog")
|
||||
if l.md.backlog <= 0 {
|
||||
l.md.backlog = defaultBacklog
|
||||
}
|
||||
|
||||
if mdutil.GetBool(md, keepAlive) {
|
||||
l.md.keepAlivePeriod = mdutil.GetDuration(md, keepAlivePeriod)
|
||||
if mdutil.GetBool(md, "keepalive") {
|
||||
l.md.keepAlivePeriod = mdutil.GetDuration(md, "ttl")
|
||||
if l.md.keepAlivePeriod <= 0 {
|
||||
l.md.keepAlivePeriod = 10 * time.Second
|
||||
}
|
||||
}
|
||||
l.md.handshakeTimeout = mdutil.GetDuration(md, handshakeTimeout)
|
||||
l.md.maxIdleTimeout = mdutil.GetDuration(md, maxIdleTimeout)
|
||||
l.md.handshakeTimeout = mdutil.GetDuration(md, "handshakeTimeout")
|
||||
l.md.maxIdleTimeout = mdutil.GetDuration(md, "maxIdleTimeout")
|
||||
|
||||
return
|
||||
}
|
||||
|
@ -11,10 +11,7 @@ type metadata struct {
|
||||
}
|
||||
|
||||
func (l *redirectListener) parseMetadata(md mdata.Metadata) (err error) {
|
||||
const (
|
||||
tproxy = "tproxy"
|
||||
)
|
||||
l.md.tproxy = mdutil.GetBool(md, tproxy)
|
||||
l.md.tproxy = mdutil.GetBool(md, "tproxy")
|
||||
l.md.mptcp = mdutil.GetBool(md, "mptcp")
|
||||
return
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user