Skip to content

Commit 63ce73c

Browse files
committed
Allow forwarding to other hosts via QUIC channel.
1 parent 8f4910e commit 63ce73c

File tree

2 files changed

+27
-24
lines changed

2 files changed

+27
-24
lines changed

channels/quic.go

+26-23
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type QUICLocalChannel struct {
3636
}
3737

3838
type QUICRemoteChannel struct {
39-
Port int64 `json:"port"`
39+
Target string `json:"target"`
4040
Host string `json:"host"`
4141
}
4242

@@ -48,20 +48,15 @@ type QUICChannel struct {
4848
var QUICRemoteForm = forms.Form{
4949
Fields: []forms.Field{
5050
{
51-
Name: "host",
51+
Name: "target",
5252
Validators: []forms.Validator{
5353
forms.IsString{},
5454
},
5555
},
5656
{
57-
Name: "port",
57+
Name: "host",
5858
Validators: []forms.Validator{
59-
forms.IsInteger{
60-
HasMin: true,
61-
Min: 1,
62-
HasMax: true,
63-
Max: 65535,
64-
},
59+
forms.IsString{},
6560
},
6661
},
6762
},
@@ -80,13 +75,6 @@ var QUICLocalForm = forms.Form{
8075
},
8176
},
8277
},
83-
{
84-
Name: "host",
85-
Validators: []forms.Validator{
86-
forms.IsOptional{Default: "0.0.0.0"},
87-
forms.IsString{},
88-
},
89-
},
9078
},
9179
}
9280

@@ -200,16 +188,25 @@ func (q *QUICChannel) server(listener *quic.Listener) {
200188
bs2 := make([]byte, 2)
201189

202190
if _, err := io.ReadFull(stream, bs2); err != nil {
203-
hyper.Log.Error("Cannot read port")
191+
hyper.Log.Error("Cannot read string length")
192+
return
193+
}
194+
195+
len := binary.LittleEndian.Uint16(bs2)
196+
197+
target := make([]byte, len)
198+
199+
if _, err := io.ReadFull(stream, target); err != nil {
200+
hyper.Log.Error("Cannot read hostame")
204201
return
205202
}
206203

207-
port := binary.LittleEndian.Uint16(bs2)
204+
hyper.Log.Info("Connecting to target '%s'...", string(target))
208205

209-
conn, err := net.Dial("tcp", fmt.Sprintf("localhost:%d", port))
206+
conn, err := net.Dial("tcp", string(target))
210207

211208
if err != nil {
212-
hyper.Log.Errorf("Cannot connect to local port %d", port)
209+
hyper.Log.Errorf("Cannot connect to target '%s'", target)
213210
stream.Close()
214211
return
215212
}
@@ -263,7 +260,7 @@ func (q *QUICChannel) handle(conn net.Conn, channel *QUICChannelConfig) {
263260
hyper.Log.Error(err)
264261
return
265262
} else {
266-
if err := q.pipe(conn, settings.Address, channel.Remote.Host, channel.Remote.Port); err != nil {
263+
if err := q.pipe(conn, settings.Address, channel.Remote.Host, channel.Remote.Target); err != nil {
267264
hyper.Log.Errorf("Cannot connect: %v", err)
268265
return
269266
}
@@ -326,7 +323,7 @@ func pipe(left, right io.ReadWriteCloser, close func()) {
326323
}
327324
}
328325

329-
func (q *QUICChannel) pipe(conn net.Conn, addr, serverName string, port int64) error {
326+
func (q *QUICChannel) pipe(conn net.Conn, addr, serverName string, target string) error {
330327

331328
config, err := tls.TLSClientConfig(q.Settings.TLS)
332329

@@ -357,12 +354,18 @@ func (q *QUICChannel) pipe(conn net.Conn, addr, serverName string, port int64) e
357354

358355
bs2 := make([]byte, 2)
359356

360-
binary.LittleEndian.PutUint16(bs2, uint16(port))
357+
binary.LittleEndian.PutUint16(bs2, uint16(len(target)))
361358

359+
// we write the target length
362360
if _, err := stream.Write(bs2); err != nil {
363361
return err
364362
}
365363

364+
// we write the target
365+
if _, err := stream.Write([]byte(target)); err != nil {
366+
return err
367+
}
368+
366369
hyper.Log.Debugf("Proxying connection...")
367370

368371
go pipe(stream, conn, close)

settings/dev/roles/quic-1/001_default.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ channels: # defines all the channels that we want to open when starting the serv
2525
channels:
2626
- remote:
2727
host: quic-2
28-
port: 6379
28+
target: "localhost:4444"
2929
local:
3030
port: 5555
3131
tls:

0 commit comments

Comments
 (0)