Skip to content

Commit

Permalink
Combined frontend and velociraptor binaries.
Browse files Browse the repository at this point in the history
Also embed static files in release binary for easy packaging.
  • Loading branch information
scudette committed Aug 3, 2018
1 parent 7ea0712 commit f62e8de
Show file tree
Hide file tree
Showing 16 changed files with 176 additions and 117 deletions.
6 changes: 4 additions & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 15 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,24 @@ LDFLAGS := \
-X www.velocidex.com/golang/velociraptor/config.build_time=$(DATE) \
-X www.velocidex.com/golang/velociraptor/config.commit_hash=$(COMMIT)

# Build templates for all supported operating systems.
# Just regular binaries for local testing. The GUI will be serving
# files from the filesystem.
build:
GOOS=linux GOARCH=amd64 \
go build \
-ldflags "$(LDFLAGS)" \
-o output/velociraptor ./bin/

#zip -r templates/velociraptor_linux_amd64.zip debian/
# Build release binaries. The GUI will embed assets and ship with
# everything in it.
release:
fileb0x gui/b0x.yaml
GOOS=linux GOARCH=amd64 \
go build \
-ldflags "$(LDFLAGS)" \
-tags release \
-o output/velociraptor ./bin/
strip output/velociraptor

clean:
rm -f gui/assets/ab0x.go
40 changes: 40 additions & 0 deletions api/assets.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// +build release

// Build during release - embed all static assets in the binary.
package api

import (
"html/template"
"net/http"
"time"
config "www.velocidex.com/golang/velociraptor/config"
"www.velocidex.com/golang/velociraptor/gui/assets"
)

func install_mux(config_obj *config.Config, mux *http.ServeMux) {
dir := "/static/"
mux.Handle(dir, http.FileServer(static.HTTP))
}

func GetTemplateHandler(config_obj *config.Config) (http.Handler, error) {
data, err := static.ReadFile("/static/templates/index.html")
if err != nil {
return nil, err
}

tmpl, err := template.New("").Parse(string(data))
if err != nil {
return nil, err
}

return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
args := _templateArgs{
Timestamp: time.Now().UTC().UnixNano() / 1000,
Heading: "Heading",
}
err := tmpl.Execute(w, args)
if err != nil {
w.WriteHeader(500)
}
}), nil
}
40 changes: 40 additions & 0 deletions api/assets_filesystem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// +build !release

// Build during development (non release) - serve static assets
// directly from filesystem. We assume we are run from the top level
// directory using go run. e.g.:

// go run bin/*.go frontend test_data/server.config.yaml
package api

import (
"html/template"
"net/http"
"time"
config "www.velocidex.com/golang/velociraptor/config"
"www.velocidex.com/golang/velociraptor/logging"
)

func install_mux(config_obj *config.Config, mux *http.ServeMux) {
logging.NewLogger(config_obj).Info("Will serve files from directory gui/static")
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(
http.Dir("gui/static"))))
}

func GetTemplateHandler(config_obj *config.Config) (http.Handler, error) {
tmpl, err := template.ParseFiles("gui/static/templates/index.html")
if err != nil {
return nil, err
}

return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
args := _templateArgs{
Timestamp: time.Now().UTC().UnixNano() / 1000,
Heading: "Heading",
}
err := tmpl.Execute(w, args)
if err != nil {
w.WriteHeader(500)
}
}), nil
}
45 changes: 7 additions & 38 deletions api/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import (
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"html/template"
"net/http"
"path"
"time"
api_proto "www.velocidex.com/golang/velociraptor/api/proto"
"www.velocidex.com/golang/velociraptor/config"
"www.velocidex.com/golang/velociraptor/logging"
Expand All @@ -29,24 +26,18 @@ func StartHTTPProxy(config_obj *config.Config) error {
mux.Handle("/api/v1/download/", flowResultDownloadHandler(config_obj))
mux.Handle("/api/v1/DownloadHuntResults", huntResultDownloadHandler(config_obj))

// Install static file handler.
if config_obj.AdminUI_document_root != nil {
// FIXME: Check if path exists.
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(
http.Dir(*config_obj.AdminUI_document_root))))
install_mux(config_obj, mux)

h, err := GetTemplateHandler(config_obj, path.Join(
*config_obj.AdminUI_document_root, "templates", "index.html"))
if err != nil {
return err
}
mux.Handle("/index.html", h)
h, err = GetTemplateHandler(config_obj)
if err != nil {
return err
}
mux.Handle("/index.html", h)

return http.ListenAndServe(
fmt.Sprintf("%s:%d",
*config_obj.API_proxy_bind_address,
*config_obj.API_proxy_bind_port),
*config_obj.GUI_bind_address,
*config_obj.GUI_bind_port),
logging.GetLoggingHandler(config_obj)(mux))
}

Expand Down Expand Up @@ -89,25 +80,3 @@ func GetAPIHandler(

return reverse_proxy_mux, nil
}

func GetTemplateHandler(
config_obj *config.Config,
template_path string) (http.Handler, error) {

tmpl, err := template.ParseFiles(path.Join(
*config_obj.AdminUI_document_root, "templates", "index.html"))
if err != nil {
return nil, err
}

return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
args := _templateArgs{
Timestamp: time.Now().UTC().UnixNano() / 1000,
Heading: "Heading",
}
err := tmpl.Execute(w, args)
if err != nil {
w.WriteHeader(500)
}
}), nil
}
36 changes: 0 additions & 36 deletions bin/frontend/frontend.go → bin/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,13 @@ import (
"os/signal"
"sync/atomic"
"time"
"www.velocidex.com/golang/velociraptor/api"
"www.velocidex.com/golang/velociraptor/config"
crypto_proto "www.velocidex.com/golang/velociraptor/crypto/proto"
"www.velocidex.com/golang/velociraptor/logging"
"www.velocidex.com/golang/velociraptor/server"
)

var (
frontend = kingpin.Command("frontend", "Run the frontend.")
config_path = kingpin.Flag("config", "The Configuration file").
Required().String()

api_command = kingpin.Command("api", "Call an API method.")
api_call_name = api_command.Arg("method", "The method name to call.").
Required().String()

healthy int32
)

Expand All @@ -40,29 +31,6 @@ func validateConfig(configuration *config.Config) error {
return nil
}

func main() {
switch kingpin.Parse() {
case "frontend":
config_obj, err := get_config(*config_path)
kingpin.FatalIfError(err, "Unable to load config file")
go func() {
err := api.StartServer(config_obj)
kingpin.FatalIfError(err, "Unable to start API server")
}()
go func() {
err := api.StartHTTPProxy(config_obj)
kingpin.FatalIfError(err, "Unable to start HTTP Proxy server")
}()

start_frontend(config_obj)

case "api":
config_obj, err := get_config(*config_path)
kingpin.FatalIfError(err, "Unable to load config file")
call_api(config_obj, *api_call_name)
}
}

func get_config(config_path string) (*config.Config, error) {
config_obj := config.GetDefaultConfig()
err := config.LoadConfig(config_path, config_obj)
Expand Down Expand Up @@ -240,7 +208,3 @@ func control(server_obj *server.Server) http.Handler {
}
})
}

func call_api(config_obj *config.Config, method string) {

}
31 changes: 27 additions & 4 deletions bin/vraptor.go → bin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import (
"log"
"os"
"strings"
"www.velocidex.com/golang/velociraptor/api"
"www.velocidex.com/golang/velociraptor/utils"
vql_subsystem "www.velocidex.com/golang/velociraptor/vql"
"www.velocidex.com/golang/vfilter"
)

var (
// Command line interface for VQL commands.
query = kingpin.Command("query", "Run a VQL query")
queries = query.Arg("query", "The VQL Query to run.").
Required().Strings()
Expand All @@ -26,9 +28,14 @@ var (
explain = kingpin.Command("explain", "Explain the output from a plugin")
explain_plugin = explain.Arg("plugin", "Plugin to explain").Required().String()

client = kingpin.Command("client", "Run the velociraptor client")
config_path = client.Arg("config", "The client's config file.").String()
show_config = client.Flag("show_config", "Display the client's configuration").Bool()
// Run the client.
client = kingpin.Command("client", "Run the velociraptor client")
client_config_path = client.Arg("config", "The client's config file.").String()
show_config = client.Flag("show_config", "Display the client's configuration").Bool()

// Run the server.
frontend = kingpin.Command("frontend", "Run the frontend.")
fe_config_path = frontend.Arg("config", "The Configuration file").String()
)

func outputJSON(scope *vfilter.Scope, vql *vfilter.VQL) {
Expand Down Expand Up @@ -127,10 +134,11 @@ func doExplain(plugin string) {
func main() {
switch kingpin.Parse() {
case "client":
RunClient()
RunClient(client_config_path)

case "explain":
doExplain(*explain_plugin)

case "query":
env := vfilter.NewDict().
Set("$uploader", &vql_subsystem.FileBasedUploader{*dump_dir})
Expand All @@ -150,10 +158,25 @@ func main() {
outputJSON(scope, vql)
}
}

case "repack":
err := RepackClient(*repack_binary, *repack_config)
if err != nil {
kingpin.FatalIfError(err, "Can not repack client")
}

case "frontend":
config_obj, err := get_config(*fe_config_path)
kingpin.FatalIfError(err, "Unable to load config file")
go func() {
err := api.StartServer(config_obj)
kingpin.FatalIfError(err, "Unable to start API server")
}()
go func() {
err := api.StartHTTPProxy(config_obj)
kingpin.FatalIfError(err, "Unable to start HTTP Proxy server")
}()

start_frontend(config_obj)
}
}
2 changes: 1 addition & 1 deletion bin/velociraptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"www.velocidex.com/golang/velociraptor/http_comms"
)

func RunClient() {
func RunClient(config_path *string) {
kingpin.Parse()

ctx := context.Background()
Expand Down
18 changes: 9 additions & 9 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ type Config struct {
Config_writeback *string `yaml:"Config.writeback,omitempty"`

// GRPC API endpoint.
API_bind_address *string `yaml:"API.bind_address,omitempty"`
API_bind_port *uint32 `yaml:"API.bind_port,omitempty"`
API_proxy_bind_address *string `yaml:"API.proxy_bind_address,omitempty"`
API_proxy_bind_port *uint32 `yaml:"API.proxy_bind_port,omitempty"`
API_bind_address *string `yaml:"API.bind_address,omitempty"`
API_bind_port *uint32 `yaml:"API.bind_port,omitempty"`
GUI_bind_address *string `yaml:"API.proxy_bind_address,omitempty"`
GUI_bind_port *uint32 `yaml:"API.proxy_bind_port,omitempty"`

Frontend_bind_address *string `yaml:"Frontend.bind_address,omitempty"`
Frontend_bind_port *Integer `yaml:"Frontend.bind_port,omitempty"`
Expand All @@ -100,7 +100,7 @@ type Config struct {
Datastore_location *string `yaml:"Datastore.location,omitempty"`

// The Admin UI
AdminUI_document_root *string `yaml:"AdminUI.document_root,omitempty"`
// AdminUI_document_root *string `yaml:"AdminUI.document_root,omitempty"`

// File Store
FileStore_directory *string `yaml:"FileStore.directory,omitempty"`
Expand All @@ -124,10 +124,10 @@ func GetDefaultConfig() *Config {
"127.0.0.1/12", "192.168.0.0/16",
},

API_bind_address: proto.String("localhost"),
API_bind_port: proto.Uint32(8888),
API_proxy_bind_address: proto.String("localhost"),
API_proxy_bind_port: proto.Uint32(8889),
API_bind_address: proto.String("localhost"),
API_bind_port: proto.Uint32(8888),
GUI_bind_address: proto.String("localhost"),
GUI_bind_port: proto.Uint32(8889),

Hunts_last_timestamp: proto.Uint64(0),
}
Expand Down
5 changes: 5 additions & 0 deletions datastore/sqlite.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
// +build sqlite

// An SQLite datastore. Each client has its own database to avoid
// database contention.

// This used to be the default data store but now the default is
// FileBaseDataStore.
package datastore

import (
Expand Down
1 change: 1 addition & 0 deletions gui/assets/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ab0x.go
Loading

0 comments on commit f62e8de

Please sign in to comment.