2023-03-07 02:02:36 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"embed"
|
2024-07-18 00:09:18 +08:00
|
|
|
"fmt"
|
2023-03-07 02:02:36 +08:00
|
|
|
"html/template"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2024-07-18 00:09:18 +08:00
|
|
|
"sync/atomic"
|
2023-03-07 02:02:36 +08:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
//go:embed template.html
|
|
|
|
var embedFS embed.FS
|
|
|
|
var htmlTemplate *template.Template
|
|
|
|
|
|
|
|
type clientState int
|
|
|
|
|
|
|
|
const (
|
2024-07-18 00:09:18 +08:00
|
|
|
INITIAL = iota
|
|
|
|
ACTIVE
|
2023-03-07 02:02:36 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type client struct {
|
|
|
|
channel chan string
|
|
|
|
state clientState
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2024-07-18 00:09:18 +08:00
|
|
|
clients = make([]*client, 0)
|
|
|
|
clientMutex sync.Mutex
|
2023-03-07 02:02:36 +08:00
|
|
|
)
|
|
|
|
|
2024-07-18 00:09:18 +08:00
|
|
|
func deleteClient(client *client) {
|
|
|
|
for i := 0; i < len(clients); i++ {
|
|
|
|
if clients[i] == client {
|
|
|
|
clients[i] = clients[len(clients)-1]
|
|
|
|
clients = clients[:len(clients)-1]
|
|
|
|
return
|
|
|
|
}
|
2024-06-18 20:48:53 +08:00
|
|
|
}
|
2023-03-07 02:02:36 +08:00
|
|
|
}
|
|
|
|
|
2024-07-18 00:09:18 +08:00
|
|
|
func httpServer() error {
|
2023-03-07 02:02:36 +08:00
|
|
|
htmlTemplate = template.Must(template.ParseFS(embedFS, "template.html"))
|
|
|
|
http.HandleFunc("/stream", stream)
|
|
|
|
http.HandleFunc("/", serveRoot)
|
2024-07-18 00:09:18 +08:00
|
|
|
return http.ListenAndServe(":9090", nil)
|
2023-03-07 02:02:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func getInterfaceBaseIP() string {
|
|
|
|
iFace, err := net.InterfaceByName(interfaceName)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
addresses, err := iFace.Addrs()
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
gua := ""
|
|
|
|
ula := ""
|
|
|
|
for _, v := range addresses {
|
|
|
|
addr := v.String()
|
|
|
|
if !strings.Contains(addr, ":") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
_, anet, err := net.ParseCIDR(addr)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if anet.IP.IsLinkLocalUnicast() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if anet.IP.IsGlobalUnicast() {
|
|
|
|
gua = strings.Split(anet.String(), "/")[0]
|
|
|
|
}
|
|
|
|
if anet.IP.IsPrivate() {
|
|
|
|
ula = strings.Split(anet.String(), "/")[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if gua != "" {
|
|
|
|
return gua
|
|
|
|
} else {
|
|
|
|
return ula
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func serveRoot(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.RequestURI != "/" {
|
2024-07-18 00:09:18 +08:00
|
|
|
http.Error(w, "Not found", http.StatusNotFound)
|
2023-03-07 02:02:36 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
type pageData struct {
|
|
|
|
BaseIP string
|
|
|
|
CanvasWidth int
|
|
|
|
CanvasHeight int
|
|
|
|
}
|
|
|
|
baseIP := getInterfaceBaseIP()
|
|
|
|
if len(baseIP) == 21 {
|
|
|
|
baseIP = strings.TrimSuffix(baseIP, ":")
|
|
|
|
}
|
|
|
|
err := htmlTemplate.Execute(w, pageData{
|
|
|
|
BaseIP: baseIP,
|
|
|
|
CanvasHeight: 512,
|
|
|
|
CanvasWidth: 512,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2024-07-18 00:09:18 +08:00
|
|
|
fmt.Println("Error executing HTML template:", err)
|
2023-03-07 02:02:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-18 00:09:18 +08:00
|
|
|
var streamServerRunning atomic.Bool
|
|
|
|
|
2023-03-07 02:02:36 +08:00
|
|
|
func streamServer() {
|
2024-07-18 00:09:18 +08:00
|
|
|
if !streamServerRunning.CompareAndSwap(false, true) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
clientMutex.Lock()
|
|
|
|
if len(clients) == 0 {
|
|
|
|
streamServerRunning.Store(false)
|
|
|
|
clientMutex.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
requiresInitial := false
|
|
|
|
requiresUpdate := false
|
|
|
|
for _, v := range clients {
|
|
|
|
if v.state == INITIAL {
|
|
|
|
requiresInitial = true
|
2023-03-07 02:02:36 +08:00
|
|
|
} else {
|
2024-07-18 00:09:18 +08:00
|
|
|
requiresUpdate = true
|
|
|
|
}
|
|
|
|
if requiresInitial && requiresUpdate {
|
2023-03-07 02:02:36 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-18 00:09:18 +08:00
|
|
|
dataInitial, dataUpdate := getPicture(requiresInitial, requiresUpdate)
|
|
|
|
tmp := clients[:0]
|
|
|
|
for _, v := range clients {
|
|
|
|
if v.state == INITIAL {
|
|
|
|
v.state = ACTIVE
|
2023-03-07 02:02:36 +08:00
|
|
|
select {
|
2024-07-18 00:09:18 +08:00
|
|
|
case v.channel <- dataInitial:
|
2023-03-07 02:02:36 +08:00
|
|
|
default:
|
2024-07-18 00:09:18 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if dataUpdate != "0" {
|
|
|
|
select {
|
|
|
|
case v.channel <- dataUpdate:
|
|
|
|
default:
|
|
|
|
// Client cannot keep up
|
|
|
|
close(v.channel)
|
|
|
|
continue
|
|
|
|
}
|
2023-03-07 02:02:36 +08:00
|
|
|
}
|
|
|
|
}
|
2024-07-18 00:09:18 +08:00
|
|
|
tmp = append(tmp, v)
|
2023-03-07 02:02:36 +08:00
|
|
|
}
|
2024-07-18 00:09:18 +08:00
|
|
|
clients = tmp
|
|
|
|
clientMutex.Unlock()
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
2023-03-07 02:02:36 +08:00
|
|
|
}
|
2024-07-18 00:09:18 +08:00
|
|
|
}()
|
2023-03-07 02:02:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func stream(w http.ResponseWriter, r *http.Request) {
|
|
|
|
flusher, ok := w.(http.Flusher)
|
|
|
|
if !ok {
|
|
|
|
http.Error(w, "Streaming unsupported!", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-07-18 00:09:18 +08:00
|
|
|
streamServer()
|
2023-03-07 02:02:36 +08:00
|
|
|
w.Header().Set("Content-Type", "text/event-stream")
|
|
|
|
w.Header().Set("Cache-Control", "no-cache")
|
|
|
|
w.Header().Set("Connection", "keep-alive")
|
|
|
|
messageChan := make(chan string, 40)
|
2024-07-18 00:09:18 +08:00
|
|
|
newClient := &client{
|
2023-03-07 02:02:36 +08:00
|
|
|
channel: messageChan,
|
|
|
|
state: INITIAL,
|
|
|
|
}
|
|
|
|
clientMutex.Lock()
|
2024-07-18 00:09:18 +08:00
|
|
|
clients = append(clients, newClient)
|
2023-03-07 02:02:36 +08:00
|
|
|
clientMutex.Unlock()
|
2024-06-18 20:48:53 +08:00
|
|
|
|
|
|
|
// For when clients are removed prior to connection closed, to avoid a call to delete(clients, id)
|
|
|
|
var channelClosedFirst = false
|
2023-03-07 02:02:36 +08:00
|
|
|
go func() {
|
|
|
|
// Listen for connection close
|
|
|
|
<-r.Context().Done()
|
|
|
|
clientMutex.Lock()
|
2024-06-18 20:48:53 +08:00
|
|
|
if !channelClosedFirst {
|
2024-07-18 00:09:18 +08:00
|
|
|
deleteClient(newClient)
|
2024-06-18 20:48:53 +08:00
|
|
|
}
|
2023-03-07 02:02:36 +08:00
|
|
|
close(messageChan)
|
|
|
|
clientMutex.Unlock()
|
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
2024-06-18 20:48:53 +08:00
|
|
|
data, ok := <-messageChan
|
|
|
|
if !ok {
|
|
|
|
channelClosedFirst = true
|
2023-03-07 02:02:36 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
_, _ = w.Write([]byte(data))
|
|
|
|
flusher.Flush()
|
|
|
|
}
|
|
|
|
}
|