generated from chevere/package-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
178 lines (169 loc) · 5.46 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
* This file is part of xrDebug.
*
* (c) Rodolfo Berrios <rodolfo@chevere.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
// Package main implements the xrDebug server, a web-based debugging interface
// that provides real-time debug information and execution control for network applications.
package main
import (
"crypto/ed25519"
"embed"
"fmt"
"net"
"net/http"
"os"
"strings"
"sync"
"time"
"github.com/xrdebug/xrdebug/internal/build"
"github.com/xrdebug/xrdebug/internal/cipher"
"github.com/xrdebug/xrdebug/internal/cli"
"github.com/xrdebug/xrdebug/internal/controller/message"
"github.com/xrdebug/xrdebug/internal/controller/pause"
"github.com/xrdebug/xrdebug/internal/controller/spa"
"github.com/xrdebug/xrdebug/internal/controller/sse"
"github.com/xrdebug/xrdebug/internal/pausectl"
"github.com/xrdebug/xrdebug/internal/server"
)
//go:embed web/* assets/*
var filesystem embed.FS
type ServerDeps struct {
Logger cli.Logger
Messages chan string
Clients map[*sse.Client]bool
ClientsMu *sync.Mutex
}
func middleware(handler http.Handler, middlewares ...func(http.Handler) http.Handler) http.Handler {
for _, middleware := range middlewares {
handler = middleware(handler)
}
return handler
}
func main() {
var clientsMu sync.Mutex
deps := &ServerDeps{
Logger: cli.NewLogger(),
Messages: make(chan string, 100),
Clients: make(map[*sse.Client]bool),
ClientsMu: &clientsMu,
}
if err := run(deps); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
}
// run initializes and starts the xrDebug server. It sets up HTTP routes,
// initializes message channels, and manages client connections.
func run(deps *ServerDeps) error {
options, err := cli.NewOptions(flags)
if err != nil {
return err
}
if options.Version {
fmt.Printf("%s %s\n", name, version)
os.Exit(0)
}
if err := validateEditor(options.Editor); err != nil {
return err
}
if err := server.ValidateTLSFiles(options.TLSCert, options.TLSPrivateKey); err != nil {
return err
}
protocol := "http"
if options.TLSCert != "" && options.TLSPrivateKey != "" {
protocol += "s"
}
var generatedKeys []string
var signPrivateKey ed25519.PrivateKey
var symmetricKey []byte
if options.EnableSignVerification {
signPrivateKey, err = cipher.LoadPrivateKey(options.SignPrivateKey)
if err != nil {
return err
}
if options.SignPrivateKey == "" {
pemKey, err := cipher.PemKey(signPrivateKey)
if err != nil {
return err
}
generatedKeys = append(generatedKeys, pemKey)
}
}
if options.EnableEncryption {
symmetricKey, err = cipher.LoadSymmetricKey(options.SymmetricKey)
if err != nil {
return err
}
if options.SymmetricKey == "" {
symmetricKeyDisplay := cipher.Base64(symmetricKey)
generatedKeys = append(generatedKeys,
fmt.Sprintf("ENCRYPTION KEY\n%s", symmetricKeyDisplay))
}
}
html, err := filesystem.ReadFile("web/index.html")
if err != nil {
return err
}
build, err := build.New(html, filesystem, "web/", version, options.SessionName, options.Editor, options.EnableEncryption, options.EnableSignVerification)
if err != nil {
return err
}
content := build.Bytes()
gzipped, err := server.GzipContent(content)
if err != nil {
return err
}
displayAddress := server.DisplayAddress(options.Address, anyIPv4, anyIPv6)
listener, err := server.NewListener(options.Address, options.Port)
if err != nil {
return err
}
displayPort := listener.Addr().(*net.TCPAddr).Port
displayAddress = server.FormatDisplayAddress(protocol, displayAddress, displayPort)
lockManager := pausectl.NewManager(5*time.Minute, 1*time.Minute)
pauseController := pause.New(lockManager, deps.Messages, deps.Logger)
sse.StartDispatcher(deps.Messages, deps.Clients, deps.ClientsMu, symmetricKey)
middlewares := []func(http.Handler) http.Handler{server.WithHeaders}
clientSignMiddleware := append([]func(http.Handler) http.Handler{}, middlewares...)
if options.EnableSignVerification {
clientSignMiddleware = append(
clientSignMiddleware,
server.VerifySignature(signPrivateKey.Public().(ed25519.PublicKey)),
)
}
http.Handle("GET /", middleware(spa.Handle(gzipped), middlewares...))
http.Handle("POST /messages", middleware(message.Handle(deps.Messages, deps.Logger), clientSignMiddleware...))
http.Handle("POST /pauses", middleware(pauseController.Post(), clientSignMiddleware...))
http.Handle("GET /pauses/{id}", middleware(pauseController.Get(), clientSignMiddleware...))
http.Handle("GET /stream", middleware(sse.Handle(deps.Messages, deps.Logger, deps.Clients, deps.ClientsMu), middlewares...))
// These are meant to be issued from the user interface (no need to sign)
http.Handle("PATCH /pauses/{id}", middleware(pauseController.Patch(), middlewares...))
http.Handle("DELETE /pauses/{id}", middleware(pauseController.Delete(), middlewares...))
logo, err := filesystem.ReadFile("assets/logo")
if err != nil {
return err
}
cli.WriteTemplate(os.Stdout, templateHeader, &Replacements{
Logo: string(logo),
Version: version,
Url: url,
Copyright: copyright,
Name: name,
DisplayAddress: displayAddress,
GeneratedKeys: joinGeneratedKeys(generatedKeys),
})
if protocol == "https" {
return http.ServeTLS(listener, nil, options.TLSCert, options.TLSPrivateKey)
}
return http.Serve(listener, nil)
}
func joinGeneratedKeys(keys []string) string {
if len(keys) > 0 {
return "\n" + strings.Join(keys, "\n\n") + "\n"
}
return ""
}