Skip to content

Commit

Permalink
Real support to hostnames
Browse files Browse the repository at this point in the history
  • Loading branch information
Eduardo committed Jun 28, 2019
1 parent 85823a8 commit ce42ab3
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 28 deletions.
56 changes: 35 additions & 21 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,39 +1,59 @@
package config

import "net"

// Config represents the main config of a node.
type Config struct {
PublicKey string
PrivateKey string
Host string
Port uint16
Server *ServerConfig
PublicKey string // Node public key
PrivateKey string // Node private key
Host string // Node host
Port uint16 // Node port
Server *ServerConfig // List of servers
}

// ServerConfig represents a server configuration.
type ServerConfig struct {
PublicKey string // Server public key
Host string // Server hostname or IP
Port uint16 // Server port
Keys []*KeyConfig // List of keys
}

// KeyConfig represents a key on a server.
type KeyConfig struct {
ID string // Key UUID
KeyShare string // Keyshare
KeyMetaInfo string // Key Metainformation
}

// Returns a server, given its ID
func (config *Config) GetServerByID(id string) *ServerConfig {
if config.Server.PublicKey == id {
return config.Server
}
return nil
}

func (config *Config) GetServerIPs() []string {
// Returns a list of IPs. If a server has a hostname instead of an IP, it resolves it.
func (config *Config) GetServerIPs() ([]string, error) {
ips := make([]string, 1)
ips[0] = config.Server.Host
return ips
// try to parse as IP
ip, err := net.ResolveIPAddr("ip", config.Server.Host)
if err != nil {
return nil, err
}
ips[0] = ip.String()
return ips, nil
}

// Returns the list of public keys of the servers
func (config *Config) GetServerPubKeys() []string {
pubkeys := make([]string, 1)
pubkeys[0] = config.Server.PublicKey
return pubkeys
}

type ServerConfig struct {
PublicKey string
Host string
Port uint16
Keys []*KeyConfig
}

// Returns a key in a server, based on its ID
func (serverConfig *ServerConfig) GetKeyByID(id string) *KeyConfig {
for _, key := range serverConfig.Keys {
if key.ID == id {
Expand All @@ -42,9 +62,3 @@ func (serverConfig *ServerConfig) GetKeyByID(id string) *KeyConfig {
}
return nil
}

type KeyConfig struct {
ID string
KeyShare string
KeyMetaInfo string
}
24 changes: 18 additions & 6 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/pebbe/zmq4"
"github.com/spf13/viper"
"log"
"net"
"sync"
)

Expand All @@ -22,7 +23,7 @@ const TchsmProtocol = "tcp"
type Node struct {
privKey string // The private key for the node, used in ZMQ CURVE Auth.
pubKey string // The public key for the node, used in ZMQ CURVE Auth.
host string // A string representing the IP the node is going to use to listen to requests.
host *net.IPAddr // A string representing the IP the node is going to use to listen to requests.
port uint16 // a int representing the port the node is going to use to listen to requests
config *config.Config // A pointer to the struct which saves the configuration of the node.
context *zmq4.Context // The context used by zmq connections.
Expand All @@ -33,11 +34,14 @@ type Node struct {

// InitNode inits the node using the configuration provided. Returns a started node or an error if the function fails.
func InitNode(config *config.Config) (*Node, error) {

ip, err := net.ResolveIPAddr("ip", config.Host)
if err != nil {
return nil, err
}
node := &Node{
pubKey: config.PublicKey,
privKey: config.PrivateKey,
host: config.Host,
host: ip,
port: config.Port,
config: config,
servers: make([]*Server, 0),
Expand All @@ -48,8 +52,11 @@ func InitNode(config *config.Config) (*Node, error) {
return nil, err
}
node.context = context

zmq4.AuthAllow(TchsmDomain, config.GetServerIPs()...)
ips, err := config.GetServerIPs()
if err != nil {
return nil, err
}
zmq4.AuthAllow(TchsmDomain, ips...)
zmq4.AuthCurveAdd(TchsmDomain, config.GetServerPubKeys()...)

in, err := context.NewSocket(zmq4.ROUTER)
Expand All @@ -68,9 +75,14 @@ func InitNode(config *config.Config) (*Node, error) {
node.socket = in

serverConfig := config.Server

serverIP, err := net.ResolveIPAddr("ip", serverConfig.Host)
if err != nil {
return nil, err
}
server := &Server{
pubKey: serverConfig.PublicKey,
host: serverConfig.Host,
host: serverIP,
port: serverConfig.Port,
client: node,
channel: make(chan *message.Message),
Expand Down
3 changes: 2 additions & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import (
"github.com/niclabs/tcrsa"
"github.com/pebbe/zmq4"
"log"
"net"
)

// Server represents the connection with the Distributed TCHSM server.
// It saves its connection values, its public key, and the keyshares and keymetainfo sent by the server.
type Server struct {
host string // IP where the server is listening.
host *net.IPAddr // IP where the server is listening.
port uint16 // Port where the server is listening.
pubKey string // Public key of the server. Used for SMQ CURVE auth.
keys map[string]*Key // Dictionary with key shares created by this server.
Expand Down

0 comments on commit ce42ab3

Please sign in to comment.