Skip to content
This repository was archived by the owner on Dec 4, 2024. It is now read-only.

Commit dc73594

Browse files
authored
Refactor (#27)
* wip commit * wip commit * refactor wip * more wip * removing test file * fixing docker and other configs * reademe ---------
1 parent 9a02d82 commit dc73594

File tree

58 files changed

+542
-71347
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+542
-71347
lines changed

.dockerignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
images*
22
Dockerfile
33
docker-compose.yaml
4-
.git
4+
.git
5+
config.toml

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
.DS_Store
2-
bin/
2+
bin/
3+
defaultConfig.json
4+
kubeSystemConfig.json
5+
config.toml

Dockerfile

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,12 @@ FROM golang:alpine as build
44
RUN mkdir /app
55
WORKDIR /app
66
COPY . ./
7-
RUN go mod download && go build -o /helix-honeypot
7+
WORKDIR /app/cmd
8+
RUN go get ./... && go build -o /helix-honeypot
89

910
#Final Stage
1011
FROM alpine:latest
1112
WORKDIR /
1213
COPY --from=build /helix-honeypot /helix-honeypot
13-
RUN addgroup -S helix && adduser -S helix -G helix
14-
USER helix
15-
16-
EXPOSE 80
1714

1815
ENTRYPOINT ["/helix-honeypot"]

cmd/main.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"helix-honeypot/server"
8+
)
9+
10+
func main() {
11+
// Start the server based on the run mode
12+
err := server.StartHoneypot()
13+
if err != nil {
14+
fmt.Println("Failed To Start Server:", err)
15+
os.Exit(1)
16+
}
17+
}

config/config.go

Lines changed: 168 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,180 @@
11
package config
22

33
import (
4+
"fmt"
45
"os"
6+
"io/ioutil"
7+
"strings"
8+
9+
"helix-honeypot/model"
10+
"github.com/BurntSushi/toml"
511
)
612

7-
type Config struct {
8-
HTTP HTTPConfig
13+
// GetEnvAsBool gets environment variables as boolean values. It returns false if the value is not "true" (case-insensitive), otherwise it returns true
14+
func GetEnvAsBool(key string) bool {
15+
value := os.Getenv(key)
16+
return strings.ToLower(value) == "true"
917
}
1018

11-
// If env vars are empty set a default
12-
func GetEnv(key, defaultValue string) string {
13-
value := os.Getenv(key) // try to get the env var
14-
if len(value) == 0 {
15-
return defaultValue // if empty set default
16-
}
17-
return value // if not empty return env var
19+
func LoadK8SConfig(cfg *model.K8SConfig) model.K8SConfig {
20+
apiVersion := os.Getenv("K8SAPI_VERSION")
21+
ipBase := os.Getenv("IP_BASE")
22+
host := os.Getenv("K8S_HOST")
23+
port := os.Getenv("K8S_PORT")
24+
25+
if apiVersion != "" {
26+
cfg.APIVersion = apiVersion
27+
}
28+
if ipBase != "" {
29+
cfg.IPBase = ipBase
30+
}
31+
if host != "" {
32+
cfg.Host = host
33+
}
34+
if port != "" {
35+
cfg.Port = port
36+
}
37+
38+
// Since the zero value for a boolean in Go is `false`,
39+
// we only need to check if the environment variable is "true".
40+
if strings.ToLower(os.Getenv("GENERATE_KUBE_SYSTEM")) == "true" {
41+
cfg.GenerateKubeSys = true
42+
}
43+
if strings.ToLower(os.Getenv("GENERATE_RANDOMNESS")) == "true" {
44+
cfg.GenerateRand = true
45+
}
46+
47+
return *cfg
48+
}
49+
50+
func LoadHTTPConfig(cfg *model.HTTPConfig) model.HTTPConfig {
51+
host := os.Getenv("HELIX_HTTP_HOST")
52+
port := os.Getenv("HELIX_HTTP_PORT")
53+
54+
if host != "" {
55+
cfg.Host = host
56+
}
57+
if port != "" {
58+
cfg.Port = port
59+
}
60+
61+
return *cfg
62+
}
63+
64+
func LoadUDPConfig(cfg *model.UDPConfig) model.UDPConfig {
65+
host := os.Getenv("HELIX_UDP_HOST")
66+
port := os.Getenv("HELIX_UDP_PORT")
67+
68+
if host != "" {
69+
cfg.Host = host
70+
}
71+
if port != "" {
72+
cfg.Port = port
73+
}
74+
75+
return *cfg
76+
}
77+
78+
func LoadTCPConfig(cfg *model.TCPConfig) model.TCPConfig {
79+
host := os.Getenv("HELIX_TCP_HOST")
80+
port := os.Getenv("HELIX_TCP_PORT")
81+
82+
if host != "" {
83+
cfg.Host = host
84+
}
85+
if port != "" {
86+
cfg.Port = port
87+
}
88+
89+
return *cfg
90+
}
91+
92+
func LoadDEFConfig(cfg *model.DEFConfig) model.DEFConfig {
93+
host := os.Getenv("HELIX_DEF_HOST")
94+
port := os.Getenv("HELIX_DEF_PORT")
95+
96+
if host != "" {
97+
cfg.Host = host
98+
}
99+
if port != "" {
100+
cfg.Port = port
101+
}
102+
103+
return *cfg
18104
}
19105

20-
func NewConfig() *Config {
21-
return &Config{
22-
HTTP: LoadHTTPConfig(),
106+
func LoadMongoDBConfig(cfg *model.MongoDBConfig) model.MongoDBConfig {
107+
username := os.Getenv("MONGODB_USERNAME")
108+
password := os.Getenv("MONGODB_PASSWORD")
109+
host := os.Getenv("MONGODB_HOST")
110+
database := os.Getenv("MONGODB_DATABASE")
111+
collection := os.Getenv("MONGODB_COLLECTION")
112+
113+
if username != "" {
114+
cfg.Username = username
115+
}
116+
if password != "" {
117+
cfg.Password = password
118+
}
119+
if host != "" {
120+
cfg.Host = host
121+
}
122+
if database != "" {
123+
cfg.Database = database
124+
}
125+
if collection != "" {
126+
cfg.Collection = collection
127+
}
128+
129+
// Format the MongoDB URI
130+
uri := fmt.Sprintf("mongodb+srv://%s:%s@%s/", cfg.Username, cfg.Password, cfg.Host)
131+
cfg.URI = uri
132+
133+
if strings.ToLower(os.Getenv("LOG_TO_MONGODB")) == "true" {
134+
cfg.LogToMongoDB = true
23135
}
136+
137+
return *cfg
138+
}
139+
140+
func LoadRunModeConfig(cfg *model.RunModeConfig) model.RunModeConfig {
141+
runMode := os.Getenv("RUN_MODE")
142+
location := os.Getenv("HELIX_LOCATION")
143+
144+
if runMode != "" {
145+
cfg.RunMode = runMode
146+
}
147+
if location != "" {
148+
cfg.Location = location
149+
}
150+
151+
return *cfg
152+
}
153+
154+
func NewConfig(configFile string) (*model.Config, error) {
155+
var cfg model.Config
156+
157+
// Check if the config file exists
158+
if _, err := os.Stat(configFile); err == nil {
159+
// If the config file exists, load default values from TOML file
160+
data, err := ioutil.ReadFile(configFile)
161+
if err != nil {
162+
return nil, err
163+
}
164+
_, err = toml.Decode(string(data), &cfg)
165+
if err != nil {
166+
return nil, err
167+
}
168+
}
169+
170+
// Override with environment variables
171+
cfg.HTTP = LoadHTTPConfig(&cfg.HTTP)
172+
cfg.UDP = LoadUDPConfig(&cfg.UDP)
173+
cfg.TCP = LoadTCPConfig(&cfg.TCP)
174+
cfg.K8S = LoadK8SConfig(&cfg.K8S)
175+
cfg.DEF = LoadDEFConfig(&cfg.DEF)
176+
cfg.MongoDB = LoadMongoDBConfig(&cfg.MongoDB)
177+
cfg.RunMode = LoadRunModeConfig(&cfg.RunMode)
178+
179+
return &cfg, nil
24180
}

config/http.go

Lines changed: 0 additions & 13 deletions
This file was deleted.

docker-compose.yaml

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,53 @@
11
version: '3.7'
22

33
services:
4-
helix-honeypot-ad:
5-
build: ./
6-
ports:
7-
- "8000:80"
8-
entrypoint: [/helix-honeypot, -mode=ad]
9-
volumes:
10-
- /dev/urandom:/dev/urandom
11-
helix-honeypot:
4+
helix-honeypot-k8s:
125
build: ./
136
ports:
14-
- "80:80"
7+
- "8111:8111"
8+
environment:
9+
- RUN_MODE=k8s
10+
- HELIX_LOCATION=testing
11+
- K8SAPI_VERSION=v1.21
12+
- IP_BASE=192.168
13+
- GENERATE_KUBE_SYSTEM=true
14+
- GENERATE_RANDOMNESS=true
15+
- K8S_HOST=0.0.0.0
16+
- K8S_PORT=8111
17+
18+
helix-honeypot-http:
19+
build: ./
20+
ports:
21+
- "8000:8000"
22+
environment:
23+
- RUN_MODE=http
24+
- HELIX_HTTP_HOST=0.0.0.0
25+
- HELIX_HTTP_PORT=8000
26+
27+
helix-honeypot-tcp:
28+
build: ./
29+
ports:
30+
- "3000:3000"
31+
environment:
32+
- RUN_MODE=tcp
33+
- HELIX_TCP_HOST=0.0.0.0
34+
- HELIX_TCP_PORT=3000
35+
36+
helix-honeypot-udp:
37+
build: ./
38+
ports:
39+
- "53:53/udp"
40+
environment:
41+
- RUN_MODE=udp
42+
- HELIX_UDP_HOST=0.0.0.0
43+
- HELIX_UDP_PORT=53
44+
45+
helix-honeypot-def:
46+
build: ./
47+
ports:
48+
- "8001:8001"
49+
environment:
50+
- RUN_MODE=def
51+
- HELIX_DEF_HOST=0.0.0.0
52+
- HELIX_DEF_PORT=8001
53+

go.mod

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,38 @@ module helix-honeypot
33
go 1.19
44

55
require (
6+
github.com/BurntSushi/toml v0.3.1
7+
github.com/denisbrodbeck/machineid v1.0.1
68
github.com/google/gnostic v0.6.9
7-
github.com/labstack/echo/v4 v4.9.0
8-
github.com/labstack/gommon v0.3.1
9-
google.golang.org/protobuf v1.28.1
9+
github.com/google/uuid v1.3.0
10+
github.com/klauspost/cpuid/v2 v2.2.5
11+
github.com/labstack/echo/v4 v4.10.2
12+
github.com/labstack/gommon v0.4.0
13+
github.com/mitchellh/hashstructure/v2 v2.0.2
14+
github.com/sirupsen/logrus v1.9.3
15+
go.mongodb.org/mongo-driver v1.12.0
16+
google.golang.org/protobuf v1.30.0
1017
)
1118

1219
require (
1320
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
14-
github.com/golang/protobuf v1.5.2 // indirect
15-
github.com/mattn/go-colorable v0.1.11 // indirect
16-
github.com/mattn/go-isatty v0.0.14 // indirect
21+
github.com/golang/protobuf v1.5.3 // indirect
22+
github.com/golang/snappy v0.0.1 // indirect
23+
github.com/klauspost/compress v1.13.6 // indirect
24+
github.com/mattn/go-colorable v0.1.13 // indirect
25+
github.com/mattn/go-isatty v0.0.19 // indirect
26+
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect
1727
github.com/valyala/bytebufferpool v1.0.0 // indirect
18-
github.com/valyala/fasttemplate v1.2.1 // indirect
19-
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect
20-
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f // indirect
21-
golang.org/x/sys v0.0.0-20211103235746-7861aae1554b // indirect
22-
golang.org/x/text v0.3.7 // indirect
23-
golang.org/x/time v0.0.0-20201208040808-7e3f01d25324 // indirect
24-
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
28+
github.com/valyala/fasttemplate v1.2.2 // indirect
29+
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
30+
github.com/xdg-go/scram v1.1.2 // indirect
31+
github.com/xdg-go/stringprep v1.0.4 // indirect
32+
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
33+
golang.org/x/crypto v0.10.0 // indirect
34+
golang.org/x/net v0.11.0 // indirect
35+
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect
36+
golang.org/x/sys v0.9.0 // indirect
37+
golang.org/x/text v0.10.0 // indirect
38+
golang.org/x/time v0.3.0 // indirect
39+
gopkg.in/yaml.v3 v3.0.1 // indirect
2540
)

0 commit comments

Comments
 (0)