Browse Source

add login fail warn &
add web client on/off &
up admin peer filter &
upgrade web client

ljw 1 year ago
parent
commit
b4965e8885

+ 24 - 21
README.md

@@ -151,6 +151,8 @@
151 151
 
152 152
 ```yaml
153 153
 lang: "en"
154
+app:
155
+  web-client: 1  # 1:启用 0:禁用
154 156
 gin:
155 157
   api-addr: "0.0.0.0:21114"
156 158
   mode: "release"
@@ -180,27 +182,28 @@ logger:
180 182
 ### 环境变量
181 183
 变量名前缀是`RUSTDESK_API`,环境变量如果存在将覆盖配置文件中的配置
182 184
 
183
-| 变量名                                 | 说明                                   | 示例                          |
184
-|-------------------------------------|--------------------------------------|-----------------------------|
185
-| TZ                                  | 时区                                   | Asia/Shanghai               |
186
-| RUSTDESK_API_LANG                   | 语言                                   | `en`,`zh-CN`                |
187
-| -----GIN配置-----                     | ----------                           | ----------                  |
188
-| RUSTDESK_API_GIN_TRUST_PROXY        | 信任的代理IP列表,以`,`分割,默认信任所有              | 192.168.1.2,192.168.1.3     |
189
-| -----------GORM配置------------------ | ------------------------------------ | --------------------------- |
190
-| RUSTDESK_API_GORM_TYPE              | 数据库类型sqlite或者mysql,默认sqlite          | sqlite                      |
191
-| RUSTDESK_API_GORM_MAX_IDLE_CONNS    | 数据库最大空闲连接数                           | 10                          |
192
-| RUSTDESK_API_GORM_MAX_OPEN_CONNS    | 数据库最大打开连接数                           | 100                         |
193
-| RUSTDESK_API_RUSTDESK_PERSONAL      | 是否启用个人版API, 1:启用,0:不启用; 默认启用         | 1                           |
194
-| -----MYSQL配置-----                   | ----------                           | ----------                  |
195
-| RUSTDESK_API_MYSQL_USERNAME         | mysql用户名                             | root                        |
196
-| RUSTDESK_API_MYSQL_PASSWORD         | mysql密码                              | 111111                      |
197
-| RUSTDESK_API_MYSQL_ADDR             | mysql地址                              | 192.168.1.66:3306           |
198
-| RUSTDESK_API_MYSQL_DBNAME           | mysql数据库名                            | rustdesk                    |
199
-| -----RUSTDESK配置-----                | ---------------                      | ----------                  |
200
-| RUSTDESK_API_RUSTDESK_ID_SERVER     | Rustdesk的id服务器地址                     | 192.168.1.66:21116          |
201
-| RUSTDESK_API_RUSTDESK_RELAY_SERVER  | Rustdesk的relay服务器地址                  | 192.168.1.66:21117          |
202
-| RUSTDESK_API_RUSTDESK_API_SERVER    | Rustdesk的api服务器地址                    | http://192.168.1.66:21114   |
203
-| RUSTDESK_API_RUSTDESK_KEY           | Rustdesk的key                         | 123456789                   |
185
+| 变量名                                | 说明                                   | 示例                          |
186
+|------------------------------------|--------------------------------------|-----------------------------|
187
+| TZ                                 | 时区                                   | Asia/Shanghai               |
188
+| RUSTDESK_API_LANG                  | 语言                                   | `en`,`zh-CN`                |
189
+| RUSTDESK_API_APP_WEB_CLIENT        | 是否启用web-client; 1:启用,0:不启用; 默认启用     | 1                           |
190
+| -----GIN配置-----                    | ----------                           | ----------                  |
191
+| RUSTDESK_API_GIN_TRUST_PROXY       | 信任的代理IP列表,以`,`分割,默认信任所有              | 192.168.1.2,192.168.1.3     |
192
+| -----------GORM配置----------------  | ------------------------------------ | --------------------------- |
193
+| RUSTDESK_API_GORM_TYPE             | 数据库类型sqlite或者mysql,默认sqlite          | sqlite                      |
194
+| RUSTDESK_API_GORM_MAX_IDLE_CONNS   | 数据库最大空闲连接数                           | 10                          |
195
+| RUSTDESK_API_GORM_MAX_OPEN_CONNS   | 数据库最大打开连接数                           | 100                         |
196
+| RUSTDESK_API_RUSTDESK_PERSONAL     | 是否启用个人版API, 1:启用,0:不启用; 默认启用         | 1                           |
197
+| -----MYSQL配置-----                  | ----------                           | ----------                  |
198
+| RUSTDESK_API_MYSQL_USERNAME        | mysql用户名                             | root                        |
199
+| RUSTDESK_API_MYSQL_PASSWORD        | mysql密码                              | 111111                      |
200
+| RUSTDESK_API_MYSQL_ADDR            | mysql地址                              | 192.168.1.66:3306           |
201
+| RUSTDESK_API_MYSQL_DBNAME          | mysql数据库名                            | rustdesk                    |
202
+| -----RUSTDESK配置-----               | ---------------                      | ----------                  |
203
+| RUSTDESK_API_RUSTDESK_ID_SERVER    | Rustdesk的id服务器地址                     | 192.168.1.66:21116          |
204
+| RUSTDESK_API_RUSTDESK_RELAY_SERVER | Rustdesk的relay服务器地址                  | 192.168.1.66:21117          |
205
+| RUSTDESK_API_RUSTDESK_API_SERVER   | Rustdesk的api服务器地址                    | http://192.168.1.66:21114   |
206
+| RUSTDESK_API_RUSTDESK_KEY          | Rustdesk的key                         | 123456789                   |
204 207
 
205 208
 ### 运行
206 209
 

+ 24 - 21
README_EN.md

@@ -157,6 +157,8 @@ installation are `admin` `admin`, please change the password immediately.
157 157
 
158 158
 ```yaml
159 159
 lang: "en"
160
+app:
161
+  web-client: 1  # web client route 1:open 0:close  
160 162
 gin:
161 163
   api-addr: "0.0.0.0:21114"
162 164
   mode: "release"
@@ -186,27 +188,28 @@ logger:
186 188
 ### Environment Variables
187 189
 The prefix for variable names is `RUSTDESK_API`. If environment variables exist, they will override the configurations in the configuration file.
188 190
 
189
-| Variable Name                      | Description                                               | Example                        |
190
-|------------------------------------|-----------------------------------------------------------|--------------------------------|
191
-| TZ                                 | timezone                                                  | Asia/Shanghai                  |
192
-| RUSTDESK_API_LANG                  | Language                                                  | `en`,`zh-CN`                   |
193
-| ----- GIN Configuration -----      | ---------------------------------------                   | ------------------------------ |
194
-| RUSTDESK_API_GIN_TRUST_PROXY       | Trusted proxy IPs, separated by commas.                   | 192.168.1.2,192.168.1.3        |
195
-| ----- GORM Configuration -----     | ---------------------------------------                   | ------------------------------ |
196
-| RUSTDESK_API_GORM_TYPE             | Database type (`sqlite` or `mysql`). Default is `sqlite`. | sqlite                         |
197
-| RUSTDESK_API_GORM_MAX_IDLE_CONNS   | Maximum idle connections                                  | 10                             |
198
-| RUSTDESK_API_GORM_MAX_OPEN_CONNS   | Maximum open connections                                  | 100                            |
199
-| RUSTDESK_API_RUSTDESK_PERSONAL     | Open Personal Api 1:Enable,0:Disable                      | 1                              |
200
-| ----- MYSQL Configuration -----    | ---------------------------------------                   | ------------------------------ |
201
-| RUSTDESK_API_MYSQL_USERNAME        | MySQL username                                            | root                           |
202
-| RUSTDESK_API_MYSQL_PASSWORD        | MySQL password                                            | 111111                         |
203
-| RUSTDESK_API_MYSQL_ADDR            | MySQL address                                             | 192.168.1.66:3306              |
204
-| RUSTDESK_API_MYSQL_DBNAME          | MySQL database name                                       | rustdesk                       |
205
-| ----- RUSTDESK Configuration ----- | ---------------------------------------                   | ------------------------------ |
206
-| RUSTDESK_API_RUSTDESK_ID_SERVER    | Rustdesk ID server address                                | 192.168.1.66:21116             |
207
-| RUSTDESK_API_RUSTDESK_RELAY_SERVER | Rustdesk relay server address                             | 192.168.1.66:21117             |
208
-| RUSTDESK_API_RUSTDESK_API_SERVER   | Rustdesk API server address                               | http://192.168.1.66:21114      |
209
-| RUSTDESK_API_RUSTDESK_KEY          | Rustdesk key                                              | 123456789                      |
191
+| Variable Name                      | Description                                               | Example                       |
192
+|------------------------------------|-----------------------------------------------------------|-------------------------------|
193
+| TZ                                 | timezone                                                  | Asia/Shanghai                 |
194
+| RUSTDESK_API_LANG                  | Language                                                  | `en`,`zh-CN`                  |
195
+| RUSTDESK_API_APP_WEB_CLIENT        | web client on/off; 1: on, 0 off, deault 1                 | 1                             |
196
+| ----- GIN Configuration -----      | ---------------------------------------                   | ----------------------------- |
197
+| RUSTDESK_API_GIN_TRUST_PROXY       | Trusted proxy IPs, separated by commas.                   | 192.168.1.2,192.168.1.3       |
198
+| ----- GORM Configuration -----     | ---------------------------------------                   | ----------------------------- |
199
+| RUSTDESK_API_GORM_TYPE             | Database type (`sqlite` or `mysql`). Default is `sqlite`. | sqlite                        |
200
+| RUSTDESK_API_GORM_MAX_IDLE_CONNS   | Maximum idle connections                                  | 10                            |
201
+| RUSTDESK_API_GORM_MAX_OPEN_CONNS   | Maximum open connections                                  | 100                           |
202
+| RUSTDESK_API_RUSTDESK_PERSONAL     | Open Personal Api 1:Enable,0:Disable                      | 1                             |
203
+| ----- MYSQL Configuration -----    | ---------------------------------------                   | ----------------------------- |
204
+| RUSTDESK_API_MYSQL_USERNAME        | MySQL username                                            | root                          |
205
+| RUSTDESK_API_MYSQL_PASSWORD        | MySQL password                                            | 111111                        |
206
+| RUSTDESK_API_MYSQL_ADDR            | MySQL address                                             | 192.168.1.66:3306             |
207
+| RUSTDESK_API_MYSQL_DBNAME          | MySQL database name                                       | rustdesk                      |
208
+| ----- RUSTDESK Configuration ----- | ---------------------------------------                   | ----------------------------- |
209
+| RUSTDESK_API_RUSTDESK_ID_SERVER    | Rustdesk ID server address                                | 192.168.1.66:21116            |
210
+| RUSTDESK_API_RUSTDESK_RELAY_SERVER | Rustdesk relay server address                             | 192.168.1.66:21117            |
211
+| RUSTDESK_API_RUSTDESK_API_SERVER   | Rustdesk API server address                               | http://192.168.1.66:21114     |
212
+| RUSTDESK_API_RUSTDESK_KEY          | Rustdesk key                                              | 123456789                     |
210 213
 
211 214
 ### Installation Steps
212 215
 

+ 3 - 1
conf/config.yaml

@@ -1,4 +1,6 @@
1 1
 lang: "zh-CN"
2
+app:
3
+  web-client: 1  # 1:启用 0:禁用
2 4
 gin:
3 5
   api-addr: "0.0.0.0:21114"
4 6
   mode: "release" #release,debug,test
@@ -16,7 +18,7 @@ mysql:
16 18
 rustdesk:
17 19
   id-server: "192.168.1.66:21116"
18 20
   relay-server: "192.168.1.66:21117"
19
-  api-server: "http://192.168.1.66:21114"
21
+  api-server: "http://127.0.0.1:21114"
20 22
   key: "123456789"
21 23
   personal: 1
22 24
 logger:

+ 5 - 0
config/config.go

@@ -14,8 +14,13 @@ const (
14 14
 	DefaultConfig = "conf/config.yaml"
15 15
 )
16 16
 
17
+type App struct {
18
+	WebClient int `mapstructure:"web-client"`
19
+}
20
+
17 21
 type Config struct {
18 22
 	Lang     string `mapstructure:"lang"`
23
+	App      App
19 24
 	Gorm     Gorm
20 25
 	Mysql    Mysql
21 26
 	Gin      Gin

+ 4 - 0
http/controller/admin/login.go

@@ -7,6 +7,7 @@ import (
7 7
 	adResp "Gwen/http/response/admin"
8 8
 	"Gwen/model"
9 9
 	"Gwen/service"
10
+	"fmt"
10 11
 	"github.com/gin-gonic/gin"
11 12
 )
12 13
 
@@ -28,18 +29,21 @@ func (ct *Login) Login(c *gin.Context) {
28 29
 	f := &admin.Login{}
29 30
 	err := c.ShouldBindJSON(f)
30 31
 	if err != nil {
32
+		global.Logger.Warn(fmt.Sprintf("Login Fail: %s %s %s", "ParamsError", c.RemoteIP(), c.ClientIP()))
31 33
 		response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
32 34
 		return
33 35
 	}
34 36
 
35 37
 	errList := global.Validator.ValidStruct(c, f)
36 38
 	if len(errList) > 0 {
39
+		global.Logger.Warn(fmt.Sprintf("Login Fail: %s %s %s", "ParamsError", c.RemoteIP(), c.ClientIP()))
37 40
 		response.Fail(c, 101, errList[0])
38 41
 		return
39 42
 	}
40 43
 	u := service.AllService.UserService.InfoByUsernamePassword(f.Username, f.Password)
41 44
 
42 45
 	if u.Id == 0 {
46
+		global.Logger.Warn(fmt.Sprintf("Login Fail: %s %s %s", "UsernameOrPasswordError", c.RemoteIP(), c.ClientIP()))
43 47
 		response.Fail(c, 101, response.TranslateMsg(c, "UsernameOrPasswordError"))
44 48
 		return
45 49
 	}

+ 6 - 0
http/controller/admin/peer.go

@@ -96,6 +96,12 @@ func (ct *Peer) List(c *gin.Context) {
96 96
 			lt := time.Now().Unix() + int64(query.TimeAgo)
97 97
 			tx.Where("last_online_time > ?", lt)
98 98
 		}
99
+		if query.Id != "" {
100
+			tx.Where("id like ?", "%"+query.Id+"%")
101
+		}
102
+		if query.Hostname != "" {
103
+			tx.Where("hostname like ?", "%"+query.Hostname+"%")
104
+		}
99 105
 	})
100 106
 	response.Success(c, res)
101 107
 }

+ 18 - 2
http/controller/admin/rustdesk.go

@@ -9,9 +9,9 @@ import (
9 9
 type Rustdesk struct {
10 10
 }
11 11
 
12
-// ServerConfig 服务配置
12
+// ServerConfig RUSTDESK服务配置
13 13
 // @Tags ADMIN
14
-// @Summary 服务配置
14
+// @Summary RUSTDESK服务配置
15 15
 // @Description 服务配置,给webclient提供api-server
16 16
 // @Accept  json
17 17
 // @Produce  json
@@ -28,3 +28,19 @@ func (r *Rustdesk) ServerConfig(c *gin.Context) {
28 28
 	}
29 29
 	response.Success(c, cf)
30 30
 }
31
+
32
+// AppConfig APP服务配置
33
+// @Tags ADMIN
34
+// @Summary APP服务配置
35
+// @Description APP服务配置
36
+// @Accept  json
37
+// @Produce  json
38
+// @Success 200 {object} response.Response
39
+// @Failure 500 {object} response.Response
40
+// @Router /admin/app-config [get]
41
+// @Security token
42
+func (r *Rustdesk) AppConfig(c *gin.Context) {
43
+	response.Success(c, &gin.H{
44
+		"web_client": global.Config.App.WebClient,
45
+	})
46
+}

+ 1 - 2
http/controller/api/ab.go

@@ -8,7 +8,6 @@ import (
8 8
 	"Gwen/model"
9 9
 	"Gwen/service"
10 10
 	"encoding/json"
11
-	"fmt"
12 11
 	"github.com/gin-gonic/gin"
13 12
 	"net/http"
14 13
 	"strconv"
@@ -402,7 +401,7 @@ func (a *Ab) PeerAdd(c *gin.Context) {
402 401
 		response.Error(c, response.TranslateMsg(c, "ParamsError")+err.Error())
403 402
 		return
404 403
 	}
405
-	fmt.Println(f)
404
+	//fmt.Println(f)
406 405
 	u := service.AllService.UserService.CurUser(c)
407 406
 	f.UserId = u.Id
408 407
 	ab := f.ToAddressBook()

+ 4 - 0
http/controller/api/login.go

@@ -8,6 +8,7 @@ import (
8 8
 	"Gwen/model"
9 9
 	"Gwen/service"
10 10
 	"encoding/json"
11
+	"fmt"
11 12
 	"github.com/gin-gonic/gin"
12 13
 	"net/http"
13 14
 )
@@ -30,12 +31,14 @@ func (l *Login) Login(c *gin.Context) {
30 31
 	err := c.ShouldBindJSON(f)
31 32
 	//fmt.Println(f)
32 33
 	if err != nil {
34
+		global.Logger.Warn(fmt.Sprintf("Login Fail: %s %s %s", "ParamsError", c.RemoteIP(), c.ClientIP()))
33 35
 		response.Error(c, response.TranslateMsg(c, "ParamsError")+err.Error())
34 36
 		return
35 37
 	}
36 38
 
37 39
 	errList := global.Validator.ValidStruct(c, f)
38 40
 	if len(errList) > 0 {
41
+		global.Logger.Warn(fmt.Sprintf("Login Fail: %s %s %s", "ParamsError", c.RemoteIP(), c.ClientIP()))
39 42
 		response.Error(c, errList[0])
40 43
 		return
41 44
 	}
@@ -43,6 +46,7 @@ func (l *Login) Login(c *gin.Context) {
43 46
 	u := service.AllService.UserService.InfoByUsernamePassword(f.Username, f.Password)
44 47
 
45 48
 	if u.Id == 0 {
49
+		global.Logger.Warn(fmt.Sprintf("Login Fail: %s %s %s", "UsernameOrPasswordError", c.RemoteIP(), c.ClientIP()))
46 50
 		response.Error(c, response.TranslateMsg(c, "UsernameOrPasswordError"))
47 51
 		return
48 52
 	}

+ 14 - 4
http/controller/api/peer.go

@@ -22,7 +22,6 @@ type Peer struct {
22 22
 // @Success 200 {string} string "SYSINFO_UPDATED,ID_NOT_FOUND"
23 23
 // @Failure 500 {object} response.ErrorResponse
24 24
 // @Router /sysinfo [post]
25
-// @Security BearerAuth
26 25
 func (p *Peer) SysInfo(c *gin.Context) {
27 26
 	f := &requstform.PeerForm{}
28 27
 	err := c.ShouldBindBodyWith(f, binding.JSON)
@@ -30,17 +29,28 @@ func (p *Peer) SysInfo(c *gin.Context) {
30 29
 		response.Error(c, response.TranslateMsg(c, "ParamsError")+err.Error())
31 30
 		return
32 31
 	}
33
-
32
+	fpe := f.ToPeer()
34 33
 	pe := service.AllService.PeerService.FindById(f.Id)
35
-	if pe == nil || pe.RowId == 0 {
34
+	if pe.RowId == 0 {
36 35
 		pe = f.ToPeer()
36
+		pe.UserId = service.AllService.UserService.FindLatestUserIdFromLoginLogByUuid(pe.Uuid)
37 37
 		err = service.AllService.PeerService.Create(pe)
38 38
 		if err != nil {
39 39
 			response.Error(c, response.TranslateMsg(c, "OperationFailed")+err.Error())
40 40
 			return
41 41
 		}
42
+	} else {
43
+		if pe.UserId == 0 {
44
+			pe.UserId = service.AllService.UserService.FindLatestUserIdFromLoginLogByUuid(pe.Uuid)
45
+		}
46
+		fpe.RowId = pe.RowId
47
+		fpe.UserId = pe.UserId
48
+		err = service.AllService.PeerService.Update(fpe)
49
+		if err != nil {
50
+			response.Error(c, response.TranslateMsg(c, "OperationFailed")+err.Error())
51
+			return
52
+		}
42 53
 	}
43
-
44 54
 	//SYSINFO_UPDATED 上传成功
45 55
 	//ID_NOT_FOUND 下次心跳会上传
46 56
 	//直接响应文本

+ 1 - 1
http/middleware/rustauth.go

@@ -7,7 +7,7 @@ import (
7 7
 
8 8
 func RustAuth() gin.HandlerFunc {
9 9
 	return func(c *gin.Context) {
10
-		//fmt.Println(c.Request.Header)
10
+		//fmt.Println(c.Request.URL, c.Request.Header)
11 11
 		//获取HTTP_AUTHORIZATION
12 12
 		token := c.GetHeader("Authorization")
13 13
 		if token == "" {

+ 3 - 1
http/request/admin/peer.go

@@ -35,5 +35,7 @@ func (f *PeerForm) ToPeer() *model.Peer {
35 35
 
36 36
 type PeerQuery struct {
37 37
 	PageQuery
38
-	TimeAgo int `json:"time_ago" form:"time_ago"`
38
+	TimeAgo  int    `json:"time_ago" form:"time_ago"`
39
+	Id       string `json:"id" form:"id"`
40
+	Hostname string `json:"hostname" form:"hostname"`
39 41
 }

+ 1 - 0
http/router/admin.go

@@ -30,6 +30,7 @@ func Init(g *gin.Engine) {
30 30
 
31 31
 	rs := &admin.Rustdesk{}
32 32
 	adg.GET("/server-config", rs.ServerConfig)
33
+	adg.GET("/app-config", rs.AppConfig)
33 34
 
34 35
 	//访问静态文件
35 36
 	//g.StaticFS("/upload", http.Dir(global.Config.Gin.ResourcesPath+"/upload"))

+ 13 - 8
http/router/api.go

@@ -47,17 +47,11 @@ func ApiInit(g *gin.Engine) {
47 47
 		frg.POST("/sysinfo", pe.SysInfo)
48 48
 	}
49 49
 
50
-	{
51
-		w := &api.WebClient{}
52
-		frg.POST("/shared-peer", w.SharedPeer)
50
+	if global.Config.App.WebClient == 1 {
51
+		WebClientRoutes(frg)
53 52
 	}
54 53
 
55 54
 	frg.Use(middleware.RustAuth())
56
-	{
57
-		w := &api.WebClient{}
58
-		frg.POST("/server-config", w.ServerConfig)
59
-	}
60
-
61 55
 	{
62 56
 		u := &api.User{}
63 57
 		frg.GET("/user/info", u.Info)
@@ -115,3 +109,14 @@ func PersonalRoutes(frg *gin.RouterGroup) {
115 109
 	}
116 110
 
117 111
 }
112
+
113
+func WebClientRoutes(frg *gin.RouterGroup) {
114
+	w := &api.WebClient{}
115
+	{
116
+		frg.POST("/shared-peer", w.SharedPeer)
117
+	}
118
+	{
119
+		frg.POST("/server-config", middleware.RustAuth(), w.ServerConfig)
120
+	}
121
+
122
+}

+ 8 - 2
http/router/router.go

@@ -10,7 +10,13 @@ import (
10 10
 func WebInit(g *gin.Engine) {
11 11
 	i := &web.Index{}
12 12
 	g.GET("/", i.Index)
13
-	g.GET("/webclient-config/index.js", i.ConfigJs)
14
-	g.StaticFS("/webclient", http.Dir(global.Config.Gin.ResourcesPath+"/web"))
13
+
14
+	if global.Config.App.WebClient == 1 {
15
+		g.GET("/webclient-config/index.js", i.ConfigJs)
16
+	}
17
+
18
+	if global.Config.App.WebClient == 1 {
19
+		g.StaticFS("/webclient", http.Dir(global.Config.Gin.ResourcesPath+"/web"))
20
+	}
15 21
 	g.StaticFS("/_admin", http.Dir(global.Config.Gin.ResourcesPath+"/admin"))
16 22
 }

+ 1 - 0
lib/orm/sqlite.go

@@ -16,6 +16,7 @@ type SqliteConfig struct {
16 16
 
17 17
 func NewSqlite(sqliteConf *SqliteConfig) *gorm.DB {
18 18
 	db, err := gorm.Open(sqlite.Open("./data/rustdeskapi.db"), &gorm.Config{
19
+		DisableForeignKeyConstraintWhenMigrating: true,
19 20
 		Logger: logger.New(
20 21
 			global.Logger, // io writer
21 22
 			logger.Config{

File diff suppressed because it is too large
+ 2 - 2
resources/web/js/dist/index.js


File diff suppressed because it is too large
+ 279 - 268
resources/web/js/src/globals.js


+ 43 - 44
resources/web/js/src/ljw.js

@@ -49,52 +49,51 @@ if (share_token) {
49 49
     })
50 50
 }
51 51
 
52
-
53
-const autoWriteServer = () => {
54
-    return setTimeout(() => {
55
-        const token = localStorage.getItem('access_token')
56
-        if (token && apiserver) {
57
-            fetch(apiserver + "/api/server-config", {
58
-                    method: 'POST',
59
-                    headers: {
60
-                        'Content-Type': 'application/json',
61
-                        'Authorization': 'Bearer ' + token
62
-                    }
63
-                }
64
-            ).then(res => res.json()).then(res => {
65
-                if (res.code === 0) {
66
-                    if (!localStorage.getItem('custom-rendezvous-server') || !localStorage.getItem('key')) {
67
-                        localStorage.setItem('custom-rendezvous-server', res.data.id_server)
68
-                        localStorage.setItem('key', res.data.key)
52
+let fetching = false
53
+export function getServerConf(token){
54
+    console.log('getServerConf', token)
55
+    if(fetching){
56
+        return
57
+    }
58
+    fetching = true
59
+    fetch(apiserver + "/api/server-config", {
60
+            method: 'POST',
61
+            headers: {
62
+                'Content-Type': 'application/json',
63
+                'Authorization': 'Bearer ' + token
64
+            }
65
+        }
66
+    ).then(res => res.json()).then(res => {
67
+        fetching = false
68
+        if (res.code === 0) {
69
+            if (!localStorage.getItem('custom-rendezvous-server') || !localStorage.getItem('key')) {
70
+                localStorage.setItem('custom-rendezvous-server', res.data.id_server)
71
+                localStorage.setItem('key', res.data.key)
72
+            }
73
+            if (res.data.peers) {
74
+                const oldPeers = JSON.parse(localStorage.getItem('peers')) || {}
75
+                let needUpdate = false
76
+                Object.keys(res.data.peers).forEach(k => {
77
+                    if (!oldPeers[k]) {
78
+                        oldPeers[k] = res.data.peers[k]
79
+                        needUpdate = true
80
+                    } else {
81
+                        oldPeers[k].info = res.data.peers[k].info
69 82
                     }
70
-
71
-                    if (res.data.peers) {
72
-                        const oldPeers = JSON.parse(localStorage.getItem('peers')) || {}
73
-                        let needUpdate = false
74
-                        Object.keys(res.data.peers).forEach(k => {
75
-                            if (!oldPeers[k]) {
76
-                                oldPeers[k] = res.data.peers[k]
77
-                                needUpdate = true
78
-                            } else {
79
-                                oldPeers[k].info = res.data.peers[k].info
80
-                            }
81
-                            if (oldPeers[k].info && oldPeers[k].info.hash && !oldPeers[k].password) {
82
-                                let p1 = window.atob(oldPeers[k].info.hash)
83
-                                const pwd = stringToUint8Array(p1)
84
-                                oldPeers[k].password = pwd.toString()
85
-                                oldPeers[k].remember = true
86
-                            }
87
-                        })
88
-                        localStorage.setItem('peers', JSON.stringify(oldPeers))
89
-                        if (needUpdate) {
90
-                            window.location.reload()
91
-                        }
83
+                    if (oldPeers[k].info && oldPeers[k].info.hash && !oldPeers[k].password) {
84
+                        let p1 = window.atob(oldPeers[k].info.hash)
85
+                        const pwd = stringToUint8Array(p1)
86
+                        oldPeers[k].password = pwd.toString()
87
+                        oldPeers[k].remember = true
92 88
                     }
89
+                })
90
+                localStorage.setItem('peers', JSON.stringify(oldPeers))
91
+                if (needUpdate) {
92
+                    window.location.reload()
93 93
                 }
94
-            })
95
-        } else {
96
-            autoWriteServer()
94
+            }
97 95
         }
98
-    }, 1000)
96
+    }).catch(_ => {
97
+        fetching = false
98
+    })
99 99
 }
100
-autoWriteServer()

+ 7 - 0
service/user.go

@@ -262,3 +262,10 @@ func (us *UserService) UserThirdInfo(userId uint, op string) *model.UserThird {
262 262
 	global.DB.Where("user_id = ? and third_type = ?", userId, op).First(ut)
263 263
 	return ut
264 264
 }
265
+
266
+// FindLatestUserIdFromLoginLogByUuid 根据uuid查找最后登录的用户id
267
+func (us *UserService) FindLatestUserIdFromLoginLogByUuid(uuid string) uint {
268
+	llog := &model.LoginLog{}
269
+	global.DB.Where("uuid = ?", uuid).Order("id desc").First(llog)
270
+	return llog.UserId
271
+}