forked from criblio/appscope
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1326429
commit a8357d9
Showing
5 changed files
with
123 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/criblio/scope/listener" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// listenerCmd represents the listener command | ||
var listenerCmd = &cobra.Command{ | ||
Use: "listener [flags]", | ||
Short: "Run the scope listener", | ||
Long: `Listen to the Appscope events.`, | ||
Example: `scope listener | ||
scope listener --listen localhost:9999 --slacktoken example_token`, | ||
Args: cobra.NoArgs, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
slackToken, _ := cmd.Flags().GetString("slacktoken") | ||
listenDest, _ := cmd.Flags().GetString("listen") | ||
listener.Listener(listenDest, slackToken) | ||
}, | ||
} | ||
|
||
func init() { | ||
listenerCmd.Flags().StringP("listen", "", "", "Set listening source (host:port defaults to tls://)") | ||
listenerCmd.Flags().StringP("slacktoken", "", "", "Token to Slack") | ||
listenerCmd.MarkFlagRequired("listen") | ||
listenerCmd.MarkFlagRequired("slacktoken") | ||
|
||
RootCmd.AddCommand(listenerCmd) | ||
} |
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,56 @@ | ||
package listener | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
|
||
"github.com/criblio/scope/notify" | ||
"github.com/criblio/scope/util" | ||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
func listenServer(listenSource string, notifyChan chan<- string) { | ||
listen, err := net.Listen("tcp", listenSource) | ||
if err != nil { | ||
util.ErrAndExit("Listen: %s", err) | ||
} | ||
defer listen.Close() | ||
for { | ||
conn, err := listen.Accept() | ||
if err != nil { | ||
util.ErrAndExit("Accept %s", err) | ||
} | ||
go handleRequest(conn, notifyChan) | ||
} | ||
} | ||
|
||
func Listener(listenSource string, slacktoken string) { | ||
slackChan := make(chan string, 1) | ||
slackSender := notify.NewSlackSender(slacktoken, "testchannel") | ||
go listenServer(listenSource, slackChan) | ||
for { | ||
select { | ||
case slackEvent := <-slackChan: | ||
log.Debug().Msgf("Notification event: %s", slackEvent) | ||
slackSender.Notify(slackEvent) | ||
} | ||
} | ||
} | ||
|
||
// Handles incoming requests. | ||
func handleRequest(conn net.Conn, notifyChan chan<- string) { | ||
// Make a buffer to hold incoming data. | ||
buf := make([]byte, 1024) | ||
// Read the incoming connection into the buffer. | ||
_, err := conn.Read(buf) | ||
if err != nil { | ||
fmt.Println("Error reading:", err.Error()) | ||
} | ||
// if notifyLogic { | ||
notifyChan <- "test" | ||
// } | ||
// Send a response back to person contacting us. | ||
conn.Write([]byte("Message received.")) | ||
// Close the connection when you're done with it. | ||
conn.Close() | ||
} |
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,30 @@ | ||
package notify | ||
|
||
import "github.com/slack-go/slack" | ||
|
||
type SlackSender struct { | ||
client *slack.Client | ||
channelId string | ||
} | ||
|
||
func getChannelId() string { | ||
return "C05HBUJTC9W" | ||
} | ||
|
||
// NewSlackSender | ||
func NewSlackSender(token string, channelName string) *SlackSender { | ||
ss := &SlackSender{ | ||
client: slack.New(token), | ||
channelId: getChannelId(), | ||
} | ||
|
||
return ss | ||
} | ||
|
||
// perform notification via post message to slack | ||
func (s *SlackSender) Notify(msg string) { | ||
s.client.PostMessage(s.channelId, | ||
slack.MsgOptionText(msg, false), | ||
slack.MsgOptionAttachments(), | ||
slack.MsgOptionAsUser(false)) | ||
} |