v2ray-core/proxy/dokodemo/dokodemo.go

101 lines
2.6 KiB
Go
Raw Normal View History

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