-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #622 from getlantern/feat/1437
Adding WATER reverse listener
- Loading branch information
Showing
10 changed files
with
339 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package water | ||
|
||
import ( | ||
"context" | ||
"encoding/base64" | ||
"log/slog" | ||
"net" | ||
|
||
"github.com/getlantern/golog" | ||
"github.com/refraction-networking/water" | ||
_ "github.com/refraction-networking/water/transport/v1" | ||
) | ||
|
||
var log = golog.LoggerFor("water") | ||
|
||
// NewWATERListener creates a WATER listener | ||
// Currently water doesn't support customized TCP connections and we need to listen and receive requests directly from the WATER listener | ||
func NewWATERListener(ctx context.Context, transport, address, wasm string) (net.Listener, error) { | ||
decodedWASM, err := base64.StdEncoding.DecodeString(wasm) | ||
if err != nil { | ||
log.Errorf("failed to decode WASM base64: %v", err) | ||
return nil, err | ||
} | ||
|
||
cfg := &water.Config{ | ||
TransportModuleBin: decodedWASM, | ||
//NetworkListener: baseListener, | ||
OverrideLogger: slog.New(newLogHandler(log, transport)), | ||
} | ||
|
||
waterListener, err := cfg.ListenContext(ctx, "tcp", address) | ||
if err != nil { | ||
log.Errorf("error creating water listener: %v", err) | ||
return nil, err | ||
} | ||
|
||
return waterListener, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package water | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"embed" | ||
"encoding/base64" | ||
"io" | ||
"net" | ||
"testing" | ||
|
||
"github.com/refraction-networking/water" | ||
_ "github.com/refraction-networking/water/transport/v1" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
//go:embed testdata/* | ||
var testData embed.FS | ||
|
||
func TestWATERListener(t *testing.T) { | ||
f, err := testData.Open("testdata/reverse_v1.wasm") | ||
require.Nil(t, err) | ||
|
||
wasm, err := io.ReadAll(f) | ||
require.Nil(t, err) | ||
|
||
b64WASM := base64.StdEncoding.EncodeToString(wasm) | ||
|
||
ctx := context.Background() | ||
|
||
cfg := &water.Config{ | ||
TransportModuleBin: wasm, | ||
} | ||
|
||
ll, err := NewWATERListener(ctx, "reverse_v0", "127.0.0.1:3000", b64WASM) | ||
require.Nil(t, err) | ||
|
||
messageRequest := "hello" | ||
expectedResponse := "world" | ||
// running listener | ||
go func() { | ||
for { | ||
var conn net.Conn | ||
conn, err = ll.Accept() | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
|
||
go func() { | ||
if conn == nil { | ||
log.Error("nil connection") | ||
return | ||
} | ||
buf := make([]byte, 2*len(messageRequest)) | ||
n, err := conn.Read(buf) | ||
if err != nil { | ||
log.Errorf("error reading: %v", err) | ||
return | ||
} | ||
|
||
buf = buf[:n] | ||
if !bytes.Equal(buf, []byte(messageRequest)) { | ||
log.Errorf("unexpected request %v %v", buf, messageRequest) | ||
return | ||
} | ||
conn.Write([]byte(expectedResponse)) | ||
}() | ||
} | ||
}() | ||
|
||
dialer, err := water.NewDialerWithContext(ctx, cfg) | ||
require.Nil(t, err) | ||
|
||
conn, err := dialer.DialContext(ctx, "tcp", ll.Addr().String()) | ||
require.Nil(t, err) | ||
defer conn.Close() | ||
|
||
n, err := conn.Write([]byte(messageRequest)) | ||
assert.Nil(t, err) | ||
assert.Equal(t, len(messageRequest), n) | ||
|
||
buf := make([]byte, 1024) | ||
n, err = conn.Read(buf) | ||
assert.Nil(t, err) | ||
assert.Equal(t, len(expectedResponse), n) | ||
assert.Equal(t, expectedResponse, string(buf[:n])) | ||
} |
Oops, something went wrong.