Skip to content

Commit

Permalink
save user config
Browse files Browse the repository at this point in the history
  • Loading branch information
xxf098 committed Aug 6, 2023
1 parent d8814f3 commit 1fee16d
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
19 changes: 19 additions & 0 deletions web/gui/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,25 @@ export default {
this.rowSelection = 'multiple';
this.domLayout = 'autoHeight';
},
beforeMount() {
const url = `${window.location.protocol}//${window.location.host}/getUserConfig`
const requestOptions = { method: "GET", headers: { "Content-Type": "application/json" } };
fetch(url, requestOptions)
.then(resp => resp.json())
.then(data => {
this.group = data.group || "Default"
this.speedtestMode = data.speedtestMode || "all"
this.pingMethod = data.pingMethod || "googleping"
this.sortMethod = data.sortMethod || "rspeed"
this.concurrency = data.concurrency || 2
this.language = data.language || "en"
this.fontSize = data.fontSize || 24
this.theme = data.theme || "rainbow"
this.unique = data.unique || true
this.subscribeProxy = data.subscribeProxy || ""
})
// this.subscribeProxy = "http://127.0.0.1:8090"
},
methods: {
updateRow(id, newData) {
const rowNode = this.gridApi.getRowNode(id);
Expand Down
4 changes: 2 additions & 2 deletions web/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,9 +342,9 @@ type ProfileTestOptions struct {
SortMethod string `json:"sortMethod"` // speed rspeed ping rping
Concurrency int `json:"concurrency"`
TestMode int `json:"testMode"` // 2: ALLTEST 3: RETEST
TestIDs []int `json:"testids"`
TestIDs []int `json:"testids,omitempty"`
Timeout time.Duration `json:"timeout"`
Links []string `json:"links"`
Links []string `json:"links,omitempty"`
Subscription string `json:"subscription"`
Language string `json:"language"`
FontSize int `json:"fontSize"`
Expand Down
41 changes: 41 additions & 0 deletions web/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"time"
Expand All @@ -37,6 +38,7 @@ func ServeFile(port int) error {
http.HandleFunc("/getSubscriptionLink", getSubscriptionLink)
http.HandleFunc("/startProxy", startProxy)
http.HandleFunc("/getSubscription", getSubscription)
http.HandleFunc("/getUserConfig", getUserConfig)
http.HandleFunc("/generateResult", generateResult)
log.Printf("Start server at http://127.0.0.1:%d\n", port)
if ipAddr, err := localIP(); err == nil {
Expand Down Expand Up @@ -104,6 +106,7 @@ func updateTest(w http.ResponseWriter, r *http.Request) {
Options: options,
}
go p.testAll(ctx)
saveUserOptions(options, strings.Split(r.RemoteAddr, ":")[0])
// err = c.WriteMessage(mt, getMsgByte(0, "gotspeed"))
// if err != nil {
// log.Println("write:", err)
Expand Down Expand Up @@ -410,6 +413,44 @@ func getSubscription(w http.ResponseWriter, r *http.Request) {
w.Write(data)
}

func getUserConfig(w http.ResponseWriter, r *http.Request) {
// p, err := getUserConfigFilepath(base64.RawStdEncoding.EncodeToString([]byte(r.RemoteAddr)))
p, err := getUserConfigFilepath(strings.Split(r.RemoteAddr, ":")[0])
if err != nil {
http.Error(w, "fail to get user config file path", 400)
return
}
data, err := os.ReadFile(p)
if err != nil {
http.Error(w, "fail to read user config file ", 400)
return
}
w.Write(data)
}

func getUserConfigFilepath(suffix string) (string, error) {
ex, err := os.Executable()
if err != nil {
return "", err
}
return filepath.Join(filepath.Dir(ex), fmt.Sprintf("userConfig%s.json", suffix)), nil
}

func saveUserOptions(options *ProfileTestOptions, suffix string) {
p, err := getUserConfigFilepath(suffix)
if err != nil {
return
}
f, err := os.OpenFile(p, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
if err != nil {
return
}
defer f.Close()
if data, err := json.Marshal(options); err == nil {
f.Write(data)
}
}

type StartProxyBody struct {
Links []string `json:"links"`
Port uint16 `json:"port"`
Expand Down

0 comments on commit 1fee16d

Please sign in to comment.