131 lines
3.1 KiB
Go
131 lines
3.1 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"shttp"
|
|
"time"
|
|
)
|
|
|
|
func main() {
|
|
rk := &shttp.RSA{}
|
|
|
|
home, err := os.UserHomeDir()
|
|
if err != nil {
|
|
Fatal(err)
|
|
}
|
|
path := filepath.Join(home, ".ssh", "id_rsa")
|
|
file, err := os.ReadFile(path)
|
|
if err != nil {
|
|
Fatal(err)
|
|
}
|
|
pub := string(file)
|
|
if err := rk.LoadPrivate(pub); err != nil {
|
|
Fatal(err)
|
|
}
|
|
|
|
err = rk.LoadPublic("ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDO1y6CLE3ZoPEy24ird1izypdVN0VEIcQiVeqZY3IOe2rQWzMG3+UJzmPj2wKFoSDxcVQxuuGe2MMX4xFCl1ChCEt85xfGrUWWAGX2IV83+UMtTS5dXLcSATs5NBhFQLgDSGBCDioSxkzBayz4IUkRihRPhd/rDSnlGITBbO6+dAtCdqIfLg/Ai7fvE2PMOcxZO7OLcZYHVkYbUn/n/DuIQbqAi2XTLWf9Z3zpKnBkqIa1RZVnQpkEO5IDj2F1X6edbDf8ScdsQwYfpBYwpQJfjLWJnRqhKy1iO9VpGoPPipiL9fJA7dA1LWlzQadaMT0/7GngFn/eFdyGvxR4zIgo4IZBzvHTjcFgaCkeVe3TOQXCVp7LwAxZc+OBzqEsuF5sjak+NE8zRjonzi3kItaG2Ldd8PonOokR45D0Vmm26fTSCPAz8exix7xm6RxZ11Y5RHrFTblQ/U/T71dxCwOgVbWbnpddm5O6Uzc5wk+BvPyscgbjJgybiXQ+kKvmiMk= AzureAD+YiFanChen@LAPTOP-SCQSNETG")
|
|
if err != nil {
|
|
Fatal(err)
|
|
}
|
|
|
|
go func() {
|
|
http.HandleFunc("/con", func(rw http.ResponseWriter, r *http.Request) {
|
|
aes := shttp.AESKey()
|
|
aesc, _ := rk.Encrypt(aes)
|
|
rw.Header().Set("aesc", aesc)
|
|
|
|
body, _ := shttp.EncryptAESByte([]byte(rk.PublicKey()), aes)
|
|
rw.Write(body)
|
|
})
|
|
http.HandleFunc("/report", func(rw http.ResponseWriter, r *http.Request) {
|
|
aesc := r.Header.Get("aesc")
|
|
aes, err := rk.Decrypt(aesc)
|
|
if err != nil {
|
|
Fatal(err)
|
|
}
|
|
defer r.Body.Close()
|
|
datab, err := ioutil.ReadAll(r.Body)
|
|
if err != nil {
|
|
Fatal(err)
|
|
}
|
|
datar, err := shttp.DecryptAESByte(datab, aes)
|
|
if err != nil {
|
|
Fatal(err)
|
|
}
|
|
data := map[string]interface{}{}
|
|
json.Unmarshal(datar, &data)
|
|
fmt.Println(data)
|
|
rw.Write([]byte("ok"))
|
|
})
|
|
err = http.ListenAndServe("localhost:8080", nil)
|
|
if err != nil {
|
|
Fatal(err)
|
|
}
|
|
}()
|
|
|
|
func() {
|
|
time.Sleep(time.Second * 10)
|
|
lk := &shttp.RSA{}
|
|
func() {
|
|
r, err := http.Get("http://localhost:8080/con")
|
|
if err != nil {
|
|
Fatal(err)
|
|
}
|
|
aesc := r.Header.Get("aesc")
|
|
aes, err := rk.Decrypt(aesc)
|
|
if err != nil {
|
|
Fatal(err)
|
|
}
|
|
defer r.Body.Close()
|
|
datab, err := ioutil.ReadAll(r.Body)
|
|
if err != nil {
|
|
Fatal(err)
|
|
}
|
|
datar, err := shttp.DecryptAESByte(datab, aes)
|
|
if err != nil {
|
|
Fatal(err)
|
|
}
|
|
if err = lk.LoadPublic(string(datar)); err != nil {
|
|
Fatal(err)
|
|
}
|
|
}()
|
|
|
|
func() {
|
|
data := map[string]interface{}{"msg": "cool"}
|
|
obj, err := json.Marshal(data)
|
|
if err != nil {
|
|
Fatal(err)
|
|
}
|
|
aes := shttp.AESKey()
|
|
body, err := shttp.EncryptAESByte(obj, aes)
|
|
if err != nil {
|
|
Fatal(err)
|
|
}
|
|
aesc, err := lk.Encrypt(aes)
|
|
if err != nil {
|
|
Fatal(err)
|
|
}
|
|
req, _ := http.NewRequest("POST", "http://localhost:8080/report", bytes.NewReader(body))
|
|
req.Header.Set("aesc", aesc)
|
|
http.DefaultClient.Do(req)
|
|
|
|
time.Sleep(time.Second * 5)
|
|
}()
|
|
|
|
}()
|
|
}
|
|
|
|
func Fatal(i ...interface{}) {
|
|
_, _, line, _ := runtime.Caller(1)
|
|
prt := []interface{}{line, ": "}
|
|
fmt.Println(append(prt, i)...)
|
|
os.Exit(1)
|
|
}
|