Merge pull request #52 from Akilan1999/serverports

Added possibility to run server on any port
This commit is contained in:
Akilan Selvacoumar
2021-07-22 13:12:41 +04:00
committed by GitHub
10 changed files with 99 additions and 23 deletions

View File

@@ -13,7 +13,7 @@ import (
) )
// UpdateIpTable Does the following to update it's IP table // UpdateIpTable Does the following to update it's IP table
func UpdateIpTable(IpAddress string) error { func UpdateIpTable(IpAddress string, serverPort string) error {
config, err := config.ConfigInit() config, err := config.ConfigInit()
if err != nil { if err != nil {
@@ -22,16 +22,17 @@ func UpdateIpTable(IpAddress string) error {
client := http.Client{} client := http.Client{}
var resp []byte var resp []byte
version := p2p.Ip4or6(IpAddress) version := p2p.Ip4or6(IpAddress)
if version == "version 6" { if version == "version 6" {
resp, err = UploadMultipartFile(client,"http://["+IpAddress+"]:8088/IpTable","json",config.IPTable) resp, err = UploadMultipartFile(client,"http://[" + IpAddress + "]:" + serverPort + "/IpTable","json",config.IPTable)
if err != nil { if err != nil {
return err return err
} }
} else { } else {
resp, err = UploadMultipartFile(client,"http://"+IpAddress+":8088/IpTable","json",config.IPTable) resp, err = UploadMultipartFile(client,"http://" + IpAddress + ":" + serverPort + "/IpTable","json",config.IPTable)
if err != nil { if err != nil {
return err return err
} }
@@ -114,9 +115,9 @@ func UpdateIpTableListClient() error {
//} //}
if Addresses.IpAddress[j].Ipv6 != "" { if Addresses.IpAddress[j].Ipv6 != "" {
err = UpdateIpTable(Addresses.IpAddress[j].Ipv6) err = UpdateIpTable(Addresses.IpAddress[j].Ipv6, Addresses.IpAddress[j].ServerPort)
} else { } else {
err = UpdateIpTable(Addresses.IpAddress[j].Ipv4) err = UpdateIpTable(Addresses.IpAddress[j].Ipv4, Addresses.IpAddress[j].ServerPort)
} }
if err != nil { if err != nil {

View File

@@ -15,6 +15,13 @@ import (
func GetSpecs(IP string)(*server.SysInfo,error) { func GetSpecs(IP string)(*server.SysInfo,error) {
var URL string var URL string
version := p2p.Ip4or6(IP) version := p2p.Ip4or6(IP)
//Get port number of the server
serverPort, err := GetServerPort(IP)
if err != nil {
return nil, err
}
if version == "version 6" { if version == "version 6" {
URL = "http://[" + IP + "]:" + serverPort + "/server_info" URL = "http://[" + IP + "]:" + serverPort + "/server_info"
} else { } else {

View File

@@ -10,13 +10,11 @@ import (
"strconv" "strconv"
) )
const ( var (
serverPort = "8088" serverPort = "8088"
client = http.Client{}
) )
var client = http.Client{}
// StartContainer Start container using REST api Implementation // StartContainer Start container using REST api Implementation
// From the selected server IP address // From the selected server IP address
// TODO: Test cases for this function // TODO: Test cases for this function
@@ -25,6 +23,13 @@ func StartContainer(IP string, NumPorts int, GPU bool, ContainerName string) (*d
// Passes URL with number of TCP ports to allocated and to give GPU access to the docker container // Passes URL with number of TCP ports to allocated and to give GPU access to the docker container
var URL string var URL string
version := p2p.Ip4or6(IP) version := p2p.Ip4or6(IP)
//Get port number of the server
serverPort, err := GetServerPort(IP)
if err != nil {
return nil, err
}
if version == "version 6" { if version == "version 6" {
URL = "http://[" + IP + "]:" + serverPort + "/startcontainer?ports=" + fmt.Sprint(NumPorts) + "&GPU=" + strconv.FormatBool(GPU) + "&ContainerName=" + ContainerName URL = "http://[" + IP + "]:" + serverPort + "/startcontainer?ports=" + fmt.Sprint(NumPorts) + "&GPU=" + strconv.FormatBool(GPU) + "&ContainerName=" + ContainerName
} else { } else {
@@ -64,6 +69,13 @@ func StartContainer(IP string, NumPorts int, GPU bool, ContainerName string) (*d
func RemoveContianer(IP string,ID string) error { func RemoveContianer(IP string,ID string) error {
var URL string var URL string
version := p2p.Ip4or6(IP) version := p2p.Ip4or6(IP)
//Get port number of the server
serverPort, err := GetServerPort(IP)
if err != nil {
return err
}
if version == "version 6" { if version == "version 6" {
URL = "http://[" + IP + "]:" + serverPort + "/RemoveContainer?id=" + ID URL = "http://[" + IP + "]:" + serverPort + "/RemoveContainer?id=" + ID
} else { } else {
@@ -99,6 +111,13 @@ func ViewContainers(IP string)(*docker.DockerContainers, error){
// Passes URL with route /ShowImages // Passes URL with route /ShowImages
var URL string var URL string
version := p2p.Ip4or6(IP) version := p2p.Ip4or6(IP)
//Get port number of the server
serverPort, err := GetServerPort(IP)
if err != nil {
return nil, err
}
if version == "version 6" { if version == "version 6" {
URL = "http://[" + IP + "]:" + serverPort + "/ShowImages" URL = "http://[" + IP + "]:" + serverPort + "/ShowImages"
} else { } else {
@@ -127,6 +146,28 @@ func ViewContainers(IP string)(*docker.DockerContainers, error){
return &Result, nil return &Result, nil
} }
// GetServerPort Helper function to do find out server port information
func GetServerPort(IpAddress string) (string,error) {
// Getting information from the clients ip table
ipTable, err := p2p.ReadIpTable()
if err != nil {
return "",err
}
// Iterate thorough ip table struct and find
// out which port is for the ip address provided
// in the parameter of the function
for _, address := range ipTable.IpAddress {
// If we found a match then return a port
if address.Ipv4 == IpAddress || address.Ipv6 == IpAddress {
return address.ServerPort, nil
}
}
// We return default port
return "8088",nil
}
// PrintStartContainer Prints results Generated container // PrintStartContainer Prints results Generated container
//func PrintStartContainer(d *docker.DockerVM){ //func PrintStartContainer(d *docker.DockerVM){
// fmt.Println("ID : " + fmt.Sprint(d.ID)) // fmt.Println("ID : " + fmt.Sprint(d.ID))

View File

@@ -1,3 +0,0 @@
{
"TrackContainer": []
}

View File

@@ -23,13 +23,24 @@ var CliAction = func(ctx *cli.Context) error {
if err != nil { if err != nil {
fmt.Print(err) fmt.Print(err)
} }
// Reads from ip table and passes it
p2p.PrintIpTable() // on to struct print function
Servers, err := p2p.ReadIpTable()
if err != nil {
return err
}
client.PrettyPrint(Servers)
} }
// Displays the IP table // Displays the IP table
if ServerList { if ServerList {
p2p.PrintIpTable() // Reads from ip table and passes it
// on to struct print function
Servers, err := p2p.ReadIpTable()
if err != nil {
return err
}
client.PrettyPrint(Servers)
} }
// Add provided IP to the IP table // Add provided IP to the IP table
@@ -51,6 +62,13 @@ var CliAction = func(ctx *cli.Context) error {
} else { } else {
IpAddr.Ipv4 = AddServer IpAddr.Ipv4 = AddServer
} }
// If a server port is provided then set it
if Ports != "" {
IpAddr.ServerPort = Ports
} else {
IpAddr.ServerPort = "8088"
}
// Append IP address to variable result which // Append IP address to variable result which
// is a list // is a list
res.IpAddress = append(res.IpAddress, IpAddr) res.IpAddress = append(res.IpAddress, IpAddr)

View File

@@ -28,6 +28,7 @@ type Config struct {
IPV6Address string IPV6Address string
PluginPath string PluginPath string
TrackContainersPath string TrackContainersPath string
ServerPort string
//NetworkInterface string //NetworkInterface string
//NetworkInterfaceIPV6Index int //NetworkInterfaceIPV6Index int
} }
@@ -96,6 +97,7 @@ func SetDefaults() error {
defaults["IPV6Address"] = "" defaults["IPV6Address"] = ""
defaults["PluginPath"] = defaultPath + "plugin/deploy" defaults["PluginPath"] = defaultPath + "plugin/deploy"
defaults["TrackContainersPath"] = defaultPath + "client/trackcontainers/trackcontainers.json" defaults["TrackContainersPath"] = defaultPath + "client/trackcontainers/trackcontainers.json"
defaults["ServerPort"] = "8088"
//defaults["NetworkInterface"] = "wlp0s20f3" //defaults["NetworkInterface"] = "wlp0s20f3"
//defaults["NetworkInterfaceIPV6Index"] = "2" //defaults["NetworkInterfaceIPV6Index"] = "2"

View File

@@ -1,11 +1,12 @@
{ {
"ip_address": [ "ip_address": [
{ {
"ipv4": "172.104.44.195", "ipv4": "139.162.246.221",
"ipv6": "", "ipv6": "",
"latency": 0, "latency": 0,
"download": 0, "download": 0,
"upload": 0 "upload": 0,
"serverport": "8088"
} }
] ]
} }

View File

@@ -23,6 +23,7 @@ type IpAddress struct {
Latency time.Duration `json:"latency"` Latency time.Duration `json:"latency"`
Download float64 `json:"download"` Download float64 `json:"download"`
Upload float64 `json:"upload"` Upload float64 `json:"upload"`
ServerPort string `json:"serverport"`
} }
type IP struct { type IP struct {
@@ -69,6 +70,7 @@ func ReadIpTable()(*IpAddresses ,error){
} }
PublicIP.Ipv4 = ip PublicIP.Ipv4 = ip
PublicIP.Ipv6 = ipv6 PublicIP.Ipv6 = ipv6
PublicIP.ServerPort = config.ServerPort
// Updates current machine IP address to the IP table // Updates current machine IP address to the IP table
ipAddresses.IpAddress = append(ipAddresses.IpAddress, PublicIP) ipAddresses.IpAddress = append(ipAddresses.IpAddress, PublicIP)
@@ -120,6 +122,7 @@ func PrintIpTable() error {
"-----------------\n",table.IpAddress[i].Ipv4,table.IpAddress[i].Ipv6, "-----------------\n",table.IpAddress[i].Ipv4,table.IpAddress[i].Ipv6,
table.IpAddress[i].Latency) table.IpAddress[i].Latency)
} }
return nil return nil
} }

View File

@@ -105,7 +105,7 @@ func (s *IpAddress)UploadSpeed() error {
b, w := createMultipartFormData("file",config.SpeedTestFile) b, w := createMultipartFormData("file",config.SpeedTestFile)
req, err := http.NewRequest("GET", "http://" + s.Ipv4 + ":8088/upload", &b) req, err := http.NewRequest("GET", "http://" + s.Ipv4 + ":" + s.ServerPort + "/upload", &b)
if err != nil { if err != nil {
return err return err
} }
@@ -152,10 +152,10 @@ func (s *IpAddress) PingTest() error {
//pingURL := strings.Split(s.URL, "/upload")[0] + "/latency.txt" //pingURL := strings.Split(s.URL, "/upload")[0] + "/latency.txt"
var pingURL string var pingURL string
if s.Ipv6 != "" { if s.Ipv6 != "" {
pingURL = "http://[" + s.Ipv6 + "]:8088/server_info" pingURL = "http://[" + s.Ipv6 + "]:" + s.ServerPort + "/server_info"
s.Ipv4 = "" s.Ipv4 = ""
} else { } else {
pingURL = "http://" + s.Ipv4 + ":8088/server_info" pingURL = "http://" + s.Ipv4 + ":" + s.ServerPort + "/server_info"
} }
l := time.Duration(100000000000) // 10sec l := time.Duration(100000000000) // 10sec

View File

@@ -122,7 +122,7 @@ func Server() error{
c.String(http.StatusOK, "success") c.String(http.StatusOK, "success")
}) })
//Show images avaliable //Show images available
r.GET("/ShowImages", func(c *gin.Context) { r.GET("/ShowImages", func(c *gin.Context) {
resp, err := docker.ViewAllContainers() resp, err := docker.ViewAllContainers()
if err != nil { if err != nil {
@@ -131,8 +131,14 @@ func Server() error{
c.JSON(http.StatusOK, resp) c.JSON(http.StatusOK, resp)
}) })
//Port running on //Get Server port based on the config file
err := r.Run(":8088") config, err := config.ConfigInit()
if err != nil {
return err
}
// Run gin server on the specified port
err = r.Run(":" + config.ServerPort)
if err != nil { if err != nil {
return err return err
} }