-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsafe_bot.go
143 lines (117 loc) · 2.88 KB
/
safe_bot.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"os/user"
"github.com/slack-go/slack"
"go.uber.org/zap"
)
type IP struct {
Query string
}
var logger *zap.Logger
// Initializes everything
func inital() (string, string, string) {
logger, _ = zap.NewProduction()
defer logger.Sync()
logger.Debug("Reading command line arguments")
// Grab the command line arguments
token := flag.String("t", "", "Slack Oauth Token")
channel := flag.String("c", "", "Slack Channel ID")
message := flag.String("m", "What's up dog", "Slack Message")
logLevel := flag.String("l", "info", "Log Level")
flag.Parse()
// Set the log level
switch *logLevel {
case "debug":
log.SetFlags(log.LstdFlags | log.Lshortfile)
logger.Debug("Log level set to debug")
case "info":
log.SetFlags(log.LstdFlags)
logger.Debug("Log level set to info")
case "warn":
log.SetFlags(log.LstdFlags)
logger.Debug("Log level set to warn")
case "error":
log.SetFlags(log.LstdFlags)
logger.Debug("Log level set to error")
case "fatal":
log.SetFlags(log.LstdFlags)
logger.Debug("Log level set to fatal")
default:
log.SetFlags(log.LstdFlags)
logger.Debug("Log level set to info")
}
return *token, *channel, *message
}
// Get internal IP for machine
func GetOutboundIP() string {
conn, err := net.Dial("udp", "8.8.8.8:80")
if err != nil {
log.Fatal(err)
}
defer conn.Close()
localAddr := conn.LocalAddr().(*net.UDPAddr)
return localAddr.IP.String()
}
// Where am I?
func getip2() string {
req, err := http.Get("http://ip-api.com/json/")
if err != nil {
return err.Error()
}
defer req.Body.Close()
body, err := ioutil.ReadAll(req.Body)
if err != nil {
return err.Error()
}
logger.Info(string(body))
var ip IP
json.Unmarshal(body, &ip)
return ip.Query
}
// Main function
func main() {
// Starting the logger
token, channel, message := inital()
logger, err := zap.NewProduction()
if err != nil {
panic(err)
}
defer logger.Sync()
// Starting the bot
logger.Debug("Starting slack bot")
// Initialize slack client
api := slack.New(token)
user, err := user.Current()
if err != nil {
logger.Error("Error getting current user", zap.Error(err))
}
me := user.Username
var ret_ip string
if message == "What's up dog" {
// Report the IP I am at since I didn't include a message in the argument (Potential security issue detection)
ret_ip = getip2()
} else {
// Report the internal IP since I sent a message in the argument
ret_ip = GetOutboundIP()
}
whoami := fmt.Sprintf(`{"Reporter": "%s", "WhereAmI":"%s"},{"Message":"%s"}`, me, ret_ip, message)
logger.Info(whoami)
// Message format
channelId, _, err := api.PostMessage(
channel,
slack.MsgOptionText(whoami, false),
)
// Error Checking
if err != nil {
panic(err)
}
// Success Log
logger.Debug("Sent Successfully", zap.String("Message", message), zap.String("Channel", channelId))
}