commit b9eb4abed0196e6c41f004ccc4eae543622c3b52 Author: mayiming <1627832236@qq.com> Date: Mon Nov 24 11:18:08 2025 +0800 first commit diff --git a/calculate/cal.go b/calculate/cal.go new file mode 100644 index 0000000..ddbe8a8 --- /dev/null +++ b/calculate/cal.go @@ -0,0 +1,23 @@ +package calculate + +import ( + "context" + "fmt" + + "github.com/go-redis/redis/v8" +) + +var ctx = context.Background() + +func Calculate() { + client := redis.NewClient(&redis.Options{ + Addr: "mayimingperson.com:7000", + Password: "", + DB: 0, + }) + Pong, eror := client.Ping(ctx).Result() + if eror != nil { + fmt.Println("redis连接失败") + } + fmt.Println(Pong) +} diff --git a/calculate/redis.go b/calculate/redis.go new file mode 100644 index 0000000..98e7239 --- /dev/null +++ b/calculate/redis.go @@ -0,0 +1,5 @@ +package calculate + +func Redis() { + +} diff --git a/cluster.conf b/cluster.conf new file mode 100644 index 0000000..1492247 --- /dev/null +++ b/cluster.conf @@ -0,0 +1,10 @@ +[serve] +ip 192.168.1.1 +port 7000 +autoconf true +clusterjoin false +key 4U8xd3BnsP0j +[selfmemory] +ip 127.0.0.1 +port 8080 + diff --git a/feedback/back.go b/feedback/back.go new file mode 100644 index 0000000..c9907ac --- /dev/null +++ b/feedback/back.go @@ -0,0 +1,5 @@ +package feedback + +func Feed() { + +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..df0c539 --- /dev/null +++ b/go.mod @@ -0,0 +1,11 @@ +module balance + +go 1.22.2 + +require ( + github.com/cespare/xxhash/v2 v2.1.2 // indirect + github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect + github.com/go-redis/redis/v8 v8.11.5 // indirect + golang.org/x/crypto v0.31.0 // indirect + golang.org/x/sys v0.28.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..f17c527 --- /dev/null +++ b/go.sum @@ -0,0 +1,10 @@ +github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE= +github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= +github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI= +github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo= +golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U= +golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk= +golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA= +golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= diff --git a/handwork/hand.go b/handwork/hand.go new file mode 100644 index 0000000..cbc341d --- /dev/null +++ b/handwork/hand.go @@ -0,0 +1,113 @@ +package handwork + +import ( + "bufio" + "fmt" + "os" + "strings" +) + +type Conf struct { + Ip string + Port string + Auto bool + Clusterjoin bool + Key string +} + +type MemoryConf struct { + Ip string + Port string +} + +func trueorfalse(i string) bool { + if i == "true" { + return true + } else { + return false + } +} + +func checkConf(i Conf) bool { + if i.Ip == "" || i.Port == "" || i.Key == "" { + return false + } else { + return true + } +} + +var conf Conf +var memoryconf MemoryConf + +func checkMemory(i MemoryConf) bool { + if i.Ip == "" || i.Port == "" { + return false + } else { + return true + } +} + +func Hand() bool { + file, error := os.Open("./cluster.conf") + if error != nil { + fmt.Println("配置文件出错") + } + defer file.Close() + scanner := bufio.NewScanner(file) + + conf.Auto = true + conf.Clusterjoin = false + var currentPart string + for scanner.Scan() { + line := scanner.Text() + if line == "" { + continue + } + if strings.HasPrefix(line, "[") && strings.HasSuffix(line, "]") { + sectionName := line[1 : len(line)-1] + currentPart = sectionName + if sectionName != "serve" && sectionName != "selfmemory" { + panic("参数错误1") + } + continue + } + param := strings.Split(line, " ") + + if len(param) != 2 { + panic("参数错误2") + } + key := param[0] + value := param[1] + switch currentPart { + case "serve": + if key == "ip" { + conf.Ip = value + } else if key == "port" { + conf.Port = value + } else if key == "key" { + conf.Key = value + } else if key == "autoconf" { + conf.Auto = trueorfalse(value) + } else if key == "clusterjoin" { + conf.Clusterjoin = trueorfalse(value) + } + case "selfmemory": + if key == "ip" { + memoryconf.Ip = value + } else if key == "port" { + memoryconf.Port = value + } + } + } + if checkConf(conf) && checkMemory(memoryconf) { + fmt.Println("自检通过") + if conf.Clusterjoin { + go Sendhand(conf.Clusterjoin) + } + return true + } else { + fmt.Println("参数错误") + return false + } + +} diff --git a/handwork/handback.go b/handwork/handback.go new file mode 100644 index 0000000..02cf009 --- /dev/null +++ b/handwork/handback.go @@ -0,0 +1,123 @@ +package handwork + +import ( + "context" + "crypto/rand" + "encoding/base64" + "encoding/hex" + "encoding/json" + "fmt" + "io" + "math" + "net/http" + "time" + + "github.com/go-redis/redis/v8" + "golang.org/x/crypto/argon2" +) + +type HandResponse struct { + Type string `json:"type"` + Key string `json:"key"` + Secret string `json:"secret"` +} + +func GenerateRandomString() string { + randomBytes := make([]byte, 16) + _, err := io.ReadFull(rand.Reader, randomBytes) + if err != nil { + return "" + } + encoded := base64.RawURLEncoding.EncodeToString(randomBytes) + result := encoded[:int(math.Min(float64(len(encoded)), 16))] + return result +} + +func HashPassword(password string) (string, error) { + timeCost := uint32(5) + memoryCost := uint32(64 * 1024) + threads := uint8(8) + keyLength := uint32(48) + salt := make([]byte, 20) + if _, err := io.ReadFull(rand.Reader, salt); err != nil { + return "", err + } + hash := argon2.IDKey([]byte(password), salt, timeCost, memoryCost, threads, keyLength) + hashedHex := hex.EncodeToString(hash) + return fmt.Sprintf("%s:%s", hex.EncodeToString(salt), hashedHex), nil +} + +func Handback(w http.ResponseWriter, r *http.Request) { + fmt.Println("收到介入请求") + + var ctx = context.Background() + client := redis.NewClient(&redis.Options{ + Addr: memoryconf.Ip + memoryconf.Port, + Password: "", + DB: 0, + }) + + _, eror := client.Ping(context.Background()).Result() + if eror != nil { + fmt.Println("redis连接失败") + handresponse := HandResponse{ + Type: "waiting", + Key: conf.Key, + } + + hashjson, err := json.Marshal(handresponse) + if err != nil { + fmt.Println("返回请求失败") + } + w.Header().Set("Content-Type", "application/json") + w.Write(hashjson) + return + } + + handkey := GenerateRandomString() + + error := client.Set(ctx, conf.Key, handkey, 300*time.Second) + if error != nil { + fmt.Println("写入失败") + handresponse := HandResponse{ + Type: "waiting", + Key: conf.Key, + } + hashjson, err := json.Marshal(handresponse) + if err != nil { + fmt.Println("返回请求失败") + } + w.Header().Set("Content-Type", "application/json") + w.Write(hashjson) + return + } + + hashedPassword, err := HashPassword(handkey) + if err != nil { + fmt.Println("加密失败:", err) + handresponse := HandResponse{ + Type: "waiting", + Key: conf.Key, + } + hashjson, err := json.Marshal(handresponse) + if err != nil { + fmt.Println("返回请求失败") + } + w.Header().Set("Content-Type", "application/json") + w.Write(hashjson) + return + } + fmt.Println("加密后的哈希:", hashedPassword) + + handresponse := HandResponse{ + Type: "waiting", + Key: conf.Key, + Secret: handkey, + } + hashjson, err := json.Marshal(handresponse) + if err != nil { + fmt.Println("返回请求失败") + } + w.Header().Set("Content-Type", "application/json") + w.Write(hashjson) +} diff --git a/handwork/send.go b/handwork/send.go new file mode 100644 index 0000000..5199801 --- /dev/null +++ b/handwork/send.go @@ -0,0 +1,44 @@ +package handwork + +import ( + "encoding/json" + "fmt" + "io" + "net/http" + "strings" +) + +func Sendhand(i bool) { + if !i { + return + } + url := conf.Ip + ":" + conf.Port + data := conf.Key + resp, err := http.Post(url, "application/x-www-form-urlencoded", strings.NewReader(data)) + if err != nil { + fmt.Println("错误请求:", err) + return + } + defer resp.Body.Close() + + body, err := io.ReadAll(resp.Body) + if err != nil { + fmt.Println("错误的返回请求:", err) + return + } + + fmt.Println("状态码:", resp.Status) + fmt.Println("返回body:", string(body)) + + var analydata map[string]string + err = json.Unmarshal(body, &analydata) + if err != nil { + fmt.Println("JSON解析错误:", err) + return + } + + if analydata["type"] == "" { + + } + +} diff --git a/heartbeat/beat.go b/heartbeat/beat.go new file mode 100644 index 0000000..a6fc1de --- /dev/null +++ b/heartbeat/beat.go @@ -0,0 +1,9 @@ +package heartbeat + +import ( + "net/http" +) + +func Beat(w http.ResponseWriter, r *http.Request) { + +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..86b919c --- /dev/null +++ b/main.go @@ -0,0 +1,15 @@ +package main + +import ( + "net/http" + + "balance/handwork" +) + +func main() { + if !handwork.Hand() { + return + } + http.HandleFunc("/test", handwork.Handback) + http.ListenAndServe(":8080", nil) +} diff --git a/request/receive.go b/request/receive.go new file mode 100644 index 0000000..64a5e5f --- /dev/null +++ b/request/receive.go @@ -0,0 +1,7 @@ +package request + +import "net/http" + +func Receive(w http.ResponseWriter, r *http.Request) { + +} diff --git a/request/send.go b/request/send.go new file mode 100644 index 0000000..aec6e38 --- /dev/null +++ b/request/send.go @@ -0,0 +1,5 @@ +package request + +func Sendhand(ip string, port string, key string) { + +} diff --git a/store/storein.go b/store/storein.go new file mode 100644 index 0000000..c22f835 --- /dev/null +++ b/store/storein.go @@ -0,0 +1,5 @@ +package store + +func Storein() { + +} diff --git a/verify/getveri.go b/verify/getveri.go new file mode 100644 index 0000000..14c75bb --- /dev/null +++ b/verify/getveri.go @@ -0,0 +1,5 @@ +package verify + +func Getv() { + +}