Commit 2c96be1d authored by Michel Roy's avatar Michel Roy Committed by Kevin Di Lallo
Browse files

demo1 iperf proxy swagger update

parent a58ef248
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -13,7 +13,7 @@ To see how to make this your own, look here:
[README](https://github.com/swagger-api/swagger-codegen/blob/master/README.md)

- API version: 0.0.1
- Build date: 2019-11-01T08:58:54.052-04:00
- Build date: 2019-11-07T07:57:56.028-05:00


### Running the server
+19 −0
Original line number Diff line number Diff line
@@ -10,109 +10,10 @@
package server

import (
	"encoding/json"
	"log"
	"net"
	"net/http"
	"os/exec"
	"strconv"
	"strings"
)

type IperfInfoBasic struct {
	name    string
	portApp string
}

var iperfRunningJobs map[IperfInfoBasic]int

func Init() {
	iperfRunningJobs = make(map[IperfInfoBasic]int)
}

func GetOutboundIP() net.IP {
	conn, err := net.Dial("udp", "8.8.8.8:80")
	if err != nil {
		log.Fatal(err)
	}
	defer conn.Close()

	localAddr := conn.LocalAddr().(*net.UDPAddr)

	return localAddr.IP
}

func cmdExec(cli string) (int, error) {
	parts := strings.Fields(cli)
	head := parts[0]
	parts = parts[1:]

	cmd := exec.Command(head, parts...)
	err := cmd.Start()
	if err != nil {
		return 0, err
	}

	pid := cmd.Process.Pid
	go func() {
		err = cmd.Wait()
		/*		if err == nil {
					log.Printf("process terminated normally for pid %d", pid)
				} else {
					log.Printf("iperf terminated abnormally for pid %d: %s", pid, err)
				}
		*/
	}()

	return pid, nil
}

func deletePidProcess(pid int) {

	str := "kill -9 " + strconv.Itoa(pid)
	_, _ = cmdExec(str)
}

func HandleIperfInfo(w http.ResponseWriter, r *http.Request) {

	iperfInfo := new(IperfInfo)

	decoder := json.NewDecoder(r.Body)
	err := decoder.Decode(&iperfInfo)

	if err != nil {
		log.Println(err.Error())
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	targetIp := GetOutboundIP()
	// debug printout
	/*      log.Printf("%s - %s - %s -%s\n",
	                iperfInfo.Name,
	                iperfInfo.App,
	                iperfInfo.Throughput,
			targetIp)
	*/
	var iperfInfoBasic IperfInfoBasic
	iperfInfoBasic.name = iperfInfo.Name
	iperfInfoBasic.portApp = iperfInfo.App

	pidExists := iperfRunningJobs[iperfInfoBasic]

	if pidExists != 0 {
		deletePidProcess(pidExists)
	}

	if iperfInfo.Throughput != "0" && iperfInfo.Throughput != "" {
		str := "iperf -u -c " + targetIp.String() + " -t 3600 -p " + iperfInfo.App + " -b " + iperfInfo.Throughput + "M"

		pid, err := cmdExec(str)
		if err != nil {
			log.Printf("ERROR: %s", err)
		}

		iperfRunningJobs[iperfInfoBasic] = pid
	}
	w.Header().Set("Content-Type", "application/json; charset=UTF-8")
	w.WriteHeader(http.StatusOK)
}