Skip to content

Commit

Permalink
add support for icmpv6
Browse files Browse the repository at this point in the history
  • Loading branch information
ginuerzh committed Jul 15, 2024
1 parent 2a75be9 commit c0a8040
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 47 deletions.
23 changes: 21 additions & 2 deletions dialer/icmp/dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down
15 changes: 4 additions & 11 deletions dialer/icmp/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
8 changes: 2 additions & 6 deletions handler/redirect/tcp/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
44 changes: 36 additions & 8 deletions internal/util/icmp/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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,
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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,
}
}
Expand All @@ -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
}
Expand All @@ -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
Expand All @@ -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
}

Expand Down Expand Up @@ -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
Expand Down
22 changes: 20 additions & 2 deletions listener/icmp/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down
19 changes: 5 additions & 14 deletions listener/icmp/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
5 changes: 1 addition & 4 deletions listener/redirect/tcp/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit c0a8040

Please sign in to comment.