Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

73 changes: 33 additions & 40 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package main

import (
"bytes"
"crypto"
"crypto/rsa"
"crypto/sha256"
Expand All @@ -11,7 +12,6 @@ import (
"encoding/pem"
"errors"
"net/http"
"strconv"

log "github.com/Sirupsen/logrus"
"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -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)
if data.Extra.Network {
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 {
Expand Down
53 changes: 27 additions & 26 deletions programmer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 == "" {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand Down