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