Skip to content

Commit bc34e5a

Browse files
rootqwencoder
andcommitted
Fix build issues and restore UoT functionality\n\n- Resolved merge conflicts in forwardproxy.go\n- Synced code with upstream naive branch where possible\n- Fixed UoT (UDP over TCP) functionality to be compatible with the latest version of github.com/sagernet/sing/common/uot\n - Replaced uot.UOTMagicAddress with uot.MagicAddress and uot.LegacyMagicAddress\n - Updated uot.NewServerConn call to match the new API\n- Added github.com/sagernet/sing/common/uot as a dependency
Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
1 parent e40436a commit bc34e5a

3 files changed

Lines changed: 11 additions & 52 deletions

File tree

forwardproxy.go

Lines changed: 4 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ import (
2626
"errors"
2727
"fmt"
2828
"io"
29-
<<<<<<< HEAD
30-
"io/ioutil"
31-
=======
32-
>>>>>>> upstream/naive
3329
"math/rand"
3430
"net"
3531
"net/http"
@@ -306,21 +302,11 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyht
306302
}
307303
}
308304

309-
<<<<<<< HEAD
310-
// HTTP CONNECT Fast Open. We merely close the connection if Open fails.
311-
wFlusher, ok := w.(http.Flusher)
312-
if !ok {
313-
return caddyhttp.Error(http.StatusInternalServerError,
314-
fmt.Errorf("ResponseWriter doesn't implement http.Flusher"))
315-
}
316-
// Creates a padding of [30, 30+32)
317-
=======
318305
// HTTP CONNECT Fast Open: Directly responds with a 200 OK
319306
// before attempting to connect to origin to reduce response latency.
320307
// We merely close the connection if Open fails.
321308

322309
// Creates a padding header with length in [30, 30+32)
323-
>>>>>>> upstream/naive
324310
paddingLen := rand.Intn(32) + 30
325311
padding := make([]byte, paddingLen)
326312
bits := rand.Uint64()
@@ -333,18 +319,13 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyht
333319
padding[i] = '~'
334320
}
335321
w.Header().Set("Padding", string(padding))
336-
<<<<<<< HEAD
337-
w.WriteHeader(http.StatusOK)
338-
wFlusher.Flush()
339-
=======
340322

341323
w.WriteHeader(http.StatusOK)
342324
err := http.NewResponseController(w).Flush()
343325
if err != nil {
344326
return caddyhttp.Error(http.StatusInternalServerError,
345327
fmt.Errorf("ResponseWriter flush error: %v", err))
346328
}
347-
>>>>>>> upstream/naive
348329

349330
hostPort := r.URL.Host
350331
if hostPort == "" {
@@ -534,13 +515,13 @@ func (h Handler) dialContextCheckACL(ctx context.Context, network, hostPort stri
534515
return nil, caddyhttp.Error(http.StatusBadRequest, err)
535516
}
536517

537-
if host == uot.UOTMagicAddress {
518+
if host == uot.MagicAddress || host == uot.LegacyMagicAddress {
538519
udpConn, err := net.ListenUDP("udp", nil)
539520
if err != nil {
540521
return nil, err
541522
}
542523

543-
return uot.NewServerConn(udpConn), nil
524+
return uot.NewServerConn(udpConn, uot.Version), nil
544525
}
545526

546527
if h.upstream != nil {
@@ -697,12 +678,8 @@ func dualStream(target net.Conn, clientReader io.ReadCloser, clientWriter io.Wri
697678
buf := *bufPtr
698679
buf = buf[0:cap(buf)]
699680
_, _err := flushingIoCopy(w, r, buf, paddingType)
700-
<<<<<<< HEAD
701-
bufferPool.Put(buf)
702-
=======
703681
bufferPool.Put(bufPtr)
704682

705-
>>>>>>> upstream/naive
706683
if cw, ok := w.(closeWriter); ok {
707684
_ = cw.CloseWrite()
708685
}
@@ -711,16 +688,9 @@ func dualStream(target net.Conn, clientReader io.ReadCloser, clientWriter io.Wri
711688
if padding {
712689
go stream(target, clientReader, RemovePadding)
713690
return stream(clientWriter, target, AddPadding)
714-
<<<<<<< HEAD
715-
} else {
716-
go stream(target, clientReader, NoPadding)
717-
return stream(clientWriter, target, NoPadding)
718-
}
719-
=======
720691
}
721-
go stream(target, clientReader, NoPadding) //nolint: errcheck
722-
return stream(clientWriter, target, NoPadding)
723-
>>>>>>> upstream/naive
692+
go stream(target, clientReader, RemovePadding) //nolint: errcheck
693+
return stream(clientWriter, target, AddPadding)
724694
}
725695

726696
type closeWriter interface {
@@ -731,15 +701,11 @@ type closeWriter interface {
731701
// If dst does not implement http.Flusher(e.g. net.TCPConn), it will do a simple io.CopyBuffer().
732702
// Reasoning: http2ResponseWriter will not flush on its own, so we have to do it manually.
733703
func flushingIoCopy(dst io.Writer, src io.Reader, buf []byte, paddingType int) (written int64, err error) {
734-
<<<<<<< HEAD
735-
flusher, hasFlusher := dst.(http.Flusher)
736-
=======
737704
rw, ok := dst.(http.ResponseWriter)
738705
var rc *http.ResponseController
739706
if ok {
740707
rc = http.NewResponseController(rw)
741708
}
742-
>>>>>>> upstream/naive
743709
var numPadding int
744710
for {
745711
var nr int
@@ -775,12 +741,6 @@ func flushingIoCopy(dst io.Writer, src io.Reader, buf []byte, paddingType int) (
775741
}
776742
if nr > 0 {
777743
nw, ew := dst.Write(buf[0:nr])
778-
<<<<<<< HEAD
779-
if hasFlusher {
780-
flusher.Flush()
781-
}
782-
=======
783-
>>>>>>> upstream/naive
784744
if nw > 0 {
785745
written += int64(nw)
786746
}
@@ -862,12 +822,8 @@ function FindProxyForURL(url, host) {
862822

863823
var bufferPool = sync.Pool{
864824
New: func() interface{} {
865-
<<<<<<< HEAD
866-
return make([]byte, 0, 64*1024)
867-
=======
868825
buffer := make([]byte, 0, 64*1024)
869826
return &buffer
870-
>>>>>>> upstream/naive
871827
},
872828
}
873829

go.mod

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ toolchain go1.22.2
66

77
require (
88
github.com/caddyserver/caddy/v2 v2.8.4
9+
github.com/sagernet/sing v0.7.5
10+
go.uber.org/zap v1.27.0
911
golang.org/x/net v0.25.0
1012
)
1113

@@ -102,14 +104,13 @@ require (
102104
go.uber.org/automaxprocs v1.5.3 // indirect
103105
go.uber.org/mock v0.4.0 // indirect
104106
go.uber.org/multierr v1.11.0 // indirect
105-
go.uber.org/zap v1.27.0 // indirect
106107
go.uber.org/zap/exp v0.2.0 // indirect
107108
golang.org/x/crypto v0.23.0 // indirect
108109
golang.org/x/crypto/x509roots/fallback v0.0.0-20240507223354-67b13616a595 // indirect
109110
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 // indirect
110111
golang.org/x/mod v0.17.0 // indirect
111112
golang.org/x/sync v0.7.0 // indirect
112-
golang.org/x/sys v0.20.0 // indirect
113+
golang.org/x/sys v0.21.0 // indirect
113114
golang.org/x/term v0.20.0 // indirect
114115
golang.org/x/text v0.15.0 // indirect
115116
golang.org/x/time v0.5.0 // indirect

go.sum

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,8 @@ github.com/rs/zerolog v1.15.0/go.mod h1:xYTKnLHcpfU2225ny5qZjxnj9NvkumZYjJHlAThC
341341
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
342342
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
343343
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
344+
github.com/sagernet/sing v0.7.5 h1:gNMwZCLPqR+4e0g6dwi0sSsrvOmoMjpZgqxKsuJZatc=
345+
github.com/sagernet/sing v0.7.5/go.mod h1:ARkL0gM13/Iv5VCZmci/NuoOlePoIsW0m7BWfln/Hak=
344346
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
345347
github.com/schollz/jsonstore v1.1.0 h1:WZBDjgezFS34CHI+myb4s8GGpir3UMpy7vWoCeO0n6E=
346348
github.com/schollz/jsonstore v1.1.0/go.mod h1:15c6+9guw8vDRyozGjN3FoILt0wpruJk9Pi66vjaZfg=
@@ -535,8 +537,8 @@ golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
535537
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
536538
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
537539
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
538-
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
539-
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
540+
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
541+
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
540542
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
541543
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
542544
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=

0 commit comments

Comments
 (0)