v2ray-core/app/point/point.go

77 lines
2.3 KiB
Go
Raw Normal View History

2015-10-14 14:51:19 +02:00
package point
2015-09-05 17:48:38 +02:00
import (
2015-09-20 00:50:21 +02:00
"github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net"
2015-10-13 12:27:50 +02:00
"github.com/v2ray/v2ray-core/common/retry"
2015-10-06 23:11:08 +02:00
"github.com/v2ray/v2ray-core/config"
2015-10-14 14:51:19 +02:00
"github.com/v2ray/v2ray-core/proxy"
"github.com/v2ray/v2ray-core/transport/ray"
2015-09-05 17:48:38 +02:00
)
2015-09-12 22:11:54 +02:00
// Point is an single server in V2Ray system.
type Point struct {
2015-10-07 00:30:44 +02:00
port uint16
2015-10-14 14:51:19 +02:00
ich proxy.InboundConnectionHandler
och proxy.OutboundConnectionHandler
2015-09-05 17:48:38 +02:00
}
2015-09-12 22:11:54 +02:00
// NewPoint returns a new Point server based on given configuration.
2015-09-07 12:00:46 +02:00
// The server is not started at this point.
2015-10-06 23:11:08 +02:00
func NewPoint(pConfig config.PointConfig) (*Point, error) {
2015-09-12 22:11:54 +02:00
var vpoint = new(Point)
2015-10-06 23:11:08 +02:00
vpoint.port = pConfig.Port()
2015-09-12 11:51:42 +02:00
2015-10-14 14:51:19 +02:00
ichFactory := proxy.GetInboundConnectionHandlerFactory(pConfig.InboundConfig().Protocol())
if ichFactory == nil {
2015-10-07 10:05:11 +02:00
log.Error("Unknown inbound connection handler factory %s", pConfig.InboundConfig().Protocol())
2015-10-13 13:55:06 +02:00
return nil, config.BadConfiguration
2015-09-12 11:51:42 +02:00
}
2015-10-07 00:30:44 +02:00
ichConfig := pConfig.InboundConfig().Settings(config.TypeInbound)
ich, err := ichFactory.Create(vpoint, ichConfig)
if err != nil {
log.Error("Failed to create inbound connection handler: %v", err)
return nil, err
}
vpoint.ich = ich
2015-09-12 11:51:42 +02:00
2015-10-14 14:51:19 +02:00
ochFactory := proxy.GetOutboundConnectionHandlerFactory(pConfig.OutboundConfig().Protocol())
if ochFactory == nil {
2015-10-07 10:05:11 +02:00
log.Error("Unknown outbound connection handler factory %s", pConfig.OutboundConfig().Protocol())
2015-10-13 13:55:06 +02:00
return nil, config.BadConfiguration
2015-09-12 11:51:42 +02:00
}
2015-10-07 00:30:44 +02:00
ochConfig := pConfig.OutboundConfig().Settings(config.TypeOutbound)
2015-10-14 14:51:19 +02:00
och, err := ochFactory.Create(ochConfig)
2015-10-07 00:30:44 +02:00
if err != nil {
log.Error("Failed to create outbound connection handler: %v", err)
return nil, err
}
vpoint.och = och
2015-09-11 00:24:18 +02:00
2015-09-06 22:10:42 +02:00
return vpoint, nil
2015-09-05 17:48:38 +02:00
}
2015-09-12 22:11:54 +02:00
// Start starts the Point server, and return any error during the process.
2015-09-07 12:00:46 +02:00
// In the case of any errors, the state of the server is unpredicatable.
2015-09-12 22:11:54 +02:00
func (vp *Point) Start() error {
2015-09-12 11:51:42 +02:00
if vp.port <= 0 {
2015-10-07 10:05:11 +02:00
log.Error("Invalid port %d", vp.port)
2015-10-13 13:55:06 +02:00
return config.BadConfiguration
2015-09-06 22:10:42 +02:00
}
2015-09-22 18:11:55 +02:00
2015-10-13 12:27:50 +02:00
return retry.Timed(100 /* times */, 100 /* ms */).On(func() error {
2015-10-14 00:08:52 +02:00
err := vp.ich.Listen(vp.port)
2015-10-14 01:15:29 +02:00
if err == nil {
log.Warning("Point server started on port %d", vp.port)
return nil
}
return err
2015-10-13 12:27:50 +02:00
})
2015-09-06 22:10:42 +02:00
}
2015-09-11 00:24:18 +02:00
2015-10-14 14:51:19 +02:00
func (p *Point) DispatchToOutbound(packet v2net.Packet) ray.InboundRay {
direct := ray.NewRay()
go p.och.Dispatch(packet, direct)
return direct
2015-09-11 00:24:18 +02:00
}