1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 18:17:52 -05:00
v2fly/proxy/dokodemo/dokodemo.go

129 lines
3.3 KiB
Go
Raw Normal View History

2015-10-30 10:56:46 -04:00
package dokodemo
2017-04-08 19:43:25 -04:00
//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg dokodemo -path Proxy,Dokodemo
2015-10-30 10:56:46 -04:00
import (
"context"
2017-01-31 06:42:05 -05:00
"time"
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"
"v2ray.com/core/app/log"
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"
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"
2017-09-06 18:55:15 -04:00
"v2ray.com/core/transport/internet/udp"
2015-10-30 10:56:46 -04:00
)
type DokodemoDoor struct {
config *Config
address net.Address
port net.Port
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) {
space := app.SpaceFromContext(ctx)
if space == nil {
2017-04-09 09:04:04 -04:00
return nil, newError("no space in context")
}
2017-01-14 18:57:06 -05:00
if config.NetworkList == nil || config.NetworkList.Size() == 0 {
2017-04-09 09:04:04 -04:00
return nil, newError("no network specified")
2017-01-14 18:57:06 -05:00
}
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
}
return d, nil
2015-10-30 10:56:46 -04:00
}
func (d *DokodemoDoor) Network() net.NetworkList {
return *(d.config.NetworkList)
2015-11-10 18:08:43 -05:00
}
func (d *DokodemoDoor) Process(ctx context.Context, network net.Network, conn internet.Connection, dispatcher dispatcher.Interface) error {
2017-04-09 09:04:04 -04:00
log.Trace(newError("processing connection from: ", conn.RemoteAddr()).AtDebug())
2017-01-31 06:42:05 -05:00
dest := net.Destination{
Network: network,
Address: d.address,
Port: d.port,
2017-01-31 06:42:05 -05:00
}
if d.config.FollowRedirect {
2017-02-09 16:49:38 -05:00
if origDest, ok := proxy.OriginalTargetFromContext(ctx); ok {
2017-01-31 06:42:05 -05:00
dest = origDest
}
}
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
2017-01-31 10:49:59 -05:00
timeout := time.Second * time.Duration(d.config.Timeout)
if timeout == 0 {
2017-09-27 15:09:13 -04:00
timeout = time.Minute * 5
2017-01-31 10:49:59 -05:00
}
2017-11-14 18:36:14 -05:00
ctx, cancel := context.WithCancel(ctx)
timer := signal.CancelAfterInactivity(ctx, cancel, timeout)
2017-01-31 06:42:05 -05:00
inboundRay, err := dispatcher.Dispatch(ctx, dest)
if err != nil {
2017-04-16 16:48:51 -04:00
return newError("failed to dispatch request").Base(err)
}
2016-05-04 18:24:18 -04:00
2016-12-29 16:17:12 -05:00
requestDone := signal.ExecuteAsync(func() error {
defer inboundRay.InboundInput().Close()
2016-12-29 16:17:12 -05:00
2017-01-31 10:49:59 -05:00
chunkReader := buf.NewReader(conn)
2017-04-27 16:30:48 -04:00
if err := buf.Copy(chunkReader, inboundRay.InboundInput(), 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
})
responseDone := signal.ExecuteAsync(func() error {
2017-05-02 17:36:37 -04:00
var writer buf.Writer
if network == net.Network_TCP {
writer = buf.NewWriter(conn)
} else {
2017-09-06 18:55:15 -04: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 15:07:33 -04:00
srca := net.UDPAddr{IP: dest.Address.IP(), Port: int(dest.Port.Value())}
2017-09-06 18:55:15 -04:00
origsend, err := udp.TransmitSocket(&srca, conn.RemoteAddr())
if err != nil {
return err
}
writer = buf.NewSequentialWriter(origsend)
}
2017-05-02 17:36:37 -04:00
}
2017-05-02 17:36:37 -04:00
if err := buf.Copy(inboundRay.InboundOutput(), 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
}
timer.SetTimeout(time.Second * 2)
2016-12-29 16:17:12 -05:00
return nil
})
2015-10-30 10:56:46 -04:00
if err := signal.ErrorOrFinish2(ctx, requestDone, responseDone); err != nil {
inboundRay.InboundInput().CloseError()
inboundRay.InboundOutput().CloseError()
2017-04-08 19:43:25 -04:00
return newError("connection ends").Base(err)
2016-12-29 18:51:39 -05:00
}
return nil
2015-10-30 10:56:46 -04:00
}
2016-05-22 13:32:37 -04:00
func init() {
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))
}))
2016-05-22 13:32:37 -04:00
}