2020-11-25 13:01:53 +02:00
|
|
|
package dokodemo
|
|
|
|
|
2020-12-04 03:36:16 +02:00
|
|
|
//go:generate go run github.com/xtls/xray-core/common/errors/errorgen
|
2020-11-25 13:01:53 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
2020-12-04 03:36:16 +02:00
|
|
|
"github.com/xtls/xray-core/common"
|
|
|
|
"github.com/xtls/xray-core/common/buf"
|
|
|
|
"github.com/xtls/xray-core/common/log"
|
|
|
|
"github.com/xtls/xray-core/common/net"
|
|
|
|
"github.com/xtls/xray-core/common/protocol"
|
|
|
|
"github.com/xtls/xray-core/common/session"
|
|
|
|
"github.com/xtls/xray-core/common/signal"
|
|
|
|
"github.com/xtls/xray-core/common/task"
|
|
|
|
"github.com/xtls/xray-core/core"
|
|
|
|
"github.com/xtls/xray-core/features/policy"
|
|
|
|
"github.com/xtls/xray-core/features/routing"
|
|
|
|
"github.com/xtls/xray-core/transport/internet"
|
2020-11-25 13:01:53 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
|
|
|
|
d := new(DokodemoDoor)
|
|
|
|
err := core.RequireFeatures(ctx, func(pm policy.Manager) error {
|
|
|
|
return d.Init(config.(*Config), pm, session.SockoptFromContext(ctx))
|
|
|
|
})
|
|
|
|
return d, err
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
type DokodemoDoor struct {
|
|
|
|
policyManager policy.Manager
|
|
|
|
config *Config
|
|
|
|
address net.Address
|
|
|
|
port net.Port
|
|
|
|
sockopt *session.Sockopt
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init initializes the DokodemoDoor instance with necessary parameters.
|
|
|
|
func (d *DokodemoDoor) Init(config *Config, pm policy.Manager, sockopt *session.Sockopt) error {
|
|
|
|
if (config.NetworkList == nil || len(config.NetworkList.Network) == 0) && len(config.Networks) == 0 {
|
|
|
|
return newError("no network specified")
|
|
|
|
}
|
|
|
|
d.config = config
|
|
|
|
d.address = config.GetPredefinedAddress()
|
|
|
|
d.port = net.Port(config.Port)
|
|
|
|
d.policyManager = pm
|
|
|
|
d.sockopt = sockopt
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Network implements proxy.Inbound.
|
|
|
|
func (d *DokodemoDoor) Network() []net.Network {
|
|
|
|
if len(d.config.Networks) > 0 {
|
|
|
|
return d.config.Networks
|
|
|
|
}
|
|
|
|
|
|
|
|
return d.config.NetworkList.Network
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DokodemoDoor) policy() policy.Session {
|
|
|
|
config := d.config
|
|
|
|
p := d.policyManager.ForLevel(config.UserLevel)
|
|
|
|
if config.Timeout > 0 && config.UserLevel == 0 {
|
|
|
|
p.Timeouts.ConnectionIdle = time.Duration(config.Timeout) * time.Second
|
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
type hasHandshakeAddress interface {
|
|
|
|
HandshakeAddress() net.Address
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process implements proxy.Inbound.
|
|
|
|
func (d *DokodemoDoor) Process(ctx context.Context, network net.Network, conn internet.Connection, dispatcher routing.Dispatcher) error {
|
|
|
|
newError("processing connection from: ", conn.RemoteAddr()).AtDebug().WriteToLog(session.ExportIDToError(ctx))
|
|
|
|
dest := net.Destination{
|
|
|
|
Network: network,
|
|
|
|
Address: d.address,
|
|
|
|
Port: d.port,
|
|
|
|
}
|
|
|
|
|
|
|
|
destinationOverridden := false
|
|
|
|
if d.config.FollowRedirect {
|
|
|
|
if outbound := session.OutboundFromContext(ctx); outbound != nil && outbound.Target.IsValid() {
|
|
|
|
dest = outbound.Target
|
|
|
|
destinationOverridden = true
|
|
|
|
} else if handshake, ok := conn.(hasHandshakeAddress); ok {
|
|
|
|
addr := handshake.HandshakeAddress()
|
|
|
|
if addr != nil {
|
|
|
|
dest.Address = addr
|
|
|
|
destinationOverridden = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !dest.IsValid() || dest.Address == nil {
|
|
|
|
return newError("unable to get destination")
|
|
|
|
}
|
|
|
|
|
2020-12-05 13:58:10 +02:00
|
|
|
inbound := session.InboundFromContext(ctx)
|
|
|
|
if inbound != nil {
|
2020-11-25 13:01:53 +02:00
|
|
|
inbound.User = &protocol.MemoryUser{
|
|
|
|
Level: d.config.UserLevel,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx = log.ContextWithAccessMessage(ctx, &log.AccessMessage{
|
|
|
|
From: conn.RemoteAddr(),
|
|
|
|
To: dest,
|
|
|
|
Status: log.AccessAccepted,
|
|
|
|
Reason: "",
|
|
|
|
})
|
|
|
|
newError("received request for ", conn.RemoteAddr()).WriteToLog(session.ExportIDToError(ctx))
|
|
|
|
|
|
|
|
plcy := d.policy()
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
timer := signal.CancelAfterInactivity(ctx, cancel, plcy.Timeouts.ConnectionIdle)
|
|
|
|
|
2020-12-05 13:58:10 +02:00
|
|
|
if inbound != nil {
|
|
|
|
inbound.Timer = timer
|
|
|
|
}
|
|
|
|
|
2020-11-25 13:01:53 +02:00
|
|
|
ctx = policy.ContextWithBufferPolicy(ctx, plcy.Buffer)
|
|
|
|
link, err := dispatcher.Dispatch(ctx, dest)
|
|
|
|
if err != nil {
|
|
|
|
return newError("failed to dispatch request").Base(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
requestCount := int32(1)
|
|
|
|
requestDone := func() error {
|
|
|
|
defer func() {
|
|
|
|
if atomic.AddInt32(&requestCount, -1) == 0 {
|
|
|
|
timer.SetTimeout(plcy.Timeouts.DownlinkOnly)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
var reader buf.Reader
|
|
|
|
if dest.Network == net.Network_UDP {
|
|
|
|
reader = buf.NewPacketReader(conn)
|
|
|
|
} else {
|
|
|
|
reader = buf.NewReader(conn)
|
|
|
|
}
|
|
|
|
if err := buf.Copy(reader, link.Writer, buf.UpdateActivity(timer)); err != nil {
|
|
|
|
return newError("failed to transport request").Base(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
tproxyRequest := func() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var writer buf.Writer
|
|
|
|
if network == net.Network_TCP {
|
|
|
|
writer = buf.NewWriter(conn)
|
|
|
|
} else {
|
|
|
|
// if we are in TPROXY mode, use linux's udp forging functionality
|
|
|
|
if !destinationOverridden {
|
|
|
|
writer = &buf.SequentialWriter{Writer: conn}
|
|
|
|
} else {
|
2021-01-07 14:21:27 +02:00
|
|
|
back := conn.RemoteAddr().(*net.UDPAddr)
|
|
|
|
if !dest.Address.Family().IsIP() {
|
|
|
|
if len(back.IP) == 4 {
|
|
|
|
dest.Address = net.AnyIP
|
|
|
|
} else {
|
|
|
|
dest.Address = net.AnyIPv6
|
2020-12-31 17:57:15 +02:00
|
|
|
}
|
2020-11-25 13:01:53 +02:00
|
|
|
}
|
2021-01-07 14:21:27 +02:00
|
|
|
addr := &net.UDPAddr{
|
|
|
|
IP: dest.Address.IP(),
|
|
|
|
Port: int(dest.Port),
|
|
|
|
}
|
|
|
|
var mark int
|
2020-11-25 13:01:53 +02:00
|
|
|
if d.sockopt != nil {
|
2020-12-31 17:57:15 +02:00
|
|
|
mark = int(d.sockopt.Mark)
|
2020-11-25 13:01:53 +02:00
|
|
|
}
|
2020-12-31 17:57:15 +02:00
|
|
|
pConn, err := FakeUDP(addr, mark)
|
2020-11-25 13:01:53 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-07 14:21:27 +02:00
|
|
|
writer = NewPacketWriter(pConn, &dest, mark, back)
|
2020-12-29 13:50:17 +02:00
|
|
|
defer writer.(*PacketWriter).Close()
|
|
|
|
/*
|
2020-12-31 17:57:15 +02:00
|
|
|
sockopt := &internet.SocketConfig{
|
|
|
|
Tproxy: internet.SocketConfig_TProxy,
|
|
|
|
}
|
|
|
|
if dest.Address.Family().IsIP() {
|
|
|
|
sockopt.BindAddress = dest.Address.IP()
|
|
|
|
sockopt.BindPort = uint32(dest.Port)
|
|
|
|
}
|
|
|
|
if d.sockopt != nil {
|
|
|
|
sockopt.Mark = d.sockopt.Mark
|
|
|
|
}
|
|
|
|
tConn, err := internet.DialSystem(ctx, net.DestinationFromAddr(conn.RemoteAddr()), sockopt)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-29 13:50:17 +02:00
|
|
|
defer tConn.Close()
|
2020-12-31 17:57:15 +02:00
|
|
|
|
2020-12-29 13:50:17 +02:00
|
|
|
writer = &buf.SequentialWriter{Writer: tConn}
|
|
|
|
tReader := buf.NewPacketReader(tConn)
|
|
|
|
requestCount++
|
|
|
|
tproxyRequest = func() error {
|
|
|
|
defer func() {
|
|
|
|
if atomic.AddInt32(&requestCount, -1) == 0 {
|
|
|
|
timer.SetTimeout(plcy.Timeouts.DownlinkOnly)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
if err := buf.Copy(tReader, link.Writer, buf.UpdateActivity(timer)); err != nil {
|
|
|
|
return newError("failed to transport request (TPROXY conn)").Base(err)
|
2020-11-25 13:01:53 +02:00
|
|
|
}
|
2020-12-29 13:50:17 +02:00
|
|
|
return nil
|
2020-11-25 13:01:53 +02:00
|
|
|
}
|
2020-12-29 13:50:17 +02:00
|
|
|
*/
|
2020-11-25 13:01:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
responseDone := func() error {
|
|
|
|
defer timer.SetTimeout(plcy.Timeouts.UplinkOnly)
|
|
|
|
|
|
|
|
if err := buf.Copy(link.Reader, writer, buf.UpdateActivity(timer)); err != nil {
|
|
|
|
return newError("failed to transport response").Base(err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := task.Run(ctx, task.OnSuccess(func() error {
|
|
|
|
return task.Run(ctx, requestDone, tproxyRequest)
|
|
|
|
}, task.Close(link.Writer)), responseDone); err != nil {
|
|
|
|
common.Interrupt(link.Reader)
|
|
|
|
common.Interrupt(link.Writer)
|
|
|
|
return newError("connection ends").Base(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-12-29 13:50:17 +02:00
|
|
|
|
2021-01-07 14:21:27 +02:00
|
|
|
func NewPacketWriter(conn net.PacketConn, d *net.Destination, mark int, back *net.UDPAddr) buf.Writer {
|
2020-12-29 13:50:17 +02:00
|
|
|
writer := &PacketWriter{
|
2020-12-31 17:57:15 +02:00
|
|
|
conn: conn,
|
|
|
|
conns: make(map[net.Destination]net.PacketConn),
|
|
|
|
mark: mark,
|
2021-01-07 14:21:27 +02:00
|
|
|
back: back,
|
2020-12-29 13:50:17 +02:00
|
|
|
}
|
|
|
|
writer.conns[*d] = conn
|
|
|
|
return writer
|
|
|
|
}
|
|
|
|
|
|
|
|
type PacketWriter struct {
|
2020-12-31 17:57:15 +02:00
|
|
|
conn net.PacketConn
|
|
|
|
conns map[net.Destination]net.PacketConn
|
|
|
|
mark int
|
|
|
|
back *net.UDPAddr
|
2020-12-29 13:50:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *PacketWriter) WriteMultiBuffer(mb buf.MultiBuffer) error {
|
|
|
|
for {
|
|
|
|
mb2, b := buf.SplitFirst(mb)
|
|
|
|
mb = mb2
|
|
|
|
if b == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
if b.UDP != nil && b.UDP.Address.Family().IsIP() {
|
|
|
|
conn := w.conns[*b.UDP]
|
|
|
|
if conn == nil {
|
2020-12-31 17:57:15 +02:00
|
|
|
conn, err = FakeUDP(
|
|
|
|
&net.UDPAddr{
|
|
|
|
IP: b.UDP.Address.IP(),
|
|
|
|
Port: int(b.UDP.Port),
|
|
|
|
},
|
|
|
|
w.mark,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2021-01-21 01:58:59 +02:00
|
|
|
newError(err).WriteToLog()
|
2020-12-29 13:50:17 +02:00
|
|
|
b.Release()
|
2021-01-21 01:58:59 +02:00
|
|
|
continue
|
2020-12-29 13:50:17 +02:00
|
|
|
}
|
|
|
|
w.conns[*b.UDP] = conn
|
|
|
|
}
|
2020-12-31 17:57:15 +02:00
|
|
|
_, err = conn.WriteTo(b.Bytes(), w.back)
|
|
|
|
if err != nil {
|
|
|
|
newError(err).WriteToLog()
|
2021-01-21 01:58:59 +02:00
|
|
|
w.conns[*b.UDP] = nil
|
|
|
|
conn.Close()
|
2020-12-31 17:57:15 +02:00
|
|
|
}
|
|
|
|
b.Release()
|
2020-12-29 13:50:17 +02:00
|
|
|
} else {
|
2020-12-31 17:57:15 +02:00
|
|
|
_, err = w.conn.WriteTo(b.Bytes(), w.back)
|
|
|
|
b.Release()
|
|
|
|
if err != nil {
|
|
|
|
buf.ReleaseMulti(mb)
|
|
|
|
return err
|
|
|
|
}
|
2020-12-29 13:50:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *PacketWriter) Close() error {
|
|
|
|
for _, conn := range w.conns {
|
|
|
|
if conn != nil {
|
|
|
|
conn.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|