redis 优化

This commit is contained in:
dong 2024-12-27 19:41:54 +08:00
parent 4a2c9ae7de
commit 369d1bc40f
13 changed files with 144 additions and 202 deletions

14
.gitignore vendored Normal file
View File

@ -0,0 +1,14 @@
/.idea/
/.idea_modules/
/.gradle/
/build/
# Logs
logs/
*.log
# Caches
*.exe
# folder
Run

View File

@ -1,9 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="Go" enabled="true" />
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

8
.idea/modules.xml generated
View File

@ -1,8 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/1.Internal Network Penetration.iml" filepath="$PROJECT_DIR$/.idea/1.Internal Network Penetration.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml generated
View File

@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

103
.idea/workspace.xml generated
View File

@ -1,103 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="AutoImportSettings">
<option name="autoReloadType" value="ALL" />
</component>
<component name="ChangeListManager">
<list default="true" id="853e96c5-6f88-46d9-b557-af403ec290fd" name="更改" comment="">
<change beforePath="$PROJECT_DIR$/.idea/.gitignore" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/.name" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/Go_Test.iml" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/modules.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/modules.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/vcs.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/vcs.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.net/Go_Frp/.vs/Go_Frp/FileContentIndex/244691fa-b957-47e6-82c7-f092bb34325c.vsidx" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/.net/Go_Frp/.vs/Go_Frp/FileContentIndex/2a69d820-eeaf-4408-afd7-97e321cda376.vsidx" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/.net/Go_Frp/.vs/Go_Frp/FileContentIndex/f1d93a69-0702-4228-8470-35fc9463cecb.vsidx" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/.net/Go_Frp/.vs/Go_Frp/v17/.suo" beforeDir="false" afterPath="$PROJECT_DIR$/.net/Go_Frp/.vs/Go_Frp/v17/.suo" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.net/Go_Frp/.vs/Go_Frp/v17/DocumentLayout.backup.json" beforeDir="false" afterPath="$PROJECT_DIR$/.net/Go_Frp/.vs/Go_Frp/v17/DocumentLayout.backup.json" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.net/Go_Frp/.vs/Go_Frp/v17/DocumentLayout.json" beforeDir="false" afterPath="$PROJECT_DIR$/.net/Go_Frp/.vs/Go_Frp/v17/DocumentLayout.json" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.net/Go_Frp/Frpc/frpc.log" beforeDir="false" afterPath="$PROJECT_DIR$/.net/Go_Frp/Frpc/frpc.log" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.net/Go_Frp/Go_Frp.Designer.cs" beforeDir="false" afterPath="$PROJECT_DIR$/.net/Go_Frp/Go_Frp.Designer.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.net/Go_Frp/Go_Frp.cs" beforeDir="false" afterPath="$PROJECT_DIR$/.net/Go_Frp/Go_Frp.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.net/Go_Frp/Go_Frp.csproj" beforeDir="false" afterPath="$PROJECT_DIR$/.net/Go_Frp/Go_Frp.csproj" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.net/Go_Frp/bin/Debug/Go_Frp.exe" beforeDir="false" afterPath="$PROJECT_DIR$/.net/Go_Frp/bin/Debug/Go_Frp.exe" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.net/Go_Frp/bin/Debug/Go_Frp.pdb" beforeDir="false" afterPath="$PROJECT_DIR$/.net/Go_Frp/bin/Debug/Go_Frp.pdb" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.net/Go_Frp/obj/Debug/DesignTimeResolveAssemblyReferences.cache" beforeDir="false" afterPath="$PROJECT_DIR$/.net/Go_Frp/obj/Debug/DesignTimeResolveAssemblyReferences.cache" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.net/Go_Frp/obj/Debug/Go_Frp.csproj.CoreCompileInputs.cache" beforeDir="false" afterPath="$PROJECT_DIR$/.net/Go_Frp/obj/Debug/Go_Frp.csproj.CoreCompileInputs.cache" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.net/Go_Frp/obj/Debug/Go_Frp.csproj.GenerateResource.cache" beforeDir="false" afterPath="$PROJECT_DIR$/.net/Go_Frp/obj/Debug/Go_Frp.csproj.GenerateResource.cache" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.net/Go_Frp/obj/Debug/Go_Frp.exe" beforeDir="false" afterPath="$PROJECT_DIR$/.net/Go_Frp/obj/Debug/Go_Frp.exe" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.net/Go_Frp/obj/Debug/Go_Frp.pdb" beforeDir="false" afterPath="$PROJECT_DIR$/.net/Go_Frp/obj/Debug/Go_Frp.pdb" afterDir="false" />
<change beforePath="$PROJECT_DIR$/connect.go" beforeDir="false" afterPath="$PROJECT_DIR$/connect.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/main.go" beforeDir="false" afterPath="$PROJECT_DIR$/main.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/users.go" beforeDir="false" afterPath="$PROJECT_DIR$/users.go" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="GOROOT" url="file://C:/Program Files/Go" />
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="ProjectColorInfo"><![CDATA[{
"associatedIndex": 4
}]]></component>
<component name="ProjectId" id="2qekXMUsC8leqWD5Flicp42jxRs" />
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent"><![CDATA[{
"keyToString": {
"Go 构建.Server.executor": "Run",
"Go 构建.go build Go_Test.executor": "Run",
"Go 构建.go build main.go.executor": "Run",
"RunOnceActivity.ShowReadmeOnStart": "true",
"RunOnceActivity.git.unshallow": "true",
"RunOnceActivity.go.formatter.settings.were.checked": "true",
"RunOnceActivity.go.migrated.go.modules.settings": "true",
"RunOnceActivity.go.modules.go.list.on.any.changes.was.set": "true",
"git-widget-placeholder": "main",
"go.import.settings.migrated": "true",
"go.sdk.automatically.set": "true",
"last_opened_file_path": "D:/File/Library/1.Internal Network Penetration/Run",
"node.js.detected.package.eslint": "true",
"node.js.selected.package.eslint": "(autodetect)",
"nodejs_package_manager_path": "npm",
"settings.editor.selected.configurable": "preferences.language.and.region"
}
}]]></component>
<component name="RunManager">
<configuration name="Server" type="GoApplicationRunConfiguration" factoryName="Go Application" temporary="true">
<module name="1.Internal Network Penetration" />
<working_directory value="$PROJECT_DIR$/Run" />
<kind value="DIRECTORY" />
<package value="Go_Test" />
<directory value="$PROJECT_DIR$" />
<filePath value="$PROJECT_DIR$/main.go" />
<output_directory value="$PROJECT_DIR$/Run" />
<method v="2" />
</configuration>
<recent_temporary>
<list>
<item itemvalue="Go 构建.Server" />
</list>
</recent_temporary>
</component>
<component name="SharedIndexes">
<attachedChunks>
<set>
<option value="bundled-gosdk-d297c17c1fbd-d82d926f19eb-org.jetbrains.plugins.go.sharedIndexes.bundled-GO-243.22562.186" />
<option value="bundled-js-predefined-d6986cc7102b-deb605915726-JavaScript-GO-243.22562.186" />
</set>
</attachedChunks>
</component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="应用程序级" UseSingleDictionary="true" transferred="true" />
<component name="TypeScriptGeneratedFilesManager">
<option name="version" value="3" />
</component>
<component name="VgoProject">
<settings-migrated>true</settings-migrated>
</component>
</project>

15
.vscode/launch.json vendored
View File

@ -1,15 +0,0 @@
{
// 使 IntelliSense
//
// 访: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}"
}
]
}

View File

@ -1,3 +0,0 @@
{
"dotnet.preferCSharpExtension": true
}

35
Dockerfile Normal file
View File

@ -0,0 +1,35 @@
# 使用官方Go镜像作为构建环境
FROM golang:1.23-alpine AS builder
# 设置工作目录
WORKDIR /app
# 复制go mod和sum文件
COPY go.mod go.sum ./
# 下载依赖
RUN go mod download
# 复制源代码
COPY . .
# 构建应用
RUN CGO_ENABLED=0 GOOS=linux go build -o main .
# 使用轻量级的alpine作为运行环境
FROM alpine:latest
# 安装必要的CA证书
RUN apk --no-cache add ca-certificates
WORKDIR /root/
# 从builder阶段复制编译好的应用
COPY --from=builder /app/main .
COPY .env .
# 暴露应用端口(根据实际需要修改)
EXPOSE 8080
# 运行应用
CMD ["./main"]

49
config/redis.go Normal file
View File

@ -0,0 +1,49 @@
package config
import (
"fmt"
"os"
"github.com/redis/go-redis/v9"
)
// Redis配置结构
type RedisConfig struct {
Host string
Port string
Password string
}
// 获取Redis配置
func GetRedisConfig() *RedisConfig {
config := &RedisConfig{
Host: os.Getenv("REDIS_HOST"),
Port: os.Getenv("REDIS_PORT"),
Password: os.Getenv("REDIS_PASSWORD"),
}
// 设置默认值
if config.Host == "" {
config.Host = "localhost"
}
if config.Port == "" {
config.Port = "6379"
}
if config.Password == "" {
config.Password = ""
}
return config
}
// 创建Redis客户端
func NewRedisClient(db int) (*redis.Client, error) {
config := GetRedisConfig()
client := redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%s", config.Host, config.Port),
Password: config.Password,
DB: db,
})
return client, nil
}

19
docker-compose.yml Normal file
View File

@ -0,0 +1,19 @@
version: '3.8'
services:
app:
build: .
ports:
- "8080:8080"
environment:
- REDIS_HOST=localhost
- REDIS_PORT=6379
- REDIS_PASSWORD=*f2iPMa#N42!M
extra_hosts:
- "host.docker.internal:host-gateway"
networks:
- app-network
networks:
app-network:
driver: bridge

10
main.go
View File

@ -3,6 +3,8 @@ package main
import (
"Go_Test/config"
"context"
"fmt"
"github.com/redis/go-redis/v9"
)
@ -25,12 +27,14 @@ var (
)
func main() {
// 初始化数据库
err := InitRedis()
var err error
// 初始化Redis客户端
RedisClient, err = config.NewRedisClient(0)
if err != nil {
fmt.Printf("Failed to initialize Redis: %v\n", err)
return
}
defer RedisClient.Close()
Connect()
}

View File

@ -2,36 +2,16 @@ package main
import (
"fmt"
"Go_Test/config"
"github.com/redis/go-redis/v9"
)
// InitRedis 初始化Redis连接
func InitRedis() error {
// 创建Redis客户端连接
RedisClient = redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "123456",
DB: 0,
})
/*
// 测试连接
_, err := RedisClient.Ping(ctx).Result()
if err != nil {
return fmt.Errorf("redis连接失败: %v", err)
}
fmt.Println("redis连接成功")
*/
// 获取所有 Frp 服务器信息
GetAllFrpServerInfo()
GetFrpServerInfo()
// 显示所有服务器信息
//ShowServerInfo()
var err error
RedisClient, err = config.NewRedisClient(0)
if err != nil {
return fmt.Errorf("failed to initialize Redis: %v", err)
}
return nil
}
@ -105,18 +85,3 @@ func GetAllFrpServerInfo() {
})
}
}
/*
// 显示服务器信息
func ShowServerInfo() {
// 获取当前时间
currentTime := time.Now().Format("2006-01-02 15:04:05")
fmt.Println("当前时间:", currentTime)
// 打印服务器信息
for i := 1; i <= GetFrpServerNum(); i++ {
fmt.Printf("Frp %d Server Info: \n", i)
fmt.Printf("Host: %s\nPort: %s\nToken: %s\n", ServerConfig[i-1].Host, ServerConfig[i-1].Port, ServerConfig[i-1].Token)
}
}
*/

View File

@ -3,27 +3,27 @@ package main
import (
"Go_Test/config"
"fmt"
"github.com/redis/go-redis/v9"
"time"
)
// ConnectRedis 连接Redis
func ConnectRedis() error {
var err error
RedisClient, err = config.NewRedisClient(1)
if err != nil {
return fmt.Errorf("failed to connect to Redis: %v", err)
}
return nil
}
// InitUser 初始化用户连接
func InitUser(username string, password string) error {
// 创建Redis客户端连接
RedisClient = redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "123456",
DB: 1,
})
/*
// 测试连接
_, err := RedisClient.Ping(ctx).Result()
if err != nil {
return fmt.Errorf("redis连接失败: %v", err)
}
fmt.Println("redis连接成功")
*/
// 连接Redis
err := ConnectRedis()
if err != nil {
return err
}
SaveUserToRedis(GetUsernameAndPassword(username, password))