forked from v2ray/v2ray-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
point.go
98 lines (80 loc) · 2.65 KB
/
point.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package core
import (
"github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net"
)
var (
inboundFactories = make(map[string]InboundConnectionHandlerFactory)
outboundFactories = make(map[string]OutboundConnectionHandlerFactory)
)
func RegisterInboundConnectionHandlerFactory(name string, factory InboundConnectionHandlerFactory) error {
// TODO check name
inboundFactories[name] = factory
return nil
}
func RegisterOutboundConnectionHandlerFactory(name string, factory OutboundConnectionHandlerFactory) error {
// TODO check name
outboundFactories[name] = factory
return nil
}
// Point is an single server in V2Ray system.
type Point struct {
port uint16
ichFactory InboundConnectionHandlerFactory
ichConfig []byte
ochFactory OutboundConnectionHandlerFactory
ochConfig []byte
}
// NewPoint returns a new Point server based on given configuration.
// The server is not started at this point.
func NewPoint(config PointConfig) (*Point, error) {
var vpoint = new(Point)
vpoint.port = config.Port()
ichFactory, ok := inboundFactories[config.InboundConfig().Protocol()]
if !ok {
panic(log.Error("Unknown inbound connection handler factory %s", config.InboundConfig().Protocol()))
}
vpoint.ichFactory = ichFactory
vpoint.ichConfig = config.InboundConfig().Content()
ochFactory, ok := outboundFactories[config.OutboundConfig().Protocol()]
if !ok {
panic(log.Error("Unknown outbound connection handler factory %s", config.OutboundConfig().Protocol))
}
vpoint.ochFactory = ochFactory
vpoint.ochConfig = config.OutboundConfig().Content()
return vpoint, nil
}
type InboundConnectionHandlerFactory interface {
Create(vp *Point, config []byte) (InboundConnectionHandler, error)
}
type InboundConnectionHandler interface {
Listen(port uint16) error
}
type OutboundConnectionHandlerFactory interface {
Initialize(config []byte) error
Create(VP *Point, firstPacket v2net.Packet) (OutboundConnectionHandler, error)
}
type OutboundConnectionHandler interface {
Start(ray OutboundRay) error
}
// Start starts the Point server, and return any error during the process.
// In the case of any errors, the state of the server is unpredicatable.
func (vp *Point) Start() error {
if vp.port <= 0 {
return log.Error("Invalid port %d", vp.port)
}
vp.ochFactory.Initialize(vp.ochConfig)
inboundConnectionHandler, err := vp.ichFactory.Create(vp, vp.ichConfig)
if err != nil {
return err
}
err = inboundConnectionHandler.Listen(vp.port)
return nil
}
func (p *Point) DispatchToOutbound(packet v2net.Packet) InboundRay {
ray := NewRay()
// TODO: handle error
och, _ := p.ochFactory.Create(p, packet)
_ = och.Start(ray)
return ray
}