Support configuring the base URL

This commit is contained in:
zusicheng
2019-10-12 15:50:52 +08:00
parent b900169f52
commit 4a3e22b1a7
8 changed files with 35 additions and 16 deletions
+1
View File
@@ -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/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 h1:KfgG9LzI+pYjr4xvmz/5H4FXjokeP+rlHLhv3iH62Fo=
github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= 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/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 h1:mkl3tvPHIuPaWsLtmHTybJeoVEW7cbePK73Ir8VtruA=
github.com/kylelemons/go-gypsy v0.0.0-20160905020020-08cad365cd28/go.mod h1:T/T7jsxVqf9k/zYOqbgNAsANsjxTd1Yq3htjDhQ1H0c= github.com/kylelemons/go-gypsy v0.0.0-20160905020020-08cad365cd28/go.mod h1:T/T7jsxVqf9k/zYOqbgNAsANsjxTd1Yq3htjDhQ1H0c=
+3 -3
View File
@@ -171,7 +171,7 @@ export default {
}); });
}, },
getDevices() { getDevices() {
this.$axios.get('/devs').then(res => { this.$axios.get(process.env.BASE_URL+'devs').then(res => {
this.loading = false; this.loading = false;
this.devlists = res.data; this.devlists = res.data;
this.handleSearch(); this.handleSearch();
@@ -209,7 +209,7 @@ export default {
item.querying = true; 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; let resp = response.data;
if (resp.err == 1005) { if (resp.err == 1005) {
@@ -287,7 +287,7 @@ export default {
env: this.cmdData.env 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; let resp = response.data;
if (resp.token) { if (resp.token) {
+1 -1
View File
@@ -42,7 +42,7 @@ export default {
username: this.form.username, username: this.form.username,
password: this.form.password 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); sessionStorage.setItem('rtty-sid', res);
this.$router.push('/'); this.$router.push('/');
}).catch(() => { }).catch(() => {
+1 -1
View File
@@ -82,7 +82,7 @@ export default {
this.username = this.$route.query.username; this.username = this.$route.query.username;
this.password = this.$route.query.password; 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.onopen = () => {
ws.binaryType = 'arraybuffer'; ws.binaryType = 'arraybuffer';
+1
View File
@@ -1,4 +1,5 @@
module.exports = { module.exports = {
publicPath:'/',
devServer: { devServer: {
proxy: { proxy: {
'/devs': { '/devs': {
+21 -10
View File
@@ -2,16 +2,17 @@ package main
import ( import (
"fmt" "fmt"
"net/http"
"os"
"strconv"
"time"
jsoniter "github.com/json-iterator/go" jsoniter "github.com/json-iterator/go"
"github.com/rakyll/statik/fs" "github.com/rakyll/statik/fs"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/zhaojh329/rttys/cache" "github.com/zhaojh329/rttys/cache"
"github.com/zhaojh329/rttys/pwauth" "github.com/zhaojh329/rttys/pwauth"
_ "github.com/zhaojh329/rttys/statik" _ "github.com/zhaojh329/rttys/statik"
"net/http"
"os"
"strconv"
"time"
) )
type Credentials struct { type Credentials struct {
@@ -76,16 +77,20 @@ func httpStart(br *Broker, cfg *RttysConfig) {
staticfs := http.FileServer(statikFS) 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) 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) allowOrigin(w)
serveCmd(br, w, r) 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 var creds Credentials
// Get the JSON body and decode into 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.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) { if !httpAuth(w, r) {
return return
} }
@@ -131,13 +136,13 @@ func httpStart(br *Broker, cfg *RttysConfig) {
w.Write(resp) 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 == "/" { if r.URL.Path == "/" {
t := r.URL.Query().Get("t") t := r.URL.Query().Get("t")
id := r.URL.Query().Get("id") id := r.URL.Query().Get("id")
if t == "" && 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 return
} }
} }
@@ -145,6 +150,12 @@ func httpStart(br *Broker, cfg *RttysConfig) {
staticfs.ServeHTTP(w, r) 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 != "" { if cfg.sslCert != "" && cfg.sslKey != "" {
_, err := os.Lstat(cfg.sslCert) _, err := os.Lstat(cfg.sslCert)
if err != nil { if err != nil {
+5 -1
View File
@@ -7,12 +7,13 @@ import (
"encoding/hex" "encoding/hex"
"flag" "flag"
"fmt" "fmt"
"golang.org/x/crypto/ssh/terminal"
"io" "io"
"os" "os"
"runtime" "runtime"
"time" "time"
"golang.org/x/crypto/ssh/terminal"
"github.com/kylelemons/go-gypsy/yaml" "github.com/kylelemons/go-gypsy/yaml"
"github.com/zhaojh329/rttys/version" "github.com/zhaojh329/rttys/version"
@@ -28,6 +29,7 @@ type RttysConfig struct {
username string username string
password string password string
token string token string
baseURL string
} }
func init() { func init() {
@@ -86,6 +88,7 @@ func parseConfig() *RttysConfig {
flag.StringVar(&cfg.sslCert, "ssl-cert", "./rttys.crt", "certFile Path") flag.StringVar(&cfg.sslCert, "ssl-cert", "./rttys.crt", "certFile Path")
flag.StringVar(&cfg.sslKey, "ssl-key", "./rttys.key", "keyFile Path") flag.StringVar(&cfg.sslKey, "ssl-key", "./rttys.key", "keyFile Path")
flag.StringVar(&cfg.token, "token", "", "token to use") 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") conf := flag.String("conf", "./rttys.conf", "config file to load")
genToken := flag.Bool("gen-token", false, "generate token") genToken := flag.Bool("gen-token", false, "generate token")
@@ -103,6 +106,7 @@ func parseConfig() *RttysConfig {
setConfigOpt(yamlCfg, "username", &cfg.username) setConfigOpt(yamlCfg, "username", &cfg.username)
setConfigOpt(yamlCfg, "password", &cfg.password) setConfigOpt(yamlCfg, "password", &cfg.password)
setConfigOpt(yamlCfg, "token", &cfg.token) setConfigOpt(yamlCfg, "token", &cfg.token)
setConfigOpt(yamlCfg, "base-url", &cfg.baseURL)
} }
return cfg return cfg
+2
View File
@@ -7,4 +7,6 @@
#ssl-cert: /etc/rttys/rttys.crt #ssl-cert: /etc/rttys/rttys.crt
#ssl-key: /etc/rttys/rttys.key #ssl-key: /etc/rttys/rttys.key
#base-url: /
#token: a1d4cdb1a3cd6a0e94aa3599afcddcf5 #token: a1d4cdb1a3cd6a0e94aa3599afcddcf5