Skip to content

Commit

Permalink
fix panic(s) and properly handle multiple addresses / ports with dmsg…
Browse files Browse the repository at this point in the history
…web (#269)
  • Loading branch information
0pcom authored Jun 27, 2024
1 parent 27926d9 commit e8cd649
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
35 changes: 26 additions & 9 deletions cmd/dmsgweb/commands/dmsgweb.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,19 @@ dmsgweb conf file detected: ` + dmsgwebconffile
if err != nil {
pk, sk = cipher.GenerateKeyPair()
}
dmsgWebLog.Info("dmsg client pk: %v", pk.String())

dmsgWebLog.Info("dmsg client pk: ", pk.String())
if len(resolveDmsgAddr) > 0 {
dialPK = make([]cipher.PubKey, len(resolveDmsgAddr))
dmsgPorts = make([]uint, dmsgSessions)
for i, dmsgaddr := range resolveDmsgAddr {
dmsgWebLog.Info("dmsg address to dial: ", dmsgaddr)
dmsgAddr = strings.Split(dmsgaddr, ":")
err = dialPK[i].Set(dmsgAddr[0])
var setpk cipher.PubKey
err := setpk.Set(dmsgAddr[0])
if err != nil {
log.Fatalf("failed to parse dmsg <address>:<port> : %v", err)
}
dialPK[i] = setpk
if len(dmsgAddr) > 1 {
dport, err := strconv.ParseUint(dmsgAddr[1], 10, 64)
if err != nil {
Expand All @@ -197,6 +201,7 @@ dmsgweb conf file detected: ` + dmsgwebconffile
}
}
}
dmsgWebLog.Info("test")
dmsgC, closeDmsg, err := startDmsg(ctx, pk, sk)
if err != nil {
dmsgWebLog.WithError(err).Fatal("failed to start dmsg")
Expand Down Expand Up @@ -347,19 +352,31 @@ func proxyHTTPConn(n int) {
})
wg.Add(1)
go func() {
dmsgWebLog.Debug(fmt.Sprintf("Serving http on: http://127.0.0.1:%v", webPort))
r.Run(":" + fmt.Sprintf("%v", webPort)) //nolint
dmsgWebLog.Debug(fmt.Sprintf("Stopped serving http on: http://127.0.0.1:%v", webPort))
var thiswebport uint
if n == -1 {
thiswebport = webPort[0]
} else {
thiswebport = webPort[n]
}
dmsgWebLog.Debug(fmt.Sprintf("Serving http on: http://127.0.0.1:%v", thiswebport))
r.Run(":" + fmt.Sprintf("%v", thiswebport)) //nolint
dmsgWebLog.Debug(fmt.Sprintf("Stopped serving http on: http://127.0.0.1:%v", thiswebport))
wg.Done()
}()
}
func proxyTCPConn(n int) {
listener, err := net.Listen("tcp", fmt.Sprintf(":%d", webPort[n]))
var thiswebport uint
if n == -1 {
thiswebport = webPort[0]
} else {
thiswebport = webPort[n]
}
listener, err := net.Listen("tcp", fmt.Sprintf(":%v", thiswebport))
if err != nil {
log.Fatalf("Failed to start TCP listener on port %d: %v", webPort[n], err)
dmsgWebLog.Fatalf("Failed to start TCP listener on port %v: %v", thiswebport, err)
}
defer listener.Close() //nolint
log.Printf("Serving TCP on 127.0.0.1:%d", webPort[n])
log.Printf("Serving TCP on 127.0.0.1:%v", thiswebport)

for {
conn, err := listener.Accept()
Expand Down
9 changes: 8 additions & 1 deletion cmd/dmsgweb/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,14 @@ func scriptExecBoolSlice(s, envfile string) []bool {
y, err := script.Exec(fmt.Sprintf(`bash -c 'SKYENV=%s ; if [[ $SKYENV != "" ]] && [[ -f $SKYENV ]] ; then source $SKYENV ; fi ; for _i in %s ; do echo "$_i" ; done'`, envfile, s)).Slice()
if err == nil {
for _, item := range y {
result = append(result, item != "")
switch strings.ToLower(item) {
case "true":
result = append(result, true)
case "false":
result = append(result, false)
default:
result = append(result, false)
}
}
return result
}
Expand Down

0 comments on commit e8cd649

Please sign in to comment.