2015-10-30 10:56:46 -04:00
|
|
|
package dokodemo
|
|
|
|
|
2022-01-02 10:16:23 -05:00
|
|
|
//go:generate go run github.com/v2fly/v2ray-core/v5/common/errors/errorgen
|
2017-04-08 19:43:25 -04:00
|
|
|
|
2015-10-30 10:56:46 -04:00
|
|
|
import (
|
2017-01-12 18:56:21 -05:00
|
|
|
"context"
|
2019-01-30 15:38:25 -05:00
|
|
|
"sync/atomic"
|
2018-01-10 06:22:37 -05:00
|
|
|
"time"
|
2015-10-30 10:56:46 -04:00
|
|
|
|
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/log"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/net"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/protocol"
|
|
|
|
"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"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/features/routing"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/transport/internet"
|
2015-10-30 10:56:46 -04:00
|
|
|
)
|
|
|
|
|
2018-10-22 09:58:52 -04:00
|
|
|
func init() {
|
|
|
|
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
|
2020-11-21 16:05:01 -05:00
|
|
|
d := new(Door)
|
2018-10-22 09:58:52 -04:00
|
|
|
err := core.RequireFeatures(ctx, func(pm policy.Manager) error {
|
2020-07-31 14:04:06 -04:00
|
|
|
return d.Init(config.(*Config), pm, session.SockoptFromContext(ctx))
|
2018-10-22 09:58:52 -04:00
|
|
|
})
|
|
|
|
return d, err
|
|
|
|
}))
|
2021-09-06 10:58:48 -04:00
|
|
|
|
|
|
|
common.Must(common.RegisterConfig((*SimplifiedConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
|
|
|
|
simplifiedServer := config.(*SimplifiedConfig)
|
|
|
|
fullConfig := &Config{
|
|
|
|
Address: simplifiedServer.Address,
|
|
|
|
Port: simplifiedServer.Port,
|
2022-08-19 10:28:20 -04:00
|
|
|
Networks: simplifiedServer.Networks.Network,
|
2021-09-06 10:58:48 -04:00
|
|
|
FollowRedirect: simplifiedServer.FollowRedirect,
|
|
|
|
}
|
|
|
|
|
|
|
|
return common.CreateObject(ctx, fullConfig)
|
|
|
|
}))
|
2018-10-22 09:58:52 -04:00
|
|
|
}
|
|
|
|
|
2020-11-21 16:05:01 -05:00
|
|
|
type Door struct {
|
2018-10-11 16:34:31 -04:00
|
|
|
policyManager policy.Manager
|
2018-01-10 06:22:37 -05:00
|
|
|
config *Config
|
|
|
|
address net.Address
|
|
|
|
port net.Port
|
2020-07-31 14:04:06 -04:00
|
|
|
sockopt *session.Sockopt
|
2015-10-30 10:56:46 -04:00
|
|
|
}
|
|
|
|
|
2020-11-21 16:05:01 -05:00
|
|
|
// Init initializes the Door instance with necessary parameters.
|
|
|
|
func (d *Door) Init(config *Config, pm policy.Manager, sockopt *session.Sockopt) error {
|
2018-11-20 11:05:32 -05:00
|
|
|
if (config.NetworkList == nil || len(config.NetworkList.Network) == 0) && len(config.Networks) == 0 {
|
2018-10-22 09:58:52 -04:00
|
|
|
return newError("no network specified")
|
2015-10-30 10:56:46 -04:00
|
|
|
}
|
2018-10-22 09:58:52 -04:00
|
|
|
d.config = config
|
|
|
|
d.address = config.GetPredefinedAddress()
|
|
|
|
d.port = net.Port(config.Port)
|
|
|
|
d.policyManager = pm
|
2020-07-31 14:04:06 -04:00
|
|
|
d.sockopt = sockopt
|
2018-01-10 06:22:37 -05:00
|
|
|
|
2018-10-22 09:58:52 -04:00
|
|
|
return nil
|
2015-10-30 10:56:46 -04:00
|
|
|
}
|
|
|
|
|
2018-11-20 10:58:26 -05:00
|
|
|
// Network implements proxy.Inbound.
|
2020-11-21 16:05:01 -05:00
|
|
|
func (d *Door) Network() []net.Network {
|
2018-11-20 10:58:26 -05:00
|
|
|
if len(d.config.Networks) > 0 {
|
|
|
|
return d.config.Networks
|
|
|
|
}
|
|
|
|
|
|
|
|
return d.config.NetworkList.Network
|
2015-11-10 18:08:43 -05:00
|
|
|
}
|
|
|
|
|
2020-11-21 16:05:01 -05:00
|
|
|
func (d *Door) policy() policy.Session {
|
2018-01-10 06:22:37 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-07-14 07:58:24 -04:00
|
|
|
type hasHandshakeAddress interface {
|
|
|
|
HandshakeAddress() net.Address
|
|
|
|
}
|
|
|
|
|
2018-11-20 10:58:26 -05:00
|
|
|
// Process implements proxy.Inbound.
|
2020-11-21 16:05:01 -05:00
|
|
|
func (d *Door) Process(ctx context.Context, network net.Network, conn internet.Connection, dispatcher routing.Dispatcher) error {
|
2018-06-24 19:09:02 -04:00
|
|
|
newError("processing connection from: ", conn.RemoteAddr()).AtDebug().WriteToLog(session.ExportIDToError(ctx))
|
2017-01-31 06:42:05 -05:00
|
|
|
dest := net.Destination{
|
2017-01-26 14:46:44 -05:00
|
|
|
Network: network,
|
|
|
|
Address: d.address,
|
|
|
|
Port: d.port,
|
2017-01-31 06:42:05 -05:00
|
|
|
}
|
2019-01-14 16:57:53 -05:00
|
|
|
|
|
|
|
destinationOverridden := false
|
2017-01-31 06:42:05 -05:00
|
|
|
if d.config.FollowRedirect {
|
2018-09-18 17:09:54 -04:00
|
|
|
if outbound := session.OutboundFromContext(ctx); outbound != nil && outbound.Target.IsValid() {
|
|
|
|
dest = outbound.Target
|
2019-01-14 16:57:53 -05:00
|
|
|
destinationOverridden = true
|
2018-07-14 16:06:39 -04:00
|
|
|
} else if handshake, ok := conn.(hasHandshakeAddress); ok {
|
2018-07-14 07:58:24 -04:00
|
|
|
addr := handshake.HandshakeAddress()
|
|
|
|
if addr != nil {
|
|
|
|
dest.Address = addr
|
2019-01-14 16:57:53 -05:00
|
|
|
destinationOverridden = true
|
2018-07-14 07:58:24 -04:00
|
|
|
}
|
|
|
|
}
|
2017-01-31 06:42:05 -05:00
|
|
|
}
|
|
|
|
if !dest.IsValid() || dest.Address == nil {
|
2017-04-08 19:43:25 -04:00
|
|
|
return newError("unable to get destination")
|
2017-01-31 06:42:05 -05:00
|
|
|
}
|
2017-03-31 15:10:33 -04:00
|
|
|
|
2019-02-14 18:46:19 -05:00
|
|
|
if inbound := session.InboundFromContext(ctx); inbound != nil {
|
|
|
|
inbound.User = &protocol.MemoryUser{
|
|
|
|
Level: d.config.UserLevel,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-13 09:02:19 -04:00
|
|
|
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))
|
|
|
|
|
2018-05-25 06:08:28 -04:00
|
|
|
plcy := d.policy()
|
2017-11-14 18:36:14 -05:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2018-05-25 06:08:28 -04:00
|
|
|
timer := signal.CancelAfterInactivity(ctx, cancel, plcy.Timeouts.ConnectionIdle)
|
2017-01-31 06:42:05 -05:00
|
|
|
|
2018-10-11 16:34:31 -04:00
|
|
|
ctx = policy.ContextWithBufferPolicy(ctx, plcy.Buffer)
|
2018-04-16 18:31:10 -04:00
|
|
|
link, err := dispatcher.Dispatch(ctx, dest)
|
2017-02-03 16:35:09 -05:00
|
|
|
if err != nil {
|
2017-04-16 16:48:51 -04:00
|
|
|
return newError("failed to dispatch request").Base(err)
|
2017-02-03 16:35:09 -05:00
|
|
|
}
|
2016-05-04 18:24:18 -04:00
|
|
|
|
2019-01-30 15:38:25 -05:00
|
|
|
requestCount := int32(1)
|
2018-04-11 10:45:09 -04:00
|
|
|
requestDone := func() error {
|
2019-01-30 15:38:25 -05:00
|
|
|
defer func() {
|
|
|
|
if atomic.AddInt32(&requestCount, -1) == 0 {
|
|
|
|
timer.SetTimeout(plcy.Timeouts.DownlinkOnly)
|
|
|
|
}
|
|
|
|
}()
|
2016-12-29 16:17:12 -05:00
|
|
|
|
2019-02-07 10:36:54 -05:00
|
|
|
var reader buf.Reader
|
|
|
|
if dest.Network == net.Network_UDP {
|
2019-02-07 13:14:37 -05:00
|
|
|
reader = buf.NewPacketReader(conn)
|
2019-02-07 10:36:54 -05:00
|
|
|
} else {
|
|
|
|
reader = buf.NewReader(conn)
|
|
|
|
}
|
2018-07-31 10:05:57 -04:00
|
|
|
if err := buf.Copy(reader, link.Writer, buf.UpdateActivity(timer)); err != nil {
|
2017-04-08 19:43:25 -04:00
|
|
|
return newError("failed to transport request").Base(err)
|
2016-11-21 18:17:49 -05:00
|
|
|
}
|
2015-10-30 10:56:46 -04:00
|
|
|
|
2016-12-29 16:17:12 -05:00
|
|
|
return nil
|
2018-04-11 10:45:09 -04:00
|
|
|
}
|
2016-12-29 16:17:12 -05:00
|
|
|
|
2019-01-27 16:30:34 -05:00
|
|
|
tproxyRequest := func() error {
|
|
|
|
return nil
|
|
|
|
}
|
2018-02-19 10:53:07 -05:00
|
|
|
|
2019-01-27 16:30:34 -05:00
|
|
|
var writer buf.Writer
|
|
|
|
if network == net.Network_TCP {
|
|
|
|
writer = buf.NewWriter(conn)
|
|
|
|
} else {
|
2020-10-11 07:22:46 -04:00
|
|
|
// if we are in TPROXY mode, use linux's udp forging functionality
|
2019-01-27 16:30:34 -05:00
|
|
|
if !destinationOverridden {
|
|
|
|
writer = &buf.SequentialWriter{Writer: conn}
|
2017-05-02 17:36:37 -04:00
|
|
|
} else {
|
2019-01-27 16:30:34 -05:00
|
|
|
sockopt := &internet.SocketConfig{
|
|
|
|
Tproxy: internet.SocketConfig_TProxy,
|
|
|
|
}
|
|
|
|
if dest.Address.Family().IsIP() {
|
|
|
|
sockopt.BindAddress = dest.Address.IP()
|
|
|
|
sockopt.BindPort = uint32(dest.Port)
|
|
|
|
}
|
2020-07-31 14:04:06 -04:00
|
|
|
if d.sockopt != nil {
|
|
|
|
sockopt.Mark = d.sockopt.Mark
|
|
|
|
}
|
2019-01-27 16:30:34 -05:00
|
|
|
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 13:14:37 -05:00
|
|
|
tReader := buf.NewPacketReader(tConn)
|
2019-01-30 15:38:25 -05:00
|
|
|
requestCount++
|
2019-01-27 16:30:34 -05:00
|
|
|
tproxyRequest = func() error {
|
2019-01-30 15:38:25 -05:00
|
|
|
defer func() {
|
|
|
|
if atomic.AddInt32(&requestCount, -1) == 0 {
|
|
|
|
timer.SetTimeout(plcy.Timeouts.DownlinkOnly)
|
|
|
|
}
|
|
|
|
}()
|
2019-01-27 16:30:34 -05: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-06 18:55:15 -04:00
|
|
|
}
|
2019-01-27 16:30:34 -05:00
|
|
|
return nil
|
2017-09-06 18:55:15 -04:00
|
|
|
}
|
2017-05-02 17:36:37 -04:00
|
|
|
}
|
2019-01-27 16:30:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
responseDone := func() error {
|
|
|
|
defer timer.SetTimeout(plcy.Timeouts.UplinkOnly)
|
2016-04-18 13:01:24 -04:00
|
|
|
|
2018-04-16 18:31:10 -04:00
|
|
|
if err := buf.Copy(link.Reader, writer, buf.UpdateActivity(timer)); err != nil {
|
2017-04-08 19:43:25 -04:00
|
|
|
return newError("failed to transport response").Base(err)
|
2016-11-21 18:17:49 -05:00
|
|
|
}
|
2016-12-29 16:17:12 -05:00
|
|
|
return nil
|
2018-04-11 10:45:09 -04:00
|
|
|
}
|
2015-10-30 10:56:46 -04:00
|
|
|
|
2020-06-18 01:40:48 -04:00
|
|
|
if err := task.Run(ctx, task.OnSuccess(func() error {
|
|
|
|
return task.Run(ctx, requestDone, tproxyRequest)
|
|
|
|
}, task.Close(link.Writer)), responseDone); err != nil {
|
2018-12-31 15:25:10 -05:00
|
|
|
common.Interrupt(link.Reader)
|
|
|
|
common.Interrupt(link.Writer)
|
2017-04-08 19:43:25 -04:00
|
|
|
return newError("connection ends").Base(err)
|
2016-12-29 18:51:39 -05:00
|
|
|
}
|
2017-01-26 14:46:44 -05:00
|
|
|
|
|
|
|
return nil
|
2015-10-30 10:56:46 -04:00
|
|
|
}
|