1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-27 12:36:34 -05:00
v2fly/app/reverse/reverse.go

97 lines
2.0 KiB
Go
Raw Normal View History

2018-10-27 18:03:11 -04:00
package reverse
2021-02-16 15:31:50 -05:00
//go:generate go run github.com/v2fly/v2ray-core/v4/common/errors/errorgen
2018-10-27 18:03:11 -04:00
import (
"context"
2021-02-16 15:31:50 -05:00
core "github.com/v2fly/v2ray-core/v4"
"github.com/v2fly/v2ray-core/v4/common"
"github.com/v2fly/v2ray-core/v4/common/errors"
"github.com/v2fly/v2ray-core/v4/common/net"
"github.com/v2fly/v2ray-core/v4/features/outbound"
"github.com/v2fly/v2ray-core/v4/features/routing"
2018-10-27 18:03:11 -04:00
)
const (
2021-02-16 15:31:50 -05:00
internalDomain = "reverse.internal.v2fly.org"
2018-10-27 18:03:11 -04:00
)
2018-10-28 02:27:07 -04:00
func isDomain(dest net.Destination, domain string) bool {
return dest.Address.Family().IsDomain() && dest.Address.Domain() == domain
}
2018-10-27 18:03:11 -04:00
func isInternalDomain(dest net.Destination) bool {
2018-10-28 02:27:07 -04:00
return isDomain(dest, internalDomain)
2018-10-27 18:03:11 -04:00
}
func init() {
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
r := new(Reverse)
if err := core.RequireFeatures(ctx, func(d routing.Dispatcher, om outbound.Manager) error {
return r.Init(config.(*Config), d, om)
}); err != nil {
return nil, err
}
return r, nil
}))
}
type Reverse struct {
bridges []*Bridge
portals []*Portal
}
func (r *Reverse) Init(config *Config, d routing.Dispatcher, ohm outbound.Manager) error {
for _, bConfig := range config.BridgeConfig {
b, err := NewBridge(bConfig, d)
if err != nil {
return err
}
r.bridges = append(r.bridges, b)
}
for _, pConfig := range config.PortalConfig {
p, err := NewPortal(pConfig, ohm)
if err != nil {
return err
}
r.portals = append(r.portals, p)
}
return nil
}
2018-10-28 02:27:07 -04:00
func (r *Reverse) Type() interface{} {
return (*Reverse)(nil)
}
func (r *Reverse) Start() error {
for _, b := range r.bridges {
if err := b.Start(); err != nil {
return err
}
}
for _, p := range r.portals {
if err := p.Start(); err != nil {
return err
}
}
return nil
}
func (r *Reverse) Close() error {
2018-12-04 08:17:08 -05:00
var errs []error
2018-10-28 02:27:07 -04:00
for _, b := range r.bridges {
2018-12-04 08:17:08 -05:00
errs = append(errs, b.Close())
2018-10-28 02:27:07 -04:00
}
for _, p := range r.portals {
2018-12-04 08:17:08 -05:00
errs = append(errs, p.Close())
2018-10-28 02:27:07 -04:00
}
2018-12-04 08:17:08 -05:00
return errors.Combine(errs...)
2018-10-28 02:27:07 -04:00
}