1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-02-20 23:47:21 -05:00
v2fly/proxy/dokodemo/dokodemo.go

199 lines
5.2 KiB
Go
Raw Normal View History

2019-02-01 20:08:21 +01:00
// +build !confonly
2015-10-30 15:56:46 +01:00
package dokodemo
2018-09-30 23:08:41 +02:00
//go:generate errorgen
2017-04-09 01:43:25 +02:00
2015-10-30 15:56:46 +01:00
import (
"context"
"sync/atomic"
"time"
2015-10-30 15:56:46 +01:00
"v2ray.com/core"
2016-12-28 00:53:29 +01:00
"v2ray.com/core/common"
2016-12-09 11:35:27 +01:00
"v2ray.com/core/common/buf"
2017-01-13 23:42:39 +01:00
"v2ray.com/core/common/net"
"v2ray.com/core/common/protocol"
"v2ray.com/core/common/session"
2016-12-29 22:17:12 +01:00
"v2ray.com/core/common/signal"
2018-05-27 13:02:29 +02:00
"v2ray.com/core/common/task"
2018-10-11 22:34:31 +02:00
"v2ray.com/core/features/policy"
"v2ray.com/core/features/routing"
2016-08-20 20:55:45 +02:00
"v2ray.com/core/transport/internet"
2015-10-30 15:56:46 +01:00
)
2018-10-22 15:58:52 +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)
})
return d, err
}))
}
2015-10-30 15:56:46 +01:00
type DokodemoDoor struct {
2018-10-11 22:34:31 +02:00
policyManager policy.Manager
config *Config
address net.Address
port net.Port
2015-10-30 15:56:46 +01:00
}
2018-10-22 15:58:52 +02:00
// Init initializes the DokodemoDoor instance with necessary parameters.
func (d *DokodemoDoor) Init(config *Config, pm policy.Manager) error {
2018-11-20 17:05:32 +01:00
if (config.NetworkList == nil || len(config.NetworkList.Network) == 0) && len(config.Networks) == 0 {
2018-10-22 15:58:52 +02:00
return newError("no network specified")
2015-10-30 15:56:46 +01:00
}
2018-10-22 15:58:52 +02:00
d.config = config
d.address = config.GetPredefinedAddress()
d.port = net.Port(config.Port)
d.policyManager = pm
2018-10-22 15:58:52 +02:00
return nil
2015-10-30 15:56:46 +01:00
}
2018-11-20 16:58:26 +01: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-11 00:08:43 +01:00
}
2018-10-11 22:34:31 +02: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 16:58:26 +01: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 12:42:05 +01:00
dest := net.Destination{
Network: network,
Address: d.address,
Port: d.port,
2017-01-31 12:42:05 +01:00
}
destinationOverridden := false
2017-01-31 12:42:05 +01:00
if d.config.FollowRedirect {
if outbound := session.OutboundFromContext(ctx); outbound != nil && outbound.Target.IsValid() {
dest = outbound.Target
destinationOverridden = true
2018-07-14 22:06:39 +02:00
} else if handshake, ok := conn.(hasHandshakeAddress); ok {
addr := handshake.HandshakeAddress()
if addr != nil {
dest.Address = addr
destinationOverridden = true
}
}
2017-01-31 12:42:05 +01:00
}
if !dest.IsValid() || dest.Address == nil {
2017-04-09 01:43:25 +02:00
return newError("unable to get destination")
2017-01-31 12:42:05 +01:00
}
2017-03-31 21:10:33 +02:00
if inbound := session.InboundFromContext(ctx); inbound != nil {
inbound.User = &protocol.MemoryUser{
Level: d.config.UserLevel,
}
}
2018-05-25 12:08:28 +02:00
plcy := d.policy()
2017-11-15 00:36:14 +01:00
ctx, cancel := context.WithCancel(ctx)
2018-05-25 12:08:28 +02:00
timer := signal.CancelAfterInactivity(ctx, cancel, plcy.Timeouts.ConnectionIdle)
2017-01-31 12:42:05 +01:00
2018-10-11 22:34:31 +02:00
ctx = policy.ContextWithBufferPolicy(ctx, plcy.Buffer)
2018-04-17 00:31:10 +02:00
link, err := dispatcher.Dispatch(ctx, dest)
if err != nil {
2017-04-16 22:48:51 +02:00
return newError("failed to dispatch request").Base(err)
}
2016-05-05 00:24:18 +02:00
requestCount := int32(1)
2018-04-11 16:45:09 +02:00
requestDone := func() error {
defer func() {
if atomic.AddInt32(&requestCount, -1) == 0 {
timer.SetTimeout(plcy.Timeouts.DownlinkOnly)
}
}()
2016-12-29 22:17:12 +01:00
2019-02-07 16:36:54 +01:00
var reader buf.Reader
if dest.Network == net.Network_UDP {
2019-02-07 19:14:37 +01:00
reader = buf.NewPacketReader(conn)
2019-02-07 16:36:54 +01:00
} else {
reader = buf.NewReader(conn)
}
if err := buf.Copy(reader, link.Writer, buf.UpdateActivity(timer)); err != nil {
2017-04-09 01:43:25 +02:00
return newError("failed to transport request").Base(err)
2016-11-22 00:17:49 +01:00
}
2015-10-30 15:56:46 +01:00
2016-12-29 22:17:12 +01:00
return nil
2018-04-11 16:45:09 +02:00
}
2016-12-29 22:17:12 +01:00
2019-01-27 22:30:34 +01:00
tproxyRequest := func() error {
return nil
}
2018-02-19 16:53:07 +01:00
2019-01-27 22:30:34 +01:00
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}
2017-05-02 23:36:37 +02:00
} else {
2019-01-27 22:30:34 +01:00
sockopt := &internet.SocketConfig{
Tproxy: internet.SocketConfig_TProxy,
}
if dest.Address.Family().IsIP() {
sockopt.BindAddress = dest.Address.IP()
sockopt.BindPort = uint32(dest.Port)
}
tConn, err := internet.DialSystem(ctx, net.DestinationFromAddr(conn.RemoteAddr()), sockopt)
if err != nil {
return err
}
defer tConn.Close()
writer = &buf.SequentialWriter{Writer: tConn}
2019-02-07 19:14:37 +01:00
tReader := buf.NewPacketReader(tConn)
requestCount++
2019-01-27 22:30:34 +01:00
tproxyRequest = func() error {
defer func() {
if atomic.AddInt32(&requestCount, -1) == 0 {
timer.SetTimeout(plcy.Timeouts.DownlinkOnly)
}
}()
2019-01-27 22:30:34 +01:00
if err := buf.Copy(tReader, link.Writer, buf.UpdateActivity(timer)); err != nil {
return newError("failed to transport request (TPROXY conn)").Base(err)
2017-09-07 06:55:15 +08:00
}
2019-01-27 22:30:34 +01:00
return nil
2017-09-07 06:55:15 +08:00
}
2017-05-02 23:36:37 +02:00
}
2019-01-27 22:30:34 +01:00
}
responseDone := func() error {
defer timer.SetTimeout(plcy.Timeouts.UplinkOnly)
2018-04-17 00:31:10 +02:00
if err := buf.Copy(link.Reader, writer, buf.UpdateActivity(timer)); err != nil {
2017-04-09 01:43:25 +02:00
return newError("failed to transport response").Base(err)
2016-11-22 00:17:49 +01:00
}
2016-12-29 22:17:12 +01:00
return nil
2018-04-11 16:45:09 +02:00
}
2015-10-30 15:56:46 +01:00
2019-01-27 22:30:34 +01:00
if err := task.Run(ctx, task.OnSuccess(requestDone, task.Close(link.Writer)), responseDone, tproxyRequest); err != nil {
2018-12-31 21:25:10 +01:00
common.Interrupt(link.Reader)
common.Interrupt(link.Writer)
2017-04-09 01:43:25 +02:00
return newError("connection ends").Base(err)
2016-12-30 00:51:39 +01:00
}
return nil
2015-10-30 15:56:46 +01:00
}