From 7ba6451d24dde5bc4e14ec4c9c3c417bb07969cf Mon Sep 17 00:00:00 2001 From: Matteo Suppo Date: Fri, 29 Apr 2016 15:26:56 +0200 Subject: [PATCH 1/3] Support safari by changing the upload handler: it now accepts a json object with the base64 encoded binary as the hex field --- conn.go | 71 +++++++++++++++++++++++---------------------------- programmer.go | 53 +++++++++++++++++++------------------- 2 files changed, 59 insertions(+), 65 deletions(-) diff --git a/conn.go b/conn.go index c341df8ac..43f4bf0b5 100644 --- a/conn.go +++ b/conn.go @@ -3,6 +3,7 @@ package main import ( + "bytes" "crypto" "crypto/rsa" "crypto/sha256" @@ -11,7 +12,6 @@ import ( "encoding/pem" "errors" "net/http" - "strconv" log "github.com/Sirupsen/logrus" "github.com/gin-gonic/gin" @@ -45,75 +45,68 @@ func (s *WsServer) ServeHTTP(c *gin.Context) { s.Server.ServeHTTP(c.Writer, c.Request) } +// Upload contains the data to upload a sketch onto a board +type Upload struct { + Port string `json:"port"` + Board string `json:"board"` + Rewrite string `json:"rewrite"` + Commandline string `json:"commandline"` + Signature string `json:"signature"` + Extra boardExtraInfo `json:"extra"` + Hex []byte `json:"hex"` + Filename string `json:"filename"` +} + func uploadHandler(c *gin.Context) { - log.Print("Received a upload") - port := c.PostForm("port") - if port == "" { + data := new(Upload) + c.BindJSON(data) + + log.Printf("%+v", data) + + if data.Port == "" { c.String(http.StatusBadRequest, "port is required") return } - board := c.PostForm("board") - if board == "" { + + if data.Board == "" { c.String(http.StatusBadRequest, "board is required") log.Error("board is required") return } - board_rewrite := c.PostForm("board_rewrite") - - var extraInfo boardExtraInfo - - extraInfo.authdata.UserName = c.PostForm("auth_user") - extraInfo.authdata.Password = c.PostForm("auth_pass") - commandline := c.PostForm("commandline") - if commandline == "undefined" { - commandline = "" - } - signature := c.PostForm("signature") - if signature == "" { + if data.Signature == "" { c.String(http.StatusBadRequest, "signature is required") - log.Error("signature is required") return } if extraInfo.networkPort { - err := verifyCommandLine(commandline, signature) + err := verifyCommandLine(data.Commandline, data.Signature) if err != nil { c.String(http.StatusBadRequest, "signature is invalid") - log.Error("signature is invalid") - log.Error(err) return } } - extraInfo.use_1200bps_touch, _ = strconv.ParseBool(c.PostForm("use_1200bps_touch")) - extraInfo.wait_for_upload_port, _ = strconv.ParseBool(c.PostForm("wait_for_upload_port")) - extraInfo.networkPort, _ = strconv.ParseBool(c.PostForm("network")) - - if extraInfo.networkPort == false && commandline == "" { + if data.Extra.Network == false && data.Commandline == "" { c.String(http.StatusBadRequest, "commandline is required for local board") - log.Error("commandline is required for local board") return } - sketch, header, err := c.Request.FormFile("sketch_hex") + buffer := bytes.NewBuffer(data.Hex) + + path, err := saveFileonTempDir(data.Filename, buffer) if err != nil { c.String(http.StatusBadRequest, err.Error()) } - if header != nil { - path, err := saveFileonTempDir(header.Filename, sketch) - if err != nil { - c.String(http.StatusBadRequest, err.Error()) - } + if data.Rewrite != "" { + data.Board = data.Rewrite + } - if board_rewrite != "" { - board = board_rewrite - } + go spProgramRW(data.Port, data.Board, path, data.Commandline, data.Extra) - go spProgramRW(port, board, path, commandline, extraInfo) - } + c.String(http.StatusAccepted, "") } func verifyCommandLine(input string, signature string) error { diff --git a/programmer.go b/programmer.go index e3bf5aa3b..36df71b6f 100644 --- a/programmer.go +++ b/programmer.go @@ -5,11 +5,6 @@ import ( "bytes" "encoding/json" "fmt" - log "github.com/Sirupsen/logrus" - "github.com/facchinm/go-serial" - "github.com/mattn/go-shellwords" - "github.com/sfreiberg/simplessh" - "github.com/xrash/smetrics" "io" "mime/multipart" "net/http" @@ -21,6 +16,12 @@ import ( "strconv" "strings" "time" + + log "github.com/Sirupsen/logrus" + "github.com/facchinm/go-serial" + "github.com/mattn/go-shellwords" + "github.com/sfreiberg/simplessh" + "github.com/xrash/smetrics" ) var compiling = false @@ -31,15 +32,15 @@ func colonToUnderscore(input string) string { } type basicAuthData struct { - UserName string - Password string + Username string `json:"username"` + Password string `json:"password"` } type boardExtraInfo struct { - use_1200bps_touch bool - wait_for_upload_port bool - networkPort bool - authdata basicAuthData + Use1200bpsTouch bool `json:"use_100bps_touch"` + WaitForUploadPort bool `json:"wait_for_upload_port"` + Network bool `json:"network"` + Auth basicAuthData `json:"auth"` } // Scp uploads sourceFile to remote machine like native scp console app. @@ -90,15 +91,15 @@ func spProgramSSHNetwork(portname string, boardname string, filePath string, com log.Println("Starting network upload") log.Println("Board Name: " + boardname) - if authdata.UserName == "" { - authdata.UserName = "root" + if authdata.Username == "" { + authdata.Username = "root" } if authdata.Password == "" { authdata.Password = "arduino" } - ssh_client, err := simplessh.ConnectWithPassword(portname+":22", authdata.UserName, authdata.Password) + ssh_client, err := simplessh.ConnectWithPassword(portname+":22", authdata.Username, authdata.Password) if err != nil { log.Println("Error connecting via ssh") return err @@ -133,8 +134,8 @@ func spProgramNetwork(portname string, boardname string, filePath string, authda log.Println("Starting network upload") log.Println("Board Name: " + boardname) - if authdata.UserName == "" { - authdata.UserName = "root" + if authdata.Username == "" { + authdata.Username = "root" } if authdata.Password == "" { @@ -182,8 +183,8 @@ func spProgramNetwork(portname string, boardname string, filePath string, authda } // Don't forget to set the content type, this will contain the boundary. req.Header.Set("Content-Type", w.FormDataContentType()) - if authdata.UserName != "" { - req.SetBasicAuth(authdata.UserName, authdata.Password) + if authdata.Username != "" { + req.SetBasicAuth(authdata.Username, authdata.Password) } //h.broadcastSys <- []byte("Start flashing with command " + cmdString) @@ -211,8 +212,8 @@ func spProgramNetwork(portname string, boardname string, filePath string, authda func spProgramLocal(portname string, boardname string, filePath string, commandline string, extraInfo boardExtraInfo) error { var err error - if extraInfo.use_1200bps_touch { - portname, err = touch_port_1200bps(portname, extraInfo.wait_for_upload_port) + if extraInfo.Use1200bpsTouch { + portname, err = touch_port_1200bps(portname, extraInfo.WaitForUploadPort) } if err != nil { @@ -264,11 +265,11 @@ func spProgramRW(portname string, boardname string, filePath string, commandline var err error - if extraInfo.networkPort { - err = spProgramNetwork(portname, boardname, filePath, extraInfo.authdata) + if extraInfo.Network { + err = spProgramNetwork(portname, boardname, filePath, extraInfo.Auth) if err != nil { // no http method available, try ssh upload - err = spProgramSSHNetwork(portname, boardname, filePath, commandline, extraInfo.authdata) + err = spProgramSSHNetwork(portname, boardname, filePath, commandline, extraInfo.Auth) } } else { err = spProgramLocal(portname, boardname, filePath, commandline, extraInfo) @@ -423,7 +424,7 @@ func findNewPortName(slice1 []string, slice2 []string) string { return "" } -func touch_port_1200bps(portname string, wait_for_upload_port bool) (string, error) { +func touch_port_1200bps(portname string, WaitForUploadPort bool) (string, error) { initialPortName := portname log.Println("Restarting in bootloader mode") @@ -455,7 +456,7 @@ func touch_port_1200bps(portname string, wait_for_upload_port bool) (string, err }() // wait for port to disappear - if wait_for_upload_port { + if WaitForUploadPort { for { ports, _ = serial.GetPortsList() log.Println(ports) @@ -471,7 +472,7 @@ func touch_port_1200bps(portname string, wait_for_upload_port bool) (string, err } // wait for port to reappear - if wait_for_upload_port { + if WaitForUploadPort { after_reset_ports, _ := serial.GetPortsList() log.Println(after_reset_ports) for { From f9cfa85541521ce11ee4a014f298ad7ec321d6f2 Mon Sep 17 00:00:00 2001 From: Matteo Suppo Date: Fri, 29 Apr 2016 16:17:43 +0200 Subject: [PATCH 2/3] Trigger jenkins build --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6d10ea013..d3e33a990 100644 --- a/README.md +++ b/README.md @@ -95,4 +95,5 @@ By making a contribution to this project, I certify that: ## Creating a release Just create a new release on github, and our drone server will build and upload -the compiled binaries for every architecture in a zip file in the release itself. +ithe compiled binaries for every architecture in a zip file in the release itself. + From 34fafb7221b986d46469c2c67c7d7c222ee791d1 Mon Sep 17 00:00:00 2001 From: Matteo Suppo Date: Wed, 4 May 2016 15:39:25 +0200 Subject: [PATCH 3/3] Fix rebase --- conn.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conn.go b/conn.go index 43f4bf0b5..50a552cd0 100644 --- a/conn.go +++ b/conn.go @@ -79,7 +79,7 @@ func uploadHandler(c *gin.Context) { return } - if extraInfo.networkPort { + if data.Extra.Network { err := verifyCommandLine(data.Commandline, data.Signature) if err != nil {