a
This commit is contained in:
parent
e80372d6c0
commit
e21dee39c7
32
rsaserver.go
32
rsaserver.go
@ -16,6 +16,7 @@ import (
|
|||||||
"github.com/liamg/magic"
|
"github.com/liamg/magic"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
"math/rand"
|
||||||
)
|
)
|
||||||
|
|
||||||
var jpegExif = []byte{0xff, 0xd8, 0xff, 0xe1}
|
var jpegExif = []byte{0xff, 0xd8, 0xff, 0xe1}
|
||||||
@ -53,6 +54,11 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func handleConnection(conn net.Conn, privateKey *rsa.PrivateKey) {
|
func handleConnection(conn net.Conn, privateKey *rsa.PrivateKey) {
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
log.Printf("Recovered from panic: %v", r)
|
||||||
|
}
|
||||||
|
}()
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
|
||||||
fmt.Println("Got conn")
|
fmt.Println("Got conn")
|
||||||
@ -105,8 +111,6 @@ func handleConnection(conn net.Conn, privateKey *rsa.PrivateKey) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
func decrypt(cipherText []byte, key []byte, iv []byte) ([]byte, error) {
|
func decrypt(cipherText []byte, key []byte, iv []byte) ([]byte, error) {
|
||||||
block, err := aes.NewCipher(key)
|
block, err := aes.NewCipher(key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -185,6 +189,7 @@ func decryptKeyIV(ed string, privateKey *rsa.PrivateKey) ([]byte, error) {
|
|||||||
func handleDecrypted(decryptedDataB []byte, uidB []byte) {
|
func handleDecrypted(decryptedDataB []byte, uidB []byte) {
|
||||||
data, _ := base64.StdEncoding.DecodeString(strings.TrimSpace(string(decryptedDataB)))
|
data, _ := base64.StdEncoding.DecodeString(strings.TrimSpace(string(decryptedDataB)))
|
||||||
|
|
||||||
|
// Determine file type
|
||||||
fileType, err := magic.Lookup(data)
|
fileType, err := magic.Lookup(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == magic.ErrUnknown {
|
if err == magic.ErrUnknown {
|
||||||
@ -198,11 +203,11 @@ func handleDecrypted(decryptedDataB []byte, uidB []byte) {
|
|||||||
if fileType != nil {
|
if fileType != nil {
|
||||||
fileTDef = fileType.Extension
|
fileTDef = fileType.Extension
|
||||||
} else {
|
} else {
|
||||||
if(string(data[:4]) == string(jpegExif)){
|
if string(data[:4]) == string(jpegExif) {
|
||||||
fileTDef = "jpeg"
|
fileTDef = "jpeg"
|
||||||
}else{
|
} else {
|
||||||
fileTDef = "unknown"
|
fileTDef = "unknown"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
uid := strings.TrimSpace(string(uidB))
|
uid := strings.TrimSpace(string(uidB))
|
||||||
@ -213,7 +218,9 @@ func handleDecrypted(decryptedDataB []byte, uidB []byte) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
timestamp := time.Now().Unix()
|
timestamp := time.Now().Unix()
|
||||||
filename := fmt.Sprintf("%d.%s", timestamp, fileTDef)
|
|
||||||
|
nonce := generateNonce(8)
|
||||||
|
filename := fmt.Sprintf("%d_%s.%s", timestamp, nonce, fileTDef)
|
||||||
|
|
||||||
filePath := fmt.Sprintf("%s/%s", folderPath, filename)
|
filePath := fmt.Sprintf("%s/%s", folderPath, filename)
|
||||||
fmt.Println(filePath)
|
fmt.Println(filePath)
|
||||||
@ -225,6 +232,15 @@ func handleDecrypted(decryptedDataB []byte, uidB []byte) {
|
|||||||
fmt.Printf("Got a %s from %s, saving to %s\n", fileTDef, uid, filePath)
|
fmt.Printf("Got a %s from %s, saving to %s\n", fileTDef, uid, filePath)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func generateNonce(length int) string {
|
||||||
|
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
||||||
|
var nonce strings.Builder
|
||||||
|
for i := 0; i < length; i++ {
|
||||||
|
nonce.WriteByte(charset[rand.Intn(len(charset))])
|
||||||
|
}
|
||||||
|
return nonce.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
func createFolderIfNotExists(folderPath string) error {
|
func createFolderIfNotExists(folderPath string) error {
|
||||||
_, err := os.Stat(folderPath)
|
_, err := os.Stat(folderPath)
|
||||||
|
Loading…
Reference in New Issue
Block a user