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"
|
||||
"os"
|
||||
"time"
|
||||
"math/rand"
|
||||
)
|
||||
|
||||
var jpegExif = []byte{0xff, 0xd8, 0xff, 0xe1}
|
||||
@ -53,6 +54,11 @@ func main() {
|
||||
}
|
||||
|
||||
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()
|
||||
|
||||
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) {
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
@ -185,6 +189,7 @@ func decryptKeyIV(ed string, privateKey *rsa.PrivateKey) ([]byte, error) {
|
||||
func handleDecrypted(decryptedDataB []byte, uidB []byte) {
|
||||
data, _ := base64.StdEncoding.DecodeString(strings.TrimSpace(string(decryptedDataB)))
|
||||
|
||||
// Determine file type
|
||||
fileType, err := magic.Lookup(data)
|
||||
if err != nil {
|
||||
if err == magic.ErrUnknown {
|
||||
@ -198,11 +203,11 @@ func handleDecrypted(decryptedDataB []byte, uidB []byte) {
|
||||
if fileType != nil {
|
||||
fileTDef = fileType.Extension
|
||||
} else {
|
||||
if(string(data[:4]) == string(jpegExif)){
|
||||
fileTDef = "jpeg"
|
||||
}else{
|
||||
fileTDef = "unknown"
|
||||
}
|
||||
if string(data[:4]) == string(jpegExif) {
|
||||
fileTDef = "jpeg"
|
||||
} else {
|
||||
fileTDef = "unknown"
|
||||
}
|
||||
}
|
||||
|
||||
uid := strings.TrimSpace(string(uidB))
|
||||
@ -213,7 +218,9 @@ func handleDecrypted(decryptedDataB []byte, uidB []byte) {
|
||||
}
|
||||
|
||||
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)
|
||||
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)
|
||||
}
|
||||
|
||||
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 {
|
||||
_, err := os.Stat(folderPath)
|
||||
|
Loading…
Reference in New Issue
Block a user