Exemple #1
0
func tunnelDisconnectCmd(cmd *cobra.Command, args []string) {
	if len(args) != 1 {
		cmd.Usage()
		os.Exit(1)
	}

	key, err := key.DecodePublic(args[0])
	if err != nil {
		fmt.Println("Error reading server public key,", err)
	}

	c := Connect()

	indexes, err := c.IpTunnel_listConnections()
	if err != nil {
		fmt.Println("Error getting tunnel information,", err)
		os.Exit(1)
	}
	for _, i := range indexes {
		info, err := c.IpTunnel_showConnection(i)
		if err != nil {
			fmt.Println(err)
			continue
		}
		if info.Key.Equal(key) {
			if err = c.IpTunnel_removeConnection(i); err != nil {
				fmt.Println("Failed to remove tunnel,", err)
				os.Exit(1)
			}
			return
		}
	}
	fmt.Println("tunnel not found")
}
Exemple #2
0
func resolve(host string) (hostname, ip string, err error) {
	if keyRegex.MatchString(host) {
		k, err := key.DecodePublic(host)
		// If err != nil, assume host is really a hostname.
		if err == nil {
			ip = k.IP().String()
			host = ip

		}
	}

	if ipRegex.MatchString(host) {
		ip = host

		var ok bool
		rMutex.RLock()
		hostname, ok = rCache[ip]
		rMutex.RUnlock()
		if ok {
			return
		}
		rMutex.Lock()
		defer rMutex.Unlock()
		hostname, ok = rCache[ip]
		if ok {
			return
		}

		if hostnames, err := net.LookupAddr(ip); err == nil {
			for _, h := range hostnames {
				hostname = hostname + h + " "
			}
		} else if Verbose {
			fmt.Fprintf(os.Stderr, "failed to resolve %s, %s\n", ip, err)
		}

		if len(hostname) > 0 {
			rCache[ip] = hostname
		}
		return
	} else {
		hostname = host
		var addrs []string
		if addrs, err = net.LookupHost(host); err != nil {
			return
		}

		for _, addr := range addrs {
			if ipRegex.MatchString(addr) {
				ip = addr
				return
			}
		}
		err = errors.New("no fc::/8 address found")
	}
	return
}
Exemple #3
0
func pubKeyToIPCmd(cmd *cobra.Command, args []string) {
	if len(args) == 0 {
		cmd.Usage()
		os.Exit(1)
	}

	for _, s := range args {
		k, err := key.DecodePublic(s)
		if err != nil {
			fmt.Println("Error converting key:", err)
			os.Exit(1)
		}

		fmt.Println(k.IP())
	}
}
Exemple #4
0
func tunnelConnectCmd(cmd *cobra.Command, args []string) {
	if len(args) != 1 {
		cmd.Usage()
		os.Exit(1)
	}

	key, err := key.DecodePublic(args[0])
	if err != nil {
		fmt.Println("Error reading server public key,", err)
		os.Exit(1)
	}

	c := Connect()

	err = c.IpTunnel_connectTo(key)
	if err != nil {
		fmt.Printf("Errror connecting to %s, %s", key.IP(), err)
		os.Exit(1)
	}
}
Exemple #5
0
func tunnelAllowCmd(cmd *cobra.Command, args []string) {
	if len(args) != 2 {
		cmd.Usage()
		os.Exit(1)
	}
	key, err := key.DecodePublic(args[0])
	if err != nil {
		fmt.Println("Error reading client public key,", err)
		os.Exit(1)
	}
	addr := net.ParseIP(args[1])

	c := Connect()

	err = c.IpTunnel_allowConnection(key, addr)
	if err != nil {
		fmt.Println("Error allowing tunnel,", err)
		os.Exit(1)
	}
}
Exemple #6
0
func peersCmd(cmd *cobra.Command, args []string) {
	if len(args) == 0 {
		showLocalPeers()
		return
	}

	if len(args) > 1 {
		cmd.Usage()
		os.Exit(1)
	}

	_, ip, err := resolve(args[0])
	if err != nil {
		fmt.Fprintf(os.Stderr, "Could not resolve %q.\n", args[0])
		os.Exit(1)
	}

	c := Connect()

	node, err := c.NodeStore_nodeForAddr(ip)
	if err != nil {
		die(err.Error())
	}

	peers, _, err := c.RouterModule_getPeers(node.RouteLabel, 0, "")
	if err != nil {
		die(err.Error())
	}
	for _, s := range peers {
		k, err := key.DecodePublic(s[24:])
		if err != nil {
			fmt.Fprintln(os.Stderr, "received malformed key ", s[24:])
			continue
		}
		if Verbose {
			fmt.Fprintln(os.Stdout, s[:3], s[3:24], s[24:], k.IP())
		} else {
			fmt.Fprintln(os.Stdout, k.IP())
		}
	}
}
Exemple #7
0
	Run: func(cmd *cobra.Command, args []string) {
		if len(args) == 0 {
			cmd.Help()
			os.Exit(1)
		}

		var (
			private *key.Private
			public  *key.Public
			err     error
		)

		for _, arg := range args {
			if len(arg) == 64 {
				private, err = key.DecodePrivate(arg)
				if err == nil {
					public = private.Pubkey()
				}
			} else {
				public, err = key.DecodePublic(arg)
			}
			if err != nil {
				fmt.Fprintln(os.Stderr, "Failed to decode key,", err)
				os.Exit(1)
			}
			fmt.Fprintln(os.Stdout, base256.Braille.EncodeToString(public[:]))
		}

	},
}
Exemple #8
0
func (t *target) trace(c *admin.Conn) (hostTrace *Host, err error) {
	var node *admin.StoreNode
	var addr string
	node, err = c.NodeStore_nodeForAddr(t.addr)
	if err != nil {
		return
	}

	var nodes []*admin.StoreNode
	for err == nil && node.RouteLabel != "0000.0000.0000.0001" {
		nodes = append(nodes, node)
		addr = node.BestParent.IP
		node, err = c.NodeStore_nodeForAddr(addr)
	}
	if err != nil {
		return
	}

	startTime := time.Now().Unix()
	trace := &Trace{Proto: "CJDNS"}

	var ttl int
	for i := len(nodes) - 1; i > -1; i-- {
		ttl++
		node = nodes[i]

		// Ping by path so we don't get RTT for a different route.
		rtt, _, err := c.RouterModule_pingNode(node.RouteLabel, 0)
		if err != nil {
			fmt.Fprintln(os.Stderr, err)
			return nil, err
		}
		if rtt == 0 {
			rtt = 1
		}
		//hostname, _, _ := resolve(p.IP.String())
		pubKey, _ := key.DecodePublic(node.Key)
		ip := pubKey.IP()
		hop := &Hop{
			TTL:    ttl,
			RTT:    rtt,
			IPAddr: ip.String(),
			//Host:   hostname,
		}

		if NmapOutput {
			fmt.Fprintf(os.Stderr, "  %02d.% 4dms %s %s\n", ttl, rtt, node.RouteLabel, ip)
		} else {
			fmt.Fprintf(os.Stdout, "  %02d.% 4dms %s %s\n", ttl, rtt, node.RouteLabel, ip)
		}
		trace.Hops = append(trace.Hops, hop)
	}

	endTime := time.Now().Unix()
	h := &Host{
		StartTime: startTime,
		EndTime:   endTime,
		Status: &Status{
			State:     HostStateUp,
			Reason:    "pingNode",
			ReasonTTL: 56,
		},
		Address: &Address{Addr: t.addr, AddrType: "ipv6"},
		Trace:   trace,
		//Times: &Times{ // Don't know what to do with this element yet.
		//	SRTT:   1,
		//	RTTVar: 1,
		//	To:     1,
		//},
	}

	if t.name != "" {
		h.Hostnames = []*Hostname{&Hostname{Name: t.name, Type: HostnameTypeUser}}
	}
	return h, nil
}