2015-10-30 14:56:46 +00:00
|
|
|
package dokodemo
|
|
|
|
|
2017-04-08 23:43:25 +00:00
|
|
|
//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg dokodemo -path Proxy,Dokodemo
|
|
|
|
|
2015-10-30 14:56:46 +00:00
|
|
|
import (
|
2017-01-12 23:56:21 +00:00
|
|
|
"context"
|
2017-01-31 11:42:05 +00:00
|
|
|
"runtime"
|
|
|
|
"time"
|
2015-10-30 14:56:46 +00:00
|
|
|
|
2016-08-20 18:55:45 +00:00
|
|
|
"v2ray.com/core/app"
|
|
|
|
"v2ray.com/core/app/dispatcher"
|
2017-02-03 21:35:09 +00:00
|
|
|
"v2ray.com/core/app/log"
|
2016-12-27 23:53:29 +00:00
|
|
|
"v2ray.com/core/common"
|
2016-12-09 10:35:27 +00:00
|
|
|
"v2ray.com/core/common/buf"
|
2017-01-13 22:42:39 +00:00
|
|
|
"v2ray.com/core/common/net"
|
2016-12-29 21:17:12 +00:00
|
|
|
"v2ray.com/core/common/signal"
|
2016-08-20 18:55:45 +00:00
|
|
|
"v2ray.com/core/proxy"
|
|
|
|
"v2ray.com/core/transport/internet"
|
2015-10-30 14:56:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type DokodemoDoor struct {
|
2017-02-03 21:35:09 +00:00
|
|
|
config *Config
|
|
|
|
address net.Address
|
|
|
|
port net.Port
|
2015-10-30 14:56:46 +00:00
|
|
|
}
|
|
|
|
|
2017-01-13 22:38:04 +00:00
|
|
|
func New(ctx context.Context, config *Config) (*DokodemoDoor, error) {
|
2017-01-12 23:56:21 +00:00
|
|
|
space := app.SpaceFromContext(ctx)
|
|
|
|
if space == nil {
|
2017-04-09 13:04:04 +00:00
|
|
|
return nil, newError("no space in context")
|
2017-01-12 23:56:21 +00:00
|
|
|
}
|
2017-01-14 23:57:06 +00:00
|
|
|
if config.NetworkList == nil || config.NetworkList.Size() == 0 {
|
2017-04-09 13:04:04 +00:00
|
|
|
return nil, newError("no network specified")
|
2017-01-14 23:57:06 +00:00
|
|
|
}
|
2016-05-22 17:32:37 +00:00
|
|
|
d := &DokodemoDoor{
|
2016-06-04 12:25:13 +00:00
|
|
|
config: config,
|
2016-09-22 14:49:20 +00:00
|
|
|
address: config.GetPredefinedAddress(),
|
2017-01-13 22:42:39 +00:00
|
|
|
port: net.Port(config.Port),
|
2015-10-30 14:56:46 +00:00
|
|
|
}
|
2017-01-12 23:56:21 +00:00
|
|
|
return d, nil
|
2015-10-30 14:56:46 +00:00
|
|
|
}
|
|
|
|
|
2017-01-26 19:46:44 +00:00
|
|
|
func (d *DokodemoDoor) Network() net.NetworkList {
|
|
|
|
return *(d.config.NetworkList)
|
2015-11-10 23:08:43 +00:00
|
|
|
}
|
|
|
|
|
2017-02-03 21:35:09 +00:00
|
|
|
func (d *DokodemoDoor) Process(ctx context.Context, network net.Network, conn internet.Connection, dispatcher dispatcher.Interface) error {
|
2017-04-09 13:04:04 +00:00
|
|
|
log.Trace(newError("processing connection from: ", conn.RemoteAddr()).AtDebug())
|
2017-01-31 11:42:05 +00:00
|
|
|
dest := net.Destination{
|
2017-01-26 19:46:44 +00:00
|
|
|
Network: network,
|
|
|
|
Address: d.address,
|
|
|
|
Port: d.port,
|
2017-01-31 11:42:05 +00:00
|
|
|
}
|
|
|
|
if d.config.FollowRedirect {
|
2017-02-09 21:49:38 +00:00
|
|
|
if origDest, ok := proxy.OriginalTargetFromContext(ctx); ok {
|
2017-01-31 11:42:05 +00:00
|
|
|
dest = origDest
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !dest.IsValid() || dest.Address == nil {
|
2017-04-08 23:43:25 +00:00
|
|
|
return newError("unable to get destination")
|
2017-01-31 11:42:05 +00:00
|
|
|
}
|
2017-03-31 19:10:33 +00:00
|
|
|
|
2017-01-31 15:49:59 +00:00
|
|
|
timeout := time.Second * time.Duration(d.config.Timeout)
|
|
|
|
if timeout == 0 {
|
|
|
|
timeout = time.Minute * 2
|
|
|
|
}
|
2017-03-31 19:45:43 +00:00
|
|
|
ctx, timer := signal.CancelAfterInactivity(ctx, timeout)
|
2017-01-31 11:42:05 +00:00
|
|
|
|
2017-02-03 21:35:09 +00:00
|
|
|
inboundRay, err := dispatcher.Dispatch(ctx, dest)
|
|
|
|
if err != nil {
|
2017-04-16 20:48:51 +00:00
|
|
|
return newError("failed to dispatch request").Base(err)
|
2017-02-03 21:35:09 +00:00
|
|
|
}
|
2016-05-04 22:24:18 +00:00
|
|
|
|
2016-12-29 21:17:12 +00:00
|
|
|
requestDone := signal.ExecuteAsync(func() error {
|
2017-01-26 19:46:44 +00:00
|
|
|
defer inboundRay.InboundInput().Close()
|
2016-12-29 21:17:12 +00:00
|
|
|
|
2017-01-31 15:49:59 +00:00
|
|
|
chunkReader := buf.NewReader(conn)
|
2016-04-18 17:01:24 +00:00
|
|
|
|
2017-04-17 20:50:02 +00:00
|
|
|
if err := buf.Copy(timer, chunkReader, inboundRay.InboundInput()); err != nil {
|
2017-04-08 23:43:25 +00:00
|
|
|
return newError("failed to transport request").Base(err)
|
2016-11-21 23:17:49 +00:00
|
|
|
}
|
2015-10-30 14:56:46 +00:00
|
|
|
|
2016-12-29 21:17:12 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
responseDone := signal.ExecuteAsync(func() error {
|
2016-12-09 12:17:34 +00:00
|
|
|
v2writer := buf.NewWriter(conn)
|
2016-04-18 17:01:24 +00:00
|
|
|
|
2017-04-17 20:50:02 +00:00
|
|
|
if err := buf.Copy(timer, inboundRay.InboundOutput(), v2writer); err != nil {
|
2017-04-08 23:43:25 +00:00
|
|
|
return newError("failed to transport response").Base(err)
|
2016-11-21 23:17:49 +00:00
|
|
|
}
|
2016-12-29 21:17:12 +00:00
|
|
|
return nil
|
|
|
|
})
|
2015-10-30 14:56:46 +00:00
|
|
|
|
2017-01-28 20:24:46 +00:00
|
|
|
if err := signal.ErrorOrFinish2(ctx, requestDone, responseDone); err != nil {
|
2017-01-26 19:46:44 +00:00
|
|
|
inboundRay.InboundInput().CloseError()
|
|
|
|
inboundRay.InboundOutput().CloseError()
|
2017-04-08 23:43:25 +00:00
|
|
|
return newError("connection ends").Base(err)
|
2016-12-29 23:51:39 +00:00
|
|
|
}
|
2017-01-26 19:46:44 +00:00
|
|
|
|
2017-01-31 11:42:05 +00:00
|
|
|
runtime.KeepAlive(timer)
|
|
|
|
|
2017-01-26 19:46:44 +00:00
|
|
|
return nil
|
2015-10-30 14:56:46 +00:00
|
|
|
}
|
2016-05-22 17:32:37 +00:00
|
|
|
|
|
|
|
func init() {
|
2017-01-12 23:56:21 +00:00
|
|
|
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
|
2017-01-13 22:38:04 +00:00
|
|
|
return New(ctx, config.(*Config))
|
2017-01-12 23:56:21 +00:00
|
|
|
}))
|
2016-05-22 17:32:37 +00:00
|
|
|
}
|