first commit
This commit is contained in:
23
calculate/cal.go
Normal file
23
calculate/cal.go
Normal file
@@ -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)
|
||||
}
|
||||
5
calculate/redis.go
Normal file
5
calculate/redis.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package calculate
|
||||
|
||||
func Redis() {
|
||||
|
||||
}
|
||||
10
cluster.conf
Normal file
10
cluster.conf
Normal file
@@ -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
|
||||
|
||||
5
feedback/back.go
Normal file
5
feedback/back.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package feedback
|
||||
|
||||
func Feed() {
|
||||
|
||||
}
|
||||
11
go.mod
Normal file
11
go.mod
Normal file
@@ -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
|
||||
)
|
||||
10
go.sum
Normal file
10
go.sum
Normal file
@@ -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=
|
||||
113
handwork/hand.go
Normal file
113
handwork/hand.go
Normal file
@@ -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
|
||||
}
|
||||
|
||||
}
|
||||
123
handwork/handback.go
Normal file
123
handwork/handback.go
Normal file
@@ -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)
|
||||
}
|
||||
44
handwork/send.go
Normal file
44
handwork/send.go
Normal file
@@ -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"] == "" {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
9
heartbeat/beat.go
Normal file
9
heartbeat/beat.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package heartbeat
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func Beat(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
}
|
||||
15
main.go
Normal file
15
main.go
Normal file
@@ -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)
|
||||
}
|
||||
7
request/receive.go
Normal file
7
request/receive.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package request
|
||||
|
||||
import "net/http"
|
||||
|
||||
func Receive(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
}
|
||||
5
request/send.go
Normal file
5
request/send.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package request
|
||||
|
||||
func Sendhand(ip string, port string, key string) {
|
||||
|
||||
}
|
||||
5
store/storein.go
Normal file
5
store/storein.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package store
|
||||
|
||||
func Storein() {
|
||||
|
||||
}
|
||||
5
verify/getveri.go
Normal file
5
verify/getveri.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package verify
|
||||
|
||||
func Getv() {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user