2022-11-25 14:30:37 +00:00
|
|
|
package trojan
|
|
|
|
|
|
|
|
import (
|
2024-06-24 01:49:15 +00:00
|
|
|
std_bufio "bufio"
|
2022-11-25 14:30:37 +00:00
|
|
|
"context"
|
|
|
|
"net"
|
2024-11-23 14:34:02 +00:00
|
|
|
"os"
|
2022-11-25 14:30:37 +00:00
|
|
|
|
2024-06-24 01:49:15 +00:00
|
|
|
"github.com/sagernet/sing/common/buf"
|
|
|
|
"github.com/sagernet/sing/common/bufio"
|
2022-11-25 14:30:37 +00:00
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
2024-11-01 16:39:02 +00:00
|
|
|
"github.com/sagernet/sing/common/logger"
|
2022-11-25 14:30:37 +00:00
|
|
|
M "github.com/sagernet/sing/common/metadata"
|
2024-11-23 14:34:02 +00:00
|
|
|
N "github.com/sagernet/sing/common/network"
|
2022-11-25 14:30:37 +00:00
|
|
|
"github.com/sagernet/sing/common/task"
|
|
|
|
"github.com/sagernet/smux"
|
|
|
|
)
|
|
|
|
|
2024-11-23 14:34:02 +00:00
|
|
|
func HandleMuxConnection(ctx context.Context, conn net.Conn, source M.Socksaddr, handler Handler, logger logger.ContextLogger, onClose N.CloseHandlerFunc) error {
|
2022-11-25 14:30:37 +00:00
|
|
|
session, err := smux.Server(conn, smuxConfig())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var group task.Group
|
2023-11-13 05:58:20 +00:00
|
|
|
group.Append0(func(_ context.Context) error {
|
2022-11-25 14:30:37 +00:00
|
|
|
var stream net.Conn
|
|
|
|
for {
|
|
|
|
stream, err = session.AcceptStream()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-11-23 14:34:02 +00:00
|
|
|
go newMuxConnection(ctx, stream, source, handler, logger)
|
2022-11-25 14:30:37 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
group.Cleanup(func() {
|
|
|
|
session.Close()
|
2024-11-23 14:34:02 +00:00
|
|
|
if onClose != nil {
|
|
|
|
onClose(os.ErrClosed)
|
|
|
|
}
|
2022-11-25 14:30:37 +00:00
|
|
|
})
|
|
|
|
return group.Run(ctx)
|
|
|
|
}
|
|
|
|
|
2024-11-23 14:34:02 +00:00
|
|
|
func newMuxConnection(ctx context.Context, conn net.Conn, source M.Socksaddr, handler Handler, logger logger.ContextLogger) {
|
|
|
|
err := newMuxConnection0(ctx, conn, source, handler)
|
2022-11-25 14:30:37 +00:00
|
|
|
if err != nil {
|
2024-11-01 16:39:02 +00:00
|
|
|
logger.ErrorContext(ctx, E.Cause(err, "process trojan-go multiplex connection"))
|
2022-11-25 14:30:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-23 14:34:02 +00:00
|
|
|
func newMuxConnection0(ctx context.Context, conn net.Conn, source M.Socksaddr, handler Handler) error {
|
2024-06-24 01:49:15 +00:00
|
|
|
reader := std_bufio.NewReader(conn)
|
|
|
|
command, err := reader.ReadByte()
|
2022-11-25 14:30:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return E.Cause(err, "read command")
|
|
|
|
}
|
2024-11-23 14:34:02 +00:00
|
|
|
destination, err := M.SocksaddrSerializer.ReadAddrPort(reader)
|
2022-11-25 14:30:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return E.Cause(err, "read destination")
|
|
|
|
}
|
2024-06-24 01:49:15 +00:00
|
|
|
if reader.Buffered() > 0 {
|
|
|
|
buffer := buf.NewSize(reader.Buffered())
|
|
|
|
_, err = buffer.ReadFullFrom(reader, buffer.Len())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
conn = bufio.NewCachedConn(conn, buffer)
|
|
|
|
}
|
2022-11-25 14:30:37 +00:00
|
|
|
switch command {
|
|
|
|
case CommandTCP:
|
2024-11-23 14:34:02 +00:00
|
|
|
handler.NewConnectionEx(ctx, conn, source, destination, nil)
|
2022-11-25 14:30:37 +00:00
|
|
|
case CommandUDP:
|
2024-11-23 14:34:02 +00:00
|
|
|
handler.NewPacketConnectionEx(ctx, &PacketConn{Conn: conn}, source, destination, nil)
|
2022-11-25 14:30:37 +00:00
|
|
|
default:
|
|
|
|
return E.New("unknown command ", command)
|
|
|
|
}
|
2024-11-23 14:34:02 +00:00
|
|
|
return nil
|
2022-11-25 14:30:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func smuxConfig() *smux.Config {
|
|
|
|
config := smux.DefaultConfig()
|
|
|
|
config.KeepAliveDisabled = true
|
|
|
|
return config
|
|
|
|
}
|