This repository has been archived by the owner on Jan 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
topology.go
146 lines (128 loc) · 4.04 KB
/
topology.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
// Copyright 2019 The Nym Mixnet Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*
Package topology implements all useful topology-related functions
which are used in the code of anonymous messaging system.
*/
package topology
import (
"encoding/base64"
"encoding/json"
"errors"
"io/ioutil"
"net"
"net/http"
"github.com/nymtech/nym-directory/models"
"github.com/nymtech/nym-mixnet/config"
)
// MixPresence defines map containing presence information of all mix nodes in given topology.
type MixPresence []models.MixNodePresence
// MixPresence defines map containing presence information of all providers in given topology.
type ProviderPresence []models.MixProviderPresence
// LayeredMixes defines map of list of mix nodes corresponding to particular layer in given topology.
type LayeredMixes map[uint][]config.MixConfig
const (
DefaultClientHost = "0.0.0.0"
DefaultClientPort = "42"
)
func GetNetworkTopology(endpoint string) (*models.Topology, error) {
resp, err := http.Get(endpoint)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
model := &models.Topology{}
if err := json.Unmarshal(body, model); err != nil {
return nil, err
}
return model, nil
}
// GetMixesPKI returns PKI data for mix nodes, grouped by layer
func GetMixesPKI(mixPresence MixPresence) (LayeredMixes, error) {
mixes := make(LayeredMixes)
for k, v := range mixPresence {
b, err := base64.URLEncoding.DecodeString(v.PubKey)
if err != nil {
continue
}
host, port, err := net.SplitHostPort(v.Host) // TODO: do we want to split them?
if err != nil {
continue
}
newMixEntry := config.MixConfig{
Id: mixPresence[k].PubKey,
Host: host,
Port: port,
PubKey: b,
Layer: uint64(v.Layer),
}
if layerMixes, ok := mixes[v.Layer]; ok {
extendedLayer := append(layerMixes, newMixEntry)
mixes[v.Layer] = extendedLayer
} else {
mixes[v.Layer] = []config.MixConfig{newMixEntry}
}
}
return mixes, nil
}
func ProviderPresenceToConfig(presence models.MixProviderPresence) (config.MixConfig, error) {
b, err := base64.URLEncoding.DecodeString(presence.PubKey)
if err != nil {
return config.MixConfig{}, errors.New("invalid provider presence")
}
host, port, err := net.SplitHostPort(presence.Host) // TODO: do we want to split them?
if err != nil {
return config.MixConfig{}, err
}
return config.NewMixConfig(presence.Host, host, port, b, config.ProviderLayer), nil
}
func RegisteredClientToConfig(client models.RegisteredClient) (config.ClientConfig, error) {
b, err := base64.URLEncoding.DecodeString(client.PubKey)
if err != nil {
return config.ClientConfig{}, errors.New("invalid client information")
}
return config.ClientConfig{
Id: client.PubKey,
Host: DefaultClientHost,
Port: DefaultClientPort,
PubKey: b,
}, nil
}
// GetClientPKI returns a map of the current client PKI from the PKI database
func GetClientPKI(providerPresence ProviderPresence) ([]config.ClientConfig, error) {
var clientsNum int = 0
for _, v := range providerPresence {
clientsNum += len(v.RegisteredClients)
}
clients := make([]config.ClientConfig, 0, clientsNum)
for _, provider := range providerPresence {
providerCfg, err := ProviderPresenceToConfig(provider)
if err != nil {
continue
}
for _, client := range provider.RegisteredClients {
clientCfg, err := RegisteredClientToConfig(client)
if err != nil {
continue
}
clientCfg.Provider = &providerCfg
clients = append(clients, clientCfg)
}
}
return clients, nil
}