Skip to content

Commit

Permalink
Merge pull request zhaojh329#34 from zsichen/dev
Browse files Browse the repository at this point in the history
Check session id before accepting request
  • Loading branch information
zhaojh329 authored Nov 5, 2019
2 parents f877694 + 3ab22bf commit ebd9b0d
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 18 deletions.
9 changes: 7 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package main

import (
"github.com/gorilla/websocket"
log "github.com/sirupsen/logrus"
"net/http"
"strconv"
"sync"
"time"

"github.com/gorilla/websocket"
log "github.com/sirupsen/logrus"
)

var upgrader = websocket.Upgrader{
Expand Down Expand Up @@ -73,6 +74,10 @@ func serveWs(br *Broker, w http.ResponseWriter, r *http.Request, cfg *RttysConfi
http.Error(w, "Forbidden", http.StatusForbidden)
return
}
} else if _, ok := httpSessions.Get(r.URL.Query().Get("sid")); !ok {
log.Error("Invalid sid from client")
http.Error(w, "Forbidden", http.StatusForbidden)
return
}

keepalive, _ := strconv.Atoi(r.URL.Query().Get("keepalive"))
Expand Down
14 changes: 9 additions & 5 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package main

import (
"fmt"
jsoniter "github.com/json-iterator/go"
log "github.com/sirupsen/logrus"
"io"
"io/ioutil"
"net/http"
"sync"
"time"

jsoniter "github.com/json-iterator/go"
log "github.com/sirupsen/logrus"

"github.com/gorilla/websocket"
)

Expand Down Expand Up @@ -38,8 +39,11 @@ type commandStatus struct {
}

type CommandInfo struct {
Devid string `json:"devid"`
Cmd string `json:"cmd"`
Devid string `json:"devid"`
Cmd string `json:"cmd"`
Sid string `json:"sid"`
Username string `json:"username"`
Password string `json:"password"`
}

var commands sync.Map
Expand Down Expand Up @@ -85,7 +89,7 @@ func serveCmd(br *Broker, w http.ResponseWriter, r *http.Request) {

cmdInfo := CommandInfo{}
err = jsoniter.Unmarshal(body, &cmdInfo)
if err != nil || cmdInfo.Cmd == "" || cmdInfo.Devid == "" {
if _, ok := httpSessions.Get(cmdInfo.Sid); err != nil || cmdInfo.Cmd == "" || cmdInfo.Devid == "" || ok == false {
cmdErrReply(RTTY_CMD_ERR_INVALID, w)
return
}
Expand Down
11 changes: 3 additions & 8 deletions html/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions html/src/views/Home.vue
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ export default {
devid: item.id,
username: this.cmdData.username,
password: this.cmdData.password,
sid: sessionStorage.getItem('rtty-sid'),
cmd: this.cmdData.cmd.trim(),
params: this.cmdData.params,
env: this.cmdData.env
Expand Down
2 changes: 1 addition & 1 deletion html/src/views/Login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default {
password: this.form.password
};
this.$axios.post(process.env.BASE_URL + 'signin', params).then(res => {
sessionStorage.setItem('rtty-sid', res);
sessionStorage.setItem('rtty-sid', res.data);
this.$router.push('/');
}).catch(() => {
this.$Message.error(this.$t('Signin Fail! username or password wrong.'));
Expand Down
2 changes: 1 addition & 1 deletion html/src/views/Rtty.vue
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export default {
this.username = this.$route.query.username;
this.password = this.$route.query.password;
let ws = new WebSocket(protocol + location.host + process.env.BASE_URL + 'ws?devid=' + devid);
let ws = new WebSocket(protocol + location.host + process.env.BASE_URL + 'ws?devid=' + devid + '&sid=' + sessionStorage.getItem('rtty-sid'));
ws.onopen = () => {
ws.binaryType = 'arraybuffer';
Expand Down
2 changes: 1 addition & 1 deletion statik/statik.go

Large diffs are not rendered by default.

0 comments on commit ebd9b0d

Please sign in to comment.