Skip to content

Commit 8a89a6b

Browse files
temp
Signed-off-by: eternal-flame-AD <[email protected]>
1 parent 5abcf5d commit 8a89a6b

File tree

7 files changed

+150
-91
lines changed

7 files changed

+150
-91
lines changed

v2/generated/protobuf/infra.pb.go

Lines changed: 16 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

v2/generated/protobuf/infra_grpc.pb.go

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

v2/go.mod

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,16 @@ module github.com/gotify/plugin-api/v2
33
go 1.24.5
44

55
require (
6-
github.com/gotify/plugin-api v1.0.0
6+
github.com/stretchr/testify v1.3.0
77
google.golang.org/grpc v1.74.2
88
google.golang.org/protobuf v1.36.7
99
)
1010

1111
require (
12-
github.com/gin-contrib/sse v0.0.0-20170109093832-22d885f9ecc7 // indirect
13-
github.com/gin-gonic/gin v1.3.0 // indirect
14-
github.com/golang/protobuf v1.5.4 // indirect
15-
github.com/json-iterator/go v1.1.5 // indirect
16-
github.com/mattn/go-isatty v0.0.4 // indirect
17-
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
18-
github.com/modern-go/reflect2 v1.0.1 // indirect
19-
github.com/ugorji/go/codec v0.0.0-20181209151446-772ced7fd4c2 // indirect
12+
github.com/davecgh/go-spew v1.1.0 // indirect
13+
github.com/pmezard/go-difflib v1.0.0 // indirect
2014
golang.org/x/net v0.40.0 // indirect
2115
golang.org/x/sys v0.33.0 // indirect
2216
golang.org/x/text v0.25.0 // indirect
2317
google.golang.org/genproto/googleapis/rpc v0.0.0-20250528174236-200df99c418a // indirect
24-
gopkg.in/go-playground/validator.v8 v8.18.2 // indirect
25-
gopkg.in/yaml.v2 v2.2.2 // indirect
2618
)

v2/go.sum

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,20 @@
1+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
12
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2-
github.com/gin-contrib/sse v0.0.0-20170109093832-22d885f9ecc7 h1:AzN37oI0cOS+cougNAV9szl6CVoj2RYwzS3DpUQNtlY=
3-
github.com/gin-contrib/sse v0.0.0-20170109093832-22d885f9ecc7/go.mod h1:VJ0WA2NBN22VlZ2dKZQPAPnyWw5XTlK1KymzLKsr59s=
4-
github.com/gin-gonic/gin v1.3.0 h1:kCmZyPklC0gVdL728E6Aj20uYBJV93nj/TkwBTKhFbs=
5-
github.com/gin-gonic/gin v1.3.0/go.mod h1:7cKuhb5qV2ggCFctp2fJQ+ErvciLZrIeoOSOm6mUr7Y=
63
github.com/go-logr/logr v1.4.3 h1:CjnDlHq8ikf6E492q6eKboGOC0T8CDaOvkHCIg8idEI=
74
github.com/go-logr/logr v1.4.3/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
85
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
96
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
10-
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
117
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
128
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
139
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
1410
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
1511
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
1612
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
17-
github.com/gotify/plugin-api v1.0.0 h1:kab40p2TEPLzjmcafOc7JOz75aTsYQyS2PXtElH8xmI=
18-
github.com/gotify/plugin-api v1.0.0/go.mod h1:xZfEyqVK/Zvu3RwA/CtpuiwFmzFDxifrrqMaH9BHnyU=
19-
github.com/json-iterator/go v1.1.5 h1:gL2yXlmiIo4+t+y32d4WGwOjKGYcGOuyrg46vadswDE=
20-
github.com/json-iterator/go v1.1.5/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
21-
github.com/mattn/go-isatty v0.0.4 h1:bnP0vzxcAdeI1zdubAl5PjU6zsERjGZb7raWodagDYs=
22-
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
23-
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
24-
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
25-
github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
26-
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
13+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
2714
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
2815
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
16+
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
2917
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
30-
github.com/ugorji/go/codec v0.0.0-20181209151446-772ced7fd4c2 h1:EICbibRW4JNKMcY+LsWmuwob+CRS1BmdRdjphAm9mH4=
31-
github.com/ugorji/go/codec v0.0.0-20181209151446-772ced7fd4c2/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
3218
go.opentelemetry.io/auto/sdk v1.1.0 h1:cH53jehLUN6UFLY71z+NDOiNJqDdPRaXzTel0sJySYA=
3319
go.opentelemetry.io/auto/sdk v1.1.0/go.mod h1:3wSPjt5PWp2RhlCcmmOial7AvC4DQqZb7a7wCow3W8A=
3420
go.opentelemetry.io/otel v1.36.0 h1:UumtzIklRBY6cI/lllNZlALOF5nNIzJVb16APdvgTXg=
@@ -41,11 +27,8 @@ go.opentelemetry.io/otel/sdk/metric v1.36.0 h1:r0ntwwGosWGaa0CrSt8cuNuTcccMXERFw
4127
go.opentelemetry.io/otel/sdk/metric v1.36.0/go.mod h1:qTNOhFDfKRwX0yXOqJYegL5WRaW376QbB7P4Pb0qva4=
4228
go.opentelemetry.io/otel/trace v1.36.0 h1:ahxWNuqZjpdiFAyrIoQ4GIiAIhxAunQR6MUoKrsNd4w=
4329
go.opentelemetry.io/otel/trace v1.36.0/go.mod h1:gQ+OnDZzrybY4k4seLzPAWNwVBBVlF2szhehOBB/tGA=
44-
golang.org/x/net v0.0.0-20190110200230-915654e7eabc/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
4530
golang.org/x/net v0.40.0 h1:79Xs7wF06Gbdcg4kdCCIQArK11Z1hr5POQ6+fIYHNuY=
4631
golang.org/x/net v0.40.0/go.mod h1:y0hY0exeL2Pku80/zKK7tpntoX23cqL3Oa6njdgRtds=
47-
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
48-
golang.org/x/sys v0.0.0-20190109145017-48ac38b7c8cb/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
4932
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
5033
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
5134
golang.org/x/text v0.25.0 h1:qVyWApTSYLk/drJRO5mDlNYskwQznZmkpV2c8q9zls4=
@@ -56,9 +39,3 @@ google.golang.org/grpc v1.74.2 h1:WoosgB65DlWVC9FqI82dGsZhWFNBSLjQ84bjROOpMu4=
5639
google.golang.org/grpc v1.74.2/go.mod h1:CtQ+BGjaAIXHs/5YS3i473GqwBBa1zGQNevxdeBEXrM=
5740
google.golang.org/protobuf v1.36.7 h1:IgrO7UwFQGJdRNXH/sQux4R1Dj1WAKcLElzeeRaXV2A=
5841
google.golang.org/protobuf v1.36.7/go.mod h1:jduwjTPXsFjZGTmRluh+L6NjiWu7pchiJ2/5YcXBHnY=
59-
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
60-
gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE=
61-
gopkg.in/go-playground/validator.v8 v8.18.2 h1:lFB4DoMU6B626w8ny76MV7VX6W2VHct2GVOI3xgiMrQ=
62-
gopkg.in/go-playground/validator.v8 v8.18.2/go.mod h1:RX2a/7Ha8BgOhfk7j780h4/u/RRjR0eouCJSH80/M2Y=
63-
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
64-
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

v2/protobuf/infra.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ message StorageLoadResponse {
2222
}
2323

2424
service Infra {
25+
rpc WhoAmI(google.protobuf.Empty) returns (Info);
2526
rpc GetServerVersion(google.protobuf.Empty) returns (ServerVersionInfo);
2627
rpc SaveConfig(StorageSaveRequest) returns (google.protobuf.Empty);
2728
rpc LoadConfig(google.protobuf.Empty) returns (StorageLoadResponse);

v2/rpc.go

Lines changed: 69 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,36 +56,59 @@ type ServerVersionInfo struct {
5656
}
5757

5858
type infraServerImpl struct {
59+
server *ServerMux
5960
version ServerVersionInfo
6061
protobuf.UnimplementedInfraServer
6162
}
6263

6364
func (s *infraServerImpl) GetServerVersion(ctx context.Context, req *emptypb.Empty) (*protobuf.ServerVersionInfo, error) {
64-
peer, ok := peer.FromContext(ctx)
65-
if !ok {
66-
return nil, fmt.Errorf("no peer in context")
67-
}
68-
authInfo := peer.AuthInfo.(*infraTlsAuthInfo)
69-
log.Printf("GetServerVersion: server name %s, module name %s", authInfo.TLSInfo.State.ServerName, authInfo.moduleName)
7065
return &protobuf.ServerVersionInfo{
7166
Version: s.version.Version,
7267
Commit: s.version.Commit,
7368
BuildDate: s.version.BuildDate,
7469
}, nil
7570
}
7671

77-
type pluginConnection struct {
72+
func (s *infraServerImpl) WhoAmI(ctx context.Context, req *emptypb.Empty) (*protobuf.Info, error) {
73+
peer, ok := peer.FromContext(ctx)
74+
if !ok {
75+
return nil, fmt.Errorf("no peer in context")
76+
}
77+
authInfo := peer.AuthInfo.(*infraTlsAuthInfo)
78+
return s.server.GetPluginInfo(authInfo.moduleName)
79+
}
80+
81+
type PluginConnection struct {
7882
info *protobuf.Info
7983
conn *grpc.ClientConn
8084
}
8185

8286
type ServerMux struct {
87+
version ServerVersionInfo
8388
tlsClient *EphemeralTLSClient
8489
infraAddr net.Addr
8590
infraListener net.Listener
8691
infraServer *grpc.Server
8792
pluginDNSToModulePath map[string]string
88-
pluginConnections map[string]pluginConnection
93+
pluginConnections map[string]PluginConnection
94+
protobuf.UnimplementedInfraServer
95+
}
96+
97+
func (s *ServerMux) GetServerVersion(ctx context.Context, req *emptypb.Empty) (*protobuf.ServerVersionInfo, error) {
98+
return &protobuf.ServerVersionInfo{
99+
Version: s.version.Version,
100+
Commit: s.version.Commit,
101+
BuildDate: s.version.BuildDate,
102+
}, nil
103+
}
104+
105+
func (s *ServerMux) WhoAmI(ctx context.Context, req *emptypb.Empty) (*protobuf.Info, error) {
106+
peer, ok := peer.FromContext(ctx)
107+
if !ok {
108+
return nil, fmt.Errorf("no peer in context")
109+
}
110+
authInfo := peer.AuthInfo.(*infraTlsAuthInfo)
111+
return s.GetPluginInfo(authInfo.moduleName)
89112
}
90113

91114
type infraTlsCreds struct {
@@ -121,6 +144,7 @@ func (c *infraTlsCreds) ServerHandshake(rawConn net.Conn) (net.Conn, credentials
121144
}, nil
122145
}
123146

147+
// NewServerMux creates a server-side mux with an infra server that handles plugin-to-server calls.
124148
func NewServerMux(info ServerVersionInfo) *ServerMux {
125149
tlsClient, err := NewEphemeralTLSClient()
126150
if err != nil {
@@ -169,33 +193,41 @@ func NewServerMux(info ServerVersionInfo) *ServerMux {
169193
pluginDNSToModulePath: pluginDNSToModulePath,
170194
TransportCredentials: credentials.NewTLS(infraTlsConfig),
171195
}))
172-
protobuf.RegisterInfraServer(infraServer, &infraServerImpl{
173-
version: info,
174-
})
196+
175197
listener, err := newListener()
176198
if err != nil {
177199
panic(err)
178200
}
179-
go infraServer.Serve(listener)
180-
181-
return &ServerMux{
201+
mux := &ServerMux{
202+
version: info,
182203
tlsClient: tlsClient,
183204
infraAddr: listener.Addr(),
184205
infraListener: listener,
185206
infraServer: infraServer,
186207
pluginDNSToModulePath: pluginDNSToModulePath,
187-
pluginConnections: make(map[string]pluginConnection),
208+
pluginConnections: make(map[string]PluginConnection),
188209
}
210+
protobuf.RegisterInfraServer(infraServer, &infraServerImpl{
211+
server: mux,
212+
version: info,
213+
})
214+
215+
go infraServer.Serve(listener)
216+
217+
return mux
189218
}
190219

220+
// InfraAddr returns the address of the infra server for plugin-to-server callbacks.
191221
func (s *ServerMux) InfraAddr() net.Addr {
192222
return s.infraAddr
193223
}
194224

225+
// CACert returns the CA certificate for mutual TLS authentication.
195226
func (s *ServerMux) CACert() *x509.Certificate {
196227
return s.tlsClient.caCert
197228
}
198229

230+
// SignPluginCSR signs a certificate request for a plugin.
199231
func (s *ServerMux) SignPluginCSR(moduleName string, csr *x509.CertificateRequest) ([]byte, error) {
200232
return s.tlsClient.SignPluginCSR(moduleName, csr)
201233
}
@@ -205,19 +237,40 @@ func (s *ServerMux) RegisterPlugin(target string, moduleName string) (*grpc.Clie
205237
if err != nil {
206238
return nil, err
207239
}
240+
if _, exists := s.pluginDNSToModulePath[buildPluginTLSName(moduleName)]; exists {
241+
return nil, fmt.Errorf("plugin %s already registered", moduleName)
242+
}
208243
s.pluginDNSToModulePath[buildPluginTLSName(moduleName)] = moduleName
209244
pluginClient := protobuf.NewPluginClient(grpcConn)
210245
pluginInfo, err := pluginClient.GetPluginInfo(context.Background(), &emptypb.Empty{})
211246
if err != nil {
212247
return nil, err
213248
}
214-
s.pluginConnections[moduleName] = pluginConnection{
249+
s.pluginConnections[moduleName] = PluginConnection{
215250
info: pluginInfo,
216251
conn: grpcConn,
217252
}
218253
return grpcConn, nil
219254
}
220255

256+
// GetPluginInfo returns the info of a plugin.
257+
func (s *ServerMux) GetPluginInfo(moduleName string) (*protobuf.Info, error) {
258+
conn, ok := s.pluginConnections[moduleName]
259+
if !ok {
260+
return nil, fmt.Errorf("plugin %s not registered", moduleName)
261+
}
262+
return conn.info, nil
263+
}
264+
265+
// GetPluginConnection returns the connection to the plugin for Server-to-Plugin calls.
266+
func (s *ServerMux) GetPluginConnection(moduleName string) (*grpc.ClientConn, error) {
267+
conn, ok := s.pluginConnections[moduleName]
268+
if !ok {
269+
return nil, fmt.Errorf("plugin %s not registered", moduleName)
270+
}
271+
return conn.conn, nil
272+
}
273+
221274
func (s *ServerMux) Close() error {
222275
for _, conn := range s.pluginConnections {
223276
conn.conn.Close()

0 commit comments

Comments
 (0)