2020-09-26 11:31:24 -04:00
|
|
|
package trojan
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-01-02 10:16:23 -05:00
|
|
|
core "github.com/v2fly/v2ray-core/v5"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/buf"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/net"
|
2022-08-20 01:04:16 -04:00
|
|
|
"github.com/v2fly/v2ray-core/v5/common/net/packetaddr"
|
2022-01-02 10:16:23 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v5/common/protocol"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/retry"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/session"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/signal"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/task"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/features/policy"
|
2022-01-03 20:38:35 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v5/proxy"
|
2022-01-02 10:16:23 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v5/transport"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/transport/internet"
|
2022-08-20 01:04:16 -04:00
|
|
|
"github.com/v2fly/v2ray-core/v5/transport/internet/udp"
|
2020-09-26 11:31:24 -04:00
|
|
|
)
|
|
|
|
|
2021-12-26 23:33:08 -05:00
|
|
|
// Client is an inbound handler for trojan protocol
|
2020-09-26 11:31:24 -04:00
|
|
|
type Client struct {
|
|
|
|
serverPicker protocol.ServerPicker
|
|
|
|
policyManager policy.Manager
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient create a new trojan client.
|
|
|
|
func NewClient(ctx context.Context, config *ClientConfig) (*Client, error) {
|
|
|
|
serverList := protocol.NewServerList()
|
|
|
|
for _, rec := range config.Server {
|
|
|
|
s, err := protocol.NewServerSpecFromPB(rec)
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("failed to parse server spec").Base(err)
|
|
|
|
}
|
|
|
|
serverList.AddServer(s)
|
|
|
|
}
|
|
|
|
if serverList.Size() == 0 {
|
|
|
|
return nil, newError("0 server")
|
|
|
|
}
|
|
|
|
|
|
|
|
v := core.MustFromContext(ctx)
|
|
|
|
client := &Client{
|
|
|
|
serverPicker: protocol.NewRoundRobinServerPicker(serverList),
|
|
|
|
policyManager: v.GetFeature(policy.ManagerType()).(policy.Manager),
|
|
|
|
}
|
|
|
|
return client, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process implements OutboundHandler.Process().
|
2020-10-11 07:22:46 -04:00
|
|
|
func (c *Client) Process(ctx context.Context, link *transport.Link, dialer internet.Dialer) error {
|
2020-09-26 11:31:24 -04:00
|
|
|
outbound := session.OutboundFromContext(ctx)
|
|
|
|
if outbound == nil || !outbound.Target.IsValid() {
|
|
|
|
return newError("target not specified")
|
|
|
|
}
|
|
|
|
destination := outbound.Target
|
|
|
|
network := destination.Network
|
|
|
|
|
|
|
|
var server *protocol.ServerSpec
|
|
|
|
var conn internet.Connection
|
|
|
|
|
2020-10-11 07:22:46 -04:00
|
|
|
err := retry.ExponentialBackoff(5, 100).On(func() error {
|
2020-09-26 11:31:24 -04:00
|
|
|
server = c.serverPicker.PickServer()
|
|
|
|
rawConn, err := dialer.Dial(ctx, server.Destination())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
conn = rawConn
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return newError("failed to find an available destination").AtWarning().Base(err)
|
|
|
|
}
|
2022-04-06 01:24:43 -04:00
|
|
|
newError("tunneling request to ", destination, " via ", server.Destination().NetAddr()).WriteToLog(session.ExportIDToError(ctx))
|
2020-09-26 11:31:24 -04:00
|
|
|
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
user := server.PickUser()
|
|
|
|
account, ok := user.Account.(*MemoryAccount)
|
|
|
|
if !ok {
|
|
|
|
return newError("user account is not valid")
|
|
|
|
}
|
|
|
|
|
|
|
|
sessionPolicy := c.policyManager.ForLevel(user.Level)
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
timer := signal.CancelAfterInactivity(ctx, cancel, sessionPolicy.Timeouts.ConnectionIdle)
|
|
|
|
|
2022-08-20 01:04:16 -04:00
|
|
|
if packetConn, err := packetaddr.ToPacketAddrConn(link, destination); err == nil {
|
|
|
|
postRequest := func() error {
|
|
|
|
defer timer.SetTimeout(sessionPolicy.Timeouts.DownlinkOnly)
|
|
|
|
|
|
|
|
var buffer [2048]byte
|
|
|
|
_, addr, err := packetConn.ReadFrom(buffer[:])
|
|
|
|
if err != nil {
|
|
|
|
return newError("failed to read a packet").Base(err)
|
|
|
|
}
|
|
|
|
dest := net.DestinationFromAddr(addr)
|
|
|
|
|
|
|
|
bufferWriter := buf.NewBufferedWriter(buf.NewWriter(conn))
|
|
|
|
connWriter := &ConnWriter{Writer: bufferWriter, Target: dest, Account: account}
|
|
|
|
packetWriter := &PacketWriter{Writer: connWriter, Target: dest}
|
|
|
|
|
|
|
|
// write some request payload to buffer
|
|
|
|
if _, err := packetWriter.WriteTo(buffer[:], addr); err != nil {
|
|
|
|
return newError("failed to write a request payload").Base(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flush; bufferWriter.WriteMultiBuffer now is bufferWriter.writer.WriteMultiBuffer
|
|
|
|
if err = bufferWriter.SetBuffered(false); err != nil {
|
|
|
|
return newError("failed to flush payload").Base(err).AtWarning()
|
|
|
|
}
|
|
|
|
|
|
|
|
return udp.CopyPacketConn(packetWriter, packetConn, udp.UpdateActivity(timer))
|
|
|
|
}
|
|
|
|
|
|
|
|
getResponse := func() error {
|
|
|
|
defer timer.SetTimeout(sessionPolicy.Timeouts.UplinkOnly)
|
|
|
|
|
|
|
|
packetReader := &PacketReader{Reader: conn}
|
2022-12-21 04:44:04 -05:00
|
|
|
packetConnectionReader := &PacketConnectionReader{reader: packetReader}
|
2022-08-20 01:04:16 -04:00
|
|
|
|
2022-12-21 04:44:04 -05:00
|
|
|
return udp.CopyPacketConn(packetConn, packetConnectionReader, udp.UpdateActivity(timer))
|
2022-08-20 01:04:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
responseDoneAndCloseWriter := task.OnSuccess(getResponse, task.Close(link.Writer))
|
|
|
|
if err := task.Run(ctx, postRequest, responseDoneAndCloseWriter); err != nil {
|
|
|
|
return newError("connection ends").Base(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-26 11:31:24 -04:00
|
|
|
postRequest := func() error {
|
|
|
|
defer timer.SetTimeout(sessionPolicy.Timeouts.DownlinkOnly)
|
|
|
|
|
|
|
|
var bodyWriter buf.Writer
|
|
|
|
bufferWriter := buf.NewBufferedWriter(buf.NewWriter(conn))
|
2020-11-20 02:53:10 -05:00
|
|
|
connWriter := &ConnWriter{Writer: bufferWriter, Target: destination, Account: account}
|
2020-09-26 11:31:24 -04:00
|
|
|
|
|
|
|
if destination.Network == net.Network_UDP {
|
|
|
|
bodyWriter = &PacketWriter{Writer: connWriter, Target: destination}
|
|
|
|
} else {
|
|
|
|
bodyWriter = connWriter
|
|
|
|
}
|
|
|
|
|
|
|
|
// write some request payload to buffer
|
2022-01-03 20:38:35 -05:00
|
|
|
if err = buf.CopyOnceTimeout(link.Reader, bodyWriter, proxy.FirstPayloadTimeout); err != nil && err != buf.ErrNotTimeoutReader && err != buf.ErrReadTimeout {
|
2022-08-20 01:04:16 -04:00
|
|
|
return newError("failed to write a request payload").Base(err).AtWarning()
|
2020-09-26 11:31:24 -04:00
|
|
|
}
|
|
|
|
|
2021-12-26 23:33:08 -05:00
|
|
|
// Flush; bufferWriter.WriteMultiBuffer now is bufferWriter.writer.WriteMultiBuffer
|
2020-09-26 11:31:24 -04:00
|
|
|
if err = bufferWriter.SetBuffered(false); err != nil {
|
|
|
|
return newError("failed to flush payload").Base(err).AtWarning()
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = buf.Copy(link.Reader, bodyWriter, buf.UpdateActivity(timer)); err != nil {
|
|
|
|
return newError("failed to transfer request payload").Base(err).AtInfo()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
getResponse := func() error {
|
|
|
|
defer timer.SetTimeout(sessionPolicy.Timeouts.UplinkOnly)
|
|
|
|
|
|
|
|
var reader buf.Reader
|
|
|
|
if network == net.Network_UDP {
|
|
|
|
reader = &PacketReader{
|
|
|
|
Reader: conn,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
reader = buf.NewReader(conn)
|
|
|
|
}
|
|
|
|
return buf.Copy(reader, link.Writer, buf.UpdateActivity(timer))
|
|
|
|
}
|
|
|
|
|
2021-05-19 17:28:52 -04:00
|
|
|
responseDoneAndCloseWriter := task.OnSuccess(getResponse, task.Close(link.Writer))
|
2020-09-26 11:31:24 -04:00
|
|
|
if err := task.Run(ctx, postRequest, responseDoneAndCloseWriter); err != nil {
|
|
|
|
return newError("connection ends").Base(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2020-10-11 07:22:46 -04:00
|
|
|
common.Must(common.RegisterConfig((*ClientConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
|
2020-09-26 11:31:24 -04:00
|
|
|
return NewClient(ctx, config.(*ClientConfig))
|
|
|
|
}))
|
|
|
|
}
|