diff --git a/go.sum b/go.sum index 2eca085..89751b9 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/html/src/views/Home.vue b/html/src/views/Home.vue index 7df1658..ceec042 100644 --- a/html/src/views/Home.vue +++ b/html/src/views/Home.vue @@ -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(); @@ -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) { @@ -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) { diff --git a/html/src/views/Login.vue b/html/src/views/Login.vue index a7cf637..3f93213 100644 --- a/html/src/views/Login.vue +++ b/html/src/views/Login.vue @@ -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(() => { diff --git a/html/src/views/Rtty.vue b/html/src/views/Rtty.vue index edf72e1..fb7697c 100644 --- a/html/src/views/Rtty.vue +++ b/html/src/views/Rtty.vue @@ -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'; diff --git a/html/vue.config.js b/html/vue.config.js index 4ca8671..e9fec62 100644 --- a/html/vue.config.js +++ b/html/vue.config.js @@ -1,4 +1,5 @@ module.exports = { + publicPath:'/', devServer: { proxy: { '/devs': { diff --git a/http.go b/http.go index 337eeb3..2ba0e9a 100644 --- a/http.go +++ b/http.go @@ -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 { @@ -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 @@ -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 } @@ -131,13 +136,13 @@ 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 } } @@ -145,6 +150,12 @@ func httpStart(br *Broker, cfg *RttysConfig) { 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 { diff --git a/main.go b/main.go index 2a8243e..1a6c430 100644 --- a/main.go +++ b/main.go @@ -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" @@ -28,6 +29,7 @@ type RttysConfig struct { username string password string token string + baseURL string } func init() { @@ -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") @@ -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 diff --git a/rttys.conf b/rttys.conf index bcfe32e..2fa622b 100644 --- a/rttys.conf +++ b/rttys.conf @@ -7,4 +7,6 @@ #ssl-cert: /etc/rttys/rttys.crt #ssl-key: /etc/rttys/rttys.key +#base-url: / + #token: a1d4cdb1a3cd6a0e94aa3599afcddcf5