Skip to content

Commit 9b52e16

Browse files
committed
working, 4x slower than yamux :(
1 parent b87d423 commit 9b52e16

File tree

11 files changed

+207
-295
lines changed

11 files changed

+207
-295
lines changed

chisel-forward/client/client.go

Lines changed: 47 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
11
package chiselclient
22

33
import (
4-
"errors"
54
"fmt"
5+
"io"
66
"net"
77
"net/url"
88
"regexp"
99
"strings"
1010
"time"
1111

12-
"github.com/hashicorp/yamux"
1312
"github.com/jpillora/backoff"
1413
"github.com/jpillora/chisel"
14+
"golang.org/x/crypto/ssh"
1515
"golang.org/x/net/websocket"
1616
)
1717

1818
type Client struct {
1919
*chisel.Logger
20-
config *chisel.Config
21-
encconfig string
22-
proxies []*Proxy
23-
session *yamux.Session
24-
running bool
25-
runningc chan error
20+
config *chisel.Config
21+
sshConfig *ssh.ClientConfig
22+
proxies []*Proxy
23+
sshConn ssh.Conn
24+
fingerprint string
25+
running bool
26+
runningc chan error
2627
}
2728

2829
func NewClient(auth, server string, remotes ...string) (*Client, error) {
@@ -63,18 +64,24 @@ func NewClient(auth, server string, remotes ...string) (*Client, error) {
6364
config.Remotes = append(config.Remotes, r)
6465
}
6566

66-
encconfig, err := chisel.EncodeConfig(config)
67-
if err != nil {
68-
return nil, fmt.Errorf("Failed to encode config: %s", err)
67+
c := &Client{
68+
Logger: chisel.NewLogger("client"),
69+
config: config,
70+
running: true,
71+
runningc: make(chan error, 1),
72+
}
73+
74+
c.sshConfig = &ssh.ClientConfig{
75+
ClientVersion: "chisel-client-" + chisel.ProtocolVersion,
76+
User: "jpillora",
77+
Auth: []ssh.AuthMethod{ssh.Password("t0ps3cr3t")},
78+
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
79+
c.fingerprint = chisel.FingerprintKey(key)
80+
return nil
81+
},
6982
}
7083

71-
return &Client{
72-
Logger: chisel.NewLogger("client"),
73-
config: config,
74-
encconfig: encconfig,
75-
running: true,
76-
runningc: make(chan error, 1),
77-
}, nil
84+
return c, nil
7885
}
7986

8087
//Start then Wait
@@ -91,81 +98,54 @@ func (c *Client) Start() {
9198
func (c *Client) start() {
9299
c.Infof("Connecting to %s\n", c.config.Server)
93100

94-
//proxies all use this function
95-
openStream := func() (net.Conn, error) {
96-
if c.session == nil || c.session.IsClosed() {
97-
return nil, c.Errorf("no session available")
98-
}
99-
stream, err := c.session.Open()
100-
if err != nil {
101-
return nil, err
102-
}
103-
return stream, nil
104-
}
105-
106101
//prepare proxies
107102
for id, r := range c.config.Remotes {
108-
proxy := NewProxy(c, id, r, openStream)
103+
proxy := NewProxy(c, id, r)
109104
go proxy.start()
110105
c.proxies = append(c.proxies, proxy)
111106
}
112107

108+
//connection loop!
113109
var connerr error
114110
b := &backoff.Backoff{Max: 5 * time.Minute}
115-
116-
//connection loop!
117111
for {
118112
if !c.running {
119113
break
120114
}
121115
if connerr != nil {
122-
connerr = nil
123116
d := b.Duration()
124117
c.Infof("Retrying in %s...\n", d)
118+
connerr = nil
125119
time.Sleep(d)
126120
}
127121

128-
ws, err := websocket.Dial(c.config.Server, c.encconfig, "http://localhost/")
122+
ws, err := websocket.Dial(c.config.Server, chisel.ConfigPrefix, "localhost:80")
129123
if err != nil {
130124
connerr = err
131125
continue
132126
}
133127

134-
buff := make([]byte, 0xff)
135-
n, _ := ws.Read(buff)
136-
if msg := string(buff[:n]); msg != "handshake-success" {
137-
//no point in retrying
138-
c.runningc <- errors.New(msg)
139-
ws.Close()
140-
break
141-
}
142-
143-
// Setup client side of yamux
144-
c.session, err = yamux.Client(ws, nil)
128+
sshConn, chans, reqs, err := ssh.NewClientConn(ws, "", c.sshConfig)
145129
if err != nil {
146130
connerr = err
131+
c.Infof("Handshake failed: %s", err)
147132
continue
148133
}
134+
c.Infof("Connected (%s)", c.fingerprint)
135+
//connected
149136
b.Reset()
150-
151-
//signal is connected
152-
connected := make(chan bool)
153-
c.Infof("Connected\n")
154-
155-
//poll websocket state
156-
go func() {
157-
for {
158-
if c.session.IsClosed() {
159-
connerr = c.Errorf("disconnected")
160-
c.Infof("Disconnected\n")
161-
close(connected)
162-
break
163-
}
164-
time.Sleep(100 * time.Millisecond)
165-
}
166-
}()
167-
//block!
168-
<-connected
137+
c.sshConn = sshConn
138+
go ssh.DiscardRequests(reqs)
139+
go chisel.RejectStreams(chans)
140+
err = sshConn.Wait()
141+
//disconnected
142+
c.sshConn = nil
143+
if err != nil && err != io.EOF {
144+
connerr = err
145+
c.Infof("Disconnection error: %s", err)
146+
continue
147+
}
148+
c.Infof("Disconnected\n")
169149
}
170150
close(c.runningc)
171151
}
@@ -178,8 +158,8 @@ func (c *Client) Wait() error {
178158
//Close manual stops the client
179159
func (c *Client) Close() error {
180160
c.running = false
181-
if c.session == nil {
161+
if c.sshConn == nil {
182162
return nil
183163
}
184-
return c.session.Close()
164+
return c.sshConn.Close()
185165
}

chisel-forward/client/proxy.go

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
package chiselclient
22

33
import (
4-
"encoding/binary"
4+
"io"
55
"net"
66

77
"github.com/jpillora/chisel"
88
)
99

1010
type Proxy struct {
1111
*chisel.Logger
12-
id int
13-
count int
14-
remote *chisel.Remote
15-
openStream func() (net.Conn, error)
12+
client *Client
13+
id int
14+
count int
15+
16+
remote *chisel.Remote
1617
}
1718

18-
func NewProxy(c *Client, id int, remote *chisel.Remote, openStream func() (net.Conn, error)) *Proxy {
19+
func NewProxy(c *Client, id int, remote *chisel.Remote) *Proxy {
1920
return &Proxy{
20-
Logger: c.Logger.Fork("%s:%s#%d", remote.RemoteHost, remote.RemotePort, id+1),
21-
id: id,
22-
remote: remote,
23-
openStream: openStream,
21+
Logger: c.Logger.Fork("%s:%s#%d", remote.RemoteHost, remote.RemotePort, id+1),
22+
client: c,
23+
id: id,
24+
remote: remote,
2425
}
2526
}
2627

@@ -43,27 +44,28 @@ func (p *Proxy) start() {
4344
}
4445
}
4546

46-
func (p *Proxy) accept(src net.Conn) {
47+
func (p *Proxy) accept(src io.ReadWriteCloser) {
4748
p.count++
4849
cid := p.count
49-
clog := p.Fork("conn#%d", cid)
50+
l := p.Fork("conn#%d", cid)
5051

51-
clog.Debugf("Open")
52+
l.Debugf("Open")
5253

53-
dst, err := p.openStream()
54-
if err != nil {
55-
clog.Debugf("Stream error: %s", err)
54+
if p.client.sshConn == nil {
55+
l.Debugf("No server connection")
5656
src.Close()
5757
return
5858
}
5959

60-
//write endpoint id
61-
b := make([]byte, 2)
62-
binary.BigEndian.PutUint16(b, uint16(p.id))
63-
dst.Write(b)
60+
remoteAddr := p.remote.RemoteHost + ":" + p.remote.RemotePort
61+
dst, err := chisel.OpenStream(p.client.sshConn, remoteAddr)
62+
if err != nil {
63+
l.Debugf("Stream error: %s", err)
64+
src.Close()
65+
return
66+
}
6467

6568
//then pipe
6669
s, r := chisel.Pipe(src, dst)
67-
68-
clog.Debugf("Close (sent %d received %d)", s, r)
70+
l.Debugf("Close (sent %d received %d)", s, r)
6971
}

chisel-forward/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ var help = `
4444
4545
Read more:
4646
https://github.com/jpillora/chisel
47+
4748
`
4849

4950
func main() {

chiseld/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ var help = `
3636
3737
Read more:
3838
https://github.com/jpillora/chisel
39+
3940
`
4041

4142
func main() {

chiseld/server/endpoint.go

Lines changed: 0 additions & 47 deletions
This file was deleted.

0 commit comments

Comments
 (0)