From bca94eea263147b8a07f7c8b363c6098de14b831 Mon Sep 17 00:00:00 2001 From: Dustin Willis Webber Date: Thu, 23 Apr 2015 17:26:22 -0400 Subject: [PATCH] clean up --- CHANGELOG.md | 6 +++ daemon.go | 2 +- node.go | 64 ++++++++++++++-------------- node_config.go | 14 +++--- node_database.go | 16 +++---- query.go | 8 ++-- server.go | 96 +++++++++++++++++++++--------------------- settings_database.go | 4 +- user_database.go | 8 ++-- web/public/js/envdb.js | 6 +++ 10 files changed, 118 insertions(+), 106 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ab4859c..51b01df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## 0.4.1 2015-04-18 + + + Add loading indicator for system information + request + + bug fixes + ## 0.4.0 2015-04-18 + Add system information window for node. diff --git a/daemon.go b/daemon.go index 14f397f..5d38267 100644 --- a/daemon.go +++ b/daemon.go @@ -69,7 +69,7 @@ func (daemon *Daemon) StartServer(svr *Server, svrWebPort int) { case sig := <-sigChan: Log.Debug("Go Signal: ", sig) svr.Shutdown() - os.Exit(1) + os.Exit(0) } } diff --git a/node.go b/node.go index 1106bcf..cafe711 100644 --- a/node.go +++ b/node.go @@ -45,12 +45,12 @@ type Message struct { } // Handlers will register all node hnadlers. -func (self *Node) Handlers() { +func (node *Node) Handlers() { handlers := gotalk.NewHandlers() handlers.HandleBufferNotification("die", func(s *gotalk.Sock, name string, b []byte) { KillClient = true - self.Socket.Close() + node.Socket.Close() Connection <- true }) @@ -73,11 +73,11 @@ func (self *Node) Handlers() { handlers.Handle("checkin", func() (Message, error) { var err error - if self.Config.HasCache { - Log.Debugf("Node has cache file. Using cached id %s", self.Config.Cache.Id) + if node.Config.HasCache { + Log.Debugf("Node has cache file. Using cached id %s", node.Config.Cache.Id) - Log.Infof("Connection successful. Id: %s", self.Config.Cache.Id) - self.Id = self.Config.Cache.Id + Log.Infof("Connection successful. Id: %s", node.Config.Cache.Id) + node.Id = node.Config.Cache.Id } else { id, uuerr := uuid.NewV4() @@ -90,10 +90,10 @@ func (self *Node) Handlers() { Log.Debugf("No cache file found. Creating cache file and new id %s", id) Log.Infof("Connection successful. Id: %s", id.String()) - self.Config.Cache.Id = id.String() - self.Id = self.Config.Cache.Id + node.Config.Cache.Id = id.String() + node.Id = node.Config.Cache.Id - self.Config.WriteCache() + node.Config.WriteCache() } has, version := OsQueryInfo() @@ -110,7 +110,7 @@ func (self *Node) Handlers() { } var hostname = "n/a" - var ip = self.Socket.Addr() + var ip = node.Socket.Addr() if os, err := os.Hostname(); err == nil { hostname = os @@ -129,8 +129,8 @@ func (self *Node) Handlers() { rmsg := Message{ Error: err, Data: map[string]interface{}{ - "name": self.Name, - "id": self.Id, + "name": node.Name, + "id": node.Id, "osquery": has, "osquery-version": version, "ip": ip, @@ -142,19 +142,19 @@ func (self *Node) Handlers() { return rmsg, nil }) - self.Socket.Handlers = handlers + node.Socket.Handlers = handlers } // Server will return the server connection string. -func (self *Node) Server() string { - return fmt.Sprintf("%s:%d", self.Host, self.Port) +func (node *Node) Server() string { + return fmt.Sprintf("%s:%d", node.Host, node.Port) } // Connect a node to the server. -func (self *Node) Connect() error { - Log.Infof("Connecting to %s", self.Server()) +func (node *Node) Connect() error { + Log.Infof("Connecting to %s", node.Server()) - s, err := gotalk.Connect("tcp", self.Server(), &tls.Config{ + s, err := gotalk.Connect("tcp", node.Server(), &tls.Config{ InsecureSkipVerify: true, }) @@ -162,23 +162,23 @@ func (self *Node) Connect() error { return err } - self.Socket = s + node.Socket = s - self.Socket.HeartbeatInterval = 20 * time.Second + node.Socket.HeartbeatInterval = 20 * time.Second - self.Socket.OnHeartbeat = func(load int, t time.Time) { + node.Socket.OnHeartbeat = func(load int, t time.Time) { Log.Debugf("Got heartbeat: Load (%d), Time: (%s)", load, t.Format(TimeFormat)) } - self.Socket.CloseHandler = func(s *gotalk.Sock, code int) { + node.Socket.CloseHandler = func(s *gotalk.Sock, code int) { if KillClient { KillClient = false Connection <- true } else { Log.Warnf("Lost connection to server. (Error Code: %d)", code) - RetryCount = self.RetryCount - self.Reconnect() + RetryCount = node.RetryCount + node.Reconnect() } } @@ -186,8 +186,8 @@ func (self *Node) Connect() error { } // Reconnect to the server if connection is lost. -func (self *Node) Reconnect() { - self.Socket.Close() +func (node *Node) Reconnect() { + node.Socket.Close() Log.Warnf("Attempting to reconnect. (Retry Count: %d)", RetryCount) @@ -198,25 +198,25 @@ func (self *Node) Reconnect() { time.Sleep(5 * time.Second) - if err := self.Run(); err != nil { + if err := node.Run(); err != nil { RetryCount-- Log.Error(err) - self.Reconnect() + node.Reconnect() return } - RetryCount = self.RetryCount + RetryCount = node.RetryCount Log.Info("Reconnect successful.") } // Run node, connect and register all handlers. -func (self *Node) Run() error { +func (node *Node) Run() error { - if err := self.Connect(); err != nil { + if err := node.Connect(); err != nil { return err } - self.Handlers() + node.Handlers() <-Connection diff --git a/node_config.go b/node_config.go index b441503..f045c32 100644 --- a/node_config.go +++ b/node_config.go @@ -9,10 +9,10 @@ import ( ) var ( - // Default node path + // DefaultNodePath node path DefaultNodePath = "" - // Default cache file path. Stores the node connection id. + // DefaultCacheFile stores the node connection id. DefaultCacheFile = "" ) @@ -31,7 +31,7 @@ type NodeCache struct { Id string } -// Initialize a new node configuration. +// NewNodeConfig new node configuration. func NewNodeConfig() (*NodeConfig, error) { config := &NodeConfig{} @@ -69,17 +69,17 @@ func NewNodeConfig() (*NodeConfig, error) { return config, err } -// Write node cache to disk. -func (self NodeConfig) WriteCache() error { +// WriteCache Write node cache to disk. +func (config NodeConfig) WriteCache() error { var cache bytes.Buffer e := toml.NewEncoder(&cache) - if err := e.Encode(self.Cache); err != nil { + if err := e.Encode(config.Cache); err != nil { return err } - f, err := os.Create(self.CacheFile) + f, err := os.Create(config.CacheFile) defer f.Close() if err != nil { diff --git a/node_database.go b/node_database.go index fd06234..526e80c 100644 --- a/node_database.go +++ b/node_database.go @@ -5,7 +5,7 @@ import ( "time" ) -// Node Database Table +// NodeDb Database Table for node type NodeDb struct { Id int64 @@ -26,7 +26,7 @@ type NodeDb struct { Updated time.Time `xorm:"UPDATED"` } -// Find all nodes in the database +// AllNodes Return all nodes in the database func AllNodes() ([]*NodeDb, error) { var nodes []*NodeDb err := x.Find(&nodes) @@ -35,7 +35,7 @@ func AllNodes() ([]*NodeDb, error) { } // Update node information in the database -func (self *NodeDb) Update() error { +func (n *NodeDb) Update() error { sess := x.NewSession() defer sess.Close() @@ -43,7 +43,7 @@ func (self *NodeDb) Update() error { return err } - if _, err := sess.Id(self.Id).AllCols().Update(self); err != nil { + if _, err := sess.Id(n.Id).AllCols().Update(n); err != nil { sess.Rollback() return err } @@ -57,7 +57,7 @@ func (self *NodeDb) Update() error { return err } -// Update or create a new node if it doesn't exist. +// NodeUpdateOrCreate Will create a new node if it doesn't exist. func NodeUpdateOrCreate(node *NodeData) (*NodeDb, error) { sess := x.NewSession() defer sess.Close() @@ -124,7 +124,7 @@ func NodeUpdateOrCreate(node *NodeData) (*NodeDb, error) { return a, nil } -// Find node by node id which is also the connection id +// GetNodeByNodeId node by node id which is also the connection id func GetNodeByNodeId(nodeId string) (*NodeDb, error) { Log.Debugf("Looking for node with id: %s", nodeId) @@ -142,7 +142,7 @@ func GetNodeByNodeId(nodeId string) (*NodeDb, error) { } // Delete node from the database. -func (self *NodeDb) Delete() error { +func (n *NodeDb) Delete() error { sess := x.NewSession() defer sess.Close() @@ -150,7 +150,7 @@ func (self *NodeDb) Delete() error { return err } - if _, err := sess.Id(self.Id).Delete(self); err != nil { + if _, err := sess.Id(n.Id).Delete(n); err != nil { sess.Rollback() return err } diff --git a/query.go b/query.go index 2392bc8..904f3b3 100644 --- a/query.go +++ b/query.go @@ -10,11 +10,11 @@ import ( ) const ( - // Min OsQueryi Supported Version + // MinOsQueryVersion Supported osqueryi version MinOsQueryVersion = "1.4.4" ) -// Query wrapper. Holds the raw sql and +// Query Holds the raw sql and // format options to be passed to osqueryi type Query struct { Sql string @@ -135,7 +135,7 @@ func OsQueryInfo() (bool, string) { // Run a query for the node and return its // combinded outpout. -func (self *Query) Run() ([]byte, error) { +func (q *Query) Run() ([]byte, error) { var output []byte binary, lookErr := exec.LookPath("osqueryi") @@ -144,7 +144,7 @@ func (self *Query) Run() ([]byte, error) { return output, lookErr } - items := []string{binary, "--" + self.Format, self.Sql} + items := []string{binary, "--" + q.Format, q.Sql} output, err := exec.Command("/usr/bin/sudo", items...).CombinedOutput() diff --git a/server.go b/server.go index 2063eec..b879494 100644 --- a/server.go +++ b/server.go @@ -70,7 +70,7 @@ func NewServer(port int) (*Server, error) { if sig.String() == "interrupt" { Log.Info("Received Interrupt.") server.Shutdown() - os.Exit(1) + os.Exit(0) } } } @@ -81,7 +81,7 @@ func NewServer(port int) (*Server, error) { // Shutdow the server and tell all connected nodes to // mark themselves as offline. -func (self *Server) Shutdown() { +func (server *Server) Shutdown() { Log.Infof("%s shutting down.", Name) nodes, _ := AllNodes() @@ -98,9 +98,9 @@ func (self *Server) Shutdown() { // When a new node connects add said node to the Server struct Nodes // and process the node checkin. -func (self *Server) onAccept(s *gotalk.Sock) { - self.mu.Lock() - defer self.mu.Unlock() +func (server *Server) onAccept(s *gotalk.Sock) { + server.mu.Lock() + defer server.mu.Unlock() go func() { var resp Message @@ -126,7 +126,7 @@ func (self *Server) onAccept(s *gotalk.Sock) { Os: resp.Data["os"].(string), } - self.Nodes[s] = node + server.Nodes[s] = node if _, err := NodeUpdateOrCreate(node); err != nil { Log.Error("unable to create or update node record") @@ -136,10 +136,10 @@ func (self *Server) onAccept(s *gotalk.Sock) { WebSocketSend("node-update", node) s.CloseHandler = func(s *gotalk.Sock, _ int) { - self.mu.Lock() - defer self.mu.Unlock() + server.mu.Lock() + defer server.mu.Unlock() - node := self.Nodes[s] + node := server.Nodes[s] node.Online = false Log.Infof("Node disconnected. (%s / %s)", node.Name, node.Id) @@ -157,25 +157,25 @@ func (self *Server) onAccept(s *gotalk.Sock) { WebSocketSend("node-update", node) } - delete(self.Nodes, s) + delete(server.Nodes, s) } }() } // Send data to all connected nodes. -func (self *Server) Broadcast(name string, in interface{}) { - self.mu.RLock() - defer self.mu.RUnlock() +func (server *Server) Broadcast(name string, in interface{}) { + server.mu.RLock() + defer server.mu.RUnlock() - for s, _ := range self.Nodes { + for s, _ := range server.Nodes { s.Notify(name, in) } } // Return true if the node is connected and working properly. -func (self *Server) Alive(id string) bool { - node, err := self.GetNodeById(id) +func (server *Server) Alive(id string) bool { + node, err := server.GetNodeById(id) if err != nil { return false @@ -196,8 +196,8 @@ func (self *Server) Alive(id string) bool { } // Disconnect a node from the server. -func (self *Server) Disconnect(id string) error { - node, err := self.GetNodeById(id) +func (server *Server) Disconnect(id string) error { + node, err := server.GetNodeById(id) if err != nil { return err @@ -215,11 +215,11 @@ func (self *Server) Disconnect(id string) error { // * NOTE: This is hacky because xorm has an issue with // sqlite and table locking. Need to move this to a // channel for node db writes and queue them. -func (self *Server) Delete(id string) error { +func (server *Server) Delete(id string) error { Log.Debugf("Deleting node: %s", id) - node, err := self.GetNodeById(id) + node, err := server.GetNodeById(id) if err != nil { @@ -237,7 +237,7 @@ func (self *Server) Delete(id string) error { return err } - err = self.Disconnect(id) + err = server.Disconnect(id) if err != nil { return err @@ -283,9 +283,9 @@ func ProcessResults(data []byte) (bool, []map[string]interface{}, []byte) { // Send a query request to all connected nodes and return // a Response struct -func (self *Server) sendAll(in interface{}) *Response { - self.mu.RLock() - defer self.mu.RUnlock() +func (server *Server) sendAll(in interface{}) *Response { + server.mu.RLock() + defer server.mu.RUnlock() var wg sync.WaitGroup @@ -296,7 +296,7 @@ func (self *Server) sendAll(in interface{}) *Response { var count int - for s, node := range self.Nodes { + for s, node := range server.Nodes { wg.Add(1) go func(s *gotalk.Sock, node *NodeData) { @@ -344,13 +344,13 @@ func (self *Server) sendAll(in interface{}) *Response { } // Send a query request to just one node and return a Response struct -func (self *Server) sendTo(id string, in interface{}) *Response { - self.mu.RLock() - defer self.mu.RUnlock() +func (server *Server) sendTo(id string, in interface{}) *Response { + server.mu.RLock() + defer server.mu.RUnlock() resp := NewResponse() - node, err := self.GetNodeById(id) + node, err := server.GetNodeById(id) if err != nil { resp.Total = 0 @@ -395,20 +395,20 @@ func (self *Server) sendTo(id string, in interface{}) *Response { // Send wraps the sendTo and sendAll functions and returns the Response // struct from the requested send type. -func (self *Server) Send(id string, in interface{}) *Response { +func (server *Server) Send(id string, in interface{}) *Response { if id == "all" { - return self.sendAll(in) + return server.sendAll(in) } - return self.sendTo(id, in) + return server.sendTo(id, in) } // Ask for information from a node by id -func (self *Server) Ask(id, question string) (map[string]interface{}, error) { +func (server *Server) Ask(id, question string) (map[string]interface{}, error) { var data map[string]interface{} - node, err := self.GetNodeById(id) + node, err := server.GetNodeById(id) if err != nil { return data, err @@ -420,9 +420,9 @@ func (self *Server) Ask(id, question string) (map[string]interface{}, error) { } // Fetch a node by id from the Server.Nodes map -func (self *Server) GetNodeById(id string) (*NodeData, error) { +func (server *Server) GetNodeById(id string) (*NodeData, error) { - for _, node := range self.Nodes { + for _, node := range server.Nodes { if node.Id == id { return node, nil } @@ -432,10 +432,10 @@ func (self *Server) GetNodeById(id string) (*NodeData, error) { } // Start the tcp server and register all handlers. -func (self *Server) Run(webPort int) error { - Log.Infof("Starting Server on port %d.", self.Port) +func (server *Server) Run(webPort int) error { + Log.Infof("Starting Server on port %d.", server.Port) - self.Nodes = make(map[*gotalk.Sock]*NodeData) + server.Nodes = make(map[*gotalk.Sock]*NodeData) handlers := gotalk.NewHandlers() @@ -447,8 +447,8 @@ func (self *Server) Run(webPort int) error { Log.Infof("Output %s: %s", name, string(b)) }) - s, err := gotalk.Listen("tcp", fmt.Sprintf(":%d", self.Port), &tls.Config{ - Certificates: []tls.Certificate{self.Config.Cert}, + s, err := gotalk.Listen("tcp", fmt.Sprintf(":%d", server.Port), &tls.Config{ + Certificates: []tls.Certificate{server.Config.Cert}, ClientAuth: tls.NoClientCert, }) @@ -456,19 +456,19 @@ func (self *Server) Run(webPort int) error { return err } - self.Socket = s - self.Socket.HeartbeatInterval = 20 * time.Second + server.Socket = s + server.Socket.HeartbeatInterval = 20 * time.Second - self.Socket.OnHeartbeat = func(load int, t time.Time) { + server.Socket.OnHeartbeat = func(load int, t time.Time) { // Log.Debugf("Got heartbeat: Load (%d), Time: (%s)", load, t.Format(TimeFormat)) } - self.Socket.AcceptHandler = self.onAccept - self.Socket.Handlers = handlers + server.Socket.AcceptHandler = server.onAccept + server.Socket.Handlers = handlers - go NewWebServer(webPort, self) + go NewWebServer(webPort, server) - self.Socket.Accept() + server.Socket.Accept() return nil } diff --git a/settings_database.go b/settings_database.go index 960c2b4..ce1266d 100644 --- a/settings_database.go +++ b/settings_database.go @@ -41,7 +41,7 @@ func DbSettings() (*SettingsDb, error) { } // Update settings in the database -func (self *SettingsDb) Update() error { +func (s *SettingsDb) Update() error { sess := x.NewSession() defer sess.Close() @@ -49,7 +49,7 @@ func (self *SettingsDb) Update() error { return err } - if _, err := sess.Id(self.Id).AllCols().Update(self); err != nil { + if _, err := sess.Id(s.Id).AllCols().Update(s); err != nil { sess.Rollback() return err } diff --git a/user_database.go b/user_database.go index f3617da..d231b2f 100644 --- a/user_database.go +++ b/user_database.go @@ -132,7 +132,7 @@ func (u *UserDb) ValidatePassword(passwd string) bool { } // Update the users information in the database -func (self *UserDb) Update() error { +func (u *UserDb) Update() error { sess := x.NewSession() defer sess.Close() @@ -140,7 +140,7 @@ func (self *UserDb) Update() error { return err } - if _, err := sess.Id(self.Id).AllCols().Update(self); err != nil { + if _, err := sess.Id(u.Id).AllCols().Update(u); err != nil { sess.Rollback() return err } @@ -214,8 +214,8 @@ func FindUserByEmail(email string) (*UserDb, error) { } // Delete a user from the database -func (self *UserDb) Delete() error { - _, err := x.Delete(self) +func (u *UserDb) Delete() error { + _, err := x.Delete(u) if err != nil { return err diff --git a/web/public/js/envdb.js b/web/public/js/envdb.js index 960fe87..a315345 100644 --- a/web/public/js/envdb.js +++ b/web/public/js/envdb.js @@ -880,7 +880,13 @@ jQuery(document).ready(function($) { e.preventDefault(); if (parent.hasClass("online")) { + + Envdb.Loading.start(); + Envdb.Node.Ask(id, "system-information", function(data, err) { + + Envdb.Loading.done(); + if (err) { Envdb.Flash.error(err); return