Skip to content

Commit

Permalink
Update:增加minichat可配置匿名访问
Browse files Browse the repository at this point in the history
  • Loading branch information
hanc00l committed Jul 19, 2024
1 parent 0869c22 commit 3775e91
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 21 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
# ChangeLog
## v2.13.1

2024-7-19

### Update

- 增加ElasticSearch数据同步和查询(测试中)
- 更新Minichat前端UI;可配置参数是否允许匿名使用。
- 更新Httpx至1.6.6。

## v2.13.0

2024-7-2
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
- ElasticSearch同步与查询(测试中)
<img src="docs/image/es-1.png" />
<img src="docs/image/es-2.png" />
-

## 演示页面

<img src="docs/demo.gif" />
Expand Down
19 changes: 17 additions & 2 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
_ "github.com/hanc00l/nemo_go/pkg/web/routers"
"net/http"
"path/filepath"
"strings"
"time"
)

Expand All @@ -30,7 +31,11 @@ type ServerOption struct {
TLSKeyFile string
}

var UrlFilterWhiteList = []string{"/"}
var UrlFilterWhiteList = []string{}

var MinichatUrlFilterWhiteList = []string{
"/minichat", "/message/precheck", "/message/ws", "/api/upload",
}

func parseServerOption() *ServerOption {
option := &ServerOption{}
Expand Down Expand Up @@ -91,11 +96,21 @@ func StartWebServer(option *ServerOption) {

// filterLoginCheck 全局的登录验证
func filterLoginCheck(ctx *beegoContext.Context) {
if ctx.Request.RequestURI == "/" {
return
}
for _, url := range UrlFilterWhiteList {
if ctx.Request.RequestURI == url {
if strings.HasPrefix(ctx.Request.RequestURI, url) {
return
}
}
if minichatConfig.EnableAnonymous {
for _, url := range MinichatUrlFilterWhiteList {
if strings.HasPrefix(ctx.Request.RequestURI, url) {
return
}
}
}
// 检查用户是否登录(检查登录成功后的session:User、UserRole、Workspace
if user, ok := ctx.Input.Session("User").(string); !ok || len(user) == 0 {
ctx.Redirect(http.StatusFound, "/")
Expand Down
36 changes: 35 additions & 1 deletion docs/question.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,38 @@ screen -r
- Nmap在扫描IPv6地址时,需要在地址前加上前缀,比如:nmap -6 2001:db8:0:1::/64;如果不加前缀,nmap会自动将地址转换为IPv4地址进行扫描。massscan可自动识别并同时对ipv4/ipv6进行扫描。
- 由于IPv6地址的长度,导致在扫描时,扫描速度会比IPv4慢很多,因此建议在扫描时,将IPv6地址单独扫描,或者将扫描的端口范围缩小, 同时Nemo默认会丢弃掩码低于/104的IPv6地址,因此建议在扫描时,将掩码设置为/104以上。
- Nemo对域名任务,会自动识别域名对应的IPv6地址。对于IP任务,如果需要扫描IPv6地址,需要在IP任务中添加IPv6地址,或者在IP任务中添加域名,Nemo会自动识别域名对应的IPv6地址。
- 最最最重要的一点:Worker必须支持IPv6地址,否则无法正常工作。请用ifconfig/ipconfig查看ip时,确认启用了ipv6并且地址并且不是fe80开头(fe80::/10为本地链路地址,用于单一链路,适用于自动配置、邻机发现等,路由器不转发,类似ipv4的169.254/地址)。
- 最最最重要的一点:Worker必须支持IPv6地址,否则无法正常工作。请用ifconfig/ipconfig查看ip时,确认启用了ipv6并且地址并且不是fe80开头(fe80::/10为本地链路地址,用于单一链路,适用于自动配置、邻机发现等,路由器不转发,类似ipv4的169.254/地址)。

### 11、ElasticSearch功能(测试中)

从v2.13.1开始,增加了数据同步到ElasticSearch的功能。

- ElasticSearch服务需单独安装;将URL及用户密码,在server.yml中进行配置(以下为配置样例,请根据实际修改)

```yaml
elastic:
url: https://127.0.0.1:9200
username: elastic
password: xxxxxxx
```
- 编译cmd/estools/main.go,生成estools可执行文件,放到server目录下。
- 启动server后,将server中的数据同步到ElasticSearch中。命令参数-i -w为要同步的workspace的GUID
```shell
-c create index
-d delete index
-f string
import by json file path
-i import data to es
-w string
import index name by workspace guid
Usage:
estools -i -w workspace_guid import data from nemo database to elasticsearch
estools -c -w workspace_guid create index
estools -d -w workspace_guid delete index
estools -i -w workspace_guid -f json_file_path import data from json file to elasticsearch
```
- Nemo的任务执行结果会自动同步到ElasticSearch中。
- 在EsSearch页面,可通过语法查询ElasticSearch中的数据,支持的语法请参考:
<img src="image/es-2.png" />
1 change: 1 addition & 0 deletions pkg/minichat/config/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var IsNotDelFileDir = false
var ChatPath = ""
var LoadHistory = true
var MaxHistoryMessage = 1000
var EnableAnonymous = false

var RsaPrivateKey = ""
var RsaPublicKey = ""
Expand Down
6 changes: 5 additions & 1 deletion pkg/web/controllers/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ type DefaultConfig struct {
MaxDomainPerIP int `json:"maxdomainperip" form:"maxdomainperip"`
TitleFilter string `json:"title" form:"title"`
// minichat
Anonymous bool `json:"anonymous" form:"anonymous"`
IsNotDelFileDir bool `json:"notdelfiledir" form:"notdelfiledir"`
LoadHistory bool `json:"loadhistory" form:"loadhistory"`
MaxHistoryMessage int `json:"maxhistorymessage" form:"maxhistorymessage"`
Expand Down Expand Up @@ -259,6 +260,7 @@ func (c *ConfigController) LoadServerConfigAction() {
FeishuAppSecret: feishu.AppSecret,
FeishuRefreshToken: feishu.UserAccessRefreshToken,
//
Anonymous: minichatConfig.EnableAnonymous,
IsNotDelFileDir: minichatConfig.IsNotDelFileDir,
LoadHistory: minichatConfig.LoadHistory,
MaxHistoryMessage: minichatConfig.MaxHistoryMessage,
Expand Down Expand Up @@ -798,13 +800,15 @@ func (c *ConfigController) UpdateMinichatConfigAction() {
c.FailedStatus("当前用户权限不允许!")
return
}
anonymous, err0 := c.GetBool("anonymous", false)
notDeleteDir, err1 := c.GetBool("notdelfiledir", false)
loadHistory, err2 := c.GetBool("loadhistory", true)
maxHistoryMessage, err3 := c.GetInt("maxhistorymessage", 1000)
if err1 != nil || err2 != nil || err3 != nil {
if err0 != nil || err1 != nil || err2 != nil || err3 != nil {
c.FailedStatus("参数错误")
return
}
minichatConfig.EnableAnonymous = anonymous
minichatConfig.IsNotDelFileDir = notDeleteDir
minichatConfig.LoadHistory = loadHistory
minichatConfig.MaxHistoryMessage = maxHistoryMessage
Expand Down
36 changes: 25 additions & 11 deletions pkg/web/controllers/minichat.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package controllers

import (
"github.com/google/uuid"
minichatConfig "github.com/hanc00l/nemo_go/pkg/minichat/config"
"github.com/hanc00l/nemo_go/pkg/minichat/server"
)

Expand All @@ -13,23 +14,36 @@ var GlobalRoomNumber = make(map[string]string)

// IndexAction 显示列表页面
func (c *MiniChatController) IndexAction() {
// 根据当前工作区映射到房间号
roomNumber := c.GetString("room", "")
// 强制检查房间号是否是由服务端生成并对应
if roomNumber != "" {
if !checkRoomNumber(roomNumber) {
c.Abort("404")
if minichatConfig.EnableAnonymous {
//允许匿名访问,但房间号只能uuid生成提高安全性
if roomNumber == "" {
roomNumber = uuid.New().String()
GlobalRoomNumber[roomNumber] = roomNumber
} else {
if !checkRoomNumber(roomNumber) {
c.Abort("404")
}
}
c.Data["roomNumber"] = roomNumber
} else {
workspaceGUID := c.GetCurrentWorkspaceGUID()
if workspaceGUID != "" {
if _, ok := GlobalRoomNumber[workspaceGUID]; !ok {
GlobalRoomNumber[workspaceGUID] = uuid.New().String()
// 根据当前工作区映射到房间号
// 强制检查房间号是否是由服务端生成并对应
if roomNumber != "" {
if !checkRoomNumber(roomNumber) {
c.Abort("404")
}
c.Data["roomNumber"] = GlobalRoomNumber[workspaceGUID]
c.Data["roomNumber"] = roomNumber
} else {
c.Data["roomNumber"] = ""
workspaceGUID := c.GetCurrentWorkspaceGUID()
if workspaceGUID != "" {
if _, ok := GlobalRoomNumber[workspaceGUID]; !ok {
GlobalRoomNumber[workspaceGUID] = uuid.New().String()
}
c.Data["roomNumber"] = GlobalRoomNumber[workspaceGUID]
} else {
c.Data["roomNumber"] = ""
}
}
}
c.TplName = "minichat-index.html"
Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.13.0
2.13.1
2 changes: 2 additions & 0 deletions web/static/js/server/config-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ $(function () {
$("#buttonUpdateMinichatConfig").click(function () {
$.post("/config-update-minichat",
{
"anonymous": $('#checkbox_anonymous').is(":checked"),
"notdelfiledir": $('#checkbox_notdelfiledir').is(":checked"),
"loadhistory": $('#checkbox_loadhistory').is(":checked"),
"maxhistorymessage": $('#input_maxhistorymessage').val(),
Expand Down Expand Up @@ -208,6 +209,7 @@ function load_config_server() {
$('#input_feishu_secret').val(data['feishusecret']);
$('#input_feishu_refreshtoken').val(data['feishurefreshtoken']);

$('#checkbox_anonymous').prop("checked", data['anonymous']);
$('#checkbox_notdelfiledir').prop("checked", data['notdelfiledir']);
$('#checkbox_loadhistory').prop("checked", data['loadhistory']);
$('#input_maxhistorymessage').val(data['maxhistorymessage']);
Expand Down
16 changes: 12 additions & 4 deletions web/views/config-server.html
Original file line number Diff line number Diff line change
Expand Up @@ -184,23 +184,26 @@ <h3 class="tile-title">知识库:飞书自建应用设置</h3>
<label class="col-form-label" for="input_feishu_appid">
<b>AppId</b>
</label>
<input class="form-control" id="input_feishu_appid" type="text" value="" placeholder="飞书自建应用的appId">
<input class="form-control" id="input_feishu_appid" type="text" value=""
placeholder="飞书自建应用的appId">
<label class="col-form-label" for="input_feishu_secret">
<b>AppSecret</b>
</label>
<input class="form-control" id="input_feishu_secret" type="text" value="" placeholder="飞书自建应用的appSecret">
<input class="form-control" id="input_feishu_secret" type="text" value=""
placeholder="飞书自建应用的appSecret">
<label class="col-form-label" for="input_feishu_refreshtoken">
<b>用户访问RefreshToken</b>
</label>
<input class="form-control" id="input_feishu_refreshtoken" type="text" value="" placeholder="飞书自建应用的用户访问权限刷新Token(通过该Token获取有效的用户访问权限Token)">
<input class="form-control" id="input_feishu_refreshtoken" type="text" value=""
placeholder="飞书自建应用的用户访问权限刷新Token(通过该Token获取有效的用户访问权限Token)">
</div>
</form>
</div>
<div class="tile-footer">
<button class="btn btn-primary" type="button" id="buttonSaveWikiFeishu"><i
class="fa fa-fw fa-lg fa-check-circle"></i>保存设置
</button>&nbsp;&nbsp;&nbsp;
<button class="btn btn-primary" type="button" id="buttonRefreshToken" ><i
<button class="btn btn-primary" type="button" id="buttonRefreshToken"><i
class="fa fa-fw fa-lg fa-refresh"></i>刷新用户访问Token
</button>
<button class="btn btn-primary" type="button" id="buttonOpenWikiFeishu" data-toggle="modal"
Expand All @@ -217,6 +220,11 @@ <h3 class="tile-title">Minichat设置</h3>
<div class="form-group">
<div class="form-check form-check-inline">
<div class="form-check form-check-inline">
<div class="form-check form-check-inline">
<label class="form-check-label" for="checkbox_anonymous">
<input class="form-check-input" id="checkbox_anonymous" type="checkbox">允许未登录进入聊天房间
</label>
</div>
<label class="form-check-label" for="checkbox_notdelfiledir">
<input class="form-check-input" id="checkbox_notdelfiledir" type="checkbox">聊天房间关闭后保留文件和图片
</label>
Expand Down

0 comments on commit 3775e91

Please sign in to comment.