cleaned up Webgatewayupload code and added SSL

This commit is contained in:
2022-10-14 20:40:24 +04:00
parent fbb552be01
commit b8484db9e4
3 changed files with 336 additions and 323 deletions

399
main.go
View File

@@ -1,239 +1,286 @@
package main
import (
"bytes"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"github.com/PeernetOfficial/core"
"github.com/PeernetOfficial/core/btcec"
"github.com/PeernetOfficial/core/webapi"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"time"
"flag"
"bytes"
"encoding/hex"
"encoding/json"
"errors"
"flag"
"fmt"
"github.com/PeernetOfficial/core"
"github.com/PeernetOfficial/core/btcec"
"github.com/PeernetOfficial/core/webapi"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"time"
)
// Variables for the flags to get the address
var (
BackEndApiAddress *string
WebpageAddress *string
SSL *bool
// BackEndApiAddress Refers to the Peernet address ex: <address>:<port no>
BackEndApiAddress *string
// WebpageAddress Refers to the address for upload webgate server
WebpageAddress *string
// SSL To ensure SSL is required checks if SSL is required and subsequently
// checks if the certificate is provided
SSL *bool
// Certificate SSL Certificate file
Certificate *string
// Key SSL Key file
Key *string
// BackendAddressWithHTTP ex: http://<address>:<port no>
BackendAddressWithHTTP string
)
// -------------------------------------------------------------------------------------------------------------
// ---------------------------------------- Initialize flags and run Peernet ---------------------------------
// -------------------------------------------------------------------------------------------------------------
// init reading flags before any part of the code is executed
func init() {
BackEndApiAddress = flag.String("BackEndApiAddress", "0.0.0.0:8088", "current environment")
WebpageAddress = flag.String("WebpageAddress", "0.0.0.0:8098", "current environment")
SSL = flag.Bool("SSL", false, "Flag to check if the SSL certificate is enabled or not")
BackEndApiAddress = flag.String("BackEndApiAddress", "localhost:8088", "current environment")
WebpageAddress = flag.String("WebpageAddress", "localhost:8098", "current environment")
SSL = flag.Bool("SSL", false, "Flag to check if the SSL certificate is enabled or not")
Certificate = flag.String("Certificate", "server.crt", "SSL Certificate file")
Key = flag.String("Key", "server.key", "SSL Key file")
}
// Initializes Peernet
// InitPeernet Initializes Peernet backend
func InitPeernet() *core.Backend {
backend, status, err := core.Init("Your application/1.0", "Config.yaml", nil, nil)
if status != core.ExitSuccess {
fmt.Printf("Error %d initializing backend: %s\n", status, err.Error())
return nil
}
backend, status, err := core.Init("Your application/1.0", "Config.yaml", nil, nil)
if status != core.ExitSuccess {
fmt.Printf("Error %d initializing backend: %s\n", status, err.Error())
return nil
}
return backend
return backend
}
// Starts the WebAPI and peernet
// RunPeernet Starts the WebAPI and peernet
func RunPeernet(backend *core.Backend) {
webapi.Start(backend, []string{*BackEndApiAddress}, false, "", "", 10*time.Second, 10*time.Second, uuid.Nil)
backend.Connect()
webapi.Start(backend, []string{*BackEndApiAddress}, false, "", "", 10*time.Second, 10*time.Second, uuid.Nil)
backend.Connect()
for {
for {
}
}
}
// -------------------------------------------------------------------------------------------------------------
// ---------------------- Custom Structs required parse information from the Peernet backend API ---------------
// -------------------------------------------------------------------------------------------------------------
// 1. Storing file in the warehouse
// 2. Storing file metadata in the blockchain
// -----------------------------------------------------------------------------------------
// ---------------------------------- Warehouse related structs ---------------------------
type WarehouseResult struct {
Status int `json:"status"`
Hash []byte `json:"hash"`
Status int `json:"status"`
Hash []byte `json:"hash"`
}
// -----------------------------------------------------------------------------------------
// -------------------------------- Blockchain related structs -----------------------------
// BlockchainRequest blockchain backend API request struct
type BlockchainRequest struct {
Files []File `json:"files"`
Files []File `json:"files"`
}
type File struct {
Hash []byte `json:"hash"`
Type int `json:"type"`
Name string `json:"name"`
}
func AddFileWarehouse(file io.Reader) *WarehouseResult {
url := *BackEndApiAddress + "/warehouse/create"
req, err := http.NewRequest("POST", url, file)
//req.Header.Set("X-Custom-Header", "myvalue")
//req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
//fmt.Println("response Status:", resp.Status)
//fmt.Println("response Headers:", resp.Header)
body, _ := ioutil.ReadAll(resp.Body)
//fmt.Println("response Body:", string(body))
var result WarehouseResult
err = json.Unmarshal(body, &result)
if err != nil {
fmt.Println(err)
}
return &result
Hash []byte `json:"hash"`
Type int `json:"type"`
Name string `json:"name"`
}
// BlockchainResponse blockchain backend API response struct
type BlockchainResponse struct {
Status int `json:"status"`
Height int `json:"height"`
Version int `json:"version"`
Status int `json:"status"`
Height int `json:"height"`
Version int `json:"version"`
}
// The follwoing function adds the filename and hash to the blockchain
func AddFileToBlockchain(hash []byte, filename string) *BlockchainResponse {
url := *BackEndApiAddress + "/blockchain/file/add"
// -----------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------
// Create file object for post
var blockchainRequest BlockchainRequest
var files File
files.Name = filename
files.Hash = hash
files.Type = 0
blockchainRequest.Files = append(blockchainRequest.Files, files)
// -------------------------------------------------------------------------------------------------------------
// -------------------------------------- Functions to call Peernet Apis ---------------------------------------
// -------------------------------------------------------------------------------------------------------------
// 1. Storing file in the warehouse (/warehouse/create)
// 2. Storing file metadata in the blockchain (/blockchain/file/add)
Byte, err := json.Marshal(blockchainRequest)
// -----------------------------------------------------------------------------------------
// ---------------------------------- Warehouse related ------------------------------------
// convert bytes
req, err := http.NewRequest("POST", url, bytes.NewBuffer(Byte))
//req.Header.Set("X-Custom-Header", "myvalue")
req.Header.Set("Content-Type", "application/json")
// AddFileWarehouse API call for (Storing file in the warehouse)
func AddFileWarehouse(file io.Reader) *WarehouseResult {
url := BackendAddressWithHTTP + "/warehouse/create"
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
req, err := http.NewRequest("POST", url, file)
//req.Header.Set("X-Custom-Header", "myvalue")
//req.Header.Set("Content-Type", "application/json")
//fmt.Println("response Status:", resp.Status)
//fmt.Println("response Headers:", resp.Header)
body, _ := ioutil.ReadAll(resp.Body)
//fmt.Println("response Body:", string(body))
var result BlockchainResponse
err = json.Unmarshal(body, &result)
if err != nil {
return nil
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
return &result
//fmt.Println("response Status:", resp.Status)
//fmt.Println("response Headers:", resp.Header)
body, _ := ioutil.ReadAll(resp.Body)
//fmt.Println("response Body:", string(body))
var result WarehouseResult
err = json.Unmarshal(body, &result)
if err != nil {
fmt.Println(err)
}
return &result
}
// UploadFile Simple abstracted function to add files to peernet core
func UploadFile(backend *core.Backend, file *multipart.File, header *multipart.FileHeader) (*btcec.PublicKey, *WarehouseResult, error) {
buf := bytes.NewBuffer(nil)
if _, err := io.Copy(buf, *file); err != nil {
return nil, nil, errors.New("io.Copy not successful")
}
buf := bytes.NewBuffer(nil)
if _, err := io.Copy(buf, *file); err != nil {
return nil, nil, errors.New("io.Copy not successful")
}
// adds file to warehouse
warehouseResult := AddFileWarehouse(buf)
fmt.Println(warehouseResult.Hash)
// current using default port for Peernet api which is 8080
// First add file to warehouse
// adds file to warehouse
warehouseResult := AddFileWarehouse(buf)
fmt.Println(warehouseResult.Hash)
// current using default port for Peernet api which is 8080
// First add file to warehouse
// Adds the file to a blockchain
Blockchainfo := AddFileToBlockchain(warehouseResult.Hash, header.Filename)
if Blockchainfo == nil {
return nil, nil, errors.New("add file to blockchain not successful")
}
// Adds the file to a blockchain
Blockchainfo := AddFileToBlockchain(warehouseResult.Hash, header.Filename)
if Blockchainfo == nil {
return nil, nil, errors.New("add file to blockchain not successful")
}
_, publicKey := backend.ExportPrivateKey()
fmt.Println(hex.EncodeToString(publicKey.SerializeCompressed()))
_, publicKey := backend.ExportPrivateKey()
fmt.Println(hex.EncodeToString(publicKey.SerializeCompressed()))
return publicKey, warehouseResult, nil
return publicKey, warehouseResult, nil
}
// Add files
// -----------------------------------------------------------------------------------------
// ----------------------------------- Blockchain related ---------------------------------
// AddFileToBlockchain The follwoing function adds the filename and hash to the blockchain
func AddFileToBlockchain(hash []byte, filename string) *BlockchainResponse {
url := BackendAddressWithHTTP + "/blockchain/file/add"
// Create file object for post
var blockchainRequest BlockchainRequest
var files File
files.Name = filename
files.Hash = hash
files.Type = 0
blockchainRequest.Files = append(blockchainRequest.Files, files)
Byte, err := json.Marshal(blockchainRequest)
// convert bytes
req, err := http.NewRequest("POST", url, bytes.NewBuffer(Byte))
//req.Header.Set("X-Custom-Header", "myvalue")
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
var result BlockchainResponse
err = json.Unmarshal(body, &result)
if err != nil {
return nil
}
return &result
}
// -----------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------------------
// --------------------------------------------- Main function -------------------------------------------------
// -------------------------------------------------------------------------------------------------------------
func main() {
// Parsing flags
flag.Parse()
// check if SSL is used or not
if *SSL {
*BackEndApiAddress = "https://" + *BackEndApiAddress
} else {
*BackEndApiAddress = "http://" + *BackEndApiAddress
}
// Start peernet
backend := InitPeernet()
go RunPeernet(backend)
// Start peernet
backend := InitPeernet()
go RunPeernet(backend)
r := gin.Default()
r.LoadHTMLGlob("templates/*.html")
r.Static("/templates", "./templates")
r.GET("/upload", func(c *gin.Context) {
c.HTML(http.StatusOK, "upload.html", nil)
})
r := gin.Default()
r.LoadHTMLGlob("templates/*.html")
r.Static("/templates", "./templates")
r.GET("/upload", func(c *gin.Context) {
c.HTML(http.StatusOK, "upload2.html", nil)
})
r.POST("/uploadFile", func(c *gin.Context) {
file, header, err := c.Request.FormFile("file")
defer file.Close()
r.POST("/uploadFile", func(c *gin.Context) {
file, header, err := c.Request.FormFile("file")
defer file.Close()
if err != nil {
fmt.Println(err)
}
if err != nil {
fmt.Println(err)
}
publicKey, warehouseResult, err := UploadFile(backend, &file, header)
if err != nil {
fmt.Println(err)
}
publicKey, warehouseResult, err := UploadFile(backend, &file, header)
if err != nil {
fmt.Println(err)
}
c.HTML(http.StatusOK, "upload.html", gin.H{
"hash": hex.EncodeToString(warehouseResult.Hash),
"filename": header.Filename,
"size": header.Size,
"link": "https://peer.ae/" + hex.EncodeToString(publicKey.SerializeCompressed()) + "/" + hex.EncodeToString(warehouseResult.Hash),
"address": BackendAddressWithHTTP,
})
c.HTML(http.StatusOK, "upload2.html", gin.H{
"hash": hex.EncodeToString(warehouseResult.Hash),
"filename": header.Filename,
"size": header.Size,
"link": "https://peer.ae/" + hex.EncodeToString(publicKey.SerializeCompressed()) + "/" + hex.EncodeToString(warehouseResult.Hash),
})
})
})
// Implement CURL script to ensure linux users can upload directly
// the Cli like https://bashupload.com
// Ex: curl http://localhost:8080/uploadCurl -F add=@<file name>
r.POST("/uploadCurl", func(c *gin.Context) {
file, header, err := c.Request.FormFile("add")
defer file.Close()
// Implement CURL script to ensure linux users can upload directly
// the Cli like https://bashupload.com
// Ex: curl peer.ae/upload -T your_file.txt
if err != nil {
fmt.Println(err)
}
r.POST("/uploadCurl", func(c *gin.Context) {
file, header, err := c.Request.FormFile("add")
defer file.Close()
publicKey, warehouseResult, err := UploadFile(backend, &file, header)
if err != nil {
fmt.Println(err)
}
if err != nil {
fmt.Println(err)
}
link := "https://peer.ae/" + hex.EncodeToString(publicKey.SerializeCompressed()) + "/" + hex.EncodeToString(warehouseResult.Hash)
c.Data(http.StatusOK, "plain/text", []byte(link))
})
publicKey, warehouseResult, err := UploadFile(backend, &file, header)
if err != nil {
fmt.Println(err)
}
link := "https://peer.ae/" + hex.EncodeToString(publicKey.SerializeCompressed()) + "/" + hex.EncodeToString(warehouseResult.Hash)
c.Data(http.StatusOK, "plain/text", []byte(link))
// c.JSON(http.StatusOK, gin.H{
// "hash": hex.EncodeToString(warehouseResult.Hash),
// "filename": header.Filename,
// "size": header.Size,
// "link": "https://peer.ae/" + hex.EncodeToString(publicKey.SerializeCompressed()) + "/" + hex.EncodeToString(warehouseResult.Hash),
//})
})
r.Run(*WebpageAddress)
// check if SSL is used or not
if *SSL {
BackendAddressWithHTTP = "https://" + *BackEndApiAddress
r.RunTLS(*WebpageAddress, *Certificate, *Key)
} else {
BackendAddressWithHTTP = "http://" + *BackEndApiAddress
r.Run(*WebpageAddress)
}
}

View File

@@ -1,34 +1,119 @@
<!DOCTYPE html>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Peernet upload file</title>
<title>Peernet Download file</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>
<body>
</head>
<body>
<form class="form-group" method="POST" action="/uploadFile" enctype="multipart/form-data">
<div>
<label for="formFileLg" class="form-label">Upload your file</label>
<input class="form-control form-control-lg" id="formFileLg" type="file" name="file">
<h3> or </h3>
<pre><code>curl http://localhost:8080/uploadCurl -F add=@&lt;file name&gt;</code></pre>
</div>
<input type="submit" value="Submit">
{{ if .hash }}
<h3> hash: {{ .hash }} </h3>
{{end}}
</form>
<link href="https://fonts.googleapis.com/css?family=Lato:300,400,700,900&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="templates/css/style.css">
</head>
<body>
<section class="ftco-section">
<div class="container">
<div class="row justify-content-center">
<div class="col-md-12 col-lg-10">
<div class="wrap d-md-flex">
<div class="text-wrap p-4 p-lg-5 text-center d-flex align-items-centert">
<div class="text w-100">
<h2 class="Peernetblue">Upload file to peernet</h2>
<br>
<br>
<form class="form-group" method="POST" action="/uploadFile" enctype="multipart/form-data">
<div class="form-group mb-3">
<!-- <label for="formFileLg" class="form-label">Upload your file</label> -->
<input class="btn btn-white btn-outline-white" id="formFileLg" type="file" name="file">
</div>
<input class="btn btn-white btn-outline-white" type="submit" value="Submit">
<!-- {{ if .hash }}
<h3> hash: {{ .hash }} </h3>
{{end}} -->
</form>
<h3 style="color:white"> or </h3>
<h4 style="color:white">curl {{ .address }}/uploadCurl -F add=@&lt;file name&gt;</h4>
<!-- <a href="#" class="btn btn-white btn-outline-white" tyle="color:#3ac4e2">Upload file</a> -->
<br>
<br>
<!-- <h2>OR</h2>
<a href="#" class="btn btn-white btn-outline-white" style="color:#3ac4e2">Download Peernet browser</a>
<br> -->
<!-- <a style="color:#3ac4e2">This is to ensure the user can download file directly
from your computer</a> -->
</div>
</div>
<div class="login-wrap p-4 p-lg-5">
<div class="d-flex">
<div class="w-100">
<h3 class="mb-4">File information</h3>
</div>
<div class="w-100">
</div>
</div>
<form action="#" class="signin-form">
<div class="form-group mb-3">
<label class="label" for="name">File Name:</label>
{{ if .filename }}
<p> {{ .filename }} </p>
{{end}}
</div>
<div class="form-group mb-3">
<label class="label" for="name">File Size:</label>
{{ if .size }}
<p> {{ .size }} bytes</p>
{{end}}
</div>
<div class="form-group mb-3">
<label class="label" for="name">File Hash:</label>
{{ if .hash }}
<p> {{ .hash }} </p>
{{end}}
</div>
<div class="form-group mb-3">
<label class="label" for="name">Sharable Link:</label>
<br>
{{ if .link }}
<a href="{{ .link }}" style="color:#1d91ac">{{ .filename }} link</a>
{{end}}
</div>
<!-- <div class="form-group mb-3">
<label class="label" for="password">Password</label>
<input type="password" class="form-control" placeholder="Password" required>
</div><div class="form-group mb-3">
<label class="label" for="name">File Name:</label>
<p>Test.txt</p>
</div> -->
</div>
<div class="form-group d-md-flex">
<!-- <div class="w-50 text-left">
<label class="checkbox-wrap checkbox-primary mb-0">Remember Me
<input type="checkbox" checked>
<span class="checkmark"></span>
</label>
</div>
<div class="w-50 text-md-right">
<a href="#">Forgot Password</a>
</div> -->
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</section>
<script src="js/jquery.min.js"></script>
<script src="js/popper.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/main.js"></script>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.14.7/dist/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>
</body>
</html>

View File

@@ -1,119 +0,0 @@
<!doctype html>
<html lang="en">
<head>
<title>Peernet Download file</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link href="https://fonts.googleapis.com/css?family=Lato:300,400,700,900&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="templates/css/style.css">
</head>
<body>
<section class="ftco-section">
<div class="container">
<div class="row justify-content-center">
<div class="col-md-12 col-lg-10">
<div class="wrap d-md-flex">
<div class="text-wrap p-4 p-lg-5 text-center d-flex align-items-centert">
<div class="text w-100">
<h2 class="Peernetblue">Upload file to peernet</h2>
<br>
<br>
<form class="form-group" method="POST" action="/uploadFile" enctype="multipart/form-data">
<div class="form-group mb-3">
<!-- <label for="formFileLg" class="form-label">Upload your file</label> -->
<input class="btn btn-white btn-outline-white" id="formFileLg" type="file" name="file">
</div>
<input class="btn btn-white btn-outline-white" type="submit" value="Submit">
<!-- {{ if .hash }}
<h3> hash: {{ .hash }} </h3>
{{end}} -->
</form>
<h3 style="color:white"> or </h3>
<h4 style="color:white">curl http://localhost:8080/uploadCurl -F add=@&lt;file name&gt;</h4>
<!-- <a href="#" class="btn btn-white btn-outline-white" tyle="color:#3ac4e2">Upload file</a> -->
<br>
<br>
<!-- <h2>OR</h2>
<a href="#" class="btn btn-white btn-outline-white" style="color:#3ac4e2">Download Peernet browser</a>
<br> -->
<!-- <a style="color:#3ac4e2">This is to ensure the user can download file directly
from your computer</a> -->
</div>
</div>
<div class="login-wrap p-4 p-lg-5">
<div class="d-flex">
<div class="w-100">
<h3 class="mb-4">File information</h3>
</div>
<div class="w-100">
</div>
</div>
<form action="#" class="signin-form">
<div class="form-group mb-3">
<label class="label" for="name">File Name:</label>
{{ if .filename }}
<p> {{ .filename }} </p>
{{end}}
</div>
<div class="form-group mb-3">
<label class="label" for="name">File Size:</label>
{{ if .size }}
<p> {{ .size }} bytes</p>
{{end}}
</div>
<div class="form-group mb-3">
<label class="label" for="name">File Hash:</label>
{{ if .hash }}
<p> {{ .hash }} </p>
{{end}}
</div>
<div class="form-group mb-3">
<label class="label" for="name">Sharable Link:</label>
<br>
{{ if .link }}
<a href="{{ .link }}" style="color:#1d91ac">{{ .filename }} link</a>
{{end}}
</div>
<!-- <div class="form-group mb-3">
<label class="label" for="password">Password</label>
<input type="password" class="form-control" placeholder="Password" required>
</div><div class="form-group mb-3">
<label class="label" for="name">File Name:</label>
<p>Test.txt</p>
</div> -->
</div>
<div class="form-group d-md-flex">
<!-- <div class="w-50 text-left">
<label class="checkbox-wrap checkbox-primary mb-0">Remember Me
<input type="checkbox" checked>
<span class="checkmark"></span>
</label>
</div>
<div class="w-50 text-md-right">
<a href="#">Forgot Password</a>
</div> -->
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</section>
<script src="js/jquery.min.js"></script>
<script src="js/popper.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/main.js"></script>
</body>
</html>