Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
djcas9 committed Apr 23, 2015
1 parent 15bbae2 commit bca94ee
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 106 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
64 changes: 32 additions & 32 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
})

Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -142,52 +142,52 @@ 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,
})

if err != nil {
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()
}
}

return nil
}

// 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)

Expand All @@ -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

Expand Down
14 changes: 7 additions & 7 deletions node_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
)

Expand All @@ -31,7 +31,7 @@ type NodeCache struct {
Id string
}

// Initialize a new node configuration.
// NewNodeConfig new node configuration.
func NewNodeConfig() (*NodeConfig, error) {
config := &NodeConfig{}

Expand Down Expand Up @@ -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 {
Expand Down
16 changes: 8 additions & 8 deletions node_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"time"
)

// Node Database Table
// NodeDb Database Table for node
type NodeDb struct {
Id int64

Expand All @@ -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)
Expand All @@ -35,15 +35,15 @@ 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()

if err := sess.Begin(); err != nil {
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
}
Expand All @@ -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()
Expand Down Expand Up @@ -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)

Expand All @@ -142,15 +142,15 @@ 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()

if err := sess.Begin(); err != nil {
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
}
Expand Down
8 changes: 4 additions & 4 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand All @@ -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()

Expand Down
Loading

0 comments on commit bca94ee

Please sign in to comment.