You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
v2ray-core/shell/point/point.go

196 lines
5.3 KiB

9 years ago
// Package point is a shell of V2Ray to run on various of systems.
// Point server is a full functionality proxying system. It consists of an inbound and an outbound
// connection, as well as any number of inbound and outbound detours. It provides a way internally
// to route network packets.
package point
9 years ago
import (
"github.com/v2ray/v2ray-core/app"
"github.com/v2ray/v2ray-core/app/controller"
"github.com/v2ray/v2ray-core/app/router"
"github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/common/retry"
"github.com/v2ray/v2ray-core/proxy"
proxyrepo "github.com/v2ray/v2ray-core/proxy/repo"
"github.com/v2ray/v2ray-core/transport/ray"
9 years ago
)
9 years ago
// Point shell of V2Ray.
type Point struct {
port v2net.Port
ich proxy.InboundConnectionHandler
och proxy.OutboundConnectionHandler
idh []*InboundDetourHandler
odh map[string]proxy.OutboundConnectionHandler
router router.Router
space *controller.SpaceController
9 years ago
}
// NewPoint returns a new Point server based on given configuration.
9 years ago
// The server is not started at this point.
func NewPoint(pConfig PointConfig) (*Point, error) {
var vpoint = new(Point)
vpoint.port = pConfig.Port()
if pConfig.LogConfig() != nil {
logConfig := pConfig.LogConfig()
if len(logConfig.AccessLog()) > 0 {
err := log.InitAccessLogger(logConfig.AccessLog())
if err != nil {
return nil, err
}
}
if len(logConfig.ErrorLog()) > 0 {
err := log.InitErrorLogger(logConfig.ErrorLog())
if err != nil {
return nil, err
}
}
log.SetLogLevel(logConfig.LogLevel())
}
vpoint.space = controller.New()
vpoint.space.Bind(vpoint)
ichConfig := pConfig.InboundConfig().Settings()
ich, err := proxyrepo.CreateInboundConnectionHandler(pConfig.InboundConfig().Protocol(), vpoint.space.ForContext("vpoint-default-inbound"), ichConfig)
if err != nil {
log.Error("Failed to create inbound connection handler: %v", err)
return nil, err
}
vpoint.ich = ich
ochConfig := pConfig.OutboundConfig().Settings()
och, err := proxyrepo.CreateOutboundConnectionHandler(pConfig.OutboundConfig().Protocol(), vpoint.space.ForContext("vpoint-default-outbound"), ochConfig)
if err != nil {
log.Error("Failed to create outbound connection handler: %v", err)
return nil, err
}
vpoint.och = och
9 years ago
9 years ago
detours := pConfig.InboundDetours()
if len(detours) > 0 {
vpoint.idh = make([]*InboundDetourHandler, len(detours))
for idx, detourConfig := range detours {
detourHandler := &InboundDetourHandler{
space: vpoint.space.ForContext(detourConfig.Tag()),
9 years ago
config: detourConfig,
}
err := detourHandler.Initialize()
if err != nil {
return nil, err
}
vpoint.idh[idx] = detourHandler
}
}
outboundDetours := pConfig.OutboundDetours()
if len(outboundDetours) > 0 {
vpoint.odh = make(map[string]proxy.OutboundConnectionHandler)
for _, detourConfig := range outboundDetours {
detourHandler, err := proxyrepo.CreateOutboundConnectionHandler(detourConfig.Protocol(), vpoint.space.ForContext(detourConfig.Tag()), detourConfig.Settings())
if err != nil {
log.Error("Failed to create detour outbound connection handler: %v", err)
return nil, err
}
vpoint.odh[detourConfig.Tag()] = detourHandler
}
}
routerConfig := pConfig.RouterConfig()
if routerConfig != nil {
r, err := router.CreateRouter(routerConfig.Strategy(), routerConfig.Settings())
if err != nil {
log.Error("Failed to create router: %v", err)
return nil, BadConfiguration
}
vpoint.router = r
}
9 years ago
return vpoint, nil
9 years ago
}
func (this *Point) Close() {
this.ich.Close()
for _, idh := range this.idh {
idh.Close()
}
}
// Start starts the Point server, and return any error during the process.
9 years ago
// In the case of any errors, the state of the server is unpredicatable.
9 years ago
func (this *Point) Start() error {
if this.port <= 0 {
log.Error("Invalid port %d", this.port)
return BadConfiguration
9 years ago
}
9 years ago
err := retry.Timed(100 /* times */, 100 /* ms */).On(func() error {
9 years ago
err := this.ich.Listen(this.port)
9 years ago
if err != nil {
return err
9 years ago
}
9 years ago
log.Warning("Point server started on port %d", this.port)
9 years ago
return nil
})
9 years ago
if err != nil {
return err
}
9 years ago
for _, detourHandler := range this.idh {
9 years ago
err := detourHandler.Start()
if err != nil {
return err
}
}
return nil
9 years ago
}
9 years ago
9 years ago
// Dispatches a Packet to an OutboundConnection.
// The packet will be passed through the router (if configured), and then sent to an outbound
// connection with matching tag.
func (this *Point) DispatchToOutbound(context app.Context, packet v2net.Packet) ray.InboundRay {
direct := ray.NewRay()
dest := packet.Destination()
dispatcher := this.och
9 years ago
if this.router != nil {
tag, err := this.router.TakeDetour(dest)
if err == nil {
9 years ago
handler, found := this.odh[tag]
if found {
dispatcher = handler
}
}
}
go this.FilterPacketAndDispatch(packet, direct, dispatcher)
return direct
9 years ago
}
func (this *Point) FilterPacketAndDispatch(packet v2net.Packet, link ray.OutboundRay, dispatcher proxy.OutboundConnectionHandler) {
// Filter empty packets
chunk := packet.Chunk()
moreChunks := packet.MoreChunks()
changed := false
for chunk == nil && moreChunks {
changed = true
chunk, moreChunks = <-link.OutboundInput()
}
if chunk == nil && !moreChunks {
close(link.OutboundOutput())
return
}
if changed {
packet = v2net.NewPacket(packet.Destination(), chunk, moreChunks)
}
dispatcher.Dispatch(packet, link)
}