Skip to content

Commit 1f3543f

Browse files
committed
2025-03-03 11:18:22
1 parent 0aaed67 commit 1f3543f

File tree

1 file changed

+23
-24
lines changed

1 file changed

+23
-24
lines changed

daze.go

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"net/url"
2323
"os"
2424
"path/filepath"
25-
"slices"
2625
"strconv"
2726
"strings"
2827
"sync"
@@ -409,6 +408,28 @@ func (l *Locale) ServeSocks5TCP(ctx *Context, cli io.ReadWriteCloser, dst string
409408
return err
410409
}
411410

411+
// ServeSocks5UDPRead handles the reading and forwarding of udp data.
412+
func (l *Locale) ServeSocks5UDPRead(srv io.Reader, bnd *net.UDPConn, app *net.UDPAddr, pre []byte) error {
413+
var (
414+
buf = make([]byte, 2048)
415+
err error
416+
m = len(pre)
417+
n int
418+
)
419+
copy(buf[:m], pre)
420+
for {
421+
n, err = srv.Read(buf[m:])
422+
if err != nil {
423+
break
424+
}
425+
_, err = bnd.WriteToUDP(buf[:m+n], app)
426+
if err != nil {
427+
break
428+
}
429+
}
430+
return err
431+
}
432+
412433
// ServeSocks5UDP serves socks5 UDP protocol.
413434
func (l *Locale) ServeSocks5UDP(ctx *Context, cli io.ReadWriteCloser) error {
414435
var (
@@ -501,10 +522,7 @@ func (l *Locale) ServeSocks5UDP(ctx *Context, cli io.ReadWriteCloser) error {
501522
continue
502523
}
503524
cpl[dst] = srv
504-
retHead := slices.Clone(appHead)
505-
go ReadCall(srv, func(data []byte) error {
506-
return doa.Err(bnd.WriteToUDP(append(retHead, data...), appAddr))
507-
})
525+
go l.ServeSocks5UDPRead(srv, bnd, appAddr, appHead)
508526
}
509527
_, err = srv.Write(buf[appHeadSize:appSize])
510528
if err != nil {
@@ -1030,25 +1048,6 @@ func (r *RandomReader) Read(p []byte) (int, error) {
10301048
return len(p), nil
10311049
}
10321050

1033-
// ReadCall reads data from the given io.Reader and passes it to the provided call function.
1034-
func ReadCall(conn io.Reader, call func([]byte) error) error {
1035-
var (
1036-
buf = make([]byte, 2048)
1037-
err error
1038-
n int
1039-
)
1040-
for {
1041-
n, err = conn.Read(buf)
1042-
if err != nil {
1043-
break
1044-
}
1045-
if err = call(buf[:n]); err != nil {
1046-
break
1047-
}
1048-
}
1049-
return err
1050-
}
1051-
10521051
// Salt converts the stupid password passed in by the user to 32-sized byte array.
10531052
func Salt(s string) []byte {
10541053
h := sha256.Sum256([]byte(s))

0 commit comments

Comments
 (0)