// readHostFile will read the host file // HOSTNAME PUBLICKEY // for each line. and returns the whole set and any errror if any are found. func readHostFile(file string) ([]string, []string, error) { // open it up hostFile, err := os.Open(file) if err != nil { return nil, nil, err } // Then read it up hosts := make([]string, 0) pubs := make([]string, 0) scanner := bufio.NewScanner(hostFile) ln := 0 for scanner.Scan() { line := scanner.Text() ln += 1 spl := strings.Split(line, " ") if len(spl) != 2 { return nil, nil, errors.New(fmt.Sprintf("Hostfile misformatted at line %s", ln)) } // add it HOSTS -> PUBLIC KEY h, err := cliutils.VerifyPort(spl[0], conode.DefaultPort) if err != nil { dbg.Fatal("Error reading address in host file:", spl[0], err) } hosts = append(hosts, h) pubs = append(pubs, spl[1]) } dbg.Lvl1("Read the hosts files:", ln, "entries") return hosts, pubs, nil }
// NewPeer returns a peer that can be used to set up // connections. func NewPeer(address string, conf *app.ConfigConode) *Peer { suite := app.GetSuite(conf.Suite) var err error // make sure address has a port or insert default one address, err = cliutils.VerifyPort(address, DefaultPort) if err != nil { dbg.Fatal(err) } // For retro compatibility issues, convert the base64 encoded key into hex // encoded keys.... convertTree(suite, conf.Tree) // Add our private key to the tree (compatibility issues again with graphs/ // lib) addPrivateKey(suite, address, conf) // load the configuration dbg.Lvl3("loading configuration") var hc *graphs.HostConfig opts := graphs.ConfigOptions{ConnType: "tcp", Host: address, Suite: suite} hc, err = graphs.LoadConfig(conf.Hosts, conf.Tree, suite, opts) if err != nil { dbg.Fatal(err) } // Listen to stamp-requests on port 2001 node := hc.Hosts[address] peer := &Peer{ conf: conf, Node: node, RLock: sync.Mutex{}, CloseChan: make(chan bool, 5), Hostname: address, } // Start the cothority-listener on port 2000 err = hc.Run(true, sign.MerkleTree, address) if err != nil { dbg.Fatal(err) } go func() { err := peer.Node.Listen() dbg.Lvl3("Node.listen quits with status", err) peer.CloseChan <- true peer.Close() }() return peer }
// ForceExit connects to the stamp-port of the conode and asks him to exit func ForceExit(address string) { add, err := cliutils.VerifyPort(address, conode.DefaultPort+1) if err != nil { dbg.Fatal("Couldn't convert", address, "to a IP:PORT") } conn := coconet.NewTCPConn(add) err = conn.Connect() if err != nil { dbg.Fatal("Error when getting the connection to the host:", err) } dbg.Lvl1("Connected to", add) msg := &conode.TimeStampMessage{ Type: conode.StampExit, } dbg.Lvl1("Asking to exit") err = conn.PutData(msg) if err != nil { dbg.Fatal("Couldn't send exit-message to server:", err) } }
// KeyGeneration will generate a fresh public / private key pair // and write those down into two separate files func KeyGeneration(key, address string) { if address == "" { dbg.Fatal("You must call keygen with ipadress !") } address, err := cliutils.VerifyPort(address, conode.DefaultPort) dbg.Lvl1("Address is", address) if err != nil { dbg.Fatal(err) } // gen keypair kp := cliutils.KeyPair(suite) // Write private if err := cliutils.WritePrivKey(suite, namePriv(key), kp.Secret); err != nil { dbg.Fatal("Error writing private key file:", err) } // Write public if err := cliutils.WritePubKey(suite, namePub(key), kp.Public, address); err != nil { dbg.Fatal("Error writing public key file:", err) } dbg.Lvl1("Keypair generated and written to", namePriv(key), "/", namePub(key)) }