From 79860a27567c448babcedcadc9e69f5adbbdbd53 Mon Sep 17 00:00:00 2001 From: Akilan1999 Date: Sat, 20 Mar 2021 19:19:34 +0400 Subject: [PATCH] commited readiptable Signed-off-by: Akilan1999 --- .idea/.gitignore | 8 ++++ .idea/modules.xml | 8 ++++ .idea/p2p-rendering-computation.iml | 8 ++++ .idea/vcs.xml | 6 +++ client/python/ipfs_config.py | 4 +- docs/About | 0 go.mod | 1 + go.sum | 7 +++ p2p/README | 3 +- p2p/ip_table.json | 10 ++++ p2p/readiptable.go | 44 ++++++++++++++++++ p2p/readiptable_test.go | 12 +++++ p2p/speedtest/speedtest.go | 71 +++++++++++++++++++++++++++++ server/server.go | 15 ++++++ server/upload_file.go | 38 +++++++++++++++ 15 files changed, 232 insertions(+), 3 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/modules.xml create mode 100644 .idea/p2p-rendering-computation.iml create mode 100644 .idea/vcs.xml create mode 100644 docs/About create mode 100644 p2p/ip_table.json create mode 100644 p2p/readiptable.go create mode 100644 p2p/readiptable_test.go create mode 100644 p2p/speedtest/speedtest.go create mode 100644 server/upload_file.go diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..73f69e0 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..9c70f85 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/p2p-rendering-computation.iml b/.idea/p2p-rendering-computation.iml new file mode 100644 index 0000000..c956989 --- /dev/null +++ b/.idea/p2p-rendering-computation.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/client/python/ipfs_config.py b/client/python/ipfs_config.py index 03df42e..2e0f8e5 100644 --- a/client/python/ipfs_config.py +++ b/client/python/ipfs_config.py @@ -1,8 +1,8 @@ ''' -This scrypt creates basic config for IPFS server +This scrypt creates basic config for IPFS server ''' import ipfsApi def config(): api = ipfsApi.Client('127.0.0.1', 5001) - return api \ No newline at end of file + return api diff --git a/docs/About b/docs/About new file mode 100644 index 0000000..e69de29 diff --git a/go.mod b/go.mod index ddd7d49..35667c3 100644 --- a/go.mod +++ b/go.mod @@ -24,6 +24,7 @@ require ( github.com/opencontainers/go-digest v1.0.0 // indirect github.com/opencontainers/image-spec v1.0.1 // indirect github.com/shirou/gopsutil v3.20.12+incompatible + github.com/showwin/speedtest-go v1.1.1 github.com/sirupsen/logrus v1.7.0 // indirect github.com/urfave/cli/v2 v2.3.0 gitlab.com/NebulousLabs/fastrand v0.0.0-20181126182046-603482d69e40 // indirect diff --git a/go.sum b/go.sum index 5e73ecb..18e4956 100644 --- a/go.sum +++ b/go.sum @@ -6,6 +6,8 @@ github.com/Microsoft/go-winio v0.4.16 h1:FtSW/jqD+l4ba5iPBj9CODVtgfYAD8w2wS923g/ github.com/Microsoft/go-winio v0.4.16/go.mod h1:XB6nPKklQyQ7GC9LdcBEcBl8PF76WugXOPRXwdLnMv0= github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d h1:G0m3OIz70MZUWq3EgK3CesDbo8upS2Vm9/P3FtgI+Jk= github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg= +github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= +github.com/alecthomas/units v0.0.0-20201120081800-1786d5ef83d4/go.mod h1:OMCwj8VM1Kc9e19TLln2VL61YJF0x1XFtfdL4JdbSyE= github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4= github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= @@ -116,6 +118,8 @@ github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0 github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/shirou/gopsutil v3.20.12+incompatible h1:6VEGkOXP/eP4o2Ilk8cSsX0PhOEfX6leqAnD+urrp9M= github.com/shirou/gopsutil v3.20.12+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA= +github.com/showwin/speedtest-go v1.1.1 h1:anVwVssgk9MsN/ev8hXBcfMHAfu7o+6fUPvOYTQYkq8= +github.com/showwin/speedtest-go v1.1.1/go.mod h1:Evr4so/j097J4zgdEyYvaBhzyKMgrTNUOwFQcXqUUzc= github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q= @@ -171,6 +175,8 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9 h1:SQFwaSi55rU7vdNs9Yr0Z324VNlrF+0wMqRXT4St8ck= golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20201207232520-09787c993a3a h1:DcqTD9SDLc+1P/r1EmRBwnVsrOwW+kk2vWf9n+1sGhs= +golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -227,6 +233,7 @@ google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2 google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= google.golang.org/protobuf v1.25.0 h1:Ejskq+SyPohKW+1uil0JJMtmHCgJPJ/qWTxr8qp+R4c= google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c= +gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/p2p/README b/p2p/README index 533000f..d6b752c 100644 --- a/p2p/README +++ b/p2p/README @@ -9,4 +9,5 @@ P2P module - Connect to reliable connection (that has some knowledge) - Check if the connection is still open (If not instruct the server to remove from table) - According speed tests - - Send list of servers connection open + - Send list of servers connection open + - default 100 MB (to store tables of ip records) diff --git a/p2p/ip_table.json b/p2p/ip_table.json new file mode 100644 index 0000000..c129c46 --- /dev/null +++ b/p2p/ip_table.json @@ -0,0 +1,10 @@ +{ + "ip_address":[ + { + "ipv4":"23.23.232.2", + "latency":3, + "download": 34, + "upload": 50 + } + ] +} diff --git a/p2p/readiptable.go b/p2p/readiptable.go new file mode 100644 index 0000000..cdad132 --- /dev/null +++ b/p2p/readiptable.go @@ -0,0 +1,44 @@ +package p2p + +import ( + "encoding/json" + "io/ioutil" + "os" +) + +// Get IP table Data + +type IpAddresses struct { + IpAddress []IpAddress `json:"ip_address"` +} + +type IpAddress struct { + Ipv4 string `json:"ipv4"` + Latency float32 `json:"latency"` + Download float32 `json:"download"` + Upload float32 `json:"upload"` +} + +func ReadIpTable()(*IpAddresses ,error){ + + jsonFile, err := os.Open("ip_table.json") + // if we os.Open returns an error then handle it + if err != nil { + return nil,err + } + + // defer the closing of our jsonFile so that we can parse it later on + defer jsonFile.Close() + + // read our opened xmlFile as a byte array. + byteValue, _ := ioutil.ReadAll(jsonFile) + + // we initialize our Users array + var ipAddresses IpAddresses + + // we unmarshal our byteArray which contains our + // jsonFile's content into 'users' which we defined above + json.Unmarshal(byteValue, &ipAddresses) + + return &ipAddresses, nil +} diff --git a/p2p/readiptable_test.go b/p2p/readiptable_test.go new file mode 100644 index 0000000..2c10dee --- /dev/null +++ b/p2p/readiptable_test.go @@ -0,0 +1,12 @@ +package p2p + +import ( + "testing" +) + +func TestReadIpTable(t *testing.T) { + _, err := ReadIpTable() + if err != nil { + t.Fatal(err) + } +} diff --git a/p2p/speedtest/speedtest.go b/p2p/speedtest/speedtest.go new file mode 100644 index 0000000..8cd597a --- /dev/null +++ b/p2p/speedtest/speedtest.go @@ -0,0 +1,71 @@ +package main + +import ( + "fmt" + "github.com/showwin/speedtest-go/speedtest" + "net/http" + "strings" + "time" +) + +type Server struct { + URL string `xml:"url,attr"` + Lat string `xml:"lat,attr"` + Lon string `xml:"lon,attr"` + Name string `xml:"name,attr"` + Country string `xml:"country,attr"` + Sponsor string `xml:"sponsor,attr"` + ID string `xml:"id,attr"` + URL2 string `xml:"url2,attr"` + Host string `xml:"host,attr"` + Distance float64 + Latency time.Duration + DLSpeed float64 + ULSpeed float64 +} + + +func main() { + SpeedTest() +} + +func SpeedTest() { + user, _ := speedtest.FetchUserInfo() + + serverList, _ := speedtest.FetchServerList(user) + targets, _ := serverList.FindServer([]int{}) + + for _, s := range targets { + fmt.Print(strings.Split(s.URL, "/upload")[0] + "/latency.txt") + s.PingTest() + s.DownloadTest(false) + s.UploadTest(false) + + fmt.Printf("Latency: %s, Download: %f, Upload: %f\n", s.Latency, s.DLSpeed, s.ULSpeed) + } +} + +// PingTest executes test to measure latency +// Function modified for custom URl +func (s *Server) PingTest() error { + pingURL := strings.Split(s.URL, "/upload")[0] + "/latency.txt" + fmt.Print(pingURL) + + l := time.Duration(100000000000) // 10sec + for i := 0; i < 3; i++ { + sTime := time.Now() + resp, err := http.Get(pingURL) + fTime := time.Now() + if err != nil { + return err + } + if fTime.Sub(sTime) < l { + l = fTime.Sub(sTime) + } + resp.Body.Close() + } + + s.Latency = time.Duration(int64(l.Nanoseconds() / 2)) + + return nil +} diff --git a/server/server.go b/server/server.go index 45b75ef..3ee612b 100644 --- a/server/server.go +++ b/server/server.go @@ -1,7 +1,9 @@ package server import ( + "fmt" "github.com/gin-gonic/gin" + "log" "net/http" //"fmt" ) @@ -14,6 +16,19 @@ func Server() { c.JSON(http.StatusOK, ServerInfo()) }) + // Route build to do a speed test + r.GET("/upload", func(c *gin.Context) { + file, _ := c.FormFile("file") + log.Println(file.Filename) + + // Upload the file to specific dst. + // c.SaveUploadedFile(file, dst) + + c.String(http.StatusOK, fmt.Sprintf("'%s' uploaded!", file.Filename)) + }) + + + /*r.GET("/create_vm/:virtualization", func(c *gin.Context) { virtualization := c.Param("virtualization") // Runs based on Preallocated VM size diff --git a/server/upload_file.go b/server/upload_file.go new file mode 100644 index 0000000..11820cc --- /dev/null +++ b/server/upload_file.go @@ -0,0 +1,38 @@ +package server + +import ( + "github.com/gin-gonic/gin" + "github.com/google/uuid" // To generate random file names + "net/http" + "path/filepath" +) + +func saveFileHandler(c *gin.Context) { + file, err := c.FormFile("file") + + // The file cannot be received. + if err != nil { + c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{ + "message": "No file is received", + }) + return + } + + // Retrieve file information + extension := filepath.Ext(file.Filename) + // Generate random file name for the new uploaded file so it doesn't override the old file with same name + newFileName := uuid.New().String() + extension + + // The file is received, so let's save it + if err := c.SaveUploadedFile(file, "/tmp/" + newFileName); err != nil { + c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{ + "message": "Unable to save the file", + }) + return + } + + // File saved successfully. Return proper result + c.JSON(http.StatusOK, gin.H{ + "message": "Your file has been successfully uploaded.", + }) +} \ No newline at end of file