From 7579fd20db8ba6b40f93139a6256cb25edd112b4 Mon Sep 17 00:00:00 2001 From: Boernsman <5207214+Boernsman@users.noreply.github.comi> Date: Wed, 3 Jul 2024 08:57:42 +0200 Subject: [PATCH] Initial commit --- .github/workflows/ci.yaml | 31 ++++ .github/workflows/release.yaml | 35 +++++ .gitignore | 1 + README.md | 5 + build.sh | 45 ++++++ example/config.yaml | 8 + src/connection/agent_module.go | 69 +++++++++ src/connection/api.go | 65 ++++++++ src/connection/connection.go | 110 ++++++++++++++ src/go.mod | 33 ++++ src/go.sum | 79 ++++++++++ src/main.go | 144 ++++++++++++++++++ src/models/models.go | 37 +++++ src/plugins/build_plugins.sh | 43 ++++++ src/plugins/plugin_camera/plugin_camera.go | 41 +++++ src/plugins/plugin_serial/plugin_serial.go | 166 +++++++++++++++++++++ src/shared/logging.go | 11 ++ src/shared/plugin.go | 9 ++ src/utils/config.go | 105 +++++++++++++ src/utils/plugins.go | 55 +++++++ 20 files changed, 1092 insertions(+) create mode 100644 .github/workflows/ci.yaml create mode 100644 .github/workflows/release.yaml create mode 100644 .gitignore create mode 100644 README.md create mode 100755 build.sh create mode 100644 example/config.yaml create mode 100644 src/connection/agent_module.go create mode 100644 src/connection/api.go create mode 100644 src/connection/connection.go create mode 100644 src/go.mod create mode 100644 src/go.sum create mode 100644 src/main.go create mode 100644 src/models/models.go create mode 100755 src/plugins/build_plugins.sh create mode 100644 src/plugins/plugin_camera/plugin_camera.go create mode 100644 src/plugins/plugin_serial/plugin_serial.go create mode 100644 src/shared/logging.go create mode 100644 src/shared/plugin.go create mode 100644 src/utils/config.go create mode 100644 src/utils/plugins.go diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 0000000..64bd09b --- /dev/null +++ b/.github/workflows/ci.yaml @@ -0,0 +1,31 @@ +name: Build and Upload + +on: + push: + branches: + - main + pull_request: + branches: + - main + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version: 1.22 + + - name: Build + run: ./build.sh + + - name: Archive production artifacts + uses: actions/upload-artifact@v4 + with: + name: bon-voyage-agent + path: build diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml new file mode 100644 index 0000000..3aa431b --- /dev/null +++ b/.github/workflows/release.yaml @@ -0,0 +1,35 @@ +name: Manual Release Upload + +on: + workflow_dispatch: + +jobs: + + upload: + runs-on: ubuntu-latest + needs: build + + steps: + - name: Download artifacts for amd64 + uses: actions/download-artifact@v3 + with: + name: ${{ github.event.repository.name }}-amd64 + + - name: Download artifacts for arm64 + uses: actions/download-artifact@v3 + with: + name: ${{ github.event.repository.name }}-arm64 + + - name: Set up GitHub CLI + uses: cli/cli-action@v2 + + - name: Get the latest tag + id: get_tag + run: echo "::set-output name=tag::$(git describe --tags `git rev-list --tags --max-count=1`)" + + - name: Upload artifacts to GitHub Packages + run: | + gh release create ${{ steps.get_tag.outputs.tag }} bin/${{ github.event.repository.name }}-amd64 --title "${{ steps.get_tag.outputs.tag }}" --notes "Release ${{ steps.get_tag.outputs.tag }}" --target main + gh release upload ${{ steps.get_tag.outputs.tag }} bin/${{ github.event.repository.name }}-arm64 --clobber + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..567609b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..145ae99 --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +# Bon Voyage Agent + +## Usage + + diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..de69b6c --- /dev/null +++ b/build.sh @@ -0,0 +1,45 @@ +#!/usr/bin/env bash + +# Exit immediately if a command exits with a non-zero status +set -e + +# Variables +BASEDIR=$(dirname $(realpath "$0")) +echo "Base Dir: $BASEDIR" +TARGET="bon-voyage-agent" +SRC_DIR="$BASEDIR/src" +PLUGIN_SRC_DIR="$BASEDIR/src/plugins" +BUILD_DIR="$BASEDIR/build" +PLUGIN_BUILD_DIR="$BUILD_DIR/plugins" + +# Check if Go is installed +if ! [ -x "$(command -v go)" ]; then + echo "Error: Go is not installed." >&2 + exit 1 +fi + +# Determine the operating system +OS="$(uname -s)" +case "$OS" in + Linux*) GOOS="linux";; + Darwin*) GOOS="darwin";; + *) echo "Unsupported OS: $OS"; exit 1;; +esac + +# Clean previous builds +echo "Cleaning previous builds..." +rm -f $BUILD_DIR/$TARGET + +# Build the agent +echo "Building agent ..." +cd $SRC_DIR + +GIT_HASH=$(git rev-parse HEAD) +go build -o $BUILD_DIR/$TARGET -ldflags="-X main.Commit=$GIT_HASH" +cd $BASEDIR + +# Build the plugins +cd $PLUGIN_SRC_DIR +./build_plugins.sh $PLUGIN_BUILD_DIR +cd $BASEDIR +echo "Build finished. See README.md for usage." diff --git a/example/config.yaml b/example/config.yaml new file mode 100644 index 0000000..44e702a --- /dev/null +++ b/example/config.yaml @@ -0,0 +1,8 @@ +# Bon Voyage Agent +agent: + name: "Voyager" + id: "60F8A8EF-0D07-4353-B238-A4D568100E3B" +server: + host: "192.168.1.148" + port: 6666 + key: "" diff --git a/src/connection/agent_module.go b/src/connection/agent_module.go new file mode 100644 index 0000000..bbfe0be --- /dev/null +++ b/src/connection/agent_module.go @@ -0,0 +1,69 @@ +package connection + +import ( + "bon-voyage-agent/models" + "encoding/json" + "fmt" +) + +const pluginName = "serial" + +// Implements interface Plugin +type PluginAgent struct { + Name string + Information models.AgentInformation + Capabilities []string + Plugins []string +} + +func (p PluginAgent) Init(params any) string { + + switch v := params.(type) { + case models.AgentInformation: + PluginInstance.Information = v + default: + fmt.Printf("Invalid plugin params") + } + p.Name = pluginName + return p.Name +} + +func AgentCall(request models.RPCRequest, response *models.RPCResponse) { + + switch request.Method { + case "agent_get_information": + i, err := json.Marshal(PluginInstance.Information) + if err != nil { + response.Error = err.Error() + return + } + response.Result = string(i) + case "agent_set_name": + var name models.SetNameMethod + err := json.Unmarshal(request.Params, &name) + if err != nil { + response.Error = err.Error() + } + PluginInstance.Information.Name = name.Name + + case "agent_get_capabilities": + i, err := json.Marshal(PluginInstance.Capabilities) + if err != nil { + response.Error = err.Error() + return + } + response.Result = string(i) + case "agent_get_plugins": + i, err := json.Marshal(PluginInstance.Plugins) + if err != nil { + response.Error = err.Error() + return + } + response.Result = string(i) + default: + response.Error = "Unknown method" + } +} + +// Exported symbol +var PluginInstance PluginAgent diff --git a/src/connection/api.go b/src/connection/api.go new file mode 100644 index 0000000..15a4e59 --- /dev/null +++ b/src/connection/api.go @@ -0,0 +1,65 @@ +package connection + +import ( + "bon-voyage-agent/models" + "encoding/json" + "fmt" + "strings" +) + +type Handler func(models.RPCRequest, *models.RPCResponse) + +type Route struct { + handler Handler +} + +func NewRoute() *Route { + return &Route{} +} + +type Router struct { + NotFoundHandler Handler + namedRoutes map[string]*Route +} + +func NewRouter() *Router { + r := Router{namedRoutes: make(map[string]*Route)} + r.HandleFunc("agent", AgentCall) + return &r +} + +func (r *Router) HandleFunc(name string, f func(request models.RPCRequest, response *models.RPCResponse)) *Route { + route := &Route{handler: f} + r.namedRoutes[name] = route + return route +} + +func (r *Router) ParseMessage(data []byte) ([]byte, error) { + + var request models.RPCRequest + err := json.Unmarshal(data, &request) + if err != nil { + return nil, fmt.Errorf("unmarshal error: %v", err) + } + if request.Jsonrpc != "2.0" { + return nil, fmt.Errorf("jsonrpc field not '2.0'") + } + + response := models.RPCResponse{ + Jsonrpc: "2.0", + ID: request.ID, + } + if len(strings.Split(request.Method, "_")) == 0 { + return nil, fmt.Errorf("method malformed") + } + routeName := strings.Split(request.Method, "_")[0] + + route := r.namedRoutes[routeName] + route.handler(request, &response) + + resBytes, err := json.Marshal(response) + if err != nil { + return nil, fmt.Errorf("marshal error: %v", err) + } + return resBytes, nil +} diff --git a/src/connection/connection.go b/src/connection/connection.go new file mode 100644 index 0000000..31b4503 --- /dev/null +++ b/src/connection/connection.go @@ -0,0 +1,110 @@ +package connection + +import ( + "fmt" + "net/http" + "net/url" + + "github.com/gorilla/websocket" +) + +type Connection struct { + Host string + Port string + Name string + Uuid string + Key string + Token string +} + +func (c Connection) serverUrl(path string) *url.URL { + return &url.URL{ + Scheme: "ws", + Host: c.Host + ":" + c.Port, + Path: path, + } +} + +func (c Connection) header() http.Header { + h := http.Header{} + h.Add("X-Device-UUID", c.Uuid) + h.Add("X-Device-Name", c.Name) + if c.Key != "" { + h.Add("X-Device-Api-Key", c.Key) + } + return h +} + +func (c Connection) ConnectDataSocket() (*websocket.Conn, error) { + + d := websocket.DefaultDialer + d.Subprotocols = []string{"serial-tunnel-v1"} + + socket, _, err := d.Dial(c.serverUrl("/api/device/data").String(), c.header()) + if err != nil { + return nil, fmt.Errorf("dial: %v", err) + } + + return socket, nil + + // done := make(chan struct{}) + + // go func() { + // defer close(done) + // for { + // _, message, err := socket.ReadMessage() + // if err != nil { + // if websocket.IsCloseError(err, websocket.CloseNormalClosure) { + // return + // } + // fmt.Println("read:", err) + // return + // } + // fmt.Printf("Received: %s\n", message) + // } + // }() + + // go func() { + // scanner := bufio.NewScanner(os.Stdin) + // for scanner.Scan() { + // text := scanner.Text() + // err := socket.WriteMessage(websocket.TextMessage, []byte(text)) + // if err != nil { + // fmt.Println("write:", err) + // return + // } + // } + // if err := scanner.Err(); err != nil { + // fmt.Println("Error reading from stdin:", err) + // } + // }() + + // select { + // case <-done: + // fmt.Println("WebSocket connection closed") + // case <-interrupt: + // fmt.Println("\nInterrupt signal received, closing connection...") + // err := socket.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, "")) + // if err != nil { + // fmt.Println("write close:", err) + // return err + // } + // select { + // case <-done: + // case <-time.After(time.Second): + // } + // } +} + +func (c Connection) ConnectConfigSocket() (*websocket.Conn, error) { + + d := websocket.DefaultDialer + d.Subprotocols = []string{"config-tunnel-v1"} + + socket, _, err := d.Dial(c.serverUrl("/api/device/config").String(), c.header()) + if err != nil { + return nil, fmt.Errorf("dial: %v", err) + } + + return socket, nil +} diff --git a/src/go.mod b/src/go.mod new file mode 100644 index 0000000..fbb746e --- /dev/null +++ b/src/go.mod @@ -0,0 +1,33 @@ +module bon-voyage-agent + +go 1.22 + +require ( + github.com/google/uuid v1.6.0 + github.com/gorilla/websocket v1.5.3 + github.com/spf13/viper v1.19.0 + go.bug.st/serial v1.6.2 +) + +require ( + github.com/creack/goselect v0.1.2 // indirect + github.com/fsnotify/fsnotify v1.7.0 // indirect + github.com/hashicorp/hcl v1.0.0 // indirect + github.com/magiconair/properties v1.8.7 // indirect + github.com/mitchellh/mapstructure v1.5.0 // indirect + github.com/pelletier/go-toml/v2 v2.2.2 // indirect + github.com/sagikazarmark/locafero v0.4.0 // indirect + github.com/sagikazarmark/slog-shim v0.1.0 // indirect + github.com/sourcegraph/conc v0.3.0 // indirect + github.com/spf13/afero v1.11.0 // indirect + github.com/spf13/cast v1.6.0 // indirect + github.com/spf13/pflag v1.0.5 // indirect + github.com/subosito/gotenv v1.6.0 // indirect + go.uber.org/atomic v1.9.0 // indirect + go.uber.org/multierr v1.9.0 // indirect + golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect + golang.org/x/sys v0.21.0 // indirect + golang.org/x/text v0.14.0 // indirect + gopkg.in/ini.v1 v1.67.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/src/go.sum b/src/go.sum new file mode 100644 index 0000000..4b7ac10 --- /dev/null +++ b/src/go.sum @@ -0,0 +1,79 @@ +github.com/creack/goselect v0.1.2 h1:2DNy14+JPjRBgPzAd1thbQp4BSIihxcBf0IXhQXDRa0= +github.com/creack/goselect v0.1.2/go.mod h1:a/NhLweNvqIYMuxcMOuWY516Cimucms3DglDzQP3hKY= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8= +github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= +github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= +github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg= +github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= +github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY= +github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0= +github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= +github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= +github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= +github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= +github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= +github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ= +github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4= +github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE= +github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ= +github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo= +github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0= +github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8= +github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY= +github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0= +github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/spf13/viper v1.19.0 h1:RWq5SEjt8o25SROyN3z2OrDB9l7RPd3lwTWU8EcEdcI= +github.com/spf13/viper v1.19.0/go.mod h1:GQUN9bilAbhU/jgc1bKs99f/suXKeUMct8Adx5+Ntkg= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8= +github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU= +go.bug.st/serial v1.6.2 h1:kn9LRX3sdm+WxWKufMlIRndwGfPWsH1/9lCWXQCasq8= +go.bug.st/serial v1.6.2/go.mod h1:UABfsluHAiaNI+La2iESysd9Vetq7VRdpxvjx7CmmOE= +go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE= +go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= +go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI= +go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ= +golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g= +golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k= +golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws= +golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA= +gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/src/main.go b/src/main.go new file mode 100644 index 0000000..e67e1bd --- /dev/null +++ b/src/main.go @@ -0,0 +1,144 @@ +package main + +import ( + "bon-voyage-agent/connection" + "bon-voyage-agent/utils" + "bufio" + "flag" + "fmt" + "os" + "os/signal" + "time" + + "github.com/gorilla/websocket" +) + +const version = "1.0.0" + +var Commit string + +func main() { + fmt.Println("Agent version", version) + + pluginFolder := flag.String("plugin", "./plugins", "Directory containing plugins") + + // Display help information + flag.Usage = func() { + fmt.Fprintf(flag.CommandLine.Output(), "Usage: %s [options]\n", "bon-voyage-agent") + flag.PrintDefaults() + } + flag.Parse() + + config := utils.NewConfig() + err := config.LoadConfig() + if err != nil { + fmt.Println("Could not load config:", err) + os.Exit(1) + } + + plugins, err := utils.LoadPlugins(*pluginFolder) + if err != nil { + fmt.Println("Could not load plugins:", err) + os.Exit(1) + } + + router := connection.NewRouter() + + for _, p := range plugins { + router.HandleFunc(p.Init(nil), p.Call) + } + + c := connection.Connection{ + Host: config.Server.Host, + Port: config.Server.Port, + Name: config.Agent.Name, + Uuid: config.Agent.Id, + Key: config.Server.Key, + } + + interrupt := make(chan os.Signal, 1) + signal.Notify(interrupt, os.Interrupt) + + dataSocket, err := c.ConnectDataSocket() + if err != nil { + fmt.Println("Could not connect data socket:", err) + } + configSocket, err := c.ConnectConfigSocket() + if err != nil { + fmt.Println("Could not connect data socket:", err) + } + + done := make(chan struct{}) + fmt.Println("------------------------------------") + go func() { // Service the DATA socket + defer close(done) + for { + _, message, err := dataSocket.ReadMessage() + if err != nil { + if websocket.IsCloseError(err, websocket.CloseNormalClosure) { + return + } + fmt.Println("read:", err) + return + } + fmt.Printf("Received data: %s\n", message) + } + }() + + go func() { // Service the CONFIG socket + defer close(done) + for { + _, message, err := configSocket.ReadMessage() + if err != nil { + if websocket.IsCloseError(err, websocket.CloseNormalClosure) { + return + } + fmt.Println("read:", err) + return + } + + reply, err := router.ParseMessage(message) + if err != nil { + fmt.Println("Parsing error:", err) + } + fmt.Println("Reply:", string(reply)) + configSocket.WriteMessage(websocket.TextMessage, reply) + } + }() + + go func() { + scanner := bufio.NewScanner(os.Stdin) + for scanner.Scan() { + text := scanner.Text() + err := dataSocket.WriteMessage(websocket.TextMessage, []byte(text)) + if err != nil { + fmt.Println("write:", err) + return + } + } + if err := scanner.Err(); err != nil { + fmt.Println("Error reading from stdin:", err) + } + }() + + select { + case <-done: + fmt.Println("WebSocket connection closed") + case <-interrupt: + fmt.Println("\nInterrupt signal received, closing connection...") + err := dataSocket.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, "")) + if err != nil { + fmt.Println("write close:", err) + } + err = configSocket.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, "")) + if err != nil { + fmt.Println("write close:", err) + return + } + select { + case <-done: + case <-time.After(time.Second): + } + } + fmt.Println("------------------------------------") +} diff --git a/src/models/models.go b/src/models/models.go new file mode 100644 index 0000000..ee6da2b --- /dev/null +++ b/src/models/models.go @@ -0,0 +1,37 @@ +package models + +import "encoding/json" + +// JSON-RPC request format +type RPCRequest struct { + Jsonrpc string `json:"jsonrpc"` + Method string `json:"method"` + Params json.RawMessage `json:"params"` + ID int `json:"id"` +} + +// JSON-RPC response format +type RPCResponse struct { + Jsonrpc string `json:"jsonrpc"` + Result string `json:"result,omitempty"` + Error string `json:"error,omitempty"` + ID int `json:"id"` +} + +// RPCError represents a JSON-RPC error +type RPCError struct { + Code int `json:"code"` + Message string `json:"message"` + Data string `json:"data,omitempty"` +} + +type AgentInformation struct { + Name string `json:"name"` + ID string `json:"id"` + Version string `json:"version"` + ApiVersion string `json:"api_version"` +} + +type SetNameMethod struct { + Name string `json:"name"` +} diff --git a/src/plugins/build_plugins.sh b/src/plugins/build_plugins.sh new file mode 100755 index 0000000..aad6eea --- /dev/null +++ b/src/plugins/build_plugins.sh @@ -0,0 +1,43 @@ +#!/bin/bash + +# Exit immediately if a command exits with a non-zero status +set -e + +# Check if the output directory is provided as a command line argument +if [[ -z "$1" ]]; then + echo "Usage: $0 " + exit 1 +fi + +OUTPUT_DIR="$1" + +# Create the output directory if it does not exist +if [[ ! -d "$OUTPUT_DIR" ]]; then + mkdir -p "$OUTPUT_DIR" +fi + +# Loop through all directories matching the pattern ./plugin_* +for dir in ./plugin_*; do + if [[ -d $dir ]]; then + # Extract the plugin name from the directory name + plugin_name=$(basename "$dir") + + # Construct the main Go file name + go_file="${dir}/${plugin_name}.go" + + # Check if the main Go file exists + if [[ -f $go_file ]]; then + # Build the plugin + output_file="${OUTPUT_DIR}/${plugin_name}.so" + echo "Building ${output_file} from ${go_file}" + go build -buildmode=plugin -o "$output_file" "$go_file" + else + echo "Main Go file ${go_file} not found, skipping ${plugin_name}" + fi + else + echo "${dir} is not a directory, skipping" + fi +done + +echo "All plugins built successfully." + diff --git a/src/plugins/plugin_camera/plugin_camera.go b/src/plugins/plugin_camera/plugin_camera.go new file mode 100644 index 0000000..6bfa6ed --- /dev/null +++ b/src/plugins/plugin_camera/plugin_camera.go @@ -0,0 +1,41 @@ +package main + +import ( + "bon-voyage-agent/models" + "bon-voyage-agent/shared" +) + +const pluginName = "camera" + +// Implements interface Plugin +type PluginCamera struct { + Name string +} + +func (p PluginCamera) Init(params any) string { + shared.Debug("Camera Init Call") + p.Name = pluginName + return p.Name +} + +func (p PluginCamera) Call(rpcReq models.RPCRequest, response *models.RPCResponse) { + shared.Debug("Camera Plugin Call") + switch rpcReq.Method { + case "camera_information": + + case "camera_start_stream": + + case "camera_stop_stream": + + case "camera_status_stream": + default: + response.Error = "Unknown method" + } +} + +func (p PluginCamera) Deinit() { + shared.Debug("Camera Deinit Call") +} + +// Exported symbol +var PluginInstance PluginCamera diff --git a/src/plugins/plugin_serial/plugin_serial.go b/src/plugins/plugin_serial/plugin_serial.go new file mode 100644 index 0000000..bb4cce1 --- /dev/null +++ b/src/plugins/plugin_serial/plugin_serial.go @@ -0,0 +1,166 @@ +package main + +import ( + "bon-voyage-agent/models" + "bon-voyage-agent/shared" + "encoding/json" + "fmt" + "log" + "strconv" + "strings" + + "go.bug.st/serial" + "go.bug.st/serial/enumerator" +) + +const pluginName = "serial" + +// Implements interface Plugin +type PluginSerial struct { + Name string +} + +func (p PluginSerial) Init(params any) string { + + shared.Debug("Serial Plugin Init") + p.Name = pluginName + return p.Name +} + +func (p PluginSerial) Call(rpcReq models.RPCRequest, response *models.RPCResponse) { + + shared.Debug("Serial Plugin Call") + + switch rpcReq.Method { + case "serial_get_ports": + data, _ := json.Marshal(serialPortList()) + response.Result = string(data) + + case "serial_open": + response.Result = "Serial port opened" + serialPort.open() + + case "serial_close": + response.Result = "Serial port closed" + serialPort.close() + + case "serial_set_baudrate": + var params map[string]int + if err := json.Unmarshal(rpcReq.Params, ¶ms); err != nil { + response.Error = "Invalid params" + return + } + serialPort.Mode.BaudRate = params["baudrate"] + serialPort.port.SetMode(&serialPort.Mode) + response.Result = "Baudrate set to " + strconv.Itoa(params["baudrate"]) + + case "serial_set_databits": + var params map[string]int + if err := json.Unmarshal(rpcReq.Params, ¶ms); err != nil { + response.Error = "Invalid params" + return + } + serialPort.Mode.DataBits = params["databits"] + serialPort.port.SetMode(&serialPort.Mode) + response.Result = "Data bits set to " + strconv.Itoa(params["databits"]) + + case "serial_set_parity": + var params map[string]string + if err := json.Unmarshal(rpcReq.Params, ¶ms); err != nil { + response.Error = "Invalid params" + return + } + if err := serialPort.setParity(params["parity"]); err != nil { + response.Error = "Invalid params" + return + } + response.Result = "Parity set to " + params["parity"] + + case "serial_set_stopbits": + var params map[string]int + if err := json.Unmarshal(rpcReq.Params, ¶ms); err != nil { + response.Error = "Invalid params" + return + } + serialPort.setStopBit(params["stopbits"]) + response.Result = "Stopbits set to " + strconv.Itoa(params["stopbits"]) + + default: + response.Error = "Unknown method" + } +} + +func (p PluginSerial) Deinit() { + shared.Debug("Serial Plugin Deinit") +} + +// Exported symbol +var PluginInstance PluginSerial + +func serialPortList() (list []string) { + + ports, err := enumerator.GetDetailedPortsList() + if err != nil { + log.Fatal(err) + return + } + if len(ports) == 0 { + fmt.Println("No serial ports found!") + return + } + fmt.Println("Found serial ports:") + for _, port := range ports { + fmt.Printf(" - %s\n", port.Name) + list = append(list, port.Name) + if port.IsUSB { + fmt.Printf(" * USB ID %s:%s\n", port.VID, port.PID) + fmt.Printf(" * USB serial %s\n", port.SerialNumber) + } + } + return +} + +var serialPort SerialPort + +type SerialPort struct { + PortName string + Mode serial.Mode + port serial.Port +} + +func (s *SerialPort) setParity(p string) error { + + switch strings.ToLower(p) { + case "odd": + s.Mode.Parity = serial.OddParity + case "even": + s.Mode.Parity = serial.EvenParity + case "no": + s.Mode.Parity = serial.NoParity + default: + return fmt.Errorf("unknown parity %s", p) + } + return nil +} + +func (s *SerialPort) setStopBit(b int) error { + + switch b { + case 1: + s.Mode.StopBits = serial.OneStopBit + case 2: + s.Mode.StopBits = serial.TwoStopBits + default: + return fmt.Errorf("stop bit count %d not supported", b) + } + return nil +} + +func (s *SerialPort) open() (err error) { + s.port, err = serial.Open(s.PortName, &s.Mode) + return +} + +func (s *SerialPort) close() error { + return s.port.Close() +} diff --git a/src/shared/logging.go b/src/shared/logging.go new file mode 100644 index 0000000..14bafa2 --- /dev/null +++ b/src/shared/logging.go @@ -0,0 +1,11 @@ +package shared + +import "log" + +func Info(v ...any) { + log.Println(v...) +} + +func Debug(v ...any) { + log.Println(v...) +} diff --git a/src/shared/plugin.go b/src/shared/plugin.go new file mode 100644 index 0000000..4c577e7 --- /dev/null +++ b/src/shared/plugin.go @@ -0,0 +1,9 @@ +package shared + +import "bon-voyage-agent/models" + +type Plugin interface { + Init(params any) string //returns name + Call(rpcReq models.RPCRequest, response *models.RPCResponse) + Deinit() +} diff --git a/src/utils/config.go b/src/utils/config.go new file mode 100644 index 0000000..256bb94 --- /dev/null +++ b/src/utils/config.go @@ -0,0 +1,105 @@ +// Copyright 2024 Bitcrush Testing + +package utils + +import ( + "fmt" + "math/rand" + + "github.com/google/uuid" + "github.com/spf13/viper" +) + +var defaultAgentNames = []string{ + "Alois", + "Anton", + "August", + "Ferdinand", + "Franz", + "Frederick", + "Gustav", + "Heinrich", + "Hermann", + "Joseph", + "Karl", + "Leopold", + "Matthias", + "Maximilian", + "Otto", + "Rudolf", +} + +var viperInstance *viper.Viper + +// Config represents the configuration structure +type Config struct { + Agent struct { + Name string `yaml:"name"` + Id string `yaml:"id"` + } `yaml:"agent"` + Server struct { + Host string `yaml:"host"` + Port string `yaml:"port"` + Key string `yaml:"key"` + } `yaml:"server"` +} + +type SerialPortConfig struct { + Port string `yaml:"port"` + BaudRate int `yaml:"baud_rate"` + DataBits int `yaml:"data_bits"` + Parity string `yaml:"parity"` + StopBits int `yaml:"stop_bits"` +} + +func NewConfig() *Config { + + id := uuid.New() + viperInstance = viper.New() + viperInstance.AddConfigPath(".") + viperInstance.AddConfigPath("/etc/bon-voyage-agent") + viperInstance.AddConfigPath("$HOME/.bon-voyage-agent") + viperInstance.SetConfigName("config.yaml") + viperInstance.SetConfigType("yaml") + viperInstance.ReadInConfig() + + c := new(Config) + c.Agent.Name = defaultAgentNames[rand.Intn(len(defaultAgentNames))] + c.Agent.Id = id.String() + c.Server.Host = "127.0.0.1" + c.Server.Port = "6666" + c.Server.Key = "" + return c +} + +func (c *Config) LoadConfig() error { + + err := viperInstance.ReadInConfig() + if err != nil { + return fmt.Errorf("fatal error config file: %w", err) + } + + err = viperInstance.Unmarshal(c) + if err != nil { + return fmt.Errorf("failed to parse config file: %v", err) + } + + return nil +} + +func (c *Config) SaveConfig() error { + + return viperInstance.SafeWriteConfig() +} + +func (c *Config) PluginConfig(name string, params *any) error { + + if !viperInstance.IsSet(name) { + return fmt.Errorf("plugin %s configuration not found", name) + } + + if err := viperInstance.Sub(name).Unmarshal(params); err != nil { + return fmt.Errorf("error unmarshalling plugin config: %w", err) + } + return nil +} diff --git a/src/utils/plugins.go b/src/utils/plugins.go new file mode 100644 index 0000000..2a6ca98 --- /dev/null +++ b/src/utils/plugins.go @@ -0,0 +1,55 @@ +package utils + +import ( + "bon-voyage-agent/shared" + "log" + "os" + "path/filepath" + "plugin" + "strings" +) + +func LoadPlugins(pluginDir string) (map[string]shared.Plugin, error) { + + plugins := make(map[string]shared.Plugin) + // Walk through the plugin directory and find .so files. + err := filepath.Walk(pluginDir, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + + // Check if the file is a .so file. + if !info.IsDir() && strings.HasSuffix(info.Name(), ".so") { + // Load the plugin. + p, err := plugin.Open(path) + if err != nil { + log.Printf("Error loading plugin %s: %v", path, err) + return nil // Continue loading other plugins + } + + // Lookup the PluginInstance symbol. + symbol, err := p.Lookup("PluginInstance") + if err != nil { + log.Printf("Error looking up PluginInstance in %s: %v", path, err) + return nil // Continue loading other plugins + } + + // Assert the symbol as the Plugin interface. + pluginInstance, ok := symbol.(shared.Plugin) + if !ok { + log.Printf("Error asserting PluginInstance in %s as Plugin interface", path) + return nil // Continue loading other plugins + } + + // Extract the plugin name from the file name. + pluginName := strings.TrimSuffix(strings.TrimPrefix(info.Name(), "plugin_"), ".so") + plugins[pluginName] = pluginInstance + log.Printf("Loaded plugin: %s", pluginName) + } + return nil + }) + if err != nil { + return nil, err + } + return plugins, nil +}