mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-04-16 12:35:54 +00:00
1. **Fixed XPadding Placement Dropdown**: - Added the missing `cookie` and `query` options to `xPaddingPlacement` (`stream_xhttp.html`). - *Why:* Previously, users wanting `cookie` obfuscation were forced to use the `header` placement string. This caused Xray-core to blindly intercept the entire monolithic HTTP Cookie header, failing internal padding-length validations and causing the inbound to silently drop the connection. 2. **Fixed Uplink Data Placement Validation**: - Replaced the unsupported `query` option with `cookie` in `uplinkDataPlacement`. - *Why:* Xray-core's `transport_internet.go` explicitly forbids `query` as an uplink placement option. Selecting it from the UI previously sent a payload that would cause Xray-core to instantly throw an `unsupported uplink data placement: query` panic. Adding `cookie` perfectly aligns the UI with Xray-core restrictions. ### Related Issues - Resolves #3992
109 lines
2.4 KiB
Go
109 lines
2.4 KiB
Go
package tcp
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
|
|
"github.com/xtls/xray-core/common/buf"
|
|
"github.com/xtls/xray-core/common/net"
|
|
"github.com/xtls/xray-core/common/task"
|
|
"github.com/xtls/xray-core/transport/internet"
|
|
"github.com/xtls/xray-core/transport/pipe"
|
|
)
|
|
|
|
type Server struct {
|
|
Port net.Port
|
|
MsgProcessor func(msg []byte) []byte
|
|
ShouldClose bool
|
|
SendFirst []byte
|
|
Listen net.Address
|
|
listener net.Listener
|
|
}
|
|
|
|
func (server *Server) Start() (net.Destination, error) {
|
|
return server.StartContext(context.Background(), nil)
|
|
}
|
|
|
|
func (server *Server) StartContext(ctx context.Context, sockopt *internet.SocketConfig) (net.Destination, error) {
|
|
listenerAddr := server.Listen
|
|
if listenerAddr == nil {
|
|
listenerAddr = net.LocalHostIP
|
|
}
|
|
listener, err := internet.ListenSystem(ctx, &net.TCPAddr{
|
|
IP: listenerAddr.IP(),
|
|
Port: int(server.Port),
|
|
}, sockopt)
|
|
if err != nil {
|
|
return net.Destination{}, err
|
|
}
|
|
|
|
localAddr := listener.Addr().(*net.TCPAddr)
|
|
server.Port = net.Port(localAddr.Port)
|
|
server.listener = listener
|
|
go server.acceptConnections(listener.(*net.TCPListener))
|
|
|
|
return net.TCPDestination(net.IPAddress(localAddr.IP), net.Port(localAddr.Port)), nil
|
|
}
|
|
|
|
func (server *Server) acceptConnections(listener *net.TCPListener) {
|
|
for {
|
|
conn, err := listener.Accept()
|
|
if err != nil {
|
|
fmt.Printf("Failed accept TCP connection: %v\n", err)
|
|
return
|
|
}
|
|
|
|
go server.handleConnection(conn)
|
|
}
|
|
}
|
|
|
|
func (server *Server) handleConnection(conn net.Conn) {
|
|
if len(server.SendFirst) > 0 {
|
|
conn.Write(server.SendFirst)
|
|
}
|
|
|
|
pReader, pWriter := pipe.New(pipe.WithoutSizeLimit())
|
|
err := task.Run(context.Background(), func() error {
|
|
defer pWriter.Close()
|
|
|
|
for {
|
|
b := buf.New()
|
|
if _, err := b.ReadFrom(conn); err != nil {
|
|
if err == io.EOF {
|
|
return nil
|
|
}
|
|
return err
|
|
}
|
|
copy(b.Bytes(), server.MsgProcessor(b.Bytes()))
|
|
if err := pWriter.WriteMultiBuffer(buf.MultiBuffer{b}); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}, func() error {
|
|
defer pReader.Interrupt()
|
|
|
|
w := buf.NewWriter(conn)
|
|
for {
|
|
mb, err := pReader.ReadMultiBuffer()
|
|
if err != nil {
|
|
if err == io.EOF {
|
|
return nil
|
|
}
|
|
return err
|
|
}
|
|
if err := w.WriteMultiBuffer(mb); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
})
|
|
if err != nil {
|
|
fmt.Println("failed to transfer data: ", err.Error())
|
|
}
|
|
|
|
conn.Close()
|
|
}
|
|
|
|
func (server *Server) Close() error {
|
|
return server.listener.Close()
|
|
}
|