-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
167 lines (140 loc) · 3.69 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
package main
import (
"crypto/subtle"
"fmt"
"io"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/joho/godotenv"
)
func ipAddrFromRemoteAddr(s string) string {
idx := strings.LastIndex(s, ":")
if idx == -1 {
return s
}
return s[:idx]
}
func requestGetRemoteAddress(r *http.Request) string {
hdr := r.Header
hdrRealIP := hdr.Get("X-Real-Ip")
hdrForwardedFor := hdr.Get("X-Forwarded-For")
if hdrRealIP == "" && hdrForwardedFor == "" {
return ipAddrFromRemoteAddr(r.RemoteAddr)
}
if hdrForwardedFor != "" {
parts := strings.Split(hdrForwardedFor, ",")
for i, p := range parts {
parts[i] = strings.TrimSpace(p)
}
return parts[0]
}
return hdrRealIP
}
func uploadHandler(w http.ResponseWriter, r *http.Request) {
ipaddr := requestGetRemoteAddress(r)
log.Println("Signature request received from:", ipaddr)
if r.Method != "POST" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
if err := r.ParseMultipartForm(1024 << 20); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
exFile, err := os.Executable()
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
exPath := filepath.Dir(exFile)
files := r.MultipartForm.File["file"]
for _, fileHeader := range files {
file, err := fileHeader.Open()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = os.MkdirAll(filepath.Join(exPath, "uploads"), os.ModePerm)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
uploadFile := filepath.Base(fileHeader.Filename)
uploadPath := filepath.Join(exPath, "uploads",
fmt.Sprintf("%d-%s", time.Now().UnixNano(), uploadFile))
f, err := os.Create(uploadPath)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
defer f.Close()
_, err = io.Copy(f, file)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
f.Close()
defer os.Remove(uploadPath)
shFile, err := filepath.Abs(filepath.Join(exPath, "sign.sh"))
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
out, err := exec.Command(shFile, uploadPath).Output()
if err != nil {
log.Println(string(out))
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Disposition",
"attachment; filename="+strconv.Quote(uploadFile))
w.Header().Set("Content-Type", "application/octet-stream")
http.ServeFile(w, r, uploadPath)
}
}
func basicAuth(next http.Handler, username, password string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
user, pass, ok := r.BasicAuth()
if !ok || subtle.ConstantTimeCompare(
[]byte(user), []byte(username)) != 1 ||
subtle.ConstantTimeCompare([]byte(pass),
[]byte(password)) != 1 {
w.Header().Set(
"WWW-Authenticate",
`Basic realm="Restricted", charset="UTF-8"`)
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
next.ServeHTTP(w, r)
})
}
func main() {
err := godotenv.Load()
if err != nil {
log.Fatalf("Error loading .env file")
}
username := os.Getenv("USERNAME")
password := os.Getenv("PASSWORD")
log.Println("Starting the signature service")
mux := http.NewServeMux()
mux.HandleFunc("/sign", uploadHandler)
authenticatedMux := basicAuth(mux, username, password)
server := &http.Server{
Addr: ":443",
Handler: authenticatedMux,
}
err = server.ListenAndServeTLS("cert/cert.pem", "cert/key.pem")
if err != nil {
log.Fatalf("Server failed: %s", err)
}
}