Commit 036ece75 authored by Thomas Bella's avatar Thomas Bella

Add MAC address lookup

parent d88d4593
Pipeline #1060 passed with stages
in 1 minute and 15 seconds
......@@ -8,6 +8,7 @@
# Previous builds
/build/*
/cmd/uTeleBot/debug
# Mac OS X files
.DS_Store
......
......@@ -2,7 +2,10 @@
uTeleBot is a bot written in Go which provides some useful tools for IT people.
Coverage report is available at [![coverage report](https://git.bella.network/thomas2500/utelebot/badges/master/coverage.svg)](https://thomas2500.dev.bella.ml/utelebot/) | [![pipeline status](https://git.bella.network/playground/golang/badges/master/pipeline.svg)](https://git.bella.network/Thomas2500/utelebot/pipelines)
## TODO
- Don't depend on mac API
- Don't depend on MAC address API
- Download fom http://standards-oui.ieee.org/oui.txt, parse and update periodically
......@@ -2,6 +2,7 @@ package main
var availableCommands = map[string]string{
"dns": "Perform a DNS lookup",
"mac": "Get the vendor of a MAC address",
}
func commandHelp() string {
......@@ -12,7 +13,7 @@ Available commands:
`
for name, desc := range availableCommands {
help += "*`" + name + "`*: " + desc + "\n"
help += "*" + name + "*: " + desc + "\n"
}
help += `
......
......@@ -20,8 +20,6 @@ func main() {
log.Fatal(err)
}
bot.Debug = true
log.Printf("Authorized on account %s", bot.Self.UserName)
// Set webhook target on program start
......
......@@ -3,6 +3,8 @@ package main
import (
"strings"
"git.bella.network/playground/golang/internal/macfetch"
"git.bella.network/playground/golang/internal/dnsfetch"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
)
......@@ -39,9 +41,12 @@ func processMessage(bot *tgbotapi.BotAPI, update tgbotapi.Update) {
}
}
case "hello":
msg.Text = "Hello World!"
case "mac":
if len(args) == 0 || len(args[0]) == 0 {
msg.Text = macfetch.GetHelp()
} else {
msg.Text = macfetch.GetStringResponse(args[0])
}
default:
// We don't know this command - skip it
return
......
......@@ -121,9 +121,9 @@ func fetch(target string, record string) (DNSresponse, error) {
}
defer response.Body.Close()
// Read content of release.json into variable
// Limit file size to 8 MB
content, err := ioutil.ReadAll(io.LimitReader(response.Body, 8<<20))
// Read content CF DoH response into variable
// Limit file size to 2 MB
content, err := ioutil.ReadAll(io.LimitReader(response.Body, 2<<20))
if err != nil {
return DNSresponse{}, err
}
......
package macfetch
import (
"io"
"io/ioutil"
"net/http"
"strings"
"git.bella.network/playground/golang/pkg/securetls"
)
var client *http.Client
func init() {
// Get TLS config
client = securetls.GetClient()
}
// GetStringResponse fetches the vendor data of a given MAC address
func GetStringResponse(mac string) string {
// Add mac padding to extend short mac to long version
if len(mac) <= 12 {
for i := len(mac); i <= 12; i++ {
mac += "0"
}
}
// Request lookup
req, _ := http.NewRequest("GET", "https://api.macvendors.com/"+mac, nil)
response, err := client.Do(req)
if err != nil {
return err.Error()
}
defer response.Body.Close()
// Read content into variable
// Limit response size to 1 MB
content, err := ioutil.ReadAll(io.LimitReader(response.Body, 1<<20))
if err != nil {
return err.Error()
}
// Send response to client
return strings.TrimSpace(string(content))
}
// GetHelp returns the help text of the command
func GetHelp() string {
return "*MAC Vendor Lookup*\nOutputs the vendor of a MAC address.\nAccepts long, short and extreme-short MAC addresses.\n`/mac 00:20:91:AB:CD:EF` or `/mac 002091ABCDEF` or `/mac 002091`"
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment