forked from vuvuzela/vuvuzela
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entry_api.go
147 lines (128 loc) · 2.57 KB
/
entry_api.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
// Types used by the entry server and client
package vuvuzela
import (
"encoding/json"
"fmt"
)
//go:generate stringer -type=MsgType
type MsgType uint8
const (
// from client to server
MsgConvoRequest MsgType = iota
MsgDialRequest
// from server to client
MsgBadRequestError
MsgConvoError
MsgConvoResponse
MsgDialError
MsgDialBucket
MsgAnnounceConvoRound
MsgAnnounceDialRound
)
type Envelope struct {
Type MsgType
Message json.RawMessage
}
func (e *Envelope) Open() (interface{}, error) {
var v interface{}
switch e.Type {
case MsgConvoRequest:
v = new(ConvoRequest)
case MsgDialRequest:
v = new(DialRequest)
case MsgBadRequestError:
v = new(BadRequestError)
case MsgConvoError:
v = new(ConvoError)
case MsgConvoResponse:
v = new(ConvoResponse)
case MsgDialBucket:
v = new(DialBucket)
case MsgAnnounceConvoRound:
v = new(AnnounceConvoRound)
case MsgAnnounceDialRound:
v = new(AnnounceDialRound)
default:
return nil, fmt.Errorf("unknown message type: %d", e.Type)
}
if err := json.Unmarshal(e.Message, v); err != nil {
return nil, fmt.Errorf("json.Unmarshal: %s", err)
}
return v, nil
}
func Envelop(v interface{}) (*Envelope, error) {
var t MsgType
switch v.(type) {
case *ConvoRequest:
t = MsgConvoRequest
case *DialRequest:
t = MsgDialRequest
case *BadRequestError:
t = MsgBadRequestError
case *ConvoError:
t = MsgConvoError
case *ConvoResponse:
t = MsgConvoResponse
case *DialError:
t = MsgDialError
case *DialBucket:
t = MsgDialBucket
case *AnnounceConvoRound:
t = MsgAnnounceConvoRound
case *AnnounceDialRound:
t = MsgAnnounceDialRound
default:
return nil, fmt.Errorf("unsupported message type: %T", v)
}
data, err := json.Marshal(v)
if err != nil {
return nil, fmt.Errorf("json.Marshal: %s", err)
}
return &Envelope{
Type: t,
Message: data,
}, nil
}
type ConvoRequest struct {
Round uint32
Onion []byte
}
type DialRequest struct {
Round uint32
Onion []byte
}
type BadRequestError struct {
Err string
}
func (e *BadRequestError) Error() string {
return e.Err
}
type ConvoError struct {
Round uint32
Err string
}
func (e *ConvoError) Error() string {
return fmt.Sprintf("round c%d: %s", e.Round, e.Err)
}
type ConvoResponse struct {
Round uint32
Onion []byte
}
type DialError struct {
Round uint32
Err string
}
func (e *DialError) Error() string {
return fmt.Sprintf("round d%d: %s", e.Round, e.Err)
}
type DialBucket struct {
Round uint32
Intros [][SizeEncryptedIntro]byte
}
type AnnounceConvoRound struct {
Round uint32
}
type AnnounceDialRound struct {
Round uint32
Buckets uint32
}