commited readiptable
Signed-off-by: Akilan1999 <akilan.selvacoumar@planetscale.com>
This commit is contained in:
@@ -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)
|
||||
|
||||
10
p2p/ip_table.json
Normal file
10
p2p/ip_table.json
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"ip_address":[
|
||||
{
|
||||
"ipv4":"23.23.232.2",
|
||||
"latency":3,
|
||||
"download": 34,
|
||||
"upload": 50
|
||||
}
|
||||
]
|
||||
}
|
||||
44
p2p/readiptable.go
Normal file
44
p2p/readiptable.go
Normal file
@@ -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
|
||||
}
|
||||
12
p2p/readiptable_test.go
Normal file
12
p2p/readiptable_test.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package p2p
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestReadIpTable(t *testing.T) {
|
||||
_, err := ReadIpTable()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
71
p2p/speedtest/speedtest.go
Normal file
71
p2p/speedtest/speedtest.go
Normal file
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user