2015-10-30 10:56:46 -04:00
|
|
|
package dokodemo
|
|
|
|
|
|
|
|
import (
|
2017-01-12 18:56:21 -05:00
|
|
|
"context"
|
2015-10-30 10:56:46 -04:00
|
|
|
|
2016-08-20 14:55:45 -04:00
|
|
|
"v2ray.com/core/app"
|
|
|
|
"v2ray.com/core/app/dispatcher"
|
2016-12-27 18:53:29 -05:00
|
|
|
"v2ray.com/core/common"
|
2016-12-09 05:35:27 -05:00
|
|
|
"v2ray.com/core/common/buf"
|
2016-12-04 03:10:47 -05:00
|
|
|
"v2ray.com/core/common/errors"
|
2016-08-20 14:55:45 -04:00
|
|
|
"v2ray.com/core/common/log"
|
2017-01-13 17:42:39 -05:00
|
|
|
"v2ray.com/core/common/net"
|
2016-12-29 16:17:12 -05:00
|
|
|
"v2ray.com/core/common/signal"
|
2016-08-20 14:55:45 -04:00
|
|
|
"v2ray.com/core/proxy"
|
|
|
|
"v2ray.com/core/transport/internet"
|
2015-10-30 10:56:46 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type DokodemoDoor struct {
|
2016-01-31 11:01:28 -05:00
|
|
|
config *Config
|
2017-01-13 17:42:39 -05:00
|
|
|
address net.Address
|
|
|
|
port net.Port
|
2017-01-13 07:53:44 -05:00
|
|
|
packetDispatcher dispatcher.Interface
|
2015-10-30 10:56:46 -04:00
|
|
|
}
|
|
|
|
|
2017-01-13 17:38:04 -05:00
|
|
|
func New(ctx context.Context, config *Config) (*DokodemoDoor, error) {
|
2017-01-12 18:56:21 -05:00
|
|
|
space := app.SpaceFromContext(ctx)
|
|
|
|
if space == nil {
|
|
|
|
return nil, errors.New("Dokodemo: No space in context.")
|
|
|
|
}
|
2017-01-14 18:57:06 -05:00
|
|
|
if config.NetworkList == nil || config.NetworkList.Size() == 0 {
|
|
|
|
return nil, errors.New("DokodemoDoor: No network specified.")
|
|
|
|
}
|
2016-05-22 13:32:37 -04:00
|
|
|
d := &DokodemoDoor{
|
2016-06-04 08:25:13 -04:00
|
|
|
config: config,
|
2016-09-22 10:49:20 -04:00
|
|
|
address: config.GetPredefinedAddress(),
|
2017-01-13 17:42:39 -05:00
|
|
|
port: net.Port(config.Port),
|
2015-10-30 10:56:46 -04:00
|
|
|
}
|
2017-01-06 09:32:36 -05:00
|
|
|
space.OnInitialize(func() error {
|
|
|
|
d.packetDispatcher = dispatcher.FromSpace(space)
|
|
|
|
if d.packetDispatcher == nil {
|
2016-11-21 15:13:01 -05:00
|
|
|
return errors.New("Dokodemo: Dispatcher is not found in the space.")
|
2016-05-22 13:32:37 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2017-01-12 18:56:21 -05:00
|
|
|
return d, nil
|
2015-10-30 10:56:46 -04:00
|
|
|
}
|
|
|
|
|
2017-01-26 14:46:44 -05:00
|
|
|
func (d *DokodemoDoor) Network() net.NetworkList {
|
|
|
|
return *(d.config.NetworkList)
|
2015-11-10 18:08:43 -05:00
|
|
|
}
|
|
|
|
|
2017-01-26 14:46:44 -05:00
|
|
|
func (d *DokodemoDoor) Process(ctx context.Context, network net.Network, conn internet.Connection) error {
|
|
|
|
log.Debug("Dokodemo: processing connection from: ", conn.RemoteAddr())
|
2017-01-03 08:53:59 -05:00
|
|
|
conn.SetReusable(false)
|
2017-01-26 14:46:44 -05:00
|
|
|
ctx = proxy.ContextWithDestination(ctx, net.Destination{
|
|
|
|
Network: network,
|
|
|
|
Address: d.address,
|
|
|
|
Port: d.port,
|
2016-08-14 11:08:01 -04:00
|
|
|
})
|
2017-01-26 14:46:44 -05:00
|
|
|
inboundRay := d.packetDispatcher.DispatchToOutbound(ctx)
|
2016-05-04 18:24:18 -04:00
|
|
|
|
2016-12-29 16:17:12 -05:00
|
|
|
requestDone := signal.ExecuteAsync(func() error {
|
2017-01-26 14:46:44 -05:00
|
|
|
defer inboundRay.InboundInput().Close()
|
2016-12-29 16:17:12 -05:00
|
|
|
|
2017-01-26 14:46:44 -05:00
|
|
|
timedReader := net.NewTimeOutReader(d.config.Timeout, conn)
|
|
|
|
chunkReader := buf.NewReader(timedReader)
|
2016-04-18 13:01:24 -04:00
|
|
|
|
2017-01-26 14:46:44 -05:00
|
|
|
if err := buf.PipeUntilEOF(chunkReader, inboundRay.InboundInput()); err != nil {
|
|
|
|
log.Info("Dokodemo: Failed to transport request: ", err)
|
2016-12-29 16:17:12 -05:00
|
|
|
return 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
|
|
|
|
})
|
|
|
|
|
|
|
|
responseDone := signal.ExecuteAsync(func() error {
|
2016-12-09 07:17:34 -05:00
|
|
|
v2writer := buf.NewWriter(conn)
|
2016-04-18 13:01:24 -04:00
|
|
|
|
2017-01-26 14:46:44 -05:00
|
|
|
if err := buf.PipeUntilEOF(inboundRay.InboundOutput(), v2writer); err != nil {
|
|
|
|
log.Info("Dokodemo: Failed to transport response: ", err)
|
2016-12-29 16:17:12 -05:00
|
|
|
return err
|
2016-11-21 18:17:49 -05:00
|
|
|
}
|
2016-12-29 16:17:12 -05:00
|
|
|
return nil
|
|
|
|
})
|
2015-10-30 10:56:46 -04:00
|
|
|
|
2016-12-29 18:51:39 -05:00
|
|
|
if err := signal.ErrorOrFinish2(requestDone, responseDone); err != nil {
|
2017-01-26 14:46:44 -05:00
|
|
|
inboundRay.InboundInput().CloseError()
|
|
|
|
inboundRay.InboundOutput().CloseError()
|
2016-12-29 18:51:39 -05:00
|
|
|
log.Info("Dokodemo: Connection ends with ", err)
|
2017-01-26 14:46:44 -05:00
|
|
|
return 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
|
|
|
}
|
2016-05-22 13:32:37 -04:00
|
|
|
|
|
|
|
func init() {
|
2017-01-12 18:56:21 -05:00
|
|
|
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
|
2017-01-13 17:38:04 -05:00
|
|
|
return New(ctx, config.(*Config))
|
2017-01-12 18:56:21 -05:00
|
|
|
}))
|
2016-05-22 13:32:37 -04:00
|
|
|
}
|