Skip to content

Commit 4cb2a12

Browse files
committed
Don't do raw/splice copy in case of MITM
1 parent 8a4217f commit 4cb2a12

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

proxy/freedom/freedom.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/xtls/xray-core/transport"
2929
"github.com/xtls/xray-core/transport/internet"
3030
"github.com/xtls/xray-core/transport/internet/stat"
31+
"github.com/xtls/xray-core/transport/internet/tls"
3132
)
3233

3334
var useSplice bool
@@ -225,9 +226,16 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer inte
225226
writeConn = inbound.Conn
226227
inTimer = inbound.Timer
227228
}
228-
return proxy.CopyRawConnIfExist(ctx, conn, writeConn, link.Writer, timer, inTimer)
229+
if !isTLSConn(conn) { // it would be tls conn in special use case of MITM, we need to let link handle traffic
230+
return proxy.CopyRawConnIfExist(ctx, conn, writeConn, link.Writer, timer, inTimer)
231+
}
232+
}
233+
var reader buf.Reader
234+
if destination.Network == net.Network_TCP {
235+
reader = buf.NewReader(conn)
236+
} else {
237+
reader = NewPacketReader(conn, UDPOverride)
229238
}
230-
reader := NewPacketReader(conn, UDPOverride)
231239
if err := buf.Copy(reader, output, buf.UpdateActivity(timer)); err != nil {
232240
return errors.New("failed to process response").Base(err)
233241
}
@@ -245,6 +253,19 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer inte
245253
return nil
246254
}
247255

256+
func isTLSConn(conn stat.Connection) bool {
257+
if conn != nil {
258+
statConn, ok := conn.(*stat.CounterConnection)
259+
if ok {
260+
conn = statConn.Connection
261+
}
262+
if _, ok := conn.(*tls.Conn); ok {
263+
return true
264+
}
265+
}
266+
return false
267+
}
268+
248269
func NewPacketReader(conn net.Conn, UDPOverride net.Destination) buf.Reader {
249270
iConn := conn
250271
statConn, ok := iConn.(*stat.CounterConnection)

0 commit comments

Comments
 (0)