1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 10:08:15 -05:00
v2fly/app/reverse/bridge.go

193 lines
4.0 KiB
Go
Raw Normal View History

2018-10-27 18:03:11 -04:00
package reverse
import (
"context"
"time"
"github.com/golang/protobuf/proto"
"v2ray.com/core/common/mux"
"v2ray.com/core/common/net"
"v2ray.com/core/common/session"
"v2ray.com/core/common/task"
"v2ray.com/core/features/routing"
2018-11-03 07:36:29 -04:00
"v2ray.com/core/transport"
2018-10-27 18:03:11 -04:00
"v2ray.com/core/transport/pipe"
)
2018-11-13 17:19:58 -05:00
// Bridge is a component in reverse proxy, that relays connections from Portal to local address.
2018-10-27 18:03:11 -04:00
type Bridge struct {
dispatcher routing.Dispatcher
tag string
domain string
workers []*BridgeWorker
monitorTask *task.Periodic
}
2018-11-13 17:19:58 -05:00
// NewBridge creates a new Bridge instance.
2018-10-27 18:03:11 -04:00
func NewBridge(config *BridgeConfig, dispatcher routing.Dispatcher) (*Bridge, error) {
if len(config.Tag) == 0 {
return nil, newError("bridge tag is empty")
}
if len(config.Domain) == 0 {
return nil, newError("bridge domain is empty")
}
b := &Bridge{
dispatcher: dispatcher,
2018-10-28 02:27:07 -04:00
tag: config.Tag,
domain: config.Domain,
2018-10-27 18:03:11 -04:00
}
b.monitorTask = &task.Periodic{
Execute: b.monitor,
Interval: time.Second * 2,
}
return b, nil
}
2018-10-28 16:46:12 -04:00
func (b *Bridge) cleanup() {
var activeWorkers []*BridgeWorker
for _, w := range b.workers {
if w.IsActive() {
activeWorkers = append(activeWorkers, w)
}
}
if len(activeWorkers) != len(b.workers) {
b.workers = activeWorkers
}
}
2018-10-27 18:03:11 -04:00
func (b *Bridge) monitor() error {
2018-10-28 16:46:12 -04:00
b.cleanup()
2018-10-27 18:03:11 -04:00
var numConnections uint32
var numWorker uint32
for _, w := range b.workers {
if w.IsActive() {
numConnections += w.Connections()
numWorker++
}
}
if numWorker == 0 || numConnections/numWorker > 16 {
worker, err := NewBridgeWorker(b.domain, b.tag, b.dispatcher)
if err != nil {
newError("failed to create bridge worker").Base(err).AtWarning().WriteToLog()
return nil
}
b.workers = append(b.workers, worker)
}
return nil
}
func (b *Bridge) Start() error {
return b.monitorTask.Start()
}
func (b *Bridge) Close() error {
return b.monitorTask.Close()
}
type BridgeWorker struct {
tag string
worker *mux.ServerWorker
dispatcher routing.Dispatcher
state Control_State
}
func NewBridgeWorker(domain string, tag string, d routing.Dispatcher) (*BridgeWorker, error) {
ctx := context.Background()
ctx = session.ContextWithInbound(ctx, &session.Inbound{
Tag: tag,
})
link, err := d.Dispatch(ctx, net.Destination{
Network: net.Network_TCP,
Address: net.DomainAddress(domain),
Port: 0,
})
if err != nil {
return nil, err
}
w := &BridgeWorker{
dispatcher: d,
tag: tag,
}
worker, err := mux.NewServerWorker(context.Background(), w, link)
if err != nil {
return nil, err
}
w.worker = worker
return w, nil
}
func (w *BridgeWorker) Type() interface{} {
return routing.DispatcherType()
}
func (w *BridgeWorker) Start() error {
return nil
}
func (w *BridgeWorker) Close() error {
return nil
}
func (w *BridgeWorker) IsActive() bool {
2018-10-29 07:51:56 -04:00
return w.state == Control_ACTIVE && !w.worker.Closed()
2018-10-27 18:03:11 -04:00
}
func (w *BridgeWorker) Connections() uint32 {
return w.worker.ActiveConnections()
}
2018-11-03 07:36:29 -04:00
func (w *BridgeWorker) handleInternalConn(link transport.Link) {
2018-10-27 18:03:11 -04:00
go func() {
reader := link.Reader
for {
mb, err := reader.ReadMultiBuffer()
if err != nil {
break
}
for _, b := range mb {
var ctl Control
if err := proto.Unmarshal(b.Bytes(), &ctl); err != nil {
newError("failed to parse proto message").Base(err).WriteToLog()
break
}
if ctl.State != w.state {
w.state = ctl.State
}
}
}
}()
}
2018-11-03 07:36:29 -04:00
func (w *BridgeWorker) Dispatch(ctx context.Context, dest net.Destination) (*transport.Link, error) {
2018-10-27 18:03:11 -04:00
if !isInternalDomain(dest) {
ctx = session.ContextWithInbound(ctx, &session.Inbound{
Tag: w.tag,
})
return w.dispatcher.Dispatch(ctx, dest)
}
opt := []pipe.Option{pipe.WithSizeLimit(16 * 1024)}
uplinkReader, uplinkWriter := pipe.New(opt...)
downlinkReader, downlinkWriter := pipe.New(opt...)
2018-11-03 07:36:29 -04:00
w.handleInternalConn(transport.Link{
2018-10-27 18:03:11 -04:00
Reader: downlinkReader,
Writer: uplinkWriter,
})
2018-11-03 07:36:29 -04:00
return &transport.Link{
2018-10-27 18:03:11 -04:00
Reader: uplinkReader,
Writer: downlinkWriter,
}, nil
}