Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test tcp #12

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
47 changes: 36 additions & 11 deletions demo/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"flag"
"fmt"
"log"
"math/rand"
"net"
"time"
Expand All @@ -21,13 +20,13 @@ var (

func runClient() {
if *server == "" {
log.Printf("Please specify a -server id")
log.Debugf("Please specify a -server id")
flag.Usage()
return
}
log.Printf("Starting client, connecting to server %s ...", *server)
log.Debugf("Starting client, connecting to server %s ...", *server)
traversalId := uint32(rand.Int31())
log.Printf("Starting traversal: %d", traversalId)
log.Debugf("Starting traversal: %d", traversalId)
serverId, err := waddell.PeerIdFromString(*server)
if err != nil {
log.Fatalf("Unable to parse PeerID for server %s: %s", *server, err)
Expand All @@ -44,9 +43,13 @@ func runClient() {
t.Close()
log.Fatalf("Unable to offer: %s", err)
}
log.Printf("Got five tuple: %s", ft)
log.Debugf("Got five tuple: %s", ft)
if <-serverReady {
writeUDP(ft)
if "tcp" == *proto {
writeTCP(ft)
} else {
writeUDP(ft)
}
}
}

Expand All @@ -56,7 +59,7 @@ func sendMessages(t *natty.Traversal, serverId waddell.PeerId, traversalId uint3
if done {
return
}
log.Printf("Sending %s", msgOut)
log.Debugf("Sending %s", msgOut)
out <- waddell.Message(serverId, idToBytes(traversalId), []byte(msgOut))
}
}
Expand All @@ -65,10 +68,10 @@ func receiveMessages(t *natty.Traversal, traversalId uint32) {
for wm := range in {
msg := message(wm.Body)
if msg.getTraversalId() != traversalId {
log.Printf("Got message for unknown traversal %d, skipping", msg.getTraversalId())
log.Debugf("Got message for unknown traversal %d, skipping", msg.getTraversalId())
continue
}
log.Printf("Received: %s", msg.getData())
log.Debugf("Received: %s", msg.getData())
msgString := string(msg.getData())
if READY == msgString {
// Server's ready!
Expand All @@ -84,17 +87,39 @@ func writeUDP(ft *natty.FiveTuple) {
if err != nil {
log.Fatalf("Unable to resolve UDP addresses: %s", err)
}
local.IP = net.IPv4(0, 0, 0, 0)
conn, err := net.DialUDP("udp", local, remote)
if err != nil {
log.Fatalf("Unable to dial UDP: %s", err)
}
for {
msg := fmt.Sprintf("Hello from %s to %s", ft.Local, ft.Remote)
log.Printf("Sending UDP message: %s", msg)
msg := fmt.Sprintf("Hello from %s to %s", local, remote)
log.Debugf("Sending UDP message: %s", msg)
_, err := conn.Write([]byte(msg))
if err != nil {
log.Fatalf("Offerer unable to write to UDP: %s", err)
}
time.Sleep(1 * time.Second)
}
}

func writeTCP(ft *natty.FiveTuple) {
local, remote, err := ft.TCPAddrs()
if err != nil {
log.Fatalf("Unable to resolve TCP addresses: %s", err)
}
local.IP = net.IPv4(0, 0, 0, 0)
conn, err := net.DialTCP("tcp", local, remote)
if err != nil {
log.Fatalf("Unable to dial TCP: %s", err)
}
for {
msg := fmt.Sprintf("Hello from %s to %s", local, remote)
log.Debugf("Sending TCP message: %s", msg)
_, err := conn.Write([]byte(msg))
if err != nil {
log.Fatalf("Offerer unable to write to TCP: %s", err)
}
time.Sleep(1 * time.Second)
}
}
9 changes: 7 additions & 2 deletions demo/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package main
import (
"encoding/binary"
"flag"
"log"
"net"
"time"

"github.com/getlantern/golog"
"github.com/getlantern/waddell"
)

Expand All @@ -22,10 +22,12 @@ const (
)

var (
log = golog.LoggerFor("demo")
endianness = binary.LittleEndian

help = flag.Bool("help", false, "Get usage help")
mode = flag.String("mode", "client", "client or server. Client initiates the NAT traversal. Defaults to client.")
proto = flag.String("proto", "udp", "tcp or udp. which protocol to test as transport. Defaults to udp.")
waddellAddr = flag.String("waddell", "128.199.130.61:443", "Address of waddell signaling server, defaults to 128.199.130.61:443")
waddellCert = flag.String("waddellcert", DefaultWaddellCert, "Certificate for waddell server")

Expand Down Expand Up @@ -79,11 +81,14 @@ func connectToWaddell() {
return net.Dial("tcp", *waddellAddr)
},
ServerCert: *waddellCert,
OnId: func(i waddell.PeerId) {
id = i
},
})
if err != nil {
log.Fatalf("Unable to connect to waddell: %s", err)
}
log.Printf("Connected")
log.Debug("Connected")
out = wc.Out(DemoTopic)
in = wc.In(DemoTopic)
}
Expand Down
60 changes: 46 additions & 14 deletions demo/server.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package main

import (
"log"
"net"
"sync"

"encoding/hex"
"github.com/getlantern/go-natty/natty"
"github.com/getlantern/waddell"
"net"
"sync"
)

type peer struct {
Expand All @@ -21,7 +20,7 @@ var (
)

func runServer() {
log.Printf("Starting server, waddell id is \"%s\"", id.String())
log.Debugf("Starting server, waddell id is \"%s\"", id.String())

peers = make(map[waddell.PeerId]*peer)

Expand Down Expand Up @@ -51,7 +50,7 @@ func (p *peer) answer(wm *waddell.MessageIn) {
traversalId := msg.getTraversalId()
t := p.traversals[traversalId]
if t == nil {
log.Printf("Answering traversal: %d", traversalId)
log.Debugf("Answering traversal: %d", traversalId)
// Set up a new Natty traversal
t = natty.Answer(TIMEOUT)
go func() {
Expand All @@ -61,7 +60,7 @@ func (p *peer) answer(wm *waddell.MessageIn) {
if done {
return
}
log.Printf("Sending %s", msgOut)
log.Debugf("Sending %s", msgOut)
out <- waddell.Message(p.id, idToBytes(traversalId), []byte(msgOut))
}
}()
Expand All @@ -77,16 +76,20 @@ func (p *peer) answer(wm *waddell.MessageIn) {

ft, err := t.FiveTuple()
if err != nil {
log.Printf("Unable to answer traversal %d: %s", traversalId, err)
log.Debugf("Unable to answer traversal %d: %s", traversalId, err)
return
}

log.Printf("Got five tuple: %s", ft)
go readUDP(p.id, traversalId, ft)
log.Debugf("Got five tuple: %s", ft)
if ft.Proto == "tcp" {
go readTCP(p.id, traversalId, ft)
} else if ft.Proto == "udp" {
go readUDP(p.id, traversalId, ft)
}
}()
p.traversals[traversalId] = t
}
log.Printf("Received for traversal %d: %s", traversalId, msg.getData())
log.Debugf("Received for traversal %d: %s", traversalId, msg.getData())
t.MsgIn(string(msg.getData()))
}

Expand All @@ -95,20 +98,49 @@ func readUDP(peerId waddell.PeerId, traversalId uint32, ft *natty.FiveTuple) {
if err != nil {
log.Fatalf("Unable to resolve UDP addresses: %s", err)
}
local.IP = net.IPv4(0, 0, 0, 0)
conn, err := net.ListenUDP("udp", local)
if err != nil {
log.Fatalf("Unable to listen on UDP: %s", err)
}
log.Printf("Listening for UDP packets at: %s", local)
log.Debugf("Listening for UDP packets at: %s", local)
notifyClientOfServerReady(peerId, traversalId)
b := make([]byte, 1024)
for {
n, addr, err := conn.ReadFrom(b)
if err != nil {
log.Fatalf("Unable to read from UDP: %s", err)
}
msg := string(b[:n])
log.Printf("Got UDP message from %s: '%s'", addr, msg)
msg := hex.Dump(b[:n])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change? The message being sent is a UTF-8 string, why hex encode it?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw case that it affected the terminal display.

log.Debugf("Got UDP message from %s: \n%s", addr, msg)
}
}

func readTCP(peerId waddell.PeerId, traversalId uint32, ft *natty.FiveTuple) {
local, _, err := ft.TCPAddrs()
if err != nil {
log.Fatalf("Unknown TCP addr: %s", err)
}
local.IP = net.IPv4(0, 0, 0, 0)
tcplisten, err := net.ListenTCP("tcp", local)
if err != nil {
log.Fatalf("Unable to listen on TCP: %s", err)
}
log.Debugf("Listening for TCP packets at: %s", local)
notifyClientOfServerReady(peerId, traversalId)
b := make([]byte, 1024)
conn, err := tcplisten.Accept()
if err != nil {
log.Fatalf("Unable to accept on TCP: %s", err)
}
addr := conn.RemoteAddr()
for {
n, err := conn.Read(b)
if err != nil {
log.Fatalf("Unable to read from TCP: %s", err)
}
msg := hex.Dump(b[:n])
log.Debugf("Got TCP message from %s: \n%s", addr, msg)
}
}

Expand Down
20 changes: 20 additions & 0 deletions natty/natty.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,26 @@ func (ft *FiveTuple) UDPAddrs() (local *net.UDPAddr, remote *net.UDPAddr, err er
return
}

// TCPAddrs returns a pair of TCPAddrs representing the Local and Remote
// addresses of this FiveTuple. If the FiveTuple's Proto is not TCP, this method
// returns an error.
func (ft *FiveTuple) TCPAddrs() (local *net.TCPAddr, remote *net.TCPAddr, err error) {
if ft.Proto != TCP {
err = fmt.Errorf("FiveTuple.Proto was not TCP!: %s", ft.Proto)
return
}
local, err = net.ResolveTCPAddr("tcp", ft.Local)
if err != nil {
err = fmt.Errorf("Unable to resolve local TCP address %s: %s", ft.Local, err)
return
}
remote, err = net.ResolveTCPAddr("tcp", ft.Remote)
if err != nil {
err = fmt.Errorf("Unable to resolve remote TCP address %s: %s", ft.Remote, err)
}
return
}

// Traversal represents a single NAT traversal using natty, whose result is
// available via the methods FiveTuple() and FiveTupleTimeout().
//
Expand Down