Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial version of frontend #22

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
dist
data
volumes
/front/node_modules
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pkg/proto/** linguist-vendored
front/src/proto/** linguist-vendored
24 changes: 24 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,30 @@ jobs:
input: proto
config: proto/buf.yaml

front:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Install pnpm
uses: pnpm/action-setup@v2
with:
version: 'latest'

- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'pnpm'
cache-dependency-path: 'front/pnpm-lock.yaml'

- name: Install deps
run: cd front && pnpm install

- name: run linter
run: make lint-front

image:
runs-on: ubuntu-latest
steps:
Expand Down
2 changes: 1 addition & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ archives:
- cmd
- internal
- pkg
- proto/go
- monitoring
- front

- id: "client_env"
format: zip
Expand Down
19 changes: 16 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ WORKDIR /app
COPY go.* ./
COPY cmd cmd
COPY internal internal
COPY proto/go proto/go
COPY pkg pkg
RUN --mount=type=cache,target=/root/.cache/go-build \
--mount=type=cache,target=/go/pkg/mod \
Expand All @@ -15,8 +14,22 @@ RUN --mount=type=cache,target=/root/.cache/go-build \
-o neo_server \
cmd/server/main.go

FROM node:20-slim AS front-base
ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
RUN corepack enable

COPY front /app
WORKDIR /app

FROM front-base AS front-build
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile
RUN pnpm run build

FROM alpine

COPY --from=build /app/neo_server /neo_server
WORKDIR /app
COPY --from=build /app/neo_server neo_server
COPY --from=front-build /app/dist front/dist

CMD ["/neo_server", "--config", "/config.yml"]
CMD ["./neo_server", "--config", "/config.yml"]
9 changes: 7 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@ lint-go:
lint-proto:
cd proto && buf lint

.PHONY: lint-front
lint-front:
cd front && pnpm lint

.PHONY: lint
lint: lint-go lint-proto
lint: lint-go lint-proto lint-front

.PHONY: goimports
goimports:
gofancyimports fix --local github.com/c4t-but-s4d/neo -w $(shell find . -type f -name '*.go' -not -path "./proto/*")
gofancyimports fix --local github.com/c4t-but-s4d/neo/v2 -w $(shell find . -type f -name '*.go' -not -path "./pkg/proto/*")

.PHONY: test
test:
Expand All @@ -32,6 +36,7 @@ validate: lint test
.PHONY: proto
proto:
cd proto && buf generate
cd front && ./add_ts_ignore.sh

.PHONY: test-cov
test-cov:
Expand Down
1 change: 0 additions & 1 deletion client_env/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ WORKDIR /app
COPY go.* ./
COPY cmd cmd
COPY internal internal
COPY proto/go proto/go
COPY pkg pkg
RUN --mount=type=cache,target=/root/.cache/go-build \
--mount=type=cache,target=/go/pkg/mod \
Expand Down
2 changes: 1 addition & 1 deletion cmd/client/cli/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

"github.com/c4t-but-s4d/neo/v2/internal/client"
"github.com/c4t-but-s4d/neo/v2/pkg/archive"
epb "github.com/c4t-but-s4d/neo/v2/proto/go/exploits"
epb "github.com/c4t-but-s4d/neo/v2/pkg/proto/exploits"
)

type addCLI struct {
Expand Down
2 changes: 1 addition & 1 deletion cmd/client/cli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const JobsPerCPU = 5
type runCLI struct {
*baseCLI
run *exploit.Runner
sender *joblogger.RemoteSender
sender joblogger.Sender
}

func parseJobsFlag(cmd *cobra.Command, name string) int {
Expand Down
2 changes: 1 addition & 1 deletion cmd/client/cli/tail.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/spf13/cobra"

"github.com/c4t-but-s4d/neo/v2/internal/client"
logspb "github.com/c4t-but-s4d/neo/v2/proto/go/logs"
logspb "github.com/c4t-but-s4d/neo/v2/pkg/proto/logs"
)

type tailCLI struct {
Expand Down
3 changes: 1 addition & 2 deletions cmd/client/cli/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import (
"google.golang.org/protobuf/types/known/durationpb"

"github.com/c4t-but-s4d/neo/v2/internal/client"

epb "github.com/c4t-but-s4d/neo/v2/proto/go/exploits"
epb "github.com/c4t-but-s4d/neo/v2/pkg/proto/exploits"
)

type updateCLI struct {
Expand Down
2 changes: 1 addition & 1 deletion cmd/client/cli/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/samber/lo"

"github.com/c4t-but-s4d/neo/v2/proto/go/exploits"
"github.com/c4t-but-s4d/neo/v2/pkg/proto/exploits"
)

func isBinary(data []byte) bool {
Expand Down
2 changes: 0 additions & 2 deletions cmd/client/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,10 @@ func init() {
rootCmd.PersistentFlags().StringP("config", "c", "client_config.yml", "config file")
rootCmd.PersistentFlags().BoolP("verbose", "v", true, "enable debug logging")
rootCmd.PersistentFlags().String("host", "127.0.0.1:5005", "server host")
rootCmd.PersistentFlags().String("metrics_host", "127.0.0.1:9091", "pushgateway host")

mustBindPersistent(rootCmd, "config")
mustBindPersistent(rootCmd, "host")
mustBindPersistent(rootCmd, "verbose")
mustBindPersistent(rootCmd, "metrics_host")
}

func mustBindPersistent(c *cobra.Command, flag string) {
Expand Down
77 changes: 54 additions & 23 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package main

import (
"context"
"errors"
"fmt"
"net"
"net/http"
"os/signal"
"strings"
Expand All @@ -20,15 +20,18 @@ import (
"google.golang.org/grpc/reflection"

"github.com/c4t-but-s4d/neo/v2/internal/logger"
"github.com/c4t-but-s4d/neo/v2/internal/logstor"
"github.com/c4t-but-s4d/neo/v2/internal/server/config"
"github.com/c4t-but-s4d/neo/v2/internal/server/exploits"
"github.com/c4t-but-s4d/neo/v2/internal/server/fs"
logs "github.com/c4t-but-s4d/neo/v2/internal/server/logs"
"github.com/c4t-but-s4d/neo/v2/pkg/grpcauth"
"github.com/c4t-but-s4d/neo/v2/pkg/mu"
"github.com/c4t-but-s4d/neo/v2/pkg/neohttp"
"github.com/c4t-but-s4d/neo/v2/pkg/neosync"
epb "github.com/c4t-but-s4d/neo/v2/proto/go/exploits"
fspb "github.com/c4t-but-s4d/neo/v2/proto/go/fileserver"
logspb "github.com/c4t-but-s4d/neo/v2/proto/go/logs"
epb "github.com/c4t-but-s4d/neo/v2/pkg/proto/exploits"
fspb "github.com/c4t-but-s4d/neo/v2/pkg/proto/fileserver"
logspb "github.com/c4t-but-s4d/neo/v2/pkg/proto/logs"
)

func main() {
Expand Down Expand Up @@ -56,7 +59,7 @@ func main() {
logrus.Fatalf("Failed to create bolt storage: %v", err)
}

logStore, err := logs.NewLogStorage(initCtx, cfg.RedisURL)
logStore, err := logstor.NewRedisStorage(initCtx, cfg.RedisURL)
if err != nil {
logrus.Fatalf("Failed to create log storage: %v", err)
}
Expand All @@ -73,11 +76,6 @@ func main() {
}
logsServer := logs.New(logStore)

lis, err := net.Listen("tcp", cfg.Addr)
if err != nil {
logrus.Fatalf("Failed to listen: %v", err)
}

var opts []grpc.ServerOption
if cfg.GrpcAuthKey != "" {
authInterceptor := grpcauth.NewServerInterceptor(cfg.GrpcAuthKey)
Expand All @@ -91,20 +89,29 @@ func main() {
logspb.RegisterServiceServer(s, logsServer)
reflection.Register(s)

http.Handle("/metrics", promhttp.Handler())
go func() {
logrus.Infof("Starting metrics server on %s", viper.GetString("metrics.address"))
if err := http.ListenAndServe(viper.GetString("metrics.address"), http.DefaultServeMux); err != nil {
logrus.Fatalf("Failed to serve metrics: %v", err)
}
}()
httpMux := http.NewServeMux()
httpMux.Handle("/", neohttp.StaticHandler(cfg.StaticDir))

muHandler := mu.NewHandler(s, mu.WithHTTPHandler(httpMux))
httpServer := &http.Server{
Handler: muHandler,
Addr: cfg.Address,
}

// Separate server to make it private.
metricsMux := http.NewServeMux()
metricsMux.Handle("/metrics", promhttp.Handler())
metricsServer := &http.Server{
Handler: metricsMux,
Addr: cfg.MetricsAddress,
}

runCtx, cancel := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGINT)
defer cancel()

wg := sync.WaitGroup{}

wg.Add(3)
wg.Add(4)
go func() {
defer wg.Done()
exploitsServer.HeartBeat(runCtx)
Expand All @@ -117,11 +124,28 @@ func main() {
defer wg.Done()
<-runCtx.Done()
logrus.Info("Received shutdown signal, stopping server")
s.GracefulStop()

shutdownCtx, shutdownCancel := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGINT)
defer shutdownCancel()
shutdownCtx, shutdownCancel = context.WithTimeout(shutdownCtx, 5*time.Second)
defer shutdownCancel()

if err := httpServer.Shutdown(shutdownCtx); err != nil {
logrus.Errorf("Failed to shutdown http server: %v", err)
}
if err := metricsServer.Shutdown(shutdownCtx); err != nil {
logrus.Errorf("Failed to shutdown metrics server: %v", err)
}
}()
go func() {
defer wg.Done()
if err := metricsServer.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
logrus.Fatalf("Failed to serve metrics: %v", err)
}
}()

logrus.Infof("Starting server on %s", cfg.Addr)
if err := s.Serve(lis); err != nil {
logrus.Infof("Starting multiproto server on %s", cfg.Address)
if err := httpServer.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
logrus.Fatalf("Failed to serve: %v", err)
}

Expand Down Expand Up @@ -149,12 +173,19 @@ func setupConfig() error {
viper.MustBindEnv("grpc_auth_key")
viper.MustBindEnv("farm.password")
viper.MustBindEnv("farm.url")
viper.MustBindEnv("db_path")
viper.MustBindEnv("redis_url")
viper.MustBindEnv("base_dir")

viper.SetDefault("config", "server_config.yml")
viper.SetDefault("ping_every", time.Second*5)
viper.SetDefault("submit_every", time.Second*2)
viper.SetDefault("metrics.address", ":3000")
viper.SetDefault("addr", ":5005")
viper.SetDefault("address", ":5005")
viper.SetDefault("metrics_address", ":3000")
viper.SetDefault("static_dir", "front/dist")
viper.SetDefault("redis_url", "redis://127.0.0.1:6379/0")
viper.SetDefault("db_path", "data/db.db")
viper.SetDefault("base_dir", "data/exploits")

return nil
}
Expand Down
19 changes: 17 additions & 2 deletions compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ services:
volumes:
- "./configs/server/config.yml:/config.yml:ro"
- "./volumes/data:/data"
extra_hosts:
- "host.docker.internal:host-gateway"
environment:
NEO_GRPC_AUTH_KEY: ${GRPC_AUTH_KEY}
NEO_FARM_PASSWORD: ${FARM_PASSWORD}
NEO_FARM_URL: ${FARM_URL}
NEO_DB_PATH: /data/db.db
NEO_REDIS_URL: 'redis://redis:6379/0'
NEO_BASE_DIR: '/data/exploits'
ports:
- '5005:5005'
restart: unless-stopped
Expand All @@ -18,6 +23,7 @@ services:

grafana:
image: grafana/grafana-oss:10.0.3
user: '0'
volumes:
- ./volumes/grafana:/var/lib/grafana
- ./monitoring/cfg/grafana/provisioning:/etc/grafana/provisioning
Expand All @@ -33,6 +39,7 @@ services:

victoria:
image: victoriametrics/victoria-metrics:v1.92.1
user: '0'
volumes:
- ./monitoring/cfg/prometheus:/etc/prometheus
- ./volumes/victoria-metrics:/victoria-metrics-data
Expand All @@ -42,7 +49,15 @@ services:
- "-storageDataPath=/victoria-metrics-data"
- "-promscrape.config=/etc/prometheus/config.yml"
restart: unless-stopped
cpus: 2
mem_limit: 4gb

victoria-proxy:
image: caddy:2.6.1-alpine
restart: unless-stopped
environment:
HTTP_BASIC_AUTH_USER: "${VICTORIA_USER:-admin}"
HTTP_BASIC_AUTH_PASSWORD: "${VICTORIA_PASSWORD:-1234}"
command: "/bin/sh -c 'export HTTP_BASIC_AUTH_PASSWORD_HASH=$(caddy hash-password --plaintext $$HTTP_BASIC_AUTH_PASSWORD) && caddy run --config /etc/caddy/Caddyfile'"
ports:
- "8428:8428"
volumes:
- "./monitoring/cfg/victoria-proxy:/etc/caddy"
6 changes: 5 additions & 1 deletion configs/client/config.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
host: "localhost:5005"
metrics_host: "localhost:8428/api/v1/import/prometheus"
exploit_dir: "exploits"
grpc_auth_key: "s3cret_t0ken_pls_d0nt_leak"

metrics:
url: "localhost:8428/api/v1/import/prometheus"
user: "admin"
password: "1234"
6 changes: 1 addition & 5 deletions configs/server/config.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
db_path: "data/db.db"
redis_url: 'redis://redis:6379/0'
base_dir: "data/exploits"
ping_every: "5s"
submit_every: "2s"
grpc_auth_key: "s3cret_t0ken_pls_d0nt_leak"

farm:
# url: "http://127.0.0.1:5137"
url: "http://192.168.82.252:5137"
url: "http://host.docker.internal:5137"
password: "1234"

env:
Expand Down
Loading
Loading