Esempio n. 1
0
// ListNeighs lists the public keys of the neighbors of the node
func (clnt *Client) ListNeighs() ([]natrium.EdDSAPublic, error) {
	req := textprot.TextReq{
		Verb: "NEIGH_LIST",
	}
	resp, err := clnt.execCmd(req)
	var ninf textprot.NeighInfo
	err = ninf.FromString(resp.Blob)
	if err != nil {
		clnt.sok.Close()
		return nil, ErrBadSocket
	}
	// do the checks
	pubkey := directory.DefaultProvider("").PublicKey()
	err = pubkey.Verify(ninf.Json.HashValue(), ninf.Signat)
	if err != nil {
		clnt.sok.Close() // this server is really bad, go away now
		return nil, ErrBadCrypto
	}
	if ninf.Json.Expires < int(time.Now().Unix()) ||
		subtle.ConstantTimeCompare(clnt.CurrentPublic(), ninf.Json.IssuedTo) != 1 {
		clnt.sok.Close()
		return nil, ErrBadCrypto
	}
	// return the val
	var toret []natrium.EdDSAPublic
	for _, v := range ninf.Json.NeighList {
		toret = append(toret, natrium.EdDSAPublic(v))
	}
	return toret, nil
}
Esempio n. 2
0
func (ns *NodeState) do_NEIGH_PUSH(sok io.ReadWriteCloser, req textprot.TextReq) (err error) {
	var info textprot.NeighInfo
	err = info.FromString(req.Blob)
	if err != nil {
		return
	}
	// validate info
	sanctum := directory.DefaultProvider("").PublicKey()
	err = sanctum.Verify(info.Json.HashValue(), info.Signat)
	if err != nil {
		return
	}
	if subtle.ConstantTimeCompare(info.Json.IssuedTo, ns.client.GetPrivate().PublicKey()) != 1 {
		return errors.New("core2core: somebody pushed me a NEIGH_PUSH to the wrong person")
	}
	// pull more info from the directory
	neiz, err := ns.provider.JoinEdge(ns.client)
	if err != nil {
		return
	}
	// return okay
	err = (&textprot.TextReq{Verb: "OKAY"}).WriteTo(sok)
	if err != nil {
		return
	}
	// commit into data structure
	ns.Lock()
	defer ns.Unlock()
	ns.fulNeighs = neiz
	ns.sigNeighs = info
	return
}
Esempio n. 3
0
func initialize() {
	kilog.Info("SAGIRI version %v initializing...", VERSION)
	flag.Parse()
	lol, err := getPublicIP()
	kilog.Info("%v %v", lol, err)

	if isDirectory {
		runDir()
		os.Exit(0)
	}

	switch dirHelper {
	case "internal":
		dirProvider = directory.DefaultProvider("sanctum.sagiri.niwl.io:2378")
	}

	// obtain directory info
	id := directory.NewClient(natrium.EdDSAGenerateKey())
	kilog.Debug("master key: %v", id.GetPrivate().PublicKey())
	getNeighs(id)

	go func() {
		for {
			time.Sleep(time.Minute)
			getNeighs(id)
		}
	}()

	// set up core node
	if onionPort != 0 {
		ip, err := getPublicIP()
		if err != nil {
			panic(err.Error())
		}
		id.SetAddress(&net.TCPAddr{IP: ip, Port: onionPort, Zone: "tcp"})
		dirClient = id
		go runCore(id)
	} else {
		dirClient = id
		startSocks()
		startControl()
		go func() {
			log.Println(http.ListenAndServe("0.0.0.0:6060", nil))
		}()
	}
	kilog.Info("SAGIRI finished initializing.")
}
Esempio n. 4
0
func TestDirectoryCoreEdge(t *testing.T) {
	spinTestDir()
	defer stopTestDir()

	for i := 0; i < 10; i++ {
		//time.Sleep(time.Second)
		clnt := directory.NewClient(natrium.EdDSAGenerateKey())
		prv := directory.DefaultProvider("127.0.0.1:12345")

		addr, _ := net.ResolveTCPAddr("tcp", "127.0.0.1:0")
		__lol, err := net.ListenTCP("tcp", addr)
		if err != nil {
			panic(err.Error())
		}
		clnt.SetAddress(__lol.Addr().(*net.TCPAddr))
		addr = __lol.Addr().(*net.TCPAddr)
		__lol.Close()

		lol, err := kiricom.EasyListen(addr.String(), nil, clnt.GetSecret())
		if err != nil {
			panic(err.Error())
		}
		defer lol.Close()

		state := NewNodeState(prv, clnt)

		go func() {
			for {
				clnt, err := lol.Accept()
				if err != nil {
					return
				}
				go func() {
					defer clnt.Close()
					err := state.HandleClient(clnt)
					if err != nil {
						kilog.Debug(err.Error())
					}
				}()
			}
		}()

		err = prv.JoinCore(clnt)
		if err != nil {
			log.Fatal(err.Error())
		}
	}

	time.Sleep(time.Second * 2)
	clnt := directory.NewClient(natrium.EdDSAGenerateKey())
	prv := directory.DefaultProvider("127.0.0.1:12345")

	neiz, err := prv.JoinEdge(clnt)
	if err != nil {
		panic(err.Error())
	}
	fmt.Println(neiz)

	// route in a circle
	func() {
		spider, err := NewClient(neiz[0])
		if err != nil {
			panic(err.Error())
		}
		defer spider.Destroy()
		for i := 0; i < 11; i++ {
			fmt.Println(spider.curpk)
			lawl, err := spider.ListNeighs()
			if err != nil {
				panic(err.Error())
			}
			err = spider.ConnectNext(lawl[0])
			if err != nil {
				panic(err.Error())
			}
			//time.Sleep(time.Second)
		}
	}()

	fmt.Println("--- ROUTING DONE ---")

	// time to test ambassadors!
	func() {
		alpha, err := NewClient(neiz[0])
		if err != nil {
			panic(err.Error())
		}
		beta, err := NewClient(neiz[0])
		if err != nil {
			panic(err.Error())
		}
		defer alpha.Destroy()
		defer beta.Destroy()

		// alpha registers, beta connects
		id := natrium.EdDSAGenerateKey()
		srv, err := alpha.RegAmbassador(id.PublicKey())
		if err != nil {
			panic(err.Error())
		}
		clnt, err := beta.ConnAmbassador(id.PublicKey())
		if err != nil {
			panic(err.Error())
		}

		// throwaway echo server
		go func() {
			lol, err := srv.Accept()
			if err != nil {
				panic(err.Error())
			}
			defer lol.Close()
			io.Copy(lol, lol)
		}()

		// test with random stuff
		xaxa := make([]byte, 16)
		rand.Read(xaxa)

		sok, err := clnt.Dial()
		if err != nil {
			panic(err.Error())
		}
		sok.Write(xaxa)
		resp := make([]byte, 16)
		io.ReadFull(sok, resp)
		fmt.Printf("%x\n", xaxa)
		fmt.Printf("%x\n", resp)
		if subtle.ConstantTimeCompare(resp, xaxa) != 1 {
			t.Fatal("ambassador broken")
		}
	}()
	fmt.Println("--- AMBASSADOR DONE ---")
}