Skip to content

Commit dd2f9b5

Browse files
committed
Resolve server host issue
1 parent dea49e6 commit dd2f9b5

10 files changed

Lines changed: 48 additions & 48 deletions

File tree

api/info.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,12 @@ func challengeInfoHandler(c *gin.Context) {
256256
PreRequisite: strings.Split(challenge.PreReqs, core.DELIMITER),
257257
DeployedStatus: challenge.Status,
258258
}
259+
deployedHost := challenge.ServerDeployed
260+
if deployedHost != core.LOCALHOST && deployedHost != "" {
261+
if s, ok := cfg.Cfg.AvailableServers[deployedHost]; ok {
262+
deployedHost = s.Host
263+
}
264+
}
259265
challengeInfo := Challenge{
260266
ChallengeMetadata: challMetadata,
261267
Description: challenge.Description,
@@ -265,7 +271,7 @@ func challengeInfoHandler(c *gin.Context) {
265271
AdditionalLinks: strings.Split(challenge.AdditionalLinks, core.DELIMITER),
266272
PreviousTries: previousTries,
267273
MaxAttemptLimit: challenge.MaxAttemptLimit,
268-
DeployedLink: challenge.ServerDeployed,
274+
DeployedLink: deployedHost,
269275
}
270276
if user.Role == core.USER_ROLES["contestant"] {
271277
c.JSON(http.StatusOK, challengeInfo)

cmd/beast/config.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,14 @@ func promptServerDetails(configuration *config.BeastConfig) {
115115
var server config.AvailableServer
116116

117117
server.Host = utils.PromptString("Enter Host Name, leave empty for localhost")
118+
if server.Host == "" {
119+
server.Host = core.LOCALHOST
120+
}
118121
server.Username = utils.PromptString("Enter Username")
119122
server.SSHKeyPath = utils.PromptString("Enter SSH Key Path")
120123
server.Active = utils.PromptBinary("Enable this server?")
121124

122-
configuration.AvailableServers[server.Username] = server
125+
configuration.AvailableServers[server.Host] = server
123126
}
124127
}
125128

core/config/config.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"os"
99
"path/filepath"
1010
"regexp"
11+
"strings"
1112
"time"
1213

1314
"github.com/sdslabs/beastv4/core"
@@ -126,7 +127,6 @@ type BeastConfig struct {
126127
HealthProber bool `toml:"health_prober"`
127128
RemoteSyncPeriod time.Duration `toml:"-"`
128129
Rsp string `toml:"remote_sync_period"`
129-
LocalHostPortRange string `toml:"local_host_port_range"`
130130
InstanceConfig InstanceConfig `toml:"instance_config"`
131131

132132
CPUShares int64 `toml:"default_cpu_shares"`
@@ -225,15 +225,23 @@ func (config *BeastConfig) ValidateConfig() error {
225225
log.Warn("No available servers provided for challenges. Using default localhost")
226226
config.AvailableServers = map[string]AvailableServer{
227227
core.LOCALHOST: {
228+
Name: core.LOCALHOST,
228229
Host: core.LOCALHOST,
229230
Username: os.Getenv("USER"),
230231
SSHKeyPath: "",
231232
Active: true,
233+
PortRange: fmt.Sprintf("%v%s%v", core.ALLOWED_MIN_PORT_VALUE, core.MappingDelimeter, core.ALLOWED_MAX_PORT_VALUE),
232234
},
233235
}
234236
}
235237

236-
for _, server := range config.AvailableServers {
238+
for name, server := range config.AvailableServers {
239+
if strings.Contains(name, ":") {
240+
return fmt.Errorf("server key %q contains invalid character ':'", name)
241+
}
242+
243+
server.Name = name
244+
config.AvailableServers[name] = server
237245
if server.Active {
238246
err := server.ValidateServerConfig()
239247
if err != nil {
@@ -284,11 +292,6 @@ func (config *BeastConfig) ValidateConfig() error {
284292
}
285293
}
286294

287-
err = ValidatePortRange(config.LocalHostPortRange)
288-
if err != nil {
289-
return fmt.Errorf("error while validating port range in global beast config: %s", err)
290-
}
291-
292295
if config.CPUShares <= 0 {
293296
log.Debug("Per container CPU shares not provided using default value")
294297
config.CPUShares = core.DEFAULT_CPU_SHARE
@@ -314,6 +317,7 @@ func (config *BeastConfig) ValidateConfig() error {
314317
}
315318

316319
type AvailableServer struct {
320+
Name string `toml:"-"`
317321
Host string `toml:"host"`
318322
Username string `toml:"username"`
319323
SSHKeyPath string `toml:"ssh_key_path"`

core/constants.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,3 +222,4 @@ var NOTIFICATION_SERVICES = []string{
222222
"discord",
223223
}
224224

225+
const MappingDelimeter = ":"

core/manager/challenge.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -698,11 +698,9 @@ func undeployChallenge(challengeName string, purge bool) error {
698698
}
699699
}
700700

701-
var host string
702-
if challenge.ServerDeployed == core.LOCALHOST || challenge.ServerDeployed == "" {
701+
host := challenge.ServerDeployed
702+
if host == "" {
703703
host = core.LOCALHOST
704-
} else {
705-
host = config.Cfg.AvailableServers[challenge.ServerDeployed].Host
706704
}
707705

708706
err = cache.FreeContainerPortsOnHost(host, challenge.ContainerId)

core/manager/health_check.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,14 @@ func ChallengesHealthProber(waitTime int) {
8787
// Do a better job at health probing mechanism.
8888
if len(allocatedPorts) > 0 {
8989
port := int(allocatedPorts[0].PortNo)
90+
probeHost := chall.ServerDeployed
91+
if probeHost != core.LOCALHOST && probeHost != "" {
92+
if s, ok := config.Cfg.AvailableServers[probeHost]; ok {
93+
probeHost = s.Host
94+
}
95+
}
9096
prober := probes.NewTcpProber()
91-
result, err := prober.Probe(chall.ServerDeployed, port, time.Duration(core.DEFAULT_PROBE_TIMEOUT)*time.Second)
97+
result, err := prober.Probe(probeHost, port, time.Duration(core.DEFAULT_PROBE_TIMEOUT)*time.Second)
9298
if err != nil {
9399
msg := fmt.Sprintf("NETWORK HEALTH CHECK %s: %s : %s", result, chall.Name, err)
94100
log.WithFields(log.Fields{

core/manager/instance.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -269,12 +269,8 @@ func allocateInstancePort(host string) (uint32, error) {
269269
var firstPort, lastPort uint32
270270
var err error
271271

272-
if host == core.LOCALHOST || host == "" {
273-
firstPort, lastPort, err = utils.ParsePortMapping(cfg.Cfg.LocalHostPortRange)
274-
} else {
275-
server := cfg.Cfg.AvailableServers[host]
276-
firstPort, lastPort, err = utils.ParsePortMapping(server.PortRange)
277-
}
272+
server := cfg.Cfg.AvailableServers[host]
273+
firstPort, lastPort, err = utils.ParsePortMapping(server.PortRange)
278274

279275
if err != nil {
280276
return 0, fmt.Errorf("failed to parse port range: %w", err)
@@ -291,8 +287,8 @@ func allocateInstancePort(host string) (uint32, error) {
291287

292288
func selectServerForInstance() string {
293289
availableServer, err := remoteManager.ServerQueue.GetNextAvailableInstance()
294-
if err == nil && availableServer.Host != "" {
295-
return availableServer.Host
290+
if err == nil && availableServer.Name != "" {
291+
return availableServer.Name
296292
}
297293
return core.LOCALHOST
298294
}

core/manager/pipeline.go

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -273,11 +273,8 @@ func deployChallenge(challenge *database.Challenge, config cfg.BeastChallengeCon
273273
return fmt.Errorf("failed to extract port variables: %w", err)
274274
}
275275

276-
var serverDeployed string
277-
if challenge.ServerDeployed != core.LOCALHOST && challenge.ServerDeployed != "" {
278-
server := cfg.Cfg.AvailableServers[challenge.ServerDeployed]
279-
serverDeployed = server.Host
280-
} else {
276+
serverDeployed := challenge.ServerDeployed
277+
if serverDeployed == "" {
281278
serverDeployed = core.LOCALHOST
282279
}
283280

@@ -356,18 +353,15 @@ func deployChallenge(challenge *database.Challenge, config cfg.BeastChallengeCon
356353
)
357354

358355
var err error
359-
var host string
360-
var firstPort, lastPort uint32
361-
if challenge.ServerDeployed == core.LOCALHOST || challenge.ServerDeployed == "" {
356+
host := challenge.ServerDeployed
357+
if host == "" {
362358
host = core.LOCALHOST
363-
firstPort, lastPort, err = utils.ParsePortMapping(cfg.Cfg.LocalHostPortRange)
364-
} else {
365-
server := cfg.Cfg.AvailableServers[challenge.ServerDeployed]
366-
367-
host = server.Host
368-
firstPort, lastPort, err = utils.ParsePortMapping(server.PortRange)
369359
}
370360

361+
var firstPort, lastPort uint32
362+
server := cfg.Cfg.AvailableServers[host]
363+
firstPort, lastPort, err = utils.ParsePortMapping(server.PortRange)
364+
371365
if err != nil {
372366
return fmt.Errorf("error while allocating ports on server %s for challenge %s: %s", host, challenge.Name, err.Error())
373367
}

core/manager/utils.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ func UpdateOrCreateChallengeDbEntry(challEntry *database.Challenge, config cfg.B
492492
availableServerHostname := core.LOCALHOST
493493
if config.Challenge.Metadata.Type != core.STATIC_CHALLENGE_TYPE_NAME {
494494
availableServer, _ := remoteManager.ServerQueue.GetNextAvailableInstance()
495-
availableServerHostname = availableServer.Host
495+
availableServerHostname = availableServer.Name
496496
}
497497
if config.Challenge.Metadata.Difficulty == "" {
498498
log.Debug("Setting difficulty to default(medium)")
@@ -570,14 +570,7 @@ func UpdateOrCreateChallengeDbEntry(challEntry *database.Challenge, config cfg.B
570570
}
571571

572572
if challEntry.ContainerId != "" {
573-
var host string
574-
if challEntry.ServerDeployed == core.LOCALHOST || challEntry.ServerDeployed == "" {
575-
host = core.LOCALHOST
576-
} else {
577-
host = cfg.Cfg.AvailableServers[challEntry.ServerDeployed].Host
578-
}
579-
580-
hostPorts, err := cache.GetContainerPortsOnHost(host, challEntry.ContainerId)
573+
hostPorts, err := cache.GetContainerPortsOnHost(challEntry.ServerDeployed, challEntry.ContainerId)
581574
if err != nil {
582575
return fmt.Errorf("error while parsing host port for challenge %s : %s", challEntry.Name, err)
583576
}

utils/datatypes.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ package utils
33
import (
44
"errors"
55
"fmt"
6+
"github.com/sdslabs/beastv4/core"
67
"strconv"
78
"strings"
89
)
910

10-
const mappingDelimeter = ":"
11-
1211
// From a list of strings generate a list containing only unique strings
1312
// from the list.
1413
func GetUniqueStrings(list []string) []string {
@@ -48,7 +47,7 @@ func UInt32InList(a uint32, list []uint32) bool {
4847
// If the portMapping string is not valid, this returns an error.
4948
// The format of the port mapping is `PORT_FIRST:PORT_LAST`
5049
func ParsePortMapping(portMap string) (uint32, uint32, error) {
51-
ports := strings.Split(portMap, mappingDelimeter)
50+
ports := strings.Split(portMap, core.MappingDelimeter)
5251

5352
if len(ports) != 2 {
5453
return 0, 0, errors.New("port mapping string is not valid")

0 commit comments

Comments
 (0)