-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
145 lines (120 loc) · 3.1 KB
/
main.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
144
145
package main
import (
"encoding/json"
"fmt"
"log"
"net"
"net/http"
"sync"
"time"
)
const rtspPort = 554
func logRequest(handlerFunc http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
log.Printf("Received request: Method=%s URL=%s From=%s", r.Method, r.URL.Path, r.RemoteAddr)
recorder := &statusRecorder{ResponseWriter: w, statusCode: http.StatusOK}
handlerFunc(recorder, r)
log.Printf("Responded: Status=%d Duration=%s", recorder.statusCode, time.Since(start))
}
}
type statusRecorder struct {
http.ResponseWriter
statusCode int
}
func checkRTSP(ip string) bool {
address := fmt.Sprintf("%s:%d", ip, rtspPort)
conn, err := net.DialTimeout("tcp", address, 50*time.Millisecond)
if err != nil {
return false
}
conn.Close()
return true
}
func getIPsInNetwork(network *net.IPNet) []string {
var ips []string
for ip := network.IP.Mask(network.Mask); network.Contains(ip); incrementIP(ip) {
ips = append(ips, ip.String())
}
return ips
}
func incrementIP(ip net.IP) {
for j := len(ip) - 1; j >= 0; j-- {
ip[j]++
if ip[j] > 0 {
break
}
}
}
func scanIPs(ips []string) []string {
var wg sync.WaitGroup
var mu sync.Mutex
var devices []string
for _, ip := range ips {
wg.Add(1)
go func(ip string) {
defer wg.Done()
if checkRTSP(ip) {
mu.Lock()
devices = append(devices, ip)
mu.Unlock()
}
}(ip)
}
wg.Wait()
return devices
}
func getLocalNetworks() ([]*net.IPNet, error) {
var networks []*net.IPNet
interfaces, err := net.Interfaces()
if err != nil {
return nil, err
}
for _, iface := range interfaces {
if iface.Flags&net.FlagUp == 0 || iface.Flags&net.FlagLoopback != 0 {
continue
}
addrs, err := iface.Addrs()
if err != nil {
log.Printf("Error getting addresses for interface %s: %v", iface.Name, err)
continue
}
for _, addr := range addrs {
if ipNet, ok := addr.(*net.IPNet); ok && ipNet.IP.To4() != nil {
if ipNet.IP[0] == 172 {
log.Printf("Excluding network: Interface=%s IP=%s Network=%s", iface.Name, ipNet.IP, ipNet)
continue
}
networks = append(networks, ipNet)
log.Printf("Found network: Interface=%s IP=%s Network=%s", iface.Name, ipNet.IP, ipNet)
}
}
}
if len(networks) == 0 {
log.Println("No active networks found.")
}
return networks, nil
}
func handleGetAllRTSPDevices(w http.ResponseWriter, r *http.Request) {
networks, err := getLocalNetworks()
if err != nil {
http.Error(w, fmt.Sprintf("Error determining local networks: %v", err), http.StatusInternalServerError)
return
}
var allDevices []string
for _, network := range networks {
ips := getIPsInNetwork(network)
devices := scanIPs(ips)
allDevices = append(allDevices, devices...)
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(allDevices)
log.Println("Found cameras:", allDevices)
}
func main() {
fmt.Println("Starting server on :7654...")
http.HandleFunc("/get_all_rtsp_cameras/", logRequest(handleGetAllRTSPDevices))
if err := http.ListenAndServe(":7654", nil); err != nil {
log.Fatalf("Error starting server: %v", err)
}
}