-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathmain.go
More file actions
218 lines (199 loc) · 4.97 KB
/
main.go
File metadata and controls
218 lines (199 loc) · 4.97 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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package main
import (
"bufio"
"context"
"crypto/tls"
"flag"
"fmt"
"io"
"log"
"net"
"net/url"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/google/uuid"
oapi "github.com/kernel/kernel-images/server/lib/oapi"
openapi_types "github.com/oapi-codegen/runtime/types"
"golang.org/x/term"
)
func main() {
var serverURL string
flag.StringVar(&serverURL, "server", "http://localhost:444", "Base URL to API server (e.g., http://localhost:444)")
flag.Parse()
u, err := ensureHTTPURL(serverURL)
if err != nil {
log.Fatalf("invalid server URL: %v", err)
}
// Determine terminal size (cols, rows)
cols, rows := 80, 24
if term.IsTerminal(int(os.Stdin.Fd())) {
if w, h, err := term.GetSize(int(os.Stdin.Fd())); err == nil {
cols, rows = w, h
}
}
// Prepare client
client, err := oapi.NewClientWithResponses(u.String())
if err != nil {
log.Fatalf("failed to init client: %v", err)
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
// Spawn bash with TTY
body := oapi.ProcessSpawnJSONRequestBody{
Command: "/bin/bash",
}
body.AllocateTty = boolPtr(true)
body.Rows = &rows
body.Cols = &cols
args := []string{}
body.Args = &args
resp, err := client.ProcessSpawnWithResponse(ctx, body)
if err != nil {
log.Fatalf("spawn request failed: %v", err)
}
if resp.JSON200 == nil || resp.JSON200.ProcessId == nil {
log.Fatalf("unexpected response: %+v", resp)
}
procID := resp.JSON200.ProcessId.String()
// Attach via HTTP hijack
var (
rawConn net.Conn
)
{
addr := u.Host
if addr == "" {
// Fallback for URLs like http://localhost
addr = u.Hostname()
if port := u.Port(); port != "" {
addr = net.JoinHostPort(addr, port)
} else {
// Default ports by scheme
if u.Scheme == "https" {
addr = net.JoinHostPort(addr, "443")
} else {
addr = net.JoinHostPort(addr, "80")
}
}
}
// Dial based on scheme
switch u.Scheme {
case "https":
tlsConf := &tls.Config{
ServerName: u.Hostname(),
MinVersion: tls.VersionTLS12,
}
rc, err := tls.Dial("tcp", addr, tlsConf)
if err != nil {
log.Fatalf("failed to tls dial %s: %v", addr, err)
}
rawConn = rc
default:
rc, err := net.Dial("tcp", addr)
if err != nil {
log.Fatalf("failed to dial %s: %v", addr, err)
}
rawConn = rc
}
}
defer rawConn.Close()
pathPrefix := strings.TrimRight(u.Path, "/")
if pathPrefix == "/" {
pathPrefix = ""
}
path := fmt.Sprintf("%s/%s/%s/%s", pathPrefix, "process", procID, "attach")
req := fmt.Sprintf("GET %s HTTP/1.1\r\nHost: %s\r\nConnection: close\r\n\r\n", path, u.Host)
// For TLS, ensure we speak HTTP/1.1 and not attempt an HTTP/2 preface
// by writing the raw bytes directly over the established connection.
if _, err := rawConn.Write([]byte(req)); err != nil {
log.Fatalf("failed to write attach request: %v", err)
}
// Read and consume HTTP response headers (until \r\n\r\n)
br := bufio.NewReader(rawConn)
if err := readHTTPHeaders(br); err != nil {
log.Fatalf("failed to read attach response headers: %v", err)
}
// Put local terminal into raw mode
var oldState *term.State
if term.IsTerminal(int(os.Stdin.Fd())) {
s, err := term.MakeRaw(int(os.Stdin.Fd()))
if err != nil {
log.Fatalf("failed to set raw mode: %v", err)
}
oldState = s
defer func() {
_ = term.Restore(int(os.Stdin.Fd()), oldState)
fmt.Println()
}()
}
// Handle window resize (SIGWINCH)
winch := make(chan os.Signal, 1)
signal.Notify(winch, syscall.SIGWINCH)
go func() {
for range winch {
if term.IsTerminal(int(os.Stdin.Fd())) {
if w, h, err := term.GetSize(int(os.Stdin.Fd())); err == nil {
// rows=h, cols=w
rows := h
cols := w
// best-effort resize; do not cancel main ctx
go func() {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
uid, _ := uuid.Parse(procID)
_, _ = client.ProcessResizeWithResponse(ctx, openapi_types.UUID(uid), oapi.ProcessResizeJSONRequestBody{
Rows: rows,
Cols: cols,
})
}()
}
}
}
}()
// Bi-directional piping
errCh := make(chan error, 2)
go func() {
_, err := io.Copy(rawConn, os.Stdin)
errCh <- err
}()
go func() {
// Use the buffered reader to include any bytes already read beyond headers
_, err := io.Copy(os.Stdout, br)
errCh <- err
}()
// Wait for either side to close/error
<-errCh
}
func ensureHTTPURL(s string) (*url.URL, error) {
if !strings.HasPrefix(s, "http://") && !strings.HasPrefix(s, "https://") {
s = "http://" + s
}
u, err := url.Parse(s)
if err != nil {
return nil, err
}
if u.Scheme == "" {
u.Scheme = "http"
}
if u.Host == "" && u.Path != "" {
// Allow bare host:port without scheme
u.Host = u.Path
u.Path = ""
}
return u, nil
}
func readHTTPHeaders(r *bufio.Reader) error {
for {
line, err := r.ReadString('\n')
if err != nil {
return err
}
if line == "\r\n" {
return nil
}
// continue until empty line
}
}
func boolPtr(b bool) *bool { return &b }