forked from loft-sh/devpod
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathserver_utils.go
More file actions
88 lines (74 loc) · 1.84 KB
/
server_utils.go
File metadata and controls
88 lines (74 loc) · 1.84 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
81
82
83
84
85
86
87
88
package framework
import (
"fmt"
"log"
"net"
"net/http"
"os"
"path/filepath"
)
// ServeAgent will be a simple http file server that will expose our
// freshly compiled devpod binaries to be downloaded as agents.
// useful for non-linux runners.
func ServeAgent() {
// Specify the directory containing the files you want to serve
dir := "bin"
wd, err := os.Getwd()
if err == nil {
dir = filepath.Join(wd, "bin")
}
// Create a file server handler for the specified directory
fileServer := http.FileServer(http.Dir(dir))
// Use a dedicated ServeMux to avoid conflicts with http.DefaultServeMux
mux := http.NewServeMux()
mux.Handle("/files/", http.StripPrefix("/files", fileServer))
ip := getIP()
listener, err := net.Listen("tcp", fmt.Sprintf("%v:0", ip))
if err != nil {
log.Fatal(err)
}
addr := listener.Addr().String()
err = os.Setenv("DEVPOD_AGENT_URL", "http://"+addr+"/files/")
if err != nil {
log.Fatal(err)
}
log.Printf("Server started on %s", addr)
// #nosec G114 -- test-only agent file server, no timeout needed
err = http.Serve(listener, mux)
if err != nil {
log.Fatal(err)
}
}
func getIP() string {
// Get a list of network interfaces
ifaces, err := net.Interfaces()
if err != nil {
return "0.0.0.0"
}
// Iterate over each network interface
for _, iface := range ifaces {
addrs, err := iface.Addrs()
if err != nil {
return "0.0.0.0"
}
for _, addr := range addrs {
switch v := addr.(type) {
case *net.IPAddr:
if v.IP.To4() != nil {
if v.IP.DefaultMask().String() == "ffffff00" ||
v.IP.DefaultMask().String() == "ff000000" {
return v.IP.String()
}
}
case *net.IPNet:
if v.IP.To4() != nil {
if v.IP.DefaultMask().String() == "ffffff00" ||
v.IP.DefaultMask().String() == "ff000000" {
return v.IP.String()
}
}
}
}
}
return "0.0.0.0"
}