-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Setup Observe of Dots server #11 and Handle DB trigger when status ch…
…anged, implement notify observer #12
- Loading branch information
1 parent
e729092
commit 5be622d
Showing
10 changed files
with
432 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"io" | ||
"net" | ||
"os" | ||
"strconv" | ||
"strings" | ||
|
||
log "github.com/sirupsen/logrus" | ||
"github.com/nttdots/go-dots/dots_server/models" | ||
"github.com/nttdots/go-dots/libcoap" | ||
"github.com/nttdots/go-dots/dots_server/controllers" | ||
"github.com/nttdots/go-dots/dots_common/messages" | ||
) | ||
|
||
var port = 9999 | ||
|
||
/* | ||
* Listen for notification from DB | ||
*/ | ||
func listenDB (context *libcoap.Context) { | ||
listen, err := net.Listen("tcp4", ":" + strconv.Itoa(port)) | ||
if err != nil { | ||
log.Debugf("[MySQL-Notification]:Socket listening on port %+v failed,%+v", port, err) | ||
os.Exit(1) | ||
} | ||
log.Debugf("[MySQL-Notification]:Begin listening on port: %+v", port) | ||
|
||
for { | ||
conn, err := listen.Accept() | ||
if err != nil { | ||
log.Debugf("[MySQL-Notification]:Error : %+v", err) | ||
continue | ||
} | ||
go handler(conn, context) | ||
} | ||
|
||
} | ||
|
||
/* | ||
* Handle notifcation from DB | ||
*/ | ||
func handler(conn net.Conn, context *libcoap.Context) { | ||
|
||
defer conn.Close() | ||
|
||
var ( | ||
buf = make([]byte, 1024) | ||
r = bufio.NewReader(conn) | ||
) | ||
|
||
ILOOP: | ||
for { | ||
n, err := r.Read(buf) | ||
data := string(buf[:n]) | ||
|
||
switch err { | ||
case io.EOF: | ||
break ILOOP | ||
case nil: | ||
log.Debugf("[MySQL-Notification]: Received mitigation status changed notification from DB for :", data) | ||
if isTransportOver(data) { | ||
break ILOOP | ||
} | ||
// Notify status changed to those clients who are observing this mitigation request | ||
log.Debug("[MySQL-Notification]: Send notification if obsevers exists") | ||
uriPath := messages.MessageTypes[messages.MITIGATION_REQUEST].Path | ||
id, cuid, mid, status, query := context.NotifyOnce(data, uriPath) | ||
|
||
idValue, iErr := strconv.ParseInt(id, 10, 64) | ||
midValue, mErr := strconv.Atoi(mid) | ||
statusValue, sErr := strconv.Atoi(status) | ||
if iErr != nil || mErr != nil || sErr != nil { | ||
log.Debugf("[MySQL-Notification]:Failed to parse string to integer") | ||
return | ||
} | ||
// If mitigation status was changed to 6: (attack mitigation is now terminated), delete this mitigation after notifying | ||
if statusValue == models.Terminated { | ||
log.Debugf("[MySQL-Notification]: Mitigation was terminated. Delete corresponding sub-resource and mitigation request.", models.Terminated) | ||
context.DeleteResourceByQuery(query) | ||
controllers.DeleteMitigation(0, cuid, midValue, idValue) | ||
} | ||
default: | ||
log.Debugf("[MySQL-Notification]: Failed to receive data:%+v", err) | ||
return | ||
} | ||
} | ||
} | ||
|
||
/* | ||
* Check if nofified data has been transported completely | ||
*/ | ||
func isTransportOver(data string) (over bool) { | ||
over = strings.HasSuffix(data, "\r\n\r\n") | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.