Skip to content

Commit

Permalink
Updates to updates (#6)
Browse files Browse the repository at this point in the history
* make cloudflare use api, make xirsys only use one of each

* move away from logrus and move to slog

* set up basic Prometheus metrics

* improve Prometheus integration
Move Prometheus Registry to Config, push registry at the end of
the client run, and add metrics.

* fix typo

* use logging URL for pushing Prometheus metrics

* fix unique metrics name

* set up using promwrite module to send metrics to Gigapipe

* do remote_write of metrics to Gigapipe at the end of the tests

* updates ot get stats in prom

* add labels, remove subsystem from metrics

* add in stats gathering to our api

* add in the stats module

* add check on ICE candidate type for offerer

---------

Co-authored-by: marcovidonis <[email protected]>
  • Loading branch information
danjenkins and marcovidonis authored Jul 4, 2024
1 parent b34c541 commit bdfbc98
Show file tree
Hide file tree
Showing 13 changed files with 2,637 additions and 397 deletions.
155 changes: 123 additions & 32 deletions adapters/cloudflare/driver.go
Original file line number Diff line number Diff line change
@@ -1,55 +1,146 @@
package cloudflare

import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strings"

"github.com/nimbleape/iceperf-agent/config"
"github.com/pion/stun/v2"
"github.com/pion/webrtc/v4"
)

type Driver struct {
Config *config.ICEConfig
}

type CloudflareIceServers struct {
URLs []string `json:"urls,omitempty"`
Username string `json:"username,omitempty"`
Credential string `json:"credential,omitempty"`
}
type CloudflareResponse struct {
IceServers CloudflareIceServers `json:"iceServers"`
}

func (d *Driver) GetIceServers() (iceServers []webrtc.ICEServer, err error) {
if d.Config.StunHost != "" && d.Config.StunEnabled {
if _, ok := d.Config.StunPorts["udp"]; ok {
for _, port := range d.Config.StunPorts["udp"] {
iceServers = append(iceServers, webrtc.ICEServer{
URLs: []string{fmt.Sprintf("stun:%s:%d", d.Config.StunHost, port)},
})
}
if d.Config.RequestUrl != "" {

client := &http.Client{}

req, err := http.NewRequest("POST", d.Config.RequestUrl, strings.NewReader(`{"ttl": 86400}`))
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", "Bearer "+d.Config.ApiKey)

if err != nil {
// log.WithFields(log.Fields{
// "error": err,
// }).Error("Error forming http request")
return nil, err
}
}

if d.Config.TurnHost != "" && d.Config.TurnEnabled {
for transport := range d.Config.TurnPorts {
switch transport {
case "udp":
for _, port := range d.Config.TurnPorts["udp"] {
iceServers = append(iceServers, webrtc.ICEServer{
URLs: []string{fmt.Sprintf("turn:%s:%d?transport=udp", d.Config.TurnHost, port)},
Username: d.Config.Username,
Credential: d.Config.Password,
})
}
case "tcp":
for _, port := range d.Config.TurnPorts["tcp"] {
res, err := client.Do(req)
if err != nil {
// log.WithFields(log.Fields{
// "error": err,
// }).Error("Error doing http response")
return nil, err
}

defer res.Body.Close()
//check the code of the response
if res.StatusCode != 201 {
err = errors.New("error from cloudflare api")
// log.WithFields(log.Fields{
// "code": res.StatusCode,
// "error": err,
// }).Error("Error status code http response")
return nil, err
}

responseData, err := io.ReadAll(res.Body)
if err != nil {
// log.WithFields(log.Fields{
// "error": err,
// }).Error("Error reading http response")
return nil, err
}
// log.Info("got a response back from cloudflare api")

responseServers := CloudflareResponse{}
json.Unmarshal([]byte(responseData), &responseServers)

// log.WithFields(log.Fields{
// "response": responseServers,
// }).Info("http response")

for _, r := range responseServers.IceServers.URLs {

info, err := stun.ParseURI(r)

if err != nil {
return nil, err
}

if ((info.Scheme == stun.SchemeTypeTURN || info.Scheme == stun.SchemeTypeTURNS) && !d.Config.TurnEnabled) || ((info.Scheme == stun.SchemeTypeSTUN || info.Scheme == stun.SchemeTypeSTUNS) && !d.Config.StunEnabled) {
continue
}

s := webrtc.ICEServer{
URLs: []string{r},
}

if responseServers.IceServers.Username != "" {
s.Username = responseServers.IceServers.Username
}
if responseServers.IceServers.Credential != "" {
s.Credential = responseServers.IceServers.Credential
}
iceServers = append(iceServers, s)
}
} else {
if d.Config.StunHost != "" && d.Config.StunEnabled {
if _, ok := d.Config.StunPorts["udp"]; ok {
for _, port := range d.Config.StunPorts["udp"] {
iceServers = append(iceServers, webrtc.ICEServer{
URLs: []string{fmt.Sprintf("turn:%s:%d?transport=tcp", d.Config.TurnHost, port)},
Username: d.Config.Username,
Credential: d.Config.Password,
URLs: []string{fmt.Sprintf("stun:%s:%d", d.Config.StunHost, port)},
})
}
case "tls":
for _, port := range d.Config.TurnPorts["tls"] {
iceServers = append(iceServers, webrtc.ICEServer{
URLs: []string{fmt.Sprintf("turns:%s:%d?transport=tcp", d.Config.TurnHost, port)},
Username: d.Config.Username,
Credential: d.Config.Password,
})
}
}

if d.Config.TurnHost != "" && d.Config.TurnEnabled {
for transport := range d.Config.TurnPorts {
switch transport {
case "udp":
for _, port := range d.Config.TurnPorts["udp"] {
iceServers = append(iceServers, webrtc.ICEServer{
URLs: []string{fmt.Sprintf("turn:%s:%d?transport=udp", d.Config.TurnHost, port)},
Username: d.Config.Username,
Credential: d.Config.Password,
})
}
case "tcp":
for _, port := range d.Config.TurnPorts["tcp"] {
iceServers = append(iceServers, webrtc.ICEServer{
URLs: []string{fmt.Sprintf("turn:%s:%d?transport=tcp", d.Config.TurnHost, port)},
Username: d.Config.Username,
Credential: d.Config.Password,
})
}
case "tls":
for _, port := range d.Config.TurnPorts["tls"] {
iceServers = append(iceServers, webrtc.ICEServer{
URLs: []string{fmt.Sprintf("turns:%s:%d?transport=tcp", d.Config.TurnHost, port)},
Username: d.Config.Username,
Credential: d.Config.Password,
})
}
default:
}
default:
}
}
}
Expand Down
31 changes: 15 additions & 16 deletions adapters/elixir/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/nimbleape/iceperf-agent/config"
"github.com/pion/stun/v2"
"github.com/pion/webrtc/v4"
log "github.com/sirupsen/logrus"
)

type Driver struct {
Expand All @@ -31,43 +30,43 @@ func (d *Driver) GetIceServers() (iceServers []webrtc.ICEServer, err error) {
client := &http.Client{}

if err != nil {
log.WithFields(log.Fields{
"error": err,
}).Error("Error forming http request URL")
// log.WithFields(log.Fields{
// "error": err,
// }).Error("Error forming http request URL")
return nil, err
}
req, err := http.NewRequest("POST", d.Config.RequestUrl+"&username="+d.Config.HttpUsername, nil)

if err != nil {
log.WithFields(log.Fields{
"error": err,
}).Error("Error forming http request")
// log.WithFields(log.Fields{
// "error": err,
// }).Error("Error forming http request")
return nil, err
}

res, err := client.Do(req)
if err != nil {
log.WithFields(log.Fields{
"error": err,
}).Error("Error doing http response")
// log.WithFields(log.Fields{
// "error": err,
// }).Error("Error doing http response")
return nil, err
}

defer res.Body.Close()
//check the code of the response
if res.StatusCode != 200 {
err = errors.New("error from elixir api")
log.WithFields(log.Fields{
"error": err,
}).Error("Error status code http response")
// log.WithFields(log.Fields{
// "error": err,
// }).Error("Error status code http response")
return nil, err
}

responseData, err := io.ReadAll(res.Body)
if err != nil {
log.WithFields(log.Fields{
"error": err,
}).Error("Error reading http response")
// log.WithFields(log.Fields{
// "error": err,
// }).Error("Error reading http response")
return nil, err
}

Expand Down
13 changes: 6 additions & 7 deletions adapters/metered/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/nimbleape/iceperf-agent/config"
"github.com/pion/stun/v2"
"github.com/pion/webrtc/v4"
log "github.com/sirupsen/logrus"
)

type Driver struct {
Expand All @@ -28,17 +27,17 @@ type MeteredIceServers struct {
func (d *Driver) GetIceServers() (iceServers []webrtc.ICEServer, err error) {
res, err := http.Get(d.Config.RequestUrl + "?apiKey=" + d.Config.ApiKey)
if err != nil {
log.WithFields(log.Fields{
"error": err,
}).Error("Error making http request")
// log.WithFields(log.Fields{
// "error": err,
// }).Error("Error making http request")
return nil, err
}

responseData, err := io.ReadAll(res.Body)
if err != nil {
log.WithFields(log.Fields{
"error": err,
}).Error("Error reading http response")
// log.WithFields(log.Fields{
// "error": err,
// }).Error("Error reading http response")
return nil, err
}

Expand Down
30 changes: 14 additions & 16 deletions adapters/twilio/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/nimbleape/iceperf-agent/config"
"github.com/pion/stun/v2"
"github.com/pion/webrtc/v4"
log "github.com/sirupsen/logrus"
// log "github.com/sirupsen/logrus"
)

type Driver struct {
Expand Down Expand Up @@ -38,35 +38,35 @@ func (d *Driver) GetIceServers() (iceServers []webrtc.ICEServer, err error) {
req.SetBasicAuth(d.Config.HttpUsername, d.Config.HttpPassword)

if err != nil {
log.WithFields(log.Fields{
"error": err,
}).Error("Error forming http request")
// log.WithFields(log.Fields{
// "error": err,
// }).Error("Error forming http request")
return nil, err
}

res, err := client.Do(req)
if err != nil {
log.WithFields(log.Fields{
"error": err,
}).Error("Error doing http response")
// log.WithFields(log.Fields{
// "error": err,
// }).Error("Error doing http response")
return nil, err
}

defer res.Body.Close()
//check the code of the response
if res.StatusCode != 201 {
err = errors.New("error from twilio api")
log.WithFields(log.Fields{
"error": err,
}).Error("Error status code http response")
// log.WithFields(log.Fields{
// "error": err,
// }).Error("Error status code http response")
return nil, err
}

responseData, err := io.ReadAll(res.Body)
if err != nil {
log.WithFields(log.Fields{
"error": err,
}).Error("Error reading http response")
// log.WithFields(log.Fields{
// "error": err,
// }).Error("Error reading http response")
return nil, err
}

Expand All @@ -87,7 +87,7 @@ func (d *Driver) GetIceServers() (iceServers []webrtc.ICEServer, err error) {
continue
}

if (info.Scheme == stun.SchemeTypeTURN) {
if info.Scheme == stun.SchemeTypeTURN {
tempTurnHost = info.Host
}

Expand All @@ -104,8 +104,6 @@ func (d *Driver) GetIceServers() (iceServers []webrtc.ICEServer, err error) {
iceServers = append(iceServers, s)
}



//apparently if you go and make a tls turn uri it will work
s := webrtc.ICEServer{
URLs: []string{"turns:" + tempTurnHost + ":5349?transport=tcp"},
Expand Down
Loading

0 comments on commit bdfbc98

Please sign in to comment.