Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates to updates #6

Merged
merged 14 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading