Skip to content

Commit 8607fa1

Browse files
committed
rework to support remote R:socks syntax
1 parent 7eba741 commit 8607fa1

File tree

5 files changed

+30
-46
lines changed

5 files changed

+30
-46
lines changed

client/client.go

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,14 @@ import (
44
"context"
55
"fmt"
66
"io"
7-
"io/ioutil"
87
"net"
9-
"log"
108
"net/http"
119
"net/url"
12-
"os"
1310
"regexp"
1411
"strings"
1512
"time"
1613

17-
socks5 "github.com/armon/go-socks5"
14+
"github.com/armon/go-socks5"
1815
"github.com/gorilla/websocket"
1916
"github.com/jpillora/backoff"
2017
"github.com/jpillora/chisel/share"
@@ -33,7 +30,6 @@ type Config struct {
3330
HTTPProxy string
3431
Remotes []string
3532
HostHeader string
36-
Socks5 bool
3733
}
3834

3935
//Client represents a client instance
@@ -74,11 +70,15 @@ func NewClient(config *Config) (*Client, error) {
7470
//swap to websockets scheme
7571
u.Scheme = strings.Replace(u.Scheme, "http", "ws", 1)
7672
shared := &chshare.Config{}
73+
createSocksServer := false
7774
for _, s := range config.Remotes {
7875
r, err := chshare.DecodeRemote(s)
7976
if err != nil {
8077
return nil, fmt.Errorf("Failed to decode remote '%s': %s", s, err)
8178
}
79+
if r.Socks && r.Reverse {
80+
createSocksServer = true
81+
}
8282
shared.Remotes = append(shared.Remotes, r)
8383
}
8484
config.shared = shared
@@ -108,18 +108,12 @@ func NewClient(config *Config) (*Client, error) {
108108
Timeout: 30 * time.Second,
109109
}
110110

111-
if config.Socks5 {
111+
if createSocksServer {
112112
socksConfig := &socks5.Config{}
113-
if client.Debug {
114-
socksConfig.Logger = log.New(os.Stdout, "[client socks]", log.Ldate|log.Ltime)
115-
} else {
116-
socksConfig.Logger = log.New(ioutil.Discard, "", 0)
117-
}
118113
client.socksServer, err = socks5.New(socksConfig)
119114
if err != nil {
120115
return nil, err
121116
}
122-
client.Infof("client-side SOCKS5 server enabled")
123117
}
124118

125119
return client, nil
@@ -161,10 +155,6 @@ func (c *Client) Start(ctx context.Context) error {
161155
}
162156
}
163157
}
164-
// start socks server
165-
if c.socksServer != nil {
166-
go c.socksServer.ListenAndServe("tcp", "127.0.0.1:1081")
167-
}
168158
c.Infof("Connecting to %s%s\n", c.server, via)
169159
//optional keepalive loop
170160
if c.config.KeepAlive > 0 {
@@ -296,13 +286,19 @@ func (c *Client) Close() error {
296286
func (c *Client) connectStreams(chans <-chan ssh.NewChannel) {
297287
for ch := range chans {
298288
remote := string(ch.ExtraData())
289+
socks := remote == "socks"
299290
stream, reqs, err := ch.Accept()
300291
if err != nil {
301292
c.Debugf("Failed to accept stream: %s", err)
302293
continue
303294
}
304295
go ssh.DiscardRequests(reqs)
305296
l := c.Logger.Fork("conn#%d", c.connStats.New())
306-
go chshare.HandleTCPStream(l, &c.connStats, stream, remote)
297+
if socks {
298+
go chshare.HandleSocksStream(l, c.socksServer, &c.connStats, stream)
299+
} else {
300+
go chshare.HandleTCPStream(l, &c.connStats, stream, remote)
301+
}
302+
307303
}
308304
}

main.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ var serverHelp = `
124124
chisel receives a normal HTTP request. Useful for hiding chisel in
125125
plain sight.
126126
127-
--socks5, Allow clients to access the server-outbound SOCKS5 proxy. See
127+
--socks5, Allow clients to access the internal SOCKS5 proxy. See
128128
chisel client --help for more information.
129129
130130
--reverse, Allow clients to specify reverse port forwarding remotes
@@ -266,12 +266,6 @@ var clientHelp = `
266266
267267
--hostname, Optionally set the 'Host' header (defaults to the host
268268
found in the server url).
269-
270-
--socks5, Start a client-outbound SOCKS5 server on 127.0.0.1:1081.
271-
Combine this option with a reverse port forward remote to expose the
272-
client network to the chisel server. For example, the following
273-
remote R:127.0.0.1:5000:127.0.0.1:1081 would allow the sever to
274-
access the client network via socks5://127.0.0.1:5000.
275269
` + commonHelp
276270

277271
func client(args []string) {
@@ -286,7 +280,6 @@ func client(args []string) {
286280
proxy := flags.String("proxy", "", "")
287281
pid := flags.Bool("pid", false, "")
288282
hostname := flags.String("hostname", "", "")
289-
socks5 := flags.Bool("socks5", false, "")
290283
verbose := flags.Bool("v", false, "")
291284
flags.Usage = func() {
292285
fmt.Print(clientHelp)
@@ -311,7 +304,6 @@ func client(args []string) {
311304
Server: args[0],
312305
Remotes: args[1:],
313306
HostHeader: *hostname,
314-
Socks5: *socks5,
315307
})
316308
if err != nil {
317309
log.Fatal(err)

server/handler.go

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package chserver
22

33
import (
44
"context"
5-
"io"
65
"net/http"
76
"strings"
87
"sync/atomic"
@@ -179,22 +178,9 @@ func (s *Server) handleSSHChannels(clientLog *chshare.Logger, chans <-chan ssh.N
179178
//handle stream type
180179
connID := s.connStats.New()
181180
if socks {
182-
go s.handleSocksStream(clientLog.Fork("socksconn#%d", connID), stream)
181+
go chshare.HandleSocksStream(clientLog.Fork("socksconn#%d", connID), s.socksServer, &s.connStats, stream)
183182
} else {
184183
go chshare.HandleTCPStream(clientLog.Fork("conn#%d", connID), &s.connStats, stream, remote)
185184
}
186185
}
187186
}
188-
189-
func (s *Server) handleSocksStream(l *chshare.Logger, src io.ReadWriteCloser) {
190-
conn := chshare.NewRWCConn(src)
191-
s.connStats.Open()
192-
l.Debugf("%s Opening", s.connStats)
193-
err := s.socksServer.ServeConn(conn)
194-
s.connStats.Close()
195-
if err != nil && !strings.HasSuffix(err.Error(), "EOF") {
196-
l.Debugf("%s: Closed (error: %s)", s.connStats, err)
197-
} else {
198-
l.Debugf("%s: Closed", s.connStats)
199-
}
200-
}

share/remote.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,6 @@ func DecodeRemote(s string) (*Remote, error) {
4343
p := parts[i]
4444
//last part "socks"?
4545
if i == len(parts)-1 && p == "socks" {
46-
if reverse {
47-
// TODO allow reverse+socks by having client
48-
// automatically start local SOCKS5 server
49-
return nil, errors.New("'socks' incompatible with reverse port forwarding")
50-
}
5146
r.Socks = true
5247
continue
5348
}

share/ssh.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"net"
1313
"strings"
1414

15+
"github.com/armon/go-socks5"
1516
"github.com/jpillora/sizestr"
1617
"golang.org/x/crypto/ssh"
1718
)
@@ -56,3 +57,17 @@ func HandleTCPStream(l *Logger, connStats *ConnStats, src io.ReadWriteCloser, re
5657
connStats.Close()
5758
l.Debugf("%s: Close (sent %s received %s)", connStats, sizestr.ToString(s), sizestr.ToString(r))
5859
}
60+
61+
func HandleSocksStream(l *Logger, server *socks5.Server, connStats *ConnStats, src io.ReadWriteCloser) {
62+
conn := NewRWCConn(src)
63+
connStats.Open()
64+
l.Debugf("%s Opening", connStats)
65+
err := server.ServeConn(conn)
66+
connStats.Close()
67+
68+
if err != nil && !strings.HasSuffix(err.Error(), "EOF") {
69+
l.Debugf("%s: Closed (error: %s)", connStats, err)
70+
} else {
71+
l.Debugf("%s: Closed", connStats)
72+
}
73+
}

0 commit comments

Comments
 (0)