Skip to content

Commit

Permalink
Support configuring the base URL
Browse files Browse the repository at this point in the history
  • Loading branch information
zusicheng committed Oct 14, 2019
1 parent b900169 commit 4a3e22b
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 16 deletions.
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ github.com/howeyc/gopass v0.0.0-20170109162249-bf9dde6d0d2c h1:kQWxfPIHVLbgLzphq
github.com/howeyc/gopass v0.0.0-20170109162249-bf9dde6d0d2c/go.mod h1:lADxMC39cJJqL93Duh1xhAs4I2Zs8mKS89XWXFGp9cs=
github.com/json-iterator/go v1.1.7 h1:KfgG9LzI+pYjr4xvmz/5H4FXjokeP+rlHLhv3iH62Fo=
github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kylelemons/go-gypsy v0.0.0-20160905020020-08cad365cd28 h1:mkl3tvPHIuPaWsLtmHTybJeoVEW7cbePK73Ir8VtruA=
github.com/kylelemons/go-gypsy v0.0.0-20160905020020-08cad365cd28/go.mod h1:T/T7jsxVqf9k/zYOqbgNAsANsjxTd1Yq3htjDhQ1H0c=
Expand Down
6 changes: 3 additions & 3 deletions html/src/views/Home.vue
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export default {
});
},
getDevices() {
this.$axios.get('/devs').then(res => {
this.$axios.get(process.env.BASE_URL+'devs').then(res => {
this.loading = false;
this.devlists = res.data;
this.handleSearch();
Expand Down Expand Up @@ -209,7 +209,7 @@ export default {
item.querying = true;
this.$axios.get('/cmd?token=' + token).then((response) => {
this.$axios.get(process.env.BASE_URL+'cmd?token=' + token).then((response) => {
let resp = response.data;
if (resp.err == 1005) {
Expand Down Expand Up @@ -287,7 +287,7 @@ export default {
env: this.cmdData.env
};
this.$axios.post('/cmd', data).then((response) => {
this.$axios.post(process.env.BASE_URL+'cmd', data).then((response) => {
let resp = response.data;
if (resp.token) {
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 @@ -42,7 +42,7 @@ export default {
username: this.form.username,
password: this.form.password
};
this.$axios.post('/signin', params).then(res => {
this.$axios.post(process.env.BASE_URL+'signin', params).then(res => {
sessionStorage.setItem('rtty-sid', res);
this.$router.push('/');
}).catch(() => {
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 + '/ws?devid=' + devid);
let ws = new WebSocket(protocol + location.host + process.env.BASE_URL + 'ws?devid=' + devid);
ws.onopen = () => {
ws.binaryType = 'arraybuffer';
Expand Down
1 change: 1 addition & 0 deletions html/vue.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
publicPath:'/',
devServer: {
proxy: {
'/devs': {
Expand Down
31 changes: 21 additions & 10 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ package main

import (
"fmt"
"net/http"
"os"
"strconv"
"time"

jsoniter "github.com/json-iterator/go"
"github.com/rakyll/statik/fs"
log "github.com/sirupsen/logrus"
"github.com/zhaojh329/rttys/cache"
"github.com/zhaojh329/rttys/pwauth"
_ "github.com/zhaojh329/rttys/statik"
"net/http"
"os"
"strconv"
"time"
)

type Credentials struct {
Expand Down Expand Up @@ -76,16 +77,20 @@ func httpStart(br *Broker, cfg *RttysConfig) {

staticfs := http.FileServer(statikFS)

http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
if cfg.baseURL == "/" {
cfg.baseURL = ""
}

http.HandleFunc(cfg.baseURL+"/ws", func(w http.ResponseWriter, r *http.Request) {
serveWs(br, w, r, cfg)
})

http.HandleFunc("/cmd", func(w http.ResponseWriter, r *http.Request) {
http.HandleFunc(cfg.baseURL+"/cmd", func(w http.ResponseWriter, r *http.Request) {
allowOrigin(w)
serveCmd(br, w, r)
})

http.HandleFunc("/signin", func(w http.ResponseWriter, r *http.Request) {
http.HandleFunc(cfg.baseURL+"/signin", func(w http.ResponseWriter, r *http.Request) {
var creds Credentials

// Get the JSON body and decode into credentials
Expand All @@ -112,7 +117,7 @@ func httpStart(br *Broker, cfg *RttysConfig) {
http.Error(w, "Forbidden", http.StatusForbidden)
})

http.HandleFunc("/devs", func(w http.ResponseWriter, r *http.Request) {
http.HandleFunc(cfg.baseURL+"/devs", func(w http.ResponseWriter, r *http.Request) {
if !httpAuth(w, r) {
return
}
Expand All @@ -131,20 +136,26 @@ func httpStart(br *Broker, cfg *RttysConfig) {
w.Write(resp)
})

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
hfunc := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
t := r.URL.Query().Get("t")
id := r.URL.Query().Get("id")

if t == "" && id == "" {
http.Redirect(w, r, "/?t="+strconv.FormatInt(time.Now().Unix(), 10), http.StatusFound)
http.Redirect(w, r, cfg.baseURL+"?t="+strconv.FormatInt(time.Now().Unix(), 10), http.StatusFound)
return
}
}

staticfs.ServeHTTP(w, r)
})

if cfg.baseURL != "" {
http.Handle(cfg.baseURL+"/", http.StripPrefix(cfg.baseURL, hfunc))
} else {
http.Handle("/", hfunc)
}

if cfg.sslCert != "" && cfg.sslKey != "" {
_, err := os.Lstat(cfg.sslCert)
if err != nil {
Expand Down
6 changes: 5 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import (
"encoding/hex"
"flag"
"fmt"
"golang.org/x/crypto/ssh/terminal"
"io"
"os"
"runtime"
"time"

"golang.org/x/crypto/ssh/terminal"

"github.com/kylelemons/go-gypsy/yaml"
"github.com/zhaojh329/rttys/version"

Expand All @@ -28,6 +29,7 @@ type RttysConfig struct {
username string
password string
token string
baseURL string
}

func init() {
Expand Down Expand Up @@ -86,6 +88,7 @@ func parseConfig() *RttysConfig {
flag.StringVar(&cfg.sslCert, "ssl-cert", "./rttys.crt", "certFile Path")
flag.StringVar(&cfg.sslKey, "ssl-key", "./rttys.key", "keyFile Path")
flag.StringVar(&cfg.token, "token", "", "token to use")
flag.StringVar(&cfg.baseURL, "base-url", "/", "base url to serve on")
conf := flag.String("conf", "./rttys.conf", "config file to load")
genToken := flag.Bool("gen-token", false, "generate token")

Expand All @@ -103,6 +106,7 @@ func parseConfig() *RttysConfig {
setConfigOpt(yamlCfg, "username", &cfg.username)
setConfigOpt(yamlCfg, "password", &cfg.password)
setConfigOpt(yamlCfg, "token", &cfg.token)
setConfigOpt(yamlCfg, "base-url", &cfg.baseURL)
}

return cfg
Expand Down
2 changes: 2 additions & 0 deletions rttys.conf
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
#ssl-cert: /etc/rttys/rttys.crt
#ssl-key: /etc/rttys/rttys.key

#base-url: /

#token: a1d4cdb1a3cd6a0e94aa3599afcddcf5

0 comments on commit 4a3e22b

Please sign in to comment.