Skip to content

Instantly share code, notes, and snippets.

@BennyThink
Created April 30, 2022 13:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save BennyThink/c78cefa446fc492209710796ee342dec to your computer and use it in GitHub Desktop.
Save BennyThink/c78cefa446fc492209710796ee342dec to your computer and use it in GitHub Desktop.
Know who is viewing your miCam
package main
import (
"bufio"
"crypto/tls"
"fmt"
log "github.com/sirupsen/logrus"
"golang.org/x/time/rate"
"net/http"
"os/exec"
"regexp"
"runtime"
"strings"
"time"
)
const (
bucketSize = 70
bucketRate = 35
)
var limiter = rate.NewLimiter(bucketRate, bucketSize)
func runner() {
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
var c string
//c = "ping localhost"
c = "/usr/bin/tcpdump -i br-lan udp and host 192.168.7.35"
args := strings.Fields(c)
cmd := exec.Command(args[0], args[1:]...)
stderr, _ := cmd.StdoutPipe()
err := cmd.Start()
if err != nil {
log.Fatalln("Running tcpdump error", err)
}
scanner := bufio.NewScanner(stderr)
// 21:10:08.443881 IP 110.43.47.109.23647 > chuangmi_camera_029a02.lan.22732: UDP, length 24
// 16:49:18.905980 IP chuangmi.lan.13701 > 120.92.158.164.32100: UDP, length 48
re1 := regexp.MustCompile(`IP chuangmi.*\.lan\.\d+ > \d+\.\d+\.\d+\.\d+\.\d+: UDP`)
re2 := regexp.MustCompile(`IP \d+\.\d+\.\d+\.\d+\.\d+ > chuangmi.*\.lan\.\d+: UDP`)
//re2 := regexp.MustCompile(`icmp`)
for scanner.Scan() {
m := scanner.Text()
result1 := re1.FindString(m)
result2 := re2.FindString(m)
if len(result1) != 0 || len(result2) != 0 {
if limiter.Allow() {
log.Infof("Potential request: %s", m)
} else {
log.Warnln("Potential request, sending now...")
sendAlert(m)
}
}
}
}
func sendAlert(viewInfo string) {
var cstZone = time.FixedZone("GMT", 8*3600)
var nowTime = time.Now().In(cstZone).Format("2006-01-02 15:04:05")
message := fmt.Sprintf("%s == %s\n", nowTime, viewInfo)
log.Infoln(message)
}
func setupLogger() {
log.SetReportCaller(true)
Formatter := &log.TextFormatter{
EnvironmentOverrideColors: true,
FullTimestamp: true,
TimestampFormat: "2006-01-02 15:04:05",
CallerPrettyfier: func(f *runtime.Frame) (string, string) {
return fmt.Sprintf("[%s()]", f.Function), ""
},
}
log.SetFormatter(Formatter)
}
func main() {
setupLogger()
fmt.Println("Watcher is running.")
runner()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment