You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tailscale/natcippool/consensus.go

73 lines
1.8 KiB
Go

package ippool
import (
"net/netip"
"path/filepath"
"strconv"
"github.com/tidwall/uhaha"
"tailscale.com/tailcfg"
)
// StartConsensusMember has this node join the consensus protocol for handing out ip addresses
func StartConsensusMember(nodeID, addr, joinAddr, varRoot string) {
var conf uhaha.Config
conf.Name = "natc"
// TODO if we don't have a varRoot? don't start?
conf.DataDir = filepath.Join(varRoot, "consensusdata")
conf.InitialData = initData()
// TODO is JSON on disk what we want?
conf.UseJSONSnapshots = true
conf.AddWriteCommand("ipcheckout", cmdCheckOut)
conf.AddReadCommand("domainlookup", cmdLookupDomain)
//conf.AddWriteCommand("ipcheckin", cmdCheckIn)
conf.NodeID = nodeID
conf.Addr = addr
if joinAddr != "" && joinAddr != addr {
conf.JoinAddr = joinAddr
}
conf.Flag.Custom = true
uhaha.Main(conf)
}
func initData() *consensusData {
return &consensusData{
// TODO get these from the user somehow
V4Ranges: []netip.Prefix{netip.MustParsePrefix("100.80.0.0/24")},
}
}
func cmdCheckOut(m uhaha.Machine, args []string) (interface{}, error) {
data := m.Data().(*consensusData)
nid, err := strconv.Atoi(args[1]) // TODO probably not really how you get a NodeID from a string
if err != nil {
panic(err)
}
domain := args[2]
return data.checkoutAddrForNode(tailcfg.NodeID(nid), domain)
}
func cmdLookupDomain(m uhaha.Machine, args []string) (interface{}, error) {
data := m.Data().(*consensusData)
nid, err := strconv.Atoi(args[1]) // TODO probably not really how you get a NodeID from a string
if err != nil {
panic(err)
}
addrString := args[2]
addr, err := netip.ParseAddr(addrString)
if err != nil {
panic(err)
}
return data.lookupDomain(tailcfg.NodeID(nid), addr), nil
}
//func cmdCheckIn(m uhaha.Machine, args []string) (interface{}, error) {
//return 0, nil
//}