2015-10-30 15:56:46 +01:00
|
|
|
package dokodemo
|
|
|
|
|
2017-12-03 01:04:57 +01:00
|
|
|
//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg dokodemo -path Proxy,Dokodemo
|
2017-04-09 01:43:25 +02:00
|
|
|
|
2015-10-30 15:56:46 +01:00
|
|
|
import (
|
2017-01-13 00:56:21 +01:00
|
|
|
"context"
|
2018-01-10 12:22:37 +01:00
|
|
|
"time"
|
2015-10-30 15:56:46 +01:00
|
|
|
|
2018-01-10 12:22:37 +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"
|
2016-12-29 22:17:12 +01:00
|
|
|
"v2ray.com/core/common/signal"
|
2016-08-20 20:55:45 +02:00
|
|
|
"v2ray.com/core/proxy"
|
|
|
|
"v2ray.com/core/transport/internet"
|
2017-09-07 06:55:15 +08:00
|
|
|
"v2ray.com/core/transport/internet/udp"
|
2015-10-30 15:56:46 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type DokodemoDoor struct {
|
2018-01-10 12:22:37 +01:00
|
|
|
policyManager core.PolicyManager
|
|
|
|
config *Config
|
|
|
|
address net.Address
|
|
|
|
port net.Port
|
|
|
|
v *core.Instance
|
2015-10-30 15:56:46 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 23:38:04 +01:00
|
|
|
func New(ctx context.Context, config *Config) (*DokodemoDoor, error) {
|
2017-01-15 00:57:06 +01:00
|
|
|
if config.NetworkList == nil || config.NetworkList.Size() == 0 {
|
2017-04-09 15:04:04 +02:00
|
|
|
return nil, newError("no network specified")
|
2017-01-15 00:57:06 +01:00
|
|
|
}
|
2018-01-10 12:22:37 +01:00
|
|
|
v := core.FromContext(ctx)
|
|
|
|
if v == nil {
|
|
|
|
return nil, newError("V is not in context.")
|
|
|
|
}
|
|
|
|
|
2016-05-22 19:32:37 +02:00
|
|
|
d := &DokodemoDoor{
|
2018-01-10 12:22:37 +01:00
|
|
|
config: config,
|
|
|
|
address: config.GetPredefinedAddress(),
|
|
|
|
port: net.Port(config.Port),
|
|
|
|
policyManager: v.PolicyManager(),
|
2015-10-30 15:56:46 +01:00
|
|
|
}
|
2018-01-10 12:22:37 +01:00
|
|
|
|
2017-01-13 00:56:21 +01:00
|
|
|
return d, nil
|
2015-10-30 15:56:46 +01:00
|
|
|
}
|
|
|
|
|
2017-01-26 20:46:44 +01:00
|
|
|
func (d *DokodemoDoor) Network() net.NetworkList {
|
|
|
|
return *(d.config.NetworkList)
|
2015-11-11 00:08:43 +01:00
|
|
|
}
|
|
|
|
|
2018-01-10 12:22:37 +01:00
|
|
|
func (d *DokodemoDoor) policy() core.Policy {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DokodemoDoor) Process(ctx context.Context, network net.Network, conn internet.Connection, dispatcher core.Dispatcher) error {
|
2017-12-19 21:28:12 +01:00
|
|
|
newError("processing connection from: ", conn.RemoteAddr()).AtDebug().WriteToLog()
|
2017-01-31 12:42:05 +01:00
|
|
|
dest := net.Destination{
|
2017-01-26 20:46:44 +01:00
|
|
|
Network: network,
|
|
|
|
Address: d.address,
|
|
|
|
Port: d.port,
|
2017-01-31 12:42:05 +01:00
|
|
|
}
|
|
|
|
if d.config.FollowRedirect {
|
2017-02-09 22:49:38 +01:00
|
|
|
if origDest, ok := proxy.OriginalTargetFromContext(ctx); ok {
|
2017-01-31 12:42:05 +01:00
|
|
|
dest = origDest
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
2017-11-15 00:36:14 +01:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2018-01-10 12:22:37 +01:00
|
|
|
timer := signal.CancelAfterInactivity(ctx, cancel, d.policy().Timeouts.ConnectionIdle)
|
2017-01-31 12:42:05 +01:00
|
|
|
|
2017-02-03 22:35:09 +01:00
|
|
|
inboundRay, err := dispatcher.Dispatch(ctx, dest)
|
|
|
|
if err != nil {
|
2017-04-16 22:48:51 +02:00
|
|
|
return newError("failed to dispatch request").Base(err)
|
2017-02-03 22:35:09 +01:00
|
|
|
}
|
2016-05-05 00:24:18 +02:00
|
|
|
|
2016-12-29 22:17:12 +01:00
|
|
|
requestDone := signal.ExecuteAsync(func() error {
|
2017-01-26 20:46:44 +01:00
|
|
|
defer inboundRay.InboundInput().Close()
|
2016-12-29 22:17:12 +01:00
|
|
|
|
2017-01-31 16:49:59 +01:00
|
|
|
chunkReader := buf.NewReader(conn)
|
2016-04-18 19:01:24 +02:00
|
|
|
|
2017-04-27 22:30:48 +02:00
|
|
|
if err := buf.Copy(chunkReader, inboundRay.InboundInput(), 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
|
|
|
|
2018-01-10 12:22:37 +01:00
|
|
|
timer.SetTimeout(d.policy().Timeouts.DownlinkOnly)
|
2017-11-27 22:09:30 +01:00
|
|
|
|
2016-12-29 22:17:12 +01:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
responseDone := signal.ExecuteAsync(func() error {
|
2017-05-02 23:36:37 +02:00
|
|
|
var writer buf.Writer
|
|
|
|
if network == net.Network_TCP {
|
|
|
|
writer = buf.NewWriter(conn)
|
|
|
|
} else {
|
2017-09-07 06:55:15 +08:00
|
|
|
//if we are in TPROXY mode, use linux's udp forging functionality
|
|
|
|
if !d.config.FollowRedirect {
|
|
|
|
writer = buf.NewSequentialWriter(conn)
|
|
|
|
} else {
|
2017-10-26 21:07:33 +02:00
|
|
|
srca := net.UDPAddr{IP: dest.Address.IP(), Port: int(dest.Port.Value())}
|
2017-09-07 06:55:15 +08:00
|
|
|
origsend, err := udp.TransmitSocket(&srca, conn.RemoteAddr())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
writer = buf.NewSequentialWriter(origsend)
|
|
|
|
}
|
2017-05-02 23:36:37 +02:00
|
|
|
}
|
2016-04-18 19:01:24 +02:00
|
|
|
|
2017-05-02 23:36:37 +02:00
|
|
|
if err := buf.Copy(inboundRay.InboundOutput(), 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
|
|
|
}
|
2017-09-27 15:29:00 +02:00
|
|
|
|
2018-01-10 12:22:37 +01:00
|
|
|
timer.SetTimeout(d.policy().Timeouts.UplinkOnly)
|
2017-09-27 15:29:00 +02:00
|
|
|
|
2016-12-29 22:17:12 +01:00
|
|
|
return nil
|
|
|
|
})
|
2015-10-30 15:56:46 +01:00
|
|
|
|
2017-01-28 21:24:46 +01:00
|
|
|
if err := signal.ErrorOrFinish2(ctx, requestDone, responseDone); err != nil {
|
2017-01-26 20:46:44 +01:00
|
|
|
inboundRay.InboundInput().CloseError()
|
|
|
|
inboundRay.InboundOutput().CloseError()
|
2017-04-09 01:43:25 +02:00
|
|
|
return newError("connection ends").Base(err)
|
2016-12-30 00:51:39 +01:00
|
|
|
}
|
2017-01-26 20:46:44 +01:00
|
|
|
|
|
|
|
return nil
|
2015-10-30 15:56:46 +01:00
|
|
|
}
|
2016-05-22 19:32:37 +02:00
|
|
|
|
|
|
|
func init() {
|
2017-01-13 00:56:21 +01:00
|
|
|
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
|
2017-01-13 23:38:04 +01:00
|
|
|
return New(ctx, config.(*Config))
|
2017-01-13 00:56:21 +01:00
|
|
|
}))
|
2016-05-22 19:32:37 +02:00
|
|
|
}
|