Skip to content

Commit d86eda1

Browse files
zxspiritclaude
andcommitted
interrupt: Add wrapper for sing-style packet connections
Add SingPacketConn and Group.NewSingPacketConn to allow registering N.PacketConn connections in an interrupt group, mirroring the existing net.Conn and net.PacketConn wrappers. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent c4801b7 commit d86eda1

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

common/interrupt/conn.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"net"
55

66
"github.com/sagernet/sing/common/bufio"
7+
N "github.com/sagernet/sing/common/network"
78
"github.com/sagernet/sing/common/x/list"
89
)
910

@@ -52,6 +53,31 @@ type PacketConn struct {
5253
element *list.Element[*groupConnItem]
5354
}
5455

56+
type SingPacketConn struct {
57+
N.PacketConn
58+
group *Group
59+
element *list.Element[*groupConnItem]
60+
}
61+
62+
func (c *SingPacketConn) Close() error {
63+
c.group.access.Lock()
64+
defer c.group.access.Unlock()
65+
c.group.connections.Remove(c.element)
66+
return c.PacketConn.Close()
67+
}
68+
69+
func (c *SingPacketConn) ReaderReplaceable() bool {
70+
return true
71+
}
72+
73+
func (c *SingPacketConn) WriterReplaceable() bool {
74+
return true
75+
}
76+
77+
func (c *SingPacketConn) Upstream() any {
78+
return c.PacketConn
79+
}
80+
5581
/*func (c *PacketConn) MarkAsInternal() {
5682
c.element.Value.internal = true
5783
}*/

common/interrupt/group.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"net"
66
"sync"
77

8+
N "github.com/sagernet/sing/common/network"
89
"github.com/sagernet/sing/common/x/list"
910
)
1011

@@ -36,6 +37,13 @@ func (g *Group) NewPacketConn(conn net.PacketConn, isExternal bool) net.PacketCo
3637
return &PacketConn{PacketConn: conn, group: g, element: item}
3738
}
3839

40+
func (g *Group) NewSingPacketConn(conn N.PacketConn, isExternal bool) N.PacketConn {
41+
g.access.Lock()
42+
defer g.access.Unlock()
43+
item := g.connections.PushBack(&groupConnItem{conn, isExternal})
44+
return &SingPacketConn{PacketConn: conn, group: g, element: item}
45+
}
46+
3947
func (g *Group) Interrupt(interruptExternalConnections bool) {
4048
g.access.Lock()
4149
defer g.access.Unlock()

0 commit comments

Comments
 (0)