-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnode.go
More file actions
132 lines (115 loc) · 3.19 KB
/
node.go
File metadata and controls
132 lines (115 loc) · 3.19 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package gozargah_node_bridge
import (
"errors"
"github.com/google/uuid"
"github.com/pasarguard/node_bridge/common"
"github.com/pasarguard/node_bridge/controller"
"github.com/pasarguard/node_bridge/rest"
"github.com/pasarguard/node_bridge/rpc"
)
type PasarGuardNode interface {
Start(string, common.BackendType, []*common.User, uint64) error
Stop()
NodeVersion() string
CoreVersion() string
SyncUsers(users []*common.User) error
Info() (*common.BaseInfoResponse, error)
GetSystemStats() (*common.SystemStatsResponse, error)
GetBackendStats() (*common.BackendStatsResponse, error)
GetStats(reset bool, name string, statType common.StatType) (*common.StatResponse, error)
GetUserOnlineStat(string) (*common.OnlineStatResponse, error)
GetUserOnlineIpList(string) (*common.StatsOnlineIpListResponse, error)
Health() controller.Health
UpdateUser(*common.User)
Logs() (chan string, error)
}
type NodeProtocol string
const (
GRPC NodeProtocol = "GRPC"
REST NodeProtocol = "REST"
)
// NodeOptions holds the configuration for creating a new node
type NodeOptions struct {
address string
port int
serverCA []byte
apiKey uuid.UUID
extra map[string]interface{}
nodeProtocol NodeProtocol
logChanSize int
}
// NodeOption is a function type for configuring NodeOptions
type NodeOption func(*NodeOptions) error
// WithPort sets the port for the node
func WithPort(port int) NodeOption {
return func(opts *NodeOptions) error {
if port <= 0 {
return errors.New("port must be greater than 0")
}
opts.port = port
return nil
}
}
// WithServerCA sets the server CA certificate
func WithServerCA(serverCA []byte) NodeOption {
return func(opts *NodeOptions) error {
opts.serverCA = serverCA
return nil
}
}
// WithAPIKey sets the API key
func WithAPIKey(apiKey uuid.UUID) NodeOption {
return func(opts *NodeOptions) error {
opts.apiKey = apiKey
return nil
}
}
// WithExtra sets extra configuration parameters
func WithExtra(extra map[string]interface{}) NodeOption {
return func(opts *NodeOptions) error {
opts.extra = extra
return nil
}
}
func WithLogChannelSize(size int) NodeOption {
return func(opts *NodeOptions) error {
if size <= 0 {
opts.logChanSize = 1000
} else {
opts.logChanSize = size
}
return nil
}
}
// New creates a new node with the given address, protocol, and options
func New(address string, nodeProtocol NodeProtocol, options ...NodeOption) (PasarGuardNode, error) {
if address == "" {
return nil, errors.New("address is empty")
}
// Initialize options with defaults
opts := &NodeOptions{
address: address,
nodeProtocol: nodeProtocol,
extra: make(map[string]interface{}),
}
// Apply all provided options
for _, option := range options {
if err := option(opts); err != nil {
return nil, err
}
}
var node PasarGuardNode
var err error
switch nodeProtocol {
case GRPC:
node, err = rpc.New(opts.address, opts.port, opts.serverCA, opts.apiKey, opts.logChanSize, opts.extra)
case REST:
node, err = rest.New(opts.address, opts.port, opts.serverCA, opts.apiKey, opts.logChanSize, opts.extra)
default:
return nil, errors.New("unknown node protocol")
}
if err != nil {
return nil, err
}
return node, nil
}