Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

fix: Add TLS option to DialICE #330

Merged
merged 2 commits into from
May 4, 2021
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
2 changes: 1 addition & 1 deletion internal/cmd/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (c *tunnneler) start(ctx context.Context) error {
CredentialType: webrtc.ICECredentialTypePassword,
}

err := wsnet.DialICE(server, 0)
err := wsnet.DialICE(server, nil)
if errors.Is(err, wsnet.ErrInvalidCredentials) {
return xerrors.Errorf("failed to authenticate your user for this workspace")
}
Expand Down
2 changes: 1 addition & 1 deletion wsnet/dial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func ExampleDial_basic() {
}}

for _, server := range servers {
err := DialICE(server, 0)
err := DialICE(server, nil)
if errors.Is(err, ErrInvalidCredentials) {
// You could do something...
}
Expand Down
23 changes: 17 additions & 6 deletions wsnet/rtc.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,30 @@ var (
controlChannel = "control"
)

// DialICEOptions provides options for dialing an ICE server.
type DialICEOptions struct {
Timeout time.Duration
// Whether to ignore TLS errors.
InsecureSkipVerify bool
}

// DialICE confirms ICE servers are dialable.
// Timeout defaults to 200ms.
func DialICE(server webrtc.ICEServer, timeout time.Duration) error {
func DialICE(server webrtc.ICEServer, options *DialICEOptions) error {
if options == nil {
options = &DialICEOptions{}
}

for _, rawURL := range server.URLs {
err := dialICEURL(server, rawURL, timeout)
err := dialICEURL(server, rawURL, options)
if err != nil {
return err
}
}
return nil
}

func dialICEURL(server webrtc.ICEServer, rawURL string, timeout time.Duration) error {
func dialICEURL(server webrtc.ICEServer, rawURL string, options *DialICEOptions) error {
url, err := ice.ParseURL(rawURL)
if err != nil {
return err
Expand All @@ -69,13 +80,13 @@ func dialICEURL(server webrtc.ICEServer, rawURL string, timeout time.Duration) e
return resErr
}
dconn, dialErr := dtls.Dial("udp4", udpAddr, &dtls.Config{
InsecureSkipVerify: true,
InsecureSkipVerify: options.InsecureSkipVerify,
})
err = dialErr
udpConn = turn.NewSTUNConn(dconn)
case ice.ProtoTypeTCP:
tcpConn, err = tls.Dial("tcp4", turnServerAddr, &tls.Config{
InsecureSkipVerify: true,
InsecureSkipVerify: options.InsecureSkipVerify,
})
}
}
Expand All @@ -100,7 +111,7 @@ func dialICEURL(server webrtc.ICEServer, rawURL string, timeout time.Duration) e
Password: pass,
Realm: "",
Conn: udpConn,
RTO: timeout,
RTO: options.Timeout,
})
if err != nil {
return err
Expand Down
20 changes: 16 additions & 4 deletions wsnet/rtc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ func TestDialICE(t *testing.T) {
Username: "example",
Credential: "test",
CredentialType: webrtc.ICECredentialTypePassword,
}, time.Millisecond)
}, &DialICEOptions{
Timeout: time.Millisecond,
InsecureSkipVerify: true,
})
if err != nil {
t.Error(err)
}
Expand All @@ -37,7 +40,10 @@ func TestDialICE(t *testing.T) {
Username: "example",
Credential: "test",
CredentialType: webrtc.ICECredentialTypePassword,
}, time.Millisecond)
}, &DialICEOptions{
Timeout: time.Millisecond,
InsecureSkipVerify: true,
})
if !errors.Is(err, ErrMismatchedProtocol) {
t.Error(err)
}
Expand All @@ -52,7 +58,10 @@ func TestDialICE(t *testing.T) {
Username: "example",
Credential: "invalid",
CredentialType: webrtc.ICECredentialTypePassword,
}, time.Millisecond)
}, &DialICEOptions{
Timeout: time.Millisecond,
InsecureSkipVerify: true,
})
if !errors.Is(err, ErrInvalidCredentials) {
t.Error(err)
}
Expand All @@ -63,7 +72,10 @@ func TestDialICE(t *testing.T) {

err := DialICE(webrtc.ICEServer{
URLs: []string{"turn:stun.l.google.com:19302"},
}, time.Millisecond)
}, &DialICEOptions{
Timeout: time.Millisecond,
InsecureSkipVerify: true,
})
if !errors.Is(err, ErrMismatchedProtocol) {
t.Error(err)
}
Expand Down