Skip to content

Commit e9fb7e8

Browse files
committed
Merge pull request #33 from facchinm/configurable_CORS
Configurable cors
2 parents 34455ab + 8494054 commit e9fb7e8

File tree

5 files changed

+179
-244
lines changed

5 files changed

+179
-244
lines changed

config.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ v = true # show debug logging
77
appName = CreateBridge
88
updateUrl = http://downloads.arduino.cc/
99
#updateUrl = http://localhost/
10+
origins = http://webide.arduino.cc:8080

conn.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ func uploadHandler(c *gin.Context) {
5757
extraInfo.authdata.UserName = c.PostForm("auth_user")
5858
extraInfo.authdata.Password = c.PostForm("auth_pass")
5959
commandline := c.PostForm("commandline")
60+
if commandline == "undefined" {
61+
commandline = ""
62+
}
6063
extraInfo.use_1200bps_touch, _ = strconv.ParseBool(c.PostForm("use_1200bps_touch"))
6164
extraInfo.wait_for_upload_port, _ = strconv.ParseBool(c.PostForm("wait_for_upload_port"))
6265
extraInfo.networkPort, _ = strconv.ParseBool(c.PostForm("network"))
@@ -78,7 +81,11 @@ func uploadHandler(c *gin.Context) {
7881
c.String(http.StatusBadRequest, err.Error())
7982
}
8083

81-
go spProgramRW(port, board, board_rewrite, path, commandline, extraInfo)
84+
if board_rewrite != "" {
85+
board = board_rewrite
86+
}
87+
88+
go spProgramRW(port, board, path, commandline, extraInfo)
8289
}
8390
}
8491

hub.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ func (h *hub) run() {
5757
c.send <- []byte("{\"Version\" : \"" + version + "\"} ")
5858
c.send <- []byte("{\"Commands\" : [\"list\", \"open [portName] [baud] [bufferAlgorithm (optional)]\", \"send [portName] [cmd]\", \"sendnobuf [portName] [cmd]\", \"close [portName]\", \"bufferalgorithms\", \"baudrates\", \"restart\", \"exit\", \"program [portName] [board:name] [$path/to/filename/without/extension]\", \"programfromurl [portName] [board:name] [urlToHexFile]\"]} ")
5959
c.send <- []byte("{\"Hostname\" : \"" + *hostname + "\"} ")
60+
c.send <- []byte("{\"OS\" : \"" + runtime.GOOS + "\"} ")
6061
case c := <-h.unregister:
6162
delete(h.connections, c)
6263
// put close in func cuz it was creating panics and want
@@ -216,11 +217,10 @@ func checkCmd(m []byte) {
216217
//log.Print("Done with checkCmd")
217218
}
218219

219-
var multi_writer = io.MultiWriter(&logger_ws, os.Stderr)
220-
221220
func logAction(sl string) {
222221
if strings.HasPrefix(sl, "log on") {
223222
*logDump = "on"
223+
multi_writer := io.MultiWriter(&logger_ws, os.Stderr)
224224
log.SetOutput(multi_writer)
225225
} else if strings.HasPrefix(sl, "log off") {
226226
*logDump = "off"

main.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ var (
4343
tempToolsPath = createToolsDir()
4444
port string
4545
portSSL string
46+
origins = flag.String("origins", "", "Allowed origin list for CORS")
4647
)
4748

4849
type NullWriter int
@@ -53,7 +54,7 @@ type logWriter struct{}
5354

5455
func (u *logWriter) Write(p []byte) (n int, err error) {
5556
h.broadcastSys <- p
56-
return 0, nil
57+
return len(p), nil
5758
}
5859

5960
var logger_ws logWriter
@@ -109,6 +110,20 @@ func main() {
109110
iniflags.Parse()
110111
}
111112

113+
// move CORS to config file compatibility, Vagrant version
114+
if *origins == "" {
115+
log.Println("Patching config.ini for compatibility")
116+
f, err := os.OpenFile(dest+"/"+*configIni, os.O_APPEND|os.O_WRONLY, 0666)
117+
if err != nil {
118+
panic(err)
119+
}
120+
_, err = f.WriteString("\norigins = http://webide.arduino.cc:8080\n")
121+
if err != nil {
122+
panic(err)
123+
}
124+
f.Close()
125+
restart("")
126+
}
112127
//log.SetFormatter(&log.JSONFormatter{})
113128

114129
log.SetLevel(log.InfoLevel)
@@ -194,8 +209,14 @@ func main() {
194209

195210
socketHandler := wsHandler().ServeHTTP
196211

212+
extraOriginStr := "https://create.arduino.cc, http://create.arduino.cc, https://create-dev.arduino.cc, http://create-dev.arduino.cc, http://create-staging.arduino.cc, https://create-staging.arduino.cc,"
213+
214+
for i := 8990; i < 9001; i++ {
215+
extraOriginStr = extraOriginStr + " http://localhost:" + strconv.Itoa(i) + ", https://localhost:" + strconv.Itoa(i)
216+
}
217+
197218
r.Use(cors.Middleware(cors.Config{
198-
Origins: "https://create.arduino.cc, http://create.arduino.cc, https://create-dev.arduino.cc, http://create-dev.arduino.cc, http://webide.arduino.cc:8080, http://create-staging.arduino.cc, https://create-staging.arduino.cc, http://localhost:8989, https://localhost:8990",
219+
Origins: *origins + "," + extraOriginStr,
199220
Methods: "GET, PUT, POST, DELETE",
200221
RequestHeaders: "Origin, Authorization, Content-Type",
201222
ExposedHeaders: "",
@@ -270,7 +291,7 @@ const homeTemplateHtml = `<!DOCTYPE html>
270291
var log = document.getElementById('log');
271292
var pause = document.getElementById('myCheck');
272293
var messages = [];
273-
var only_log = false;
294+
var only_log = true;
274295
275296
function appendLog(msg) {
276297

0 commit comments

Comments
 (0)