|
1 | 1 | package network |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "io" |
| 5 | + "syscall" |
| 6 | + |
| 7 | + "github.com/sagernet/sing/common" |
4 | 8 | "github.com/sagernet/sing/common/buf" |
5 | 9 | M "github.com/sagernet/sing/common/metadata" |
6 | 10 | ) |
@@ -109,3 +113,87 @@ type VectorisedPacketReadWaiter interface { |
109 | 113 | type VectorisedPacketReadWaitCreator interface { |
110 | 114 | CreateVectorisedPacketReadWaiter() (VectorisedPacketReadWaiter, bool) |
111 | 115 | } |
| 116 | + |
| 117 | +type SyscallReadCreator interface { |
| 118 | + SyscallConnForRead() syscall.Conn |
| 119 | +} |
| 120 | + |
| 121 | +func SyscallAvailableForRead(reader io.Reader) bool { |
| 122 | + if _, ok := reader.(syscall.Conn); ok { |
| 123 | + return true |
| 124 | + } |
| 125 | + if _, ok := reader.(SyscallReadCreator); ok { |
| 126 | + return true |
| 127 | + } |
| 128 | + if u, ok := reader.(ReaderWithUpstream); !ok || !u.ReaderReplaceable() { |
| 129 | + return false |
| 130 | + } |
| 131 | + if u, ok := reader.(WithUpstreamReader); ok { |
| 132 | + return SyscallAvailableForRead(u.UpstreamReader().(io.Reader)) |
| 133 | + } |
| 134 | + if u, ok := reader.(common.WithUpstream); ok { |
| 135 | + return SyscallAvailableForRead(u.Upstream().(io.Reader)) |
| 136 | + } |
| 137 | + return false |
| 138 | +} |
| 139 | + |
| 140 | +func SyscallConnForRead(reader io.Reader) syscall.Conn { |
| 141 | + if c, ok := reader.(syscall.Conn); ok { |
| 142 | + return c |
| 143 | + } |
| 144 | + if c, ok := reader.(SyscallReadCreator); ok { |
| 145 | + return c.SyscallConnForRead() |
| 146 | + } |
| 147 | + if u, ok := reader.(ReaderWithUpstream); !ok || !u.ReaderReplaceable() { |
| 148 | + return nil |
| 149 | + } |
| 150 | + if u, ok := reader.(WithUpstreamReader); ok { |
| 151 | + return SyscallConnForRead(u.UpstreamReader().(io.Reader)) |
| 152 | + } |
| 153 | + if u, ok := reader.(common.WithUpstream); ok { |
| 154 | + return SyscallConnForRead(u.Upstream().(io.Reader)) |
| 155 | + } |
| 156 | + return nil |
| 157 | +} |
| 158 | + |
| 159 | +type SyscallWriteCreator interface { |
| 160 | + SyscallConnForWrite() syscall.Conn |
| 161 | +} |
| 162 | + |
| 163 | +func SyscallAvailableForWrite(writer io.Writer) bool { |
| 164 | + if _, ok := writer.(syscall.Conn); ok { |
| 165 | + return true |
| 166 | + } |
| 167 | + if _, ok := writer.(SyscallWriteCreator); ok { |
| 168 | + return true |
| 169 | + } |
| 170 | + if u, ok := writer.(WriterWithUpstream); !ok || !u.WriterReplaceable() { |
| 171 | + return false |
| 172 | + } |
| 173 | + if u, ok := writer.(WithUpstreamWriter); ok { |
| 174 | + return SyscallAvailableForWrite(u.UpstreamWriter().(io.Writer)) |
| 175 | + } |
| 176 | + if u, ok := writer.(common.WithUpstream); ok { |
| 177 | + return SyscallAvailableForWrite(u.Upstream().(io.Writer)) |
| 178 | + } |
| 179 | + return false |
| 180 | +} |
| 181 | + |
| 182 | +func SyscallConnForWrite(writer io.Writer) syscall.Conn { |
| 183 | + if c, ok := writer.(syscall.Conn); ok { |
| 184 | + return c |
| 185 | + } |
| 186 | + if c, ok := writer.(SyscallWriteCreator); ok { |
| 187 | + return c.SyscallConnForWrite() |
| 188 | + } |
| 189 | + if u, ok := writer.(WriterWithUpstream); !ok || !u.WriterReplaceable() { |
| 190 | + return nil |
| 191 | + } |
| 192 | + if u, ok := writer.(WithUpstreamWriter); ok { |
| 193 | + return SyscallConnForWrite(u.UpstreamWriter().(io.Writer)) |
| 194 | + } |
| 195 | + if u, ok := writer.(common.WithUpstream); ok { |
| 196 | + return SyscallConnForWrite(u.Upstream().(io.Writer)) |
| 197 | + } |
| 198 | + return nil |
| 199 | +} |
0 commit comments