Skip to content

Commit

Permalink
Wss, proxy, cookie jar
Browse files Browse the repository at this point in the history
  • Loading branch information
dsnezhkov committed Nov 30, 2018
1 parent f7fbf7c commit 1bfeb67
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 1 deletion.
122 changes: 122 additions & 0 deletions rssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@
package main

import (
"crypto/tls"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"net/http/cookiejar"
"net/url"
"os"
"runtime"
"time"

"github.com/gorilla/websocket"
"golang.org/x/crypto/ssh"
)

Expand Down Expand Up @@ -55,11 +61,68 @@ func main() {

// Client side:
// <-> Likely where websocket base network is plugged in


tlsClient := tls.Config{InsecureSkipVerify: true}
d := websocket.Dialer{
//ReadBufferSize: 1024,
//WriteBufferSize: 1024,
HandshakeTimeout: 45 * time.Second,
Subprotocols: []string{},
TLSClientConfig: &tlsClient,

}

httpProxyURL, err := url.Parse("http://127.0.0.1:8088")
if err != nil {
log.Fatal(err)
}
httpEndpoint, err := url.Parse("https://127.0.0.1:8080")
if err != nil {
log.Fatal(err)
}

jar, _ := cookiejar.New(nil)
d.Jar = jar
d.Proxy = func(*http.Request) (*url.URL, error) {
return httpProxyURL, nil

}

cookies := []*http.Cookie{{Name: "gorilla", Value: "ws", Path: "/"}}

d.Jar.SetCookies( httpEndpoint, cookies)

// TODO: test auth: https://github.com/gorilla/websocket/blob/master/client_server_test.go
wsConn, resp, err := d.Dial("wss://127.0.0.1:8080", nil)


if err != nil {
log.Printf("WS-Dial INTO remote server error: %s", err)
if err == websocket.ErrBadHandshake {
log.Printf("Response Status: %s", resp.Status)
log.Fatalln(fmt.Printf("handshake failed with status %d\n", resp.StatusCode))
}

}


conn := NewWebSocketConn(wsConn)

sshConn, chans, reqs, err := ssh.NewClientConn(conn, "", sshConfig)

serverConn := ssh.NewClient(sshConn, chans, reqs)

/*
// Connect to SSH remote server using serverEndpoint (port 22)
serverConn, err := ssh.Dial("tcp", serverEndpoint.String(), sshConfig)
if err != nil {
log.Fatalln(fmt.Printf("Dial INTO remote server error: %s", err))
}
*/




// Server side:
// Listen on remote server port - CMD
Expand Down Expand Up @@ -100,3 +163,62 @@ func main() {
acceptLoop(listener, config)

}


func NewWebSocketConn(websocketConn *websocket.Conn) net.Conn {
c := wsConn{
Conn: websocketConn,
}
return &c
}

//Read is not threadsafe though thats okay since there
//should never be more than one reader
func (c *wsConn) Read(dst []byte) (int, error) {
ldst := len(dst)
//use buffer or read new message
var src []byte
if l := len(c.buff); l > 0 {
src = c.buff
c.buff = nil
} else {
t, msg, err := c.Conn.ReadMessage()
if err != nil {
return 0, err
} else if t != websocket.BinaryMessage {
log.Printf("<WARNING> non-binary msg")
}
src = msg
}
//copy src->dest
var n int
if len(src) > ldst {
//copy as much as possible of src into dst
n = copy(dst, src[:ldst])
//copy remainder into buffer
r := src[ldst:]
lr := len(r)
c.buff = make([]byte, lr)
copy(c.buff, r)
} else {
//copy all of src into dst
n = copy(dst, src)
}
//return bytes copied
return n, nil
}

func (c *wsConn) Write(b []byte) (int, error) {
if err := c.Conn.WriteMessage(websocket.BinaryMessage, b); err != nil {
return 0, err
}
n := len(b)
return n, nil
}

func (c *wsConn) SetDeadline(t time.Time) error {
if err := c.Conn.SetReadDeadline(t); err != nil {
return err
}
return c.Conn.SetWriteDeadline(t)
}
10 changes: 9 additions & 1 deletion types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package main

import "fmt"
import (
"fmt"
"github.com/gorilla/websocket"
)

// Types
type Endpoint struct {
Expand All @@ -11,3 +14,8 @@ type Endpoint struct {
func (endpoint *Endpoint) String() string {
return fmt.Sprintf("%s:%s", endpoint.Host, endpoint.Port)
}

type wsConn struct {
*websocket.Conn
buff []byte
}
2 changes: 2 additions & 0 deletions websocketd/gencert.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
openssl req -x509 -nodes -newkey rsa:2048 -keyout sslkey.pem -out sslcert.pem -days 365 -subj "/C=GB/ST=London/L=London/O=Global Security/OU=IT Department/CN=example.com"

0 comments on commit 1bfeb67

Please sign in to comment.