This commit is contained in:
iceBear67
2026-07-24 13:29:16 +00:00
commit 2275bd7dc4
29 changed files with 3641 additions and 0 deletions
+11
View File
@@ -0,0 +1,11 @@
// This file is substituted in via `go build -overlay` for the js/wasm build ONLY.
//
// Upstream common/buf/buffer_unix.go (build tag `!windows`) defines
// Buffer.Iovec returning golang.org/x/sys/unix.Iovec, which does not exist for
// GOARCH=wasm. Iovec's only callers are the platform-specific bufio syscall
// paths, none of which are exercised under wasm (there are no raw socket fds in
// a browser). Replacing the file with an empty package body keeps common/buf
// compilable without pulling in unix.Iovec. Native builds do not use the
// overlay and keep the real implementation.
package buf
@@ -0,0 +1,76 @@
// Substituted in via `go build -overlay` for the js/wasm build ONLY.
//
// Upstream common/bufio/copy_direct_posix.go (build tag `!windows`) implements
// the syscall read-waiters using golang.org/x/sys/unix (readv/recvmsg). wait.go
// (cross-platform) references the createSyscall*ReadWaiter constructors, so the
// symbols must exist for the package to compile. Under wasm there are no raw
// socket fds, so the constructors report "not created" and the read paths are
// never taken. This provides the same identifiers with unix stripped out.
package bufio
import (
"os"
"syscall"
"github.com/sagernet/sing/common/buf"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
)
var _ N.ReadWaiter = (*syscallReadWaiter)(nil)
type syscallReadWaiter struct {
rawConn syscall.RawConn
buffer *buf.Buffer
options N.ReadWaitOptions
}
func createSyscallReadWaiter(any) (*syscallReadWaiter, bool) { return nil, false }
func (w *syscallReadWaiter) InitializeReadWaiter(options N.ReadWaitOptions) (needCopy bool) {
w.options = options
return false
}
func (w *syscallReadWaiter) WaitReadBuffer() (buffer *buf.Buffer, err error) {
return nil, os.ErrInvalid
}
var _ N.VectorisedReadWaiter = (*vectorisedSyscallReadWaiter)(nil)
type vectorisedSyscallReadWaiter struct {
rawConn syscall.RawConn
buffers []*buf.Buffer
options N.ReadWaitOptions
}
func createVectorisedSyscallReadWaiter(any) (*vectorisedSyscallReadWaiter, bool) { return nil, false }
func (w *vectorisedSyscallReadWaiter) InitializeReadWaiter(options N.ReadWaitOptions) (needCopy bool) {
w.options = options
return false
}
func (w *vectorisedSyscallReadWaiter) WaitReadBuffers() (buffers []*buf.Buffer, err error) {
return nil, os.ErrInvalid
}
var _ N.PacketReadWaiter = (*syscallPacketReadWaiter)(nil)
type syscallPacketReadWaiter struct {
rawConn syscall.RawConn
buffer *buf.Buffer
options N.ReadWaitOptions
}
func createSyscallPacketReadWaiter(any) (*syscallPacketReadWaiter, bool) { return nil, false }
func (w *syscallPacketReadWaiter) InitializeReadWaiter(options N.ReadWaitOptions) (needCopy bool) {
w.options = options
return false
}
func (w *syscallPacketReadWaiter) WaitReadPacket() (buffer *buf.Buffer, destination M.Socksaddr, err error) {
return nil, M.Socksaddr{}, os.ErrInvalid
}
+32
View File
@@ -0,0 +1,32 @@
// Substituted in via `go build -overlay` for the js/wasm build ONLY.
//
// Upstream common/bufio/vectorised_unix.go (build tag `!windows`) implements the
// syscall-based vectorised writers using golang.org/x/sys/unix (Iovec, writev,
// sendmsg). Those types are referenced by the cross-platform vectorised.go, so
// the symbols must exist for the package to compile, but the code never runs
// under wasm (a browser has no raw socket fds). This provides the same
// identifiers with unix stripped out; the write paths return os.ErrInvalid.
package bufio
import (
"os"
"github.com/sagernet/sing/common/buf"
M "github.com/sagernet/sing/common/metadata"
)
// syscallVectorisedWriterFields is embedded into SyscallVectorisedWriter and
// SyscallVectorisedPacketWriter (declared in vectorised.go). No fields are
// needed for the wasm stub.
type syscallVectorisedWriterFields struct{}
func (w *SyscallVectorisedWriter) WriteVectorised(buffers []*buf.Buffer) error {
buf.ReleaseMulti(buffers)
return os.ErrInvalid
}
func (w *SyscallVectorisedPacketWriter) WriteVectorisedPacket(buffers []*buf.Buffer, destination M.Socksaddr) error {
buf.ReleaseMulti(buffers)
return os.ErrInvalid
}
+21
View File
@@ -0,0 +1,21 @@
#!/usr/bin/env bash
# Generates the -overlay JSON that swaps the three sing source files that pull in
# golang.org/x/sys/unix (unavailable for GOARCH=wasm) for wasm-safe stubs in
# ./_stubs. Only the js/wasm build uses this overlay; native builds and tests are
# unaffected. The stubs live in an underscore-prefixed directory so the go tool
# ignores them during ./... builds (they carry mixed package names). See
# _stubs/*.go for the rationale of each substitution.
set -euo pipefail
here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
sing="$(cd "$here/.." && go list -m -f '{{.Dir}}' github.com/sagernet/sing)"
cat <<JSON
{
"Replace": {
"$sing/common/buf/buffer_unix.go": "$here/_stubs/buf_buffer_unix.go",
"$sing/common/bufio/vectorised_unix.go": "$here/_stubs/bufio_vectorised_unix.go",
"$sing/common/bufio/copy_direct_posix.go": "$here/_stubs/bufio_copy_direct_posix.go"
}
}
JSON