diff --git a/main.go b/main.go index bf8c140..aedc6d5 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "flag" "fmt" "lux/crypto" + "lux/host" "lux/node" "lux/proto" "lux/rpc" @@ -322,8 +323,94 @@ signaling: node.Stop() } -func hostMain() { +type HostCommand struct { + XMLName xml.Name `xml:"command"` + Executable string `xml:",innerxml"` +} +type HostConfig struct { + XMLName xml.Name `xml:"host"` + KeyStore string `xml:"keystore"` + ID string `xml:"id"` + Hostname string `xml:"hostname"` + + Heartbeat int `xml:"heartbeat"` + + Options []struct { + XMLName xml.Name `xml:"option"` + Type string `xml:"type,attr"` + + WAN struct { + XMLName xml.Name `xml:"wan"` + + Method string `xml:"method,attr"` + + Addr4 string `xml:"addr4"` + Addr6 string `xml:"addr6"` + + Command HostCommand `xml:"command"` + } `xml:"wan"` + } `xml:"option"` + + Nodes []struct { + XMLName xml.Name `xml:"node"` + ID string `xml:"id"` + Exterior string `xml:"exterior"` + } `xml:"node"` +} + +func hostMain() { + xmlBytes, err := os.ReadFile(configPath) + if err != nil { + fmt.Fprintf(os.Stderr, "failed to open config: %v\n", err) + os.Exit(1) + } + + var config HostConfig + if err := xml.Unmarshal(xmlBytes, &config); err != nil { + fmt.Fprintf(os.Stderr, "failed to parse host config: %v\n", err) + os.Exit(1) + } + + ks := crypto.NewLuxKeyStore(config.KeyStore) + if err := ks.Load(); err != nil { + fmt.Fprintf(os.Stderr, "failed to load keystore: %v\n", err) + os.Exit(1) + } + + hostId, err := proto.ParseLuxID(config.ID) + if err != nil { + fmt.Fprintf(os.Stderr, "failed to parse host ID: %v\n", err) + os.Exit(1) + } + hostKey, ok := ks.Get(hostId) + if !ok { + fmt.Fprintln(os.Stderr, "host key is not present in keystore!") + os.Exit(1) + } + + if config.Hostname == "" { + fmt.Fprintln(os.Stderr, "no hostname specified!") + os.Exit(1) + } + + // daemonize if needed + if daemonize { + becomeDaemon() + } + + // create host + host := host.NewLuxHost(config.Hostname, hostKey, ks) + + // populate option providers + + // start host + + // start heartbeat timer + + // handle signals in main thread + + // stop host } var rpcPath string