-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
151 lines (132 loc) · 4.17 KB
/
main.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* ORBIT - Interlink Remote Applications
*
* The MIT License (MIT)
*
* Copyright (c) 2020 Roland Singer <roland.singer[at]desertbit.com>
* Copyright (c) 2020 Sebastian Borchers <sebastian[at]desertbit.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package main
import (
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"encoding/pem"
"fmt"
"log"
"math/big"
"time"
"github.com/desertbit/orbit/examples/simple/hello"
olog "github.com/desertbit/orbit/pkg/hook/log"
"github.com/desertbit/orbit/pkg/service"
"github.com/desertbit/orbit/pkg/transport"
"github.com/desertbit/orbit/pkg/transport/quic"
)
func main() {
tr, err := quic.NewTransport(&quic.Options{
TLSConfig: generateTLSConfig(),
})
if err != nil {
log.Fatalln(err)
}
s, err := hello.NewService(&ServiceHandler{},
&service.Options{
ListenAddr: ":1122",
Transport: tr,
Hooks: service.Hooks{
olog.ServiceHook(),
},
})
if err != nil {
log.Fatalln(err)
}
defer s.Close()
err = s.Run()
if err != nil {
log.Fatalln(err)
}
}
func generateTLSConfig() *tls.Config {
key, err := rsa.GenerateKey(rand.Reader, 1024)
if err != nil {
panic(err)
}
template := x509.Certificate{SerialNumber: big.NewInt(1)}
certDER, err := x509.CreateCertificate(rand.Reader, &template, &template, &key.PublicKey, key)
if err != nil {
panic(err)
}
keyPEM := pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key)})
certPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: certDER})
tlsCert, err := tls.X509KeyPair(certPEM, keyPEM)
if err != nil {
panic(err)
}
return &tls.Config{
Certificates: []tls.Certificate{tlsCert},
NextProtos: []string{"orbit-simple-example"},
}
}
// #############
type ServiceHandler struct{}
func (s *ServiceHandler) SayHi(ctx service.Context, arg hello.SayHiArg) (ret hello.SayHiRet, err error) {
fmt.Printf("handler: SayHi, %s, %s\n", arg.Name, arg.Ts.String())
ret = hello.SayHiRet{Res: []int{1, 2, 3}}
return
}
func (s *ServiceHandler) Test(ctx service.Context, arg hello.TestArg) (ret hello.TestRet, err error) {
fmt.Printf("handler: Test, %s\n", arg.S)
ret = hello.TestRet{Name: "horst", Ts: time.Now()}
return
}
func (s *ServiceHandler) ClockTime(ctx service.Context, ret *hello.ClockTimeServiceStream) error {
for i := 0; i < 3; i++ {
err := ret.Write(hello.ClockTimeRet{Ts: time.Now()})
if err != nil {
fmt.Printf("ERROR handler.ClockTime: %v\n", err)
return err
}
}
return nil
}
func (s *ServiceHandler) Lul(ctx service.Context, stream transport.Stream) {
panic("implement me")
}
func (s *ServiceHandler) TimeStream(ctx service.Context, arg *hello.TimeStreamServiceStream) error {
panic("implement me")
}
func (s *ServiceHandler) Bidirectional(ctx service.Context, stream *hello.BidirectionalServiceStream) error {
for i := 0; i < 3; i++ {
a, err := stream.Read()
if err != nil {
fmt.Printf("ERROR handler.Bidirectional read: %v\n", err)
return err
}
fmt.Printf("Question: %s?\n", a.Question)
err = stream.Write(hello.BidirectionalRet{Answer: "42"})
if err != nil {
fmt.Printf("ERROR handler.Bidirectional write: %v\n", err)
return err
}
}
return nil
}