自动启动代理服务器

This commit is contained in:
eson 2023-07-19 10:59:13 +08:00
parent e3373bde65
commit ad60c63e2c
4 changed files with 153 additions and 139 deletions

View File

@ -1,136 +1 @@
package main 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
}

View File

@ -3,14 +3,19 @@ package main
import ( import (
"fmt" "fmt"
"io" "io"
"io/ioutil"
"log" "log"
"net" "net"
"net/http" "net/http"
"net/http/httputil" "net/http/httputil"
"net/url" "net/url"
"os"
"path/filepath"
"strings" "strings"
"sync" "sync"
"time" "time"
"gopkg.in/yaml.v2"
) )
// Backend结构体 // Backend结构体
@ -125,8 +130,8 @@ func NewBackend(mux *http.ServeMux, httpAddress string, muxPaths ...string) *Bac
client := &http.Client{ client := &http.Client{
Transport: &http.Transport{ Transport: &http.Transport{
DialContext: (&net.Dialer{ DialContext: (&net.Dialer{
Timeout: 30 * time.Second, Timeout: 60 * time.Second,
KeepAlive: 30 * time.Second, KeepAlive: 60 * time.Second,
}).DialContext, }).DialContext,
ForceAttemptHTTP2: true, ForceAttemptHTTP2: true,
MaxIdleConns: 100, MaxIdleConns: 100,
@ -202,3 +207,130 @@ func NewBackend(mux *http.ServeMux, httpAddress string, muxPaths ...string) *Bac
// 返回后端服务对象 // 返回后端服务对象
return backend 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
}

View File

@ -28,4 +28,21 @@ server_dirs=("backend" "canteen" "data-transfer" "home-user-auth" "inventory" "m
# 在每个服务器目录下运行相应的 go 程序 # 在每个服务器目录下运行相应的 go 程序
for server_dir in "${server_dirs[@]}"; do for server_dir in "${server_dirs[@]}"; do
run_server $server_dir run_server $server_dir
done 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

View File

@ -63,7 +63,7 @@ func (l *UserGoogleLoginLogic) UserGoogleLogin(req *types.RequestGoogleLogin, us
resp.SetStatus(basic.CodeApiErr) resp.SetStatus(basic.CodeApiErr)
} }
ses := requests.NewSession() 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() r, err := ses.Get("https://www.googleapis.com/oauth2/v2/userinfo?access_token=" + token.AccessToken).Execute()
if err != nil { if err != nil {