-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathrelay.go
More file actions
429 lines (378 loc) · 11.6 KB
/
relay.go
File metadata and controls
429 lines (378 loc) · 11.6 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
package main
import (
"crypto/rand"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"sync"
"time"
)
const maxTelegramFileSize = 50 * 1024 * 1024 // 50MB
const defaultRelayURL = "https://ccc-relay.fly.dev"
// handleSendFile sends a file to the current session's Telegram topic
func handleSendFile(filePath string) error {
config, err := loadConfig()
if err != nil {
return fmt.Errorf("no config found: %w", err)
}
// Get absolute path
if !filepath.IsAbs(filePath) {
cwd, _ := os.Getwd()
filePath = filepath.Join(cwd, filePath)
}
// Check file exists
fileInfo, err := os.Stat(filePath)
if err != nil {
return fmt.Errorf("file not found: %w", err)
}
// Find session from current directory
cwd, _ := os.Getwd()
var sessionName string
var topicID int64
for name, info := range config.Sessions {
if info == nil {
continue
}
if cwd == info.Path || strings.HasPrefix(cwd, info.Path+"/") {
sessionName = name
topicID = info.TopicID
break
}
}
if topicID == 0 || config.GroupID == 0 {
return fmt.Errorf("no session found for current directory")
}
fileName := filepath.Base(filePath)
fileSize := fileInfo.Size()
// Small file: send directly via Telegram
if fileSize < maxTelegramFileSize {
fmt.Printf("📤 Sending %s (%d MB) via Telegram...\n", fileName, fileSize/(1024*1024))
return sendFile(config, config.GroupID, topicID, filePath, "")
}
// Large file: use streaming relay
relayURL := config.RelayURL
if relayURL == "" {
relayURL = defaultRelayURL
}
fmt.Printf("📤 Preparing %s (%d MB) for streaming relay...\n", fileName, fileSize/(1024*1024))
// Generate one-time token
tokenBytes := make([]byte, 16)
rand.Read(tokenBytes)
token := hex.EncodeToString(tokenBytes)
// Register with relay
regPayload, _ := json.Marshal(map[string]interface{}{
"token": token,
"filename": fileName,
"size": fileSize,
})
regData := string(regPayload)
resp, err := http.Post(relayURL+"/register", "application/json", strings.NewReader(regData))
if err != nil {
return fmt.Errorf("failed to register with relay: %w", err)
}
resp.Body.Close()
// Send download link to Telegram (include filename in URL for browser compatibility)
downloadURL := fmt.Sprintf("%s/d/%s/%s", relayURL, token, fileName)
msg := fmt.Sprintf("📦 %s (%d MB)\n\n🔗 Download:\n%s", fileName, fileSize/(1024*1024), downloadURL)
fmt.Printf("📤 Sending link to %s...\n", sessionName)
if err := sendMessage(config, config.GroupID, topicID, msg); err != nil {
return err
}
// Wait for download request and stream
fmt.Printf("⏳ Waiting for download (link expires in 10 min)...\n")
return streamFileToRelay(relayURL, token, filePath, fileName, fileSize)
}
func streamFileToRelay(relayURL, token, filePath, fileName string, fileSize int64) error {
// Poll for download requests - loop to allow multiple downloads
timeout := time.After(10 * time.Minute)
ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop()
downloadCount := 0
for {
select {
case <-timeout:
http.Get(relayURL + "/cancel/" + token)
if downloadCount > 0 {
fmt.Printf("⏰ Session expired after %d download(s)\n", downloadCount)
return nil
}
return fmt.Errorf("download timed out (10 min)")
case <-ticker.C:
resp, err := http.Get(relayURL + "/status/" + token)
if err != nil {
continue
}
body, _ := io.ReadAll(io.LimitReader(resp.Body, maxResponseSize))
resp.Body.Close()
status := string(body)
if status == "waiting" {
continue
} else if status == "ready" {
// Someone requested download, start streaming
downloadCount++
fmt.Printf("📤 Streaming %s (download #%d)...\n", fileName, downloadCount)
file, err := os.Open(filePath)
if err != nil {
return err
}
// Stream to relay
req, _ := http.NewRequest("POST", relayURL+"/stream/"+token, file)
req.Header.Set("Content-Type", "application/octet-stream")
req.Header.Set("X-Filename", fileName)
req.ContentLength = fileSize
client := &http.Client{Timeout: 30 * time.Minute}
streamResp, err := client.Do(req)
file.Close()
if err != nil {
fmt.Printf("⚠️ Streaming error: %v\n", err)
continue
}
streamResp.Body.Close()
fmt.Printf("✅ Download #%d complete! Waiting for more requests...\n", downloadCount)
// Continue looping for more downloads
} else if status == "cancelled" || status == "not_found" {
if downloadCount > 0 {
return nil
}
return fmt.Errorf("transfer %s", status)
}
}
}
}
// Relay server - streams from sender to receiver without storing
var relayTransfers = struct {
sync.RWMutex
transfers map[string]*relayTransfer
}{transfers: make(map[string]*relayTransfer)}
type relayTransfer struct {
Token string
Filename string
Size int64
Status string // "waiting", "ready", "streaming", "done", "cancelled"
Created time.Time
DataChan chan []byte
DoneChan chan struct{}
}
func runRelayServer(port string) {
// Clean up old transfers periodically
go func() {
for {
time.Sleep(1 * time.Minute)
relayTransfers.Lock()
for token, t := range relayTransfers.transfers {
if time.Since(t.Created) > 15*time.Minute {
t.Status = "cancelled"
select {
case <-t.DoneChan:
default:
close(t.DoneChan)
}
delete(relayTransfers.transfers, token)
}
}
relayTransfers.Unlock()
}
}()
// Register a new transfer
http.HandleFunc("/register", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
var data struct {
Token string `json:"token"`
Filename string `json:"filename"`
Size int64 `json:"size"`
}
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
http.Error(w, "Invalid JSON", http.StatusBadRequest)
return
}
relayTransfers.Lock()
relayTransfers.transfers[data.Token] = &relayTransfer{
Token: data.Token,
Filename: data.Filename,
Size: data.Size,
Status: "waiting",
Created: time.Now(),
DataChan: make(chan []byte, 100),
DoneChan: make(chan struct{}),
}
relayTransfers.Unlock()
fmt.Printf("📋 Registered: %s (%s)\n", data.Filename, data.Token[:8])
w.WriteHeader(http.StatusOK)
})
// Check transfer status
http.HandleFunc("/status/", func(w http.ResponseWriter, r *http.Request) {
token := strings.TrimPrefix(r.URL.Path, "/status/")
relayTransfers.RLock()
t, exists := relayTransfers.transfers[token]
relayTransfers.RUnlock()
if !exists {
fmt.Fprint(w, "not_found")
return
}
fmt.Fprint(w, t.Status)
})
// Cancel transfer
http.HandleFunc("/cancel/", func(w http.ResponseWriter, r *http.Request) {
token := strings.TrimPrefix(r.URL.Path, "/cancel/")
relayTransfers.Lock()
if t, exists := relayTransfers.transfers[token]; exists {
t.Status = "cancelled"
select {
case <-t.DoneChan:
default:
close(t.DoneChan)
}
delete(relayTransfers.transfers, token)
}
relayTransfers.Unlock()
w.WriteHeader(http.StatusOK)
})
// Sender streams file data
http.HandleFunc("/stream/", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
token := strings.TrimPrefix(r.URL.Path, "/stream/")
relayTransfers.RLock()
t, exists := relayTransfers.transfers[token]
relayTransfers.RUnlock()
if !exists || t.Status != "ready" {
http.Error(w, "Transfer not ready", http.StatusBadRequest)
return
}
t.Status = "streaming"
fmt.Printf("📤 Streaming: %s (%s)\n", t.Filename, token[:8])
var bytesSent int64
// Read from sender and send to channel
buf := make([]byte, 32*1024)
for {
n, err := r.Body.Read(buf)
if n > 0 {
data := make([]byte, n)
copy(data, buf[:n])
bytesSent += int64(n)
select {
case t.DataChan <- data:
case <-t.DoneChan:
// Receiver finished/disconnected early
fmt.Printf("📤 Receiver done early: %s (%s) after %d bytes\n", t.Filename, token[:8], bytesSent)
return
}
}
if err != nil {
break
}
}
close(t.DataChan)
<-t.DoneChan // Wait for receiver to finish
// DON'T delete transfer - allow multiple downloads
// Transfer is cleaned up by timeout goroutine or /cancel endpoint
fmt.Printf("✅ Stream complete: %s (%s) - %d bytes\n", t.Filename, token[:8], bytesSent)
})
// Download endpoint - receiver gets file
http.HandleFunc("/d/", func(w http.ResponseWriter, r *http.Request) {
// Ignore Telegram link preview bots and HEAD requests
ua := r.UserAgent()
if strings.Contains(ua, "TelegramBot") || strings.Contains(ua, "Telegram") {
fmt.Printf("🚫 Ignored Telegram preview bot: %s\n", ua)
http.Error(w, "Preview not available", http.StatusForbidden)
return
}
if r.Method == http.MethodHead {
fmt.Printf("🚫 Ignored HEAD request\n")
w.WriteHeader(http.StatusOK)
return
}
// URL format: /d/{token}/{filename} - extract just the token
pathParts := strings.SplitN(strings.TrimPrefix(r.URL.Path, "/d/"), "/", 2)
token := pathParts[0]
relayTransfers.Lock()
t, exists := relayTransfers.transfers[token]
if exists && t.Status == "waiting" {
t.Status = "ready"
// Create fresh channels for this download
t.DataChan = make(chan []byte, 100)
t.DoneChan = make(chan struct{})
}
relayTransfers.Unlock()
if !exists {
http.Error(w, "File not found - sender may have disconnected", http.StatusNotFound)
return
}
if t.Status != "ready" && t.Status != "streaming" {
http.Error(w, "Transfer in progress, please wait and retry", http.StatusConflict)
return
}
fmt.Printf("📥 Download started: %s (%s) from %s\n", t.Filename, token[:8], r.UserAgent())
// Sanitize filename: remove quotes, newlines, and control characters
safeName := strings.Map(func(r rune) rune {
if r == '"' || r == '\n' || r == '\r' || r < 32 {
return '_'
}
return r
}, t.Filename)
w.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, safeName))
w.Header().Set("Content-Type", "application/octet-stream")
if t.Size > 0 {
w.Header().Set("Content-Length", fmt.Sprintf("%d", t.Size))
}
flusher, _ := w.(http.Flusher)
ctx := r.Context()
var bytesWritten int64
var writeErr error
// Stream data from sender to receiver
downloadLoop:
for {
select {
case <-ctx.Done():
// Client disconnected
fmt.Printf("❌ Client disconnected: %s (%s) after %d bytes\n", t.Filename, token[:8], bytesWritten)
writeErr = ctx.Err()
break downloadLoop
case data, ok := <-t.DataChan:
if !ok {
// Channel closed, transfer complete
break downloadLoop
}
n, err := w.Write(data)
bytesWritten += int64(n)
if err != nil {
fmt.Printf("❌ Write error: %s (%s) after %d bytes: %v\n", t.Filename, token[:8], bytesWritten, err)
writeErr = err
break downloadLoop
}
if flusher != nil {
flusher.Flush()
}
}
}
// Signal sender we're done (success or failure)
relayTransfers.Lock()
if t, exists := relayTransfers.transfers[token]; exists {
close(t.DoneChan)
if writeErr == nil {
t.Status = "waiting"
fmt.Printf("📥 Download complete: %s (%s) - %d bytes sent\n", t.Filename, token[:8], bytesWritten)
} else {
t.Status = "waiting" // Still allow retry
fmt.Printf("📥 Download failed: %s (%s) - allowing retry\n", t.Filename, token[:8])
}
}
relayTransfers.Unlock()
})
http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "OK")
})
fmt.Printf("🚀 Streaming relay server on :%s\n", port)
fmt.Println(" No files stored - direct sender→relay→receiver streaming!")
http.ListenAndServe(":"+port, nil)
}