v2fly/proxy/dokodemo/dokodemo.go

161 lines
4.3 KiB
Go
Raw Normal View History

2015-10-30 14:56:46 +00:00
package dokodemo
2018-09-30 21:08:41 +00:00
//go:generate errorgen
2017-04-08 23:43:25 +00:00
2015-10-30 14:56:46 +00:00
import (
"context"
"time"
2015-10-30 14:56:46 +00:00
"v2ray.com/core"
2016-12-27 23:53:29 +00:00
"v2ray.com/core/common"
2016-12-09 10:35:27 +00:00
"v2ray.com/core/common/buf"
2017-01-13 22:42:39 +00:00
"v2ray.com/core/common/net"
"v2ray.com/core/common/session"
2016-12-29 21:17:12 +00:00
"v2ray.com/core/common/signal"
2018-05-27 11:02:29 +00:00
"v2ray.com/core/common/task"
2018-10-11 20:34:31 +00:00
"v2ray.com/core/features/policy"
"v2ray.com/core/features/routing"
2016-08-20 18:55:45 +00:00
"v2ray.com/core/transport/internet"
2018-04-16 22:31:10 +00:00
"v2ray.com/core/transport/pipe"
2015-10-30 14:56:46 +00:00
)
2018-10-22 13:58:52 +00: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)
})
return d, err
}))
}
2015-10-30 14:56:46 +00:00
type DokodemoDoor struct {
2018-10-11 20:34:31 +00:00
policyManager policy.Manager
config *Config
address net.Address
port net.Port
2015-10-30 14:56:46 +00:00
}
2018-10-22 13:58:52 +00:00
// Init initializes the DokodemoDoor instance with necessary parameters.
func (d *DokodemoDoor) Init(config *Config, pm policy.Manager) error {
2018-11-20 15:58:26 +00:00
if (config.NetworkList == nil || config.NetworkList.Size() == 0) && len(config.Networks) == 0 {
2018-10-22 13:58:52 +00:00
return newError("no network specified")
2015-10-30 14:56:46 +00:00
}
2018-10-22 13:58:52 +00:00
d.config = config
d.address = config.GetPredefinedAddress()
d.port = net.Port(config.Port)
d.policyManager = pm
2018-10-22 13:58:52 +00:00
return nil
2015-10-30 14:56:46 +00:00
}
2018-11-20 15:58:26 +00:00
// 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
2015-11-10 23:08:43 +00:00
}
2018-10-11 20:34:31 +00:00
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
}
2018-11-20 15:58:26 +00:00
// 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))
2017-01-31 11:42:05 +00:00
dest := net.Destination{
Network: network,
Address: d.address,
Port: d.port,
2017-01-31 11:42:05 +00:00
}
if d.config.FollowRedirect {
if outbound := session.OutboundFromContext(ctx); outbound != nil && outbound.Target.IsValid() {
dest = outbound.Target
2018-07-14 20:06:39 +00:00
} else if handshake, ok := conn.(hasHandshakeAddress); ok {
addr := handshake.HandshakeAddress()
if addr != nil {
dest.Address = addr
}
}
2017-01-31 11:42:05 +00:00
}
if !dest.IsValid() || dest.Address == nil {
2017-04-08 23:43:25 +00:00
return newError("unable to get destination")
2017-01-31 11:42:05 +00:00
}
2017-03-31 19:10:33 +00:00
2018-05-25 10:08:28 +00:00
plcy := d.policy()
2017-11-14 23:36:14 +00:00
ctx, cancel := context.WithCancel(ctx)
2018-05-25 10:08:28 +00:00
timer := signal.CancelAfterInactivity(ctx, cancel, plcy.Timeouts.ConnectionIdle)
2017-01-31 11:42:05 +00:00
2018-10-11 20:34:31 +00:00
ctx = policy.ContextWithBufferPolicy(ctx, plcy.Buffer)
2018-04-16 22:31:10 +00:00
link, err := dispatcher.Dispatch(ctx, dest)
if err != nil {
2017-04-16 20:48:51 +00:00
return newError("failed to dispatch request").Base(err)
}
2016-05-04 22:24:18 +00:00
2018-04-11 14:45:09 +00:00
requestDone := func() error {
2018-05-25 10:08:28 +00:00
defer timer.SetTimeout(plcy.Timeouts.DownlinkOnly)
2016-12-29 21:17:12 +00:00
reader := buf.NewReader(conn)
if err := buf.Copy(reader, link.Writer, buf.UpdateActivity(timer)); err != nil {
2017-04-08 23:43:25 +00:00
return newError("failed to transport request").Base(err)
2016-11-21 23:17:49 +00:00
}
2015-10-30 14:56:46 +00:00
2016-12-29 21:17:12 +00:00
return nil
2018-04-11 14:45:09 +00:00
}
2016-12-29 21:17:12 +00:00
2018-04-11 14:45:09 +00:00
responseDone := func() error {
2018-05-25 10:08:28 +00:00
defer timer.SetTimeout(plcy.Timeouts.UplinkOnly)
2018-02-19 15:53:07 +00:00
2017-05-02 21:36:37 +00:00
var writer buf.Writer
if network == net.Network_TCP {
writer = buf.NewWriter(conn)
} else {
2017-09-06 22:55:15 +00:00
//if we are in TPROXY mode, use linux's udp forging functionality
if !d.config.FollowRedirect {
2018-07-31 11:43:27 +00:00
writer = &buf.SequentialWriter{Writer: conn}
2017-09-06 22:55:15 +00:00
} else {
2018-09-17 13:12:58 +00:00
tCtx := internet.ContextWithBindAddress(context.Background(), dest)
tCtx = internet.ContextWithStreamSettings(tCtx, &internet.MemoryStreamConfig{
ProtocolName: "udp",
SocketSettings: &internet.SocketConfig{
Tproxy: internet.SocketConfig_TProxy,
},
})
tConn, err := internet.DialSystem(tCtx, net.DestinationFromAddr(conn.RemoteAddr()))
2017-09-06 22:55:15 +00:00
if err != nil {
return err
}
2018-09-17 13:12:58 +00:00
writer = &buf.SequentialWriter{Writer: tConn}
2017-09-06 22:55:15 +00:00
}
2017-05-02 21:36:37 +00:00
}
2018-04-16 22:31:10 +00:00
if err := buf.Copy(link.Reader, writer, buf.UpdateActivity(timer)); err != nil {
2017-04-08 23:43:25 +00:00
return newError("failed to transport response").Base(err)
2016-11-21 23:17:49 +00:00
}
2016-12-29 21:17:12 +00:00
return nil
2018-04-11 14:45:09 +00:00
}
2015-10-30 14:56:46 +00:00
2018-05-27 11:02:29 +00:00
if err := task.Run(task.WithContext(ctx),
task.Parallel(
task.Single(requestDone, task.OnSuccess(task.Close(link.Writer))),
responseDone))(); err != nil {
2018-04-16 22:31:10 +00:00
pipe.CloseError(link.Reader)
pipe.CloseError(link.Writer)
2017-04-08 23:43:25 +00:00
return newError("connection ends").Base(err)
2016-12-29 23:51:39 +00:00
}
return nil
2015-10-30 14:56:46 +00:00
}