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

fix: correcting tls, tls1only and mode parameters for WebSocket relay #252

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
15 changes: 12 additions & 3 deletions pkg/wsman/client/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ type WsTransport struct {
tls bool
tls1only bool
token string
mode string
conn *websocket.Conn
tlsconfig *tls.Config
buf_mutex sync.Mutex
messages []byte
}

// NewTransport creates a new Websocket RoundTripper.
func NewWsTransport(wsurl string, protocol int, host, username, password string, port int, tls, tls1only bool, token string, tlsconfig *tls.Config) *WsTransport {
func NewWsTransport(wsurl string, protocol int, host, username, password string, port int, tls, tls1only bool, token string, mode string, tlsconfig *tls.Config) *WsTransport {
t := &WsTransport{
wsurl: wsurl,
protocol: protocol,
Expand All @@ -50,6 +51,7 @@ func NewWsTransport(wsurl string, protocol int, host, username, password string,
tls: tls,
tls1only: tls1only,
token: token,
mode: mode,
tlsconfig: tlsconfig,
buf_mutex: sync.Mutex{},
}
Expand All @@ -76,8 +78,15 @@ func (t *WsTransport) buildUrl() string {
q.Set("user", t.username)
q.Set("pass", t.password)
q.Set("port", strconv.Itoa(t.port))
q.Set("tls", strconv.FormatBool(t.tls))
q.Set("tls1only", strconv.FormatBool(t.tls1only))
q.Set("tls", "0")
if t.tls {
q.Set("tls", "1")
}
q.Set("tls1only", "0")
if t.tls1only {
q.Set("tls1only", "1")
}
q.Set("mode", t.mode)
// Set query string to URL
u.RawQuery = q.Encode()
return u.String()
Expand Down
24 changes: 14 additions & 10 deletions pkg/wsman/client/relay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ import (
var tlsconfig = &tls.Config{}

func TestNewWsTransport(t *testing.T) {
trans := NewWsTransport("wss://localhost/mps/ws/relay/webrelay.ashx", 1, "9b3ee6a0-c1dc-5546-f7f3-54b2039edfb9", "user", "pass", 16992, false, false, "token", tlsconfig)
trans := NewWsTransport("wss://localhost/mps/ws/relay/webrelay.ashx", 1, "9b3ee6a0-c1dc-5546-f7f3-54b2039edfb9", "user", "pass", 16992, false, false, "token", "wsman", tlsconfig)
if trans == nil {
t.Error("NewWSTransporter constructor fails")
}
}

func TestNewWsTransportBuildUrl(t *testing.T) {
baseurl := "wss://localhost/mps/ws/relay/webrelay.ashx"
trans := NewWsTransport(baseurl, 1, "9b3ee6a0-c1dc-5546-f7f3-54b2039edfb9", "user", "pass", 16992, false, false, "token", tlsconfig)
trans := NewWsTransport(baseurl, 1, "9b3ee6a0-c1dc-5546-f7f3-54b2039edfb9", "user", "pass", 16992, false, false, "token", "wsman", tlsconfig)
if trans == nil {
t.Error("NewWSTransporter constructor fails")
}
Expand All @@ -35,14 +35,18 @@ func TestNewWsTransportBuildUrl(t *testing.T) {
t.Error("Failed to build url")
}
// second path
trans = NewWsTransport(baseurl, 1, "9b3ee6a0-c1dc-5546-f7f3-54b2039edfb9", "user", "pass", 16992, true, true, "token", tlsconfig)
trans = NewWsTransport(baseurl, 1, "9b3ee6a0-c1dc-5546-f7f3-54b2039edfb9", "user", "pass", 16992, true, true, "token", "wsman", tlsconfig)
if trans == nil {
t.Error("NewWSTransporter constructor fails")
}
url = trans.buildUrl()
if url == "" {
t.Error("Failed to build url")
}
//check if it has tls=1 and tls1only=1
if !strings.Contains(url, "tls=1") || !strings.Contains(url, "tls1only=1") {
t.Error("tls or tls1only invalid")
}
}

var upgrader = websocket.Upgrader{}
Expand Down Expand Up @@ -120,7 +124,7 @@ func TestNewWsTransportRoundtripBadParam(t *testing.T) {
baseurl := "ws" + strings.TrimPrefix(s.URL, "http")

// Connect to the server
trans := NewWsTransport(baseurl, 1, "9b3ee6a0-c1dc-5546-f7f3-54b2039edfb9", "user", "", 16992, false, false, "token", tlsconfig)
trans := NewWsTransport(baseurl, 1, "9b3ee6a0-c1dc-5546-f7f3-54b2039edfb9", "user", "", 16992, false, false, "token", "wsman", tlsconfig)
if trans == nil {
t.Error("NewWSTransporter constructor fails")
}
Expand All @@ -141,7 +145,7 @@ func TestNewWsTransportRoundtripBadUrl(t *testing.T) {
baseurl := "ws://localhot/"

// Connect to the server
trans := NewWsTransport(baseurl, 1, "9b3ee6a0-c1dc-5546-f7f3-54b2039edfb9", "user", "pass", 16992, false, false, "token", tlsconfig)
trans := NewWsTransport(baseurl, 1, "9b3ee6a0-c1dc-5546-f7f3-54b2039edfb9", "user", "pass", 16992, false, false, "token", "wsman", tlsconfig)
if trans == nil {
t.Error("NewWSTransporter constructor fails")
}
Expand All @@ -162,7 +166,7 @@ func TestNewWsTransportRoundtripGet(t *testing.T) {
baseurl := "ws" + strings.TrimPrefix(s.URL, "http")

// Connect to the server
trans := NewWsTransport(baseurl, 1, "9b3ee6a0-c1dc-5546-f7f3-54b2039edfb9", "user", "pass", 16992, false, false, "token", tlsconfig)
trans := NewWsTransport(baseurl, 1, "9b3ee6a0-c1dc-5546-f7f3-54b2039edfb9", "user", "pass", 16992, false, false, "token", "wsman", tlsconfig)
if trans == nil {
t.Error("NewWSTransporter constructor fails")
}
Expand All @@ -186,7 +190,7 @@ func TestNewWsTransportRoundtripPost(t *testing.T) {
baseurl := "ws" + strings.TrimPrefix(s.URL, "http")

// Connect to the server
trans := NewWsTransport(baseurl, 1, "9b3ee6a0-c1dc-5546-f7f3-54b2039edfb9", "user", "pass", 16992, false, false, "token", tlsconfig)
trans := NewWsTransport(baseurl, 1, "9b3ee6a0-c1dc-5546-f7f3-54b2039edfb9", "user", "pass", 16992, false, false, "token", "wsman", tlsconfig)
if trans == nil {
t.Error("NewWSTransporter constructor fails")
}
Expand All @@ -210,7 +214,7 @@ func TestNewWsTransportFailedConnection(t *testing.T) {
baseurl := "ws" + strings.TrimPrefix(s.URL, "http") + "/simulate_fail"

// Connect to the server
trans := NewWsTransport(baseurl, 1, "9b3ee6a0-c1dc-5546-f7f3-54b2039edfb9", "user", "pass", 16992, false, false, "token", tlsconfig)
trans := NewWsTransport(baseurl, 1, "9b3ee6a0-c1dc-5546-f7f3-54b2039edfb9", "user", "pass", 16992, false, false, "token", "wsman", tlsconfig)
if trans == nil {
t.Error("NewWSTransporter constructor fails")
}
Expand All @@ -231,7 +235,7 @@ func TestNewWsTransportCloseConnection(t *testing.T) {
baseurl := "ws" + strings.TrimPrefix(s.URL, "http") + "/simulate_close"

// Connect to the server
trans := NewWsTransport(baseurl, 1, "9b3ee6a0-c1dc-5546-f7f3-54b2039edfb9", "user", "pass", 16992, false, false, "token", tlsconfig)
trans := NewWsTransport(baseurl, 1, "9b3ee6a0-c1dc-5546-f7f3-54b2039edfb9", "user", "pass", 16992, false, false, "token", "wsman", tlsconfig)
if trans == nil {
t.Error("NewWSTransporter constructor fails")
}
Expand All @@ -252,7 +256,7 @@ func TestNewWsTransportDelay(t *testing.T) {
baseurl := "ws" + strings.TrimPrefix(s.URL, "http") + "/simulate_delay"

// Connect to the server
trans := NewWsTransport(baseurl, 1, "9b3ee6a0-c1dc-5546-f7f3-54b2039edfb9", "user", "pass", 16992, false, false, "token", tlsconfig)
trans := NewWsTransport(baseurl, 1, "9b3ee6a0-c1dc-5546-f7f3-54b2039edfb9", "user", "pass", 16992, false, false, "token", "wsman", tlsconfig)
if trans == nil {
t.Error("NewWSTransporter constructor fails")
}
Expand Down
Loading