From ad60c63e2c07c32cece6e1d8dc1479517591a17a Mon Sep 17 00:00:00 2001 From: eson <9673575+githubcontent@user.noreply.gitee.com> Date: Wed, 19 Jul 2023 10:59:13 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=8A=A8=E5=90=AF=E5=8A=A8=E4=BB=A3?= =?UTF-8?q?=E7=90=86=E6=9C=8D=E5=8A=A1=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- proxyserver/get_zero_Info.go | 135 ----------------- proxyserver/main.go | 136 +++++++++++++++++- run_all_server.sh | 19 ++- .../internal/logic/usergoogleloginlogic.go | 2 +- 4 files changed, 153 insertions(+), 139 deletions(-) diff --git a/proxyserver/get_zero_Info.go b/proxyserver/get_zero_Info.go index a90ad753..06ab7d0f 100644 --- a/proxyserver/get_zero_Info.go +++ b/proxyserver/get_zero_Info.go @@ -1,136 +1 @@ package main - -import ( - "io/ioutil" - "log" - "os" - "path/filepath" - "strings" - - "gopkg.in/yaml.v2" -) - -// Config 结构体用于解析yaml配置文件 -type Config struct { - Host string `yaml:"Host"` - Port int `yaml:"Port"` -} - -// Result 结构体用于存储解析结果 -type Result struct { - FolderName string - Host string - Port int - PrefixRoute map[string]bool -} - -// GetZeroInfo 遍历指定目录,并解析相关信息 -func GetZeroInfo(rootDir string) (results []*Result) { - entries, err := ioutil.ReadDir(rootDir) - if err != nil { - log.Fatal(err) - } - - for _, entry := range entries { - // 只处理目录类型 - if entry.IsDir() { - result, err := findFoldersAndExtractInfo(rootDir, entry) - if err != nil { - log.Fatal(err) - } - - results = append(results, result) - } - } - - return -} - -// findFoldersAndExtractInfo 查找目录并提取信息 -func findFoldersAndExtractInfo(rootDir string, entry os.FileInfo) (*Result, error) { - var result *Result - - folderName := entry.Name() - path := filepath.Join(rootDir, folderName) - - err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error { - if err != nil { - return err - } - - // 跳过非目录类型 - if !info.IsDir() { - return nil - } - - relPath, err := filepath.Rel(path, path) - if err != nil { - return err - } - - // 跳过非当前目录的子目录 - if strings.Contains(relPath, string(os.PathSeparator)) { - return filepath.SkipDir - } - - // 读取配置文件 - configPath := filepath.Join(path, "etc", folderName+".yaml") - routesPath := filepath.Join(path, "internal", "handler", "routes.go") - - configContent, err := ioutil.ReadFile(configPath) - if err != nil { - return err - } - - var config Config - err = yaml.Unmarshal(configContent, &config) - if err != nil { - return err - } - - // 读取路由文件 - routesContent, err := ioutil.ReadFile(routesPath) - if err != nil { - return err - } - - PrefixRoute := extractPrefixRouteValues(string(routesContent)) - - // 构建结果 - result = &Result{ - FolderName: folderName, - Host: config.Host, - Port: config.Port, - PrefixRoute: PrefixRoute, - } - - return filepath.SkipDir - }) - - if err != nil { - return nil, err - } - - return result, nil -} - -// extractPrefixRouteValues 提取路由前缀 -func extractPrefixRouteValues(content string) map[string]bool { - lines := strings.Split(content, "\n") - var prefixPath map[string]bool = make(map[string]bool) - - for _, line := range lines { - // 查找包含 "Path:" 的行 - if strings.Contains(line, "Path:") { - path := strings.TrimSpace(strings.TrimPrefix(line, "Path:")) - paths := strings.Split(strings.Trim(path, `"`), "/") - path1 := "/" + paths[1] + "/" + paths[2] - - if _, ok := prefixPath[path1]; !ok { - prefixPath[path1] = true - } - } - } - - return prefixPath -} diff --git a/proxyserver/main.go b/proxyserver/main.go index 1ac84415..066a16d1 100644 --- a/proxyserver/main.go +++ b/proxyserver/main.go @@ -3,14 +3,19 @@ package main import ( "fmt" "io" + "io/ioutil" "log" "net" "net/http" "net/http/httputil" "net/url" + "os" + "path/filepath" "strings" "sync" "time" + + "gopkg.in/yaml.v2" ) // Backend结构体 @@ -125,8 +130,8 @@ func NewBackend(mux *http.ServeMux, httpAddress string, muxPaths ...string) *Bac client := &http.Client{ Transport: &http.Transport{ DialContext: (&net.Dialer{ - Timeout: 30 * time.Second, - KeepAlive: 30 * time.Second, + Timeout: 60 * time.Second, + KeepAlive: 60 * time.Second, }).DialContext, ForceAttemptHTTP2: true, MaxIdleConns: 100, @@ -202,3 +207,130 @@ func NewBackend(mux *http.ServeMux, httpAddress string, muxPaths ...string) *Bac // 返回后端服务对象 return backend } + +// get_zero_info.go + +// Config 结构体用于解析yaml配置文件 +type Config struct { + Host string `yaml:"Host"` + Port int `yaml:"Port"` +} + +// Result 结构体用于存储解析结果 +type Result struct { + FolderName string + Host string + Port int + PrefixRoute map[string]bool +} + +// GetZeroInfo 遍历指定目录,并解析相关信息 +func GetZeroInfo(rootDir string) (results []*Result) { + entries, err := ioutil.ReadDir(rootDir) + if err != nil { + log.Fatal(err) + } + + for _, entry := range entries { + // 只处理目录类型 + if entry.IsDir() { + result, err := findFoldersAndExtractInfo(rootDir, entry) + if err != nil { + log.Fatal(err) + } + + results = append(results, result) + } + } + + return +} + +// findFoldersAndExtractInfo 查找目录并提取信息 +func findFoldersAndExtractInfo(rootDir string, entry os.FileInfo) (*Result, error) { + var result *Result + + folderName := entry.Name() + path := filepath.Join(rootDir, folderName) + + err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + + // 跳过非目录类型 + if !info.IsDir() { + return nil + } + + relPath, err := filepath.Rel(path, path) + if err != nil { + return err + } + + // 跳过非当前目录的子目录 + if strings.Contains(relPath, string(os.PathSeparator)) { + return filepath.SkipDir + } + + // 读取配置文件 + configPath := filepath.Join(path, "etc", folderName+".yaml") + routesPath := filepath.Join(path, "internal", "handler", "routes.go") + + configContent, err := ioutil.ReadFile(configPath) + if err != nil { + return err + } + + var config Config + err = yaml.Unmarshal(configContent, &config) + if err != nil { + return err + } + + // 读取路由文件 + routesContent, err := ioutil.ReadFile(routesPath) + if err != nil { + return err + } + + PrefixRoute := extractPrefixRouteValues(string(routesContent)) + + // 构建结果 + result = &Result{ + FolderName: folderName, + Host: config.Host, + Port: config.Port, + PrefixRoute: PrefixRoute, + } + + return filepath.SkipDir + }) + + if err != nil { + return nil, err + } + + return result, nil +} + +// extractPrefixRouteValues 提取路由前缀 +func extractPrefixRouteValues(content string) map[string]bool { + lines := strings.Split(content, "\n") + var prefixPath map[string]bool = make(map[string]bool) + + for _, line := range lines { + // 查找包含 "Path:" 的行 + if strings.Contains(line, "Path:") { + path := strings.TrimSpace(strings.TrimPrefix(line, "Path:")) + paths := strings.Split(strings.Trim(path, `"`), "/") + path1 := "/" + paths[1] + "/" + paths[2] + + if _, ok := prefixPath[path1]; !ok { + prefixPath[path1] = true + } + } + } + + return prefixPath +} diff --git a/run_all_server.sh b/run_all_server.sh index c9579729..cd46e711 100755 --- a/run_all_server.sh +++ b/run_all_server.sh @@ -28,4 +28,21 @@ server_dirs=("backend" "canteen" "data-transfer" "home-user-auth" "inventory" "m # 在每个服务器目录下运行相应的 go 程序 for server_dir in "${server_dirs[@]}"; do run_server $server_dir -done \ No newline at end of file +done + + +# 定义目录和screen名称 +dir_path="./proxyserver" +screen_name="proxyserver" + +# 进入目录 +cd $dir_path + +# 检查是否存在screen session +if screen -list | grep -q "$screen_name"; then + # 结束存在的screen session + screen -S $screen_name -X quit +fi + +# 启动新的screen session并运行go程序 +screen -dmS $screen_name -L go run main.go \ No newline at end of file diff --git a/server/home-user-auth/internal/logic/usergoogleloginlogic.go b/server/home-user-auth/internal/logic/usergoogleloginlogic.go index 68205df8..12fbf720 100644 --- a/server/home-user-auth/internal/logic/usergoogleloginlogic.go +++ b/server/home-user-auth/internal/logic/usergoogleloginlogic.go @@ -63,7 +63,7 @@ func (l *UserGoogleLoginLogic) UserGoogleLogin(req *types.RequestGoogleLogin, us resp.SetStatus(basic.CodeApiErr) } ses := requests.NewSession() - ses.Config().SetProxy("socks5://127.0.0.1:1080") + ses.Config().SetProxy("socks5://127.0.0.1:1080") // 代理 为了测试功能 r, err := ses.Get("https://www.googleapis.com/oauth2/v2/userinfo?access_token=" + token.AccessToken).Execute() if err != nil {