first commit
This commit is contained in:
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"] == "" {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user