Skip to content

Commit e662352

Browse files
authored
Merge branch 'develop' into dependabot/go_modules/develop/golang.org/x/term-0.29.0
2 parents 4991b7e + 0dbbc4f commit e662352

File tree

9 files changed

+521
-7
lines changed

9 files changed

+521
-7
lines changed

Diff for: README.md

-2
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,3 @@ Many thanks to all of the sponsors who support Swiftwave.
6464
### License
6565

6666
[Apache-2.0](LICENSE)
67-
68-
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fswiftwave-org%2Fswiftwave.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2Fswiftwave-org%2Fswiftwave?ref=badge_large)

Diff for: agent/api_server.go

+4
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ func startHttpServer() {
7373
e.GET("/nf-rules/:uuid", getNFRule)
7474
e.DELETE("/nf-rules/:uuid", deleteNFRule)
7575

76+
// HAProxy API
77+
e.GET("/haproxy/service-status", getHAProxyStatus)
78+
e.Any("/haproxy/*", sendRequestToHAProxy)
79+
7680
if err := e.Start(":8080"); err != nil && !errors.Is(err, http.ErrServerClosed) {
7781
e.Logger.Fatal(err)
7882
}

Diff for: agent/cli.go

+60-3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ func init() {
2525
setupCmd.Flags().String("wireguard-address", "", "Wireguard address")
2626
setupCmd.Flags().String("docker-network-gateway-address", "", "Docker network gateway address")
2727
setupCmd.Flags().String("docker-network-subnet", "", "Docker network subnet")
28+
setupCmd.Flags().String("swiftwave-service-address", "", "Swiftwave service address ip:port")
29+
setupCmd.Flags().Bool("enable-haproxy", false, "Enable haproxy")
2830

2931
setupCmd.Flags().Bool("master-node", false, "Setup as a master node")
3032
setupCmd.Flags().String("master-node-endpoint", "", "Master server endpoint")
@@ -35,6 +37,7 @@ func init() {
3537
setupCmd.MarkFlagRequired("wireguard-address")
3638
setupCmd.MarkFlagRequired("docker-network-gateway-address")
3739
setupCmd.MarkFlagRequired("docker-network-subnet")
40+
setupCmd.MarkFlagRequired("swiftwave-service-address")
3841
}
3942

4043
var rootCmd = &cobra.Command{
@@ -68,7 +71,14 @@ var startCmd = &cobra.Command{
6871
var setupCmd = &cobra.Command{
6972
Use: "setup",
7073
Run: func(cmd *cobra.Command, args []string) {
71-
_, err := GetConfig()
74+
// Migrate the database
75+
err := MigrateDatabase()
76+
if err != nil {
77+
cmd.PrintErr("Failed to migrate database")
78+
return
79+
}
80+
// Try to get the config
81+
_, err = GetConfig()
7282
if err == nil {
7383
cmd.Println("Sorry, you can't change any config")
7484
return
@@ -80,13 +90,27 @@ var setupCmd = &cobra.Command{
8090
return
8191
}
8292

93+
isEnableHaproxy, err := cmd.Flags().GetBool("enable-haproxy")
94+
if err != nil {
95+
cmd.PrintErr("Invalid enable haproxy flag")
96+
return
97+
}
98+
99+
// validate swiftwave service address
100+
_, _, err = net.SplitHostPort(cmd.Flag("swiftwave-service-address").Value.String())
101+
if err != nil {
102+
cmd.PrintErr("Invalid swiftwave service address")
103+
return
104+
}
105+
83106
nodeType := WorkerNode
84107
if isMasterNode {
85108
nodeType = MasterNode
86109
}
87110

88111
config := AgentConfig{
89-
NodeType: nodeType,
112+
NodeType: nodeType,
113+
SwiftwaveServiceAddress: cmd.Flag("swiftwave-service-address").Value.String(),
90114
WireguardConfig: WireguardConfig{
91115
PrivateKey: cmd.Flag("wireguard-private-key").Value.String(),
92116
Address: cmd.Flag("wireguard-address").Value.String(),
@@ -100,6 +124,11 @@ var setupCmd = &cobra.Command{
100124
PublicKey: cmd.Flag("master-node-public-key").Value.String(),
101125
AllowedIPs: cmd.Flag("master-node-allowed-ips").Value.String(),
102126
},
127+
HaproxyConfig: HAProxyConfig{
128+
Enabled: isEnableHaproxy,
129+
Username: GenerateRandomString(10),
130+
Password: GenerateRandomString(30),
131+
},
103132
}
104133

105134
if !isMasterNode {
@@ -137,6 +166,20 @@ var setupCmd = &cobra.Command{
137166
}
138167
}()
139168

169+
// Get ip from wireguard address
170+
ip, _, err := net.ParseCIDR(config.WireguardConfig.Address)
171+
if err != nil {
172+
cmd.PrintErr("Failed to parse wireguard address")
173+
return
174+
}
175+
176+
// Install haproxy
177+
err = installHAProxy(config.SwiftwaveServiceAddress, fmt.Sprintf("%s:53", ip), config.HaproxyConfig.Username, config.HaproxyConfig.Password)
178+
if err != nil {
179+
cmd.PrintErr(err.Error())
180+
return
181+
}
182+
140183
err = SetConfig(&config)
141184
if err != nil {
142185
cmd.PrintErr(err.Error())
@@ -162,6 +205,12 @@ var setupCmd = &cobra.Command{
162205
cmd.PrintErr(err.Error())
163206
return
164207
}
208+
// Enable haproxy and data plane api
209+
if config.HaproxyConfig.Enabled {
210+
enableHAProxy()
211+
}
212+
cmd.Println("Haproxy and data plane api enabled")
213+
165214
isSuccess = true
166215
},
167216
}
@@ -208,6 +257,11 @@ var getConfig = &cobra.Command{
208257
cmd.Printf(" • Bridge ID ---------- %s\n", config.DockerNetwork.BridgeId)
209258
cmd.Printf(" • Gateway Address ---- %s\n", config.DockerNetwork.GatewayAddress)
210259
cmd.Printf(" • Subnet ------------- %s\n", config.DockerNetwork.Subnet)
260+
cmd.Println()
261+
cmd.Println("Haproxy Configuration:")
262+
cmd.Printf(" • Enabled ------------ %t\n", config.HaproxyConfig.Enabled)
263+
cmd.Printf(" • Username ---------- %s\n", config.HaproxyConfig.Username)
264+
cmd.Printf(" • Password ---------- %s\n", config.HaproxyConfig.Password)
211265
},
212266
}
213267

@@ -216,7 +270,7 @@ var cleanup = &cobra.Command{
216270
Run: func(cmd *cobra.Command, args []string) {
217271
// Ask for confirmation
218272
fmt.Println("This will delete all containers and remove docker and wireguard networks")
219-
fmt.Println("Are you sure you want to continue? (y/n)")
273+
fmt.Print("Are you sure you want to continue? (y/n) : ")
220274
var response string
221275
_, err := fmt.Scanln(&response)
222276
if err != nil {
@@ -274,6 +328,9 @@ var cleanup = &cobra.Command{
274328
_ = IPTablesClient.ClearChain("nat", NatPostroutingChainName)
275329
_ = IPTablesClient.ClearChain("nat", NatInputChainName)
276330
_ = IPTablesClient.ClearChain("nat", NatOutputChainName)
331+
// Disable haproxy and data plane api
332+
disableHAProxy()
333+
cmd.Println("Haproxy and data plane api disabled")
277334
// Backup and remove the database file
278335
moveDBFilesToBackup()
279336
// Done

Diff for: agent/config.go

+8
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ const (
1515
type AgentConfig struct {
1616
ID uint `gorm:"primaryKey"`
1717
NodeType NodeType `json:"node_type" gorm:"column:node_type"`
18+
SwiftwaveServiceAddress string `json:"swiftwave_service_address" gorm:"column:swiftwave_service_address"`
1819
WireguardConfig WireguardConfig `json:"wireguard_config" gorm:"embedded;embeddedPrefix:wireguard_"`
1920
MasterNodeConnectConfig MasterNodeConnectConfig `json:"master_node_connect_config" gorm:"embedded;embeddedPrefix:master_node_connect_config_"`
2021
DockerNetwork DockerNetworkConfig `json:"docker_network" gorm:"embedded;embeddedPrefix:docker_network_"`
22+
HaproxyConfig HAProxyConfig `json:"haproxy_config" gorm:"embedded;embeddedPrefix:haproxy_"`
2123
}
2224

2325
type WireguardConfig struct {
@@ -37,6 +39,12 @@ type DockerNetworkConfig struct {
3739
Subnet string `json:"subnet" gorm:"column:subnet"`
3840
}
3941

42+
type HAProxyConfig struct {
43+
Enabled bool `json:"enabled" gorm:"column:enabled"`
44+
Username string `json:"username" gorm:"column:username"`
45+
Password string `json:"password" gorm:"column:password"`
46+
}
47+
4048
func GetConfig() (*AgentConfig, error) {
4149
var config AgentConfig
4250
if err := rDB.First(&config).Error; err != nil {

Diff for: agent/go.mod

+4-2
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,13 @@ require (
3030
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.34.0 // indirect
3131
go.opentelemetry.io/otel/sdk v1.34.0 // indirect
3232
golang.org/x/crypto v0.32.0 // indirect
33-
golang.org/x/mod v0.18.0 // indirect
33+
golang.org/x/exp v0.0.0-20250207012021-f9890c6ad9f3 // indirect
34+
golang.org/x/mod v0.22.0 // indirect
3435
golang.org/x/net v0.34.0 // indirect
3536
golang.org/x/sync v0.10.0 // indirect
3637
golang.org/x/text v0.21.0 // indirect
3738
golang.org/x/time v0.8.0 // indirect
38-
golang.org/x/tools v0.22.0 // indirect
39+
golang.org/x/tools v0.29.0 // indirect
3940
golang.zx2c4.com/wireguard v0.0.0-20231211153847-12269c276173 // indirect
4041
gotest.tools/v3 v3.5.1 // indirect
4142
)
@@ -58,6 +59,7 @@ require (
5859
github.com/opencontainers/image-spec v1.1.0 // indirect
5960
github.com/pkg/errors v0.9.1 // indirect
6061
github.com/spf13/cobra v1.8.1
62+
github.com/tredoe/osutil v1.5.0
6163
github.com/vishvananda/netlink v1.3.0
6264
github.com/vishvananda/netns v0.0.4 // indirect
6365
go.opentelemetry.io/auto/sdk v1.1.0 // indirect

Diff for: agent/go.sum

+6
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
116116
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
117117
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
118118
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
119+
github.com/tredoe/osutil v1.5.0 h1:UGVxbbHRoZi8xXVmbNZ2vgG6XoJ15ndE4LniiQ3rJKg=
120+
github.com/tredoe/osutil v1.5.0/go.mod h1:TEzphzUUunysbdDRfdOgqkg10POQbnfIPV50ynqOfIg=
119121
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
120122
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
121123
github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo=
@@ -149,10 +151,13 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U
149151
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
150152
golang.org/x/crypto v0.32.0 h1:euUpcYgM8WcP71gNpTqQCn6rC2t6ULUPiOzfWaXVVfc=
151153
golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc=
154+
golang.org/x/exp v0.0.0-20250207012021-f9890c6ad9f3 h1:qNgPs5exUA+G0C96DrPwNrvLSj7GT/9D+3WMWUcUg34=
155+
golang.org/x/exp v0.0.0-20250207012021-f9890c6ad9f3/go.mod h1:tujkw807nyEEAamNbDrEGzRav+ilXA7PCRAd6xsmwiU=
152156
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
153157
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
154158
golang.org/x/mod v0.18.0 h1:5+9lSbEzPSdWkH32vYPBwEpX8KwDbM52Ud9xBUvNlb0=
155159
golang.org/x/mod v0.18.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
160+
golang.org/x/mod v0.22.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
156161
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
157162
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
158163
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
@@ -188,6 +193,7 @@ golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roY
188193
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
189194
golang.org/x/tools v0.22.0 h1:gqSGLZqv+AI9lIQzniJ0nZDRG5GBPsSi+DRNHWNz6yA=
190195
golang.org/x/tools v0.22.0/go.mod h1:aCwcsjqvq7Yqt6TNyX7QMU2enbQ/Gt0bo6krSeEri+c=
196+
golang.org/x/tools v0.29.0/go.mod h1:KMQVMRsVxU6nHCFXrBPhDB8XncLNLM0lIy/F14RP588=
191197
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
192198
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
193199
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

Diff for: agent/haproxy_api.go

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package main
2+
3+
import (
4+
"net/http"
5+
"net/http/httputil"
6+
"net/url"
7+
"strings"
8+
9+
"github.com/labstack/echo/v4"
10+
)
11+
12+
func sendRequestToHAProxy(c echo.Context) error {
13+
config, err := GetConfig()
14+
if err != nil {
15+
return c.String(http.StatusNotFound, "Failed to fetch config")
16+
}
17+
if !config.HaproxyConfig.Enabled {
18+
return c.String(http.StatusNotFound, "Haproxy is not enabled")
19+
}
20+
// // Get the original request path and strip "/haproxy"
21+
// newPath := strings.TrimPrefix(c.Request().URL.Path, "/haproxy")
22+
23+
// Define the target URL
24+
targetURL, _ := url.Parse(DataplaneAPIBaseAddress)
25+
26+
// Create a reverse proxy
27+
proxy := httputil.NewSingleHostReverseProxy(targetURL)
28+
// Modify request before forwarding
29+
proxy.Director = func(req *http.Request) {
30+
req.URL.Scheme = targetURL.Scheme
31+
req.URL.Host = targetURL.Host
32+
req.URL.Path = strings.TrimPrefix(req.URL.Path, "/haproxy")
33+
req.Host = targetURL.Host
34+
35+
// Set Basic Auth Header
36+
req.SetBasicAuth(config.HaproxyConfig.Username, config.HaproxyConfig.Password)
37+
}
38+
39+
// Serve the request
40+
proxy.ServeHTTP(c.Response(), c.Request())
41+
return nil
42+
}
43+
44+
func getHAProxyStatus(c echo.Context) error {
45+
config, err := GetConfig()
46+
if err != nil {
47+
return c.JSON(http.StatusInternalServerError, Response{
48+
Message: "Failed to fetch config",
49+
Error: err.Error(),
50+
})
51+
}
52+
return c.JSON(http.StatusOK, Response{
53+
Message: "Successfully fetched status",
54+
Data: map[string]interface{}{
55+
"enabled": config.HaproxyConfig.Enabled,
56+
"haproxy_service_active": GetServiceStatus("haproxy"),
57+
"dataplaneapi_service_active": GetServiceStatus("dataplaneapi"),
58+
},
59+
})
60+
}

0 commit comments

Comments
 (0)