@@ -19,6 +19,7 @@ package main
19
19
20
20
import (
21
21
"bytes"
22
+
"crypto/rsa"
22
23
"encoding/json"
23
24
"errors"
24
25
"fmt"
@@ -79,111 +80,114 @@ type Upload struct {
79
80
80
81
var uploadStatusStr = "ProgrammerStatus"
81
82
82
-
func uploadHandler(c *gin.Context) {
83
-
data := new(Upload)
84
-
if err := c.BindJSON(data); err != nil {
85
-
c.String(http.StatusBadRequest, fmt.Sprintf("err with the payload. %v", err.Error()))
86
-
return
87
-
}
88
-
89
-
log.Printf("%+v %+v %+v %+v %+v %+v", data.Port, data.Board, data.Rewrite, data.Commandline, data.Extra, data.Filename)
90
-
91
-
if data.Port == "" {
92
-
c.String(http.StatusBadRequest, "port is required")
93
-
return
94
-
}
95
-
96
-
if data.Board == "" {
97
-
c.String(http.StatusBadRequest, "board is required")
98
-
log.Error("board is required")
99
-
return
100
-
}
101
-
102
-
if !data.Extra.Network {
103
-
if data.Signature == "" {
104
-
c.String(http.StatusBadRequest, "signature is required")
83
+
func uploadHandler(pubKey *rsa.PublicKey) func(*gin.Context) {
84
+
return func(c *gin.Context) {
85
+
data := new(Upload)
86
+
if err := c.BindJSON(data); err != nil {
87
+
c.String(http.StatusBadRequest, fmt.Sprintf("err with the payload. %v", err.Error()))
105
88
return
106
89
}
107
90
108
-
if data.Commandline == "" {
109
-
c.String(http.StatusBadRequest, "commandline is required for local board")
91
+
log.Printf("%+v %+v %+v %+v %+v %+v", data.Port, data.Board, data.Rewrite, data.Commandline, data.Extra, data.Filename)
92
+
93
+
if data.Port == "" {
94
+
c.String(http.StatusBadRequest, "port is required")
110
95
return
111
96
}
112
97
113
-
err := utilities.VerifyInput(data.Commandline, data.Signature)
114
-
115
-
if err != nil {
116
-
c.String(http.StatusBadRequest, "signature is invalid")
98
+
if data.Board == "" {
99
+
c.String(http.StatusBadRequest, "board is required")
100
+
log.Error("board is required")
117
101
return
118
102
}
119
-
}
120
103
121
-
buffer := bytes.NewBuffer(data.Hex)
104
+
if !data.Extra.Network {
105
+
if data.Signature == "" {
106
+
c.String(http.StatusBadRequest, "signature is required")
107
+
return
108
+
}
122
109
123
-
filePath, err := utilities.SaveFileonTempDir(data.Filename, buffer)
124
-
if err != nil {
125
-
c.String(http.StatusBadRequest, err.Error())
126
-
return
127
-
}
110
+
if data.Commandline == "" {
111
+
c.String(http.StatusBadRequest, "commandline is required for local board")
112
+
return
113
+
}
128
114
129
-
tmpdir, err := os.MkdirTemp("", "extrafiles")
130
-
if err != nil {
131
-
c.String(http.StatusBadRequest, err.Error())
132
-
return
133
-
}
115
+
err := utilities.VerifyInput(data.Commandline, data.Signature, pubKey)
134
116
135
-
for _, extraFile := range data.ExtraFiles {
136
-
path, err := utilities.SafeJoin(tmpdir, extraFile.Filename)
137
-
if err != nil {
138
-
c.String(http.StatusBadRequest, err.Error())
139
-
return
117
+
if err != nil {
118
+
log.WithField("err", err).Error("Error verifying the command")
119
+
c.String(http.StatusBadRequest, "signature is invalid")
120
+
return
121
+
}
140
122
}
141
-
log.Printf("Saving %s on %s", extraFile.Filename, path)
142
123
143
-
err = os.MkdirAll(filepath.Dir(path), 0744)
144
-
if err != nil {
145
-
c.String(http.StatusBadRequest, err.Error())
146
-
return
147
-
}
124
+
buffer := bytes.NewBuffer(data.Hex)
148
125
149
-
err = os.WriteFile(path, extraFile.Hex, 0644)
126
+
filePath, err := utilities.SaveFileonTempDir(data.Filename, buffer)
150
127
if err != nil {
151
128
c.String(http.StatusBadRequest, err.Error())
152
129
return
153
130
}
154
-
}
155
131
156
-
if data.Rewrite != "" {
157
-
data.Board = data.Rewrite
158
-
}
159
-
160
-
go func() {
161
-
// Resolve commandline
162
-
commandline, err := upload.PartiallyResolve(data.Board, filePath, tmpdir, data.Commandline, data.Extra, Tools)
132
+
tmpdir, err := os.MkdirTemp("", "extrafiles")
163
133
if err != nil {
164
-
send(map[string]string{uploadStatusStr: "Error", "Msg": err.Error()})
134
+
c.String(http.StatusBadRequest, err.Error())
165
135
return
166
136
}
167
137
168
-
l := PLogger{Verbose: true}
169
-
170
-
// Upload
171
-
if data.Extra.Network {
172
-
err = errors.New("network upload is not supported anymore, pease use OTA instead")
173
-
} else {
174
-
send(map[string]string{uploadStatusStr: "Starting", "Cmd": "Serial"})
175
-
err = upload.Serial(data.Port, commandline, data.Extra, l)
138
+
for _, extraFile := range data.ExtraFiles {
139
+
path, err := utilities.SafeJoin(tmpdir, extraFile.Filename)
140
+
if err != nil {
141
+
c.String(http.StatusBadRequest, err.Error())
142
+
return
143
+
}
144
+
log.Printf("Saving %s on %s", extraFile.Filename, path)
145
+
146
+
err = os.MkdirAll(filepath.Dir(path), 0744)
147
+
if err != nil {
148
+
c.String(http.StatusBadRequest, err.Error())
149
+
return
150
+
}
151
+
152
+
err = os.WriteFile(path, extraFile.Hex, 0644)
153
+
if err != nil {
154
+
c.String(http.StatusBadRequest, err.Error())
155
+
return
156
+
}
176
157
}
177
158
178
-
// Handle result
179
-
if err != nil {
180
-
send(map[string]string{uploadStatusStr: "Error", "Msg": err.Error()})
181
-
return
159
+
if data.Rewrite != "" {
160
+
data.Board = data.Rewrite
182
161
}
183
-
send(map[string]string{uploadStatusStr: "Done", "Flash": "Ok"})
184
-
}()
185
162
186
-
c.String(http.StatusAccepted, "")
163
+
go func() {
164
+
// Resolve commandline
165
+
commandline, err := upload.PartiallyResolve(data.Board, filePath, tmpdir, data.Commandline, data.Extra, Tools)
166
+
if err != nil {
167
+
send(map[string]string{uploadStatusStr: "Error", "Msg": err.Error()})
168
+
return
169
+
}
170
+
171
+
l := PLogger{Verbose: true}
172
+
173
+
// Upload
174
+
if data.Extra.Network {
175
+
err = errors.New("network upload is not supported anymore, pease use OTA instead")
176
+
} else {
177
+
send(map[string]string{uploadStatusStr: "Starting", "Cmd": "Serial"})
178
+
err = upload.Serial(data.Port, commandline, data.Extra, l)
179
+
}
180
+
181
+
// Handle result
182
+
if err != nil {
183
+
send(map[string]string{uploadStatusStr: "Error", "Msg": err.Error()})
184
+
return
185
+
}
186
+
send(map[string]string{uploadStatusStr: "Done", "Flash": "Ok"})
187
+
}()
188
+
189
+
c.String(http.StatusAccepted, "")
190
+
}
187
191
}
188
192
189
193
// PLogger sends the info from the upload to the websocket
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4