-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpserver.go
More file actions
80 lines (72 loc) · 2.19 KB
/
httpserver.go
File metadata and controls
80 lines (72 loc) · 2.19 KB
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
package main
// httpserver.go — local HTTP file server for Chromecast LAN streaming.
//
// Chromecast (Google Home) is a network device: it cannot read file:// paths.
// It fetches audio by making an HTTP request to a URL on the local network.
// This embedded server starts on a random available port, serving local music
// files by absolute path so the Chromecast can stream them directly.
import (
"fmt"
"net"
"net/http"
"net/url"
"strings"
)
var (
localServerPort int
localServerIP string
)
// startLocalFileServer binds to a random free port on the LAN interface,
// starts serving files, and stores the IP:port for URL generation.
// Called once from main() before the MCP server starts.
func startLocalFileServer() error {
listener, err := net.Listen("tcp", ":0") // :0 → OS picks a free port
if err != nil {
return fmt.Errorf("local file server: cannot bind: %w", err)
}
localServerPort = listener.Addr().(*net.TCPAddr).Port
localServerIP = getLANIP()
mux := http.NewServeMux()
mux.HandleFunc("/localfile", func(w http.ResponseWriter, r *http.Request) {
path := r.URL.Query().Get("path")
// Basic safety: reject empty paths and traversal attempts
if path == "" || strings.Contains(path, "..") {
http.Error(w, "bad path", http.StatusBadRequest)
return
}
// Set CORS header so Chromecast won't reject the response
w.Header().Set("Access-Control-Allow-Origin", "*")
http.ServeFile(w, r, path)
})
go func() {
_ = http.Serve(listener, mux)
}()
return nil
}
// getLANIP returns the machine's first non-loopback IPv4 address —
// the address reachable by Chromecast on the same Wi-Fi.
func getLANIP() string {
addrs, err := net.InterfaceAddrs()
if err != nil {
return "127.0.0.1"
}
for _, addr := range addrs {
ipnet, ok := addr.(*net.IPNet)
if !ok || ipnet.IP.IsLoopback() {
continue
}
if ip4 := ipnet.IP.To4(); ip4 != nil {
return ip4.String()
}
}
return "127.0.0.1"
}
// localFileHTTPURL converts an absolute local file path to an HTTP URL
// that the Chromecast can fetch over the LAN.
func localFileHTTPURL(absPath string) string {
return fmt.Sprintf("http://%s:%d/localfile?path=%s",
localServerIP,
localServerPort,
url.QueryEscape(absPath),
)
}