Skip to content

Commit

Permalink
Support nat traversal
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffyjf authored and lp-learn committed Sep 6, 2023
1 parent 8cc2e9e commit edf5f5f
Show file tree
Hide file tree
Showing 6 changed files with 318 additions and 200 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@

dist/
.vscode/
.devcontainer/
26 changes: 24 additions & 2 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,30 @@ type ClientEndpoint struct {

func (c *ClientEndpoint) Start(nt *options.NATTraversalOptions) {
if nt.NATTraversalMode {
conn := nattraversal.DialUDP(&nattraversal.ConnConfig{Nt: *nt})
c.new(conn, conn.Conn.RemoteAddr(), conn.Conn.LocalAddr().String())
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()
connCtrl := nattraversal.NewConnCtrl(nt)
go nattraversal.DialUDP(ctx, connCtrl)
select {
case <-connCtrl.ExitChan:
log.Warn("first nat traversal failed, the second nat traversal attempt")
// Subscribe and wait for messages from the remote peer
connCtrl.MqttClient = nattraversal.NewMQTTClient(connCtrl.Nt, connCtrl.SdCh)
nattraversal.Subscribe(connCtrl.MqttClient)
connCtrl.RemoteSd = <-connCtrl.SdCh
go nattraversal.ListenUDP(ctx, connCtrl)
select {
case <-connCtrl.ConvertExitChan:
log.Warn("nat traversal faild!")
cancel()
case conn := <-connCtrl.ConnChan:
log.Info("nat traversal success!")
c.new(conn, conn.Conn.RemoteAddr(), conn.Conn.LocalAddr().String())
}
case conn := <-connCtrl.ConnChan:
log.Infof("nat traversal success! Remote address is %s", conn.Conn.RemoteAddr())
c.new(conn, conn.Conn.RemoteAddr(), conn.Conn.LocalAddr().String())
}
} else {
raddr, err := net.ResolveUDPAddr("udp", c.ServerEndpointSocket)
if err != nil {
Expand Down
Loading

0 comments on commit edf5f5f

Please sign in to comment.