-
Notifications
You must be signed in to change notification settings - Fork 0
/
oc_server.go
115 lines (100 loc) · 2.9 KB
/
oc_server.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
package main
import (
"crypto/rand"
"encoding/hex"
"flag"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"time"
)
const defaultPassword = "secretPassword" // Default password
var (
filePath string
password string // Now a variable to store either default or overridden password
)
func init() {
flag.StringVar(&filePath, "p", "", "Path to save uploaded files")
flag.StringVar(&password, "o", defaultPassword, "Override default password")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s -p <path> [-o <password>]\n", os.Args[0])
fmt.Fprintf(os.Stderr, " -p <path> : Specify the path to save uploaded files (required)\n")
fmt.Fprintf(os.Stderr, " -o <password>: Set custom password for this session (optional)\n")
}
flag.Parse()
if filePath == "" {
flag.Usage()
os.Exit(1)
}
}
func main() {
http.HandleFunc("/upload", handleUpload)
fmt.Printf("Server is running on http://localhost:8080\nFiles will be saved to: %s\n", filePath)
http.ListenAndServe(":8080", nil)
}
func handleUpload(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Only POST requests are allowed", http.StatusMethodNotAllowed)
return
}
// Check the password
if r.Header.Get("X-Password") != password {
http.Error(w, "Invalid password", http.StatusUnauthorized)
return
}
file, header, err := r.FormFile("file")
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
defer file.Close()
var filename string
if header.Filename == "message.txt" {
// Came through an Onion Courier middleman, use random filename
randomName, err := generateRandomFilename()
if err != nil {
http.Error(w, "Error generating filename", http.StatusInternalServerError)
return
}
filename = randomName
} else {
// Use original filename
filename = header.Filename
}
// Create the destination file with the specified path and filename
dstPath := filepath.Join(filePath, filename)
dst, err := os.Create(dstPath)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer dst.Close()
_, err = io.Copy(dst, file)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Output to stderr with timestamp and username (if provided)
currentTime := time.Now().Format("15:04:05")
username := r.Header.Get("X-Username")
if username == "" {
username = "Anonymous"
}
fmt.Fprintf(os.Stderr, "File %s received at %s by %s\n", filename, currentTime, username)
// Output to the client
if filename != header.Filename {
fmt.Fprintf(w, "File received and saved as %s!", filename)
} else {
fmt.Fprintf(w, "File %s received!", filename)
}
}
func generateRandomFilename() (string, error) {
randomBytes := make([]byte, 4) // 4 bytes will give us 8 hex characters
_, err := rand.Read(randomBytes)
if err != nil {
return "", err
}
return fmt.Sprintf("m%s", hex.EncodeToString(randomBytes)[:7]), nil
}