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

fix tuna exit not started in udp only mode #52

Merged
merged 1 commit into from
Mar 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 68 additions & 46 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type TunaSessionClient struct {
onConnect chan struct{}
onClose chan struct{}
connectedOnce sync.Once
udpConn *udp.EncryptUDPConn

sync.RWMutex
listeners []net.Listener
Expand Down Expand Up @@ -175,51 +176,7 @@ func (c *TunaSessionClient) Listen(addrsRe *nkngomobile.StringArray) error {
return errors.New("wallet is empty")
}

listeners := make([]net.Listener, c.config.NumTunaListeners)
for i := 0; i < len(listeners); i++ {
listeners[i], err = net.Listen("tcp", "127.0.0.1:")
if err != nil {
return err
}
}
c.listeners = listeners

exits := make([]*tuna.TunaExit, c.config.NumTunaListeners)
for i := 0; i < len(listeners); i++ {
exits[i], err = c.newTunaExit(i)
if err != nil {
return err
}

go func(te *tuna.TunaExit) {
select {
case <-te.OnConnect.C:
c.connectedOnce.Do(func() {
close(c.onConnect)
})
case <-c.onClose:
return
}
}(exits[i])

go exits[i].StartReverse(true)
}

c.tunaExits = exits

go func() {
select {
case <-c.onConnect:
go c.listenNKN()
for i := 0; i < len(listeners); i++ {
go c.listenNet(i)
}
case <-c.onClose:
return
}
}()

return nil
return c.startExits()
}

// OnConnect returns a channel that will be closed when at least one tuna exit
Expand Down Expand Up @@ -842,17 +799,80 @@ func (c *TunaSessionClient) removeClosedSessions() {
}
}

func (c *TunaSessionClient) startExits() error {
yilunzhang marked this conversation as resolved.
Show resolved Hide resolved
if len(c.tunaExits) > 0 {
return nil
}
listeners := make([]net.Listener, c.config.NumTunaListeners)
var err error
for i := 0; i < len(listeners); i++ {
listeners[i], err = net.Listen("tcp", "127.0.0.1:")
if err != nil {
return err
}
}
c.listeners = listeners

exits := make([]*tuna.TunaExit, c.config.NumTunaListeners)
for i := 0; i < len(listeners); i++ {
exits[i], err = c.newTunaExit(i)
if err != nil {
return err
}

go func(te *tuna.TunaExit) {
select {
case <-te.OnConnect.C:
c.connectedOnce.Do(func() {
close(c.onConnect)
})
case <-c.onClose:
return
}
}(exits[i])

go exits[i].StartReverse(true)
}
c.tunaExits = exits
go func() {
select {
case <-c.onConnect:
go c.listenNKN()
for i := 0; i < len(listeners); i++ {
go c.listenNet(i)
}
case <-c.onClose:
return
}
}()

return nil
}

func (c *TunaSessionClient) ListenUDP(addrsRe *nkngomobile.StringArray) (*udp.EncryptUDPConn, error) {
acceptAddrs, err := getAcceptAddrs(addrsRe)
if err != nil {
return nil, err
}

c.Lock()
yilunzhang marked this conversation as resolved.
Show resolved Hide resolved
defer c.Unlock()

c.acceptAddrs = acceptAddrs

if c.udpConn != nil {
yilunzhang marked this conversation as resolved.
Show resolved Hide resolved
return c.udpConn, nil
}

if c.wallet == nil {
return nil, errors.New("wallet is empty")
}

err = c.startExits()
if err != nil {
return nil, err
}

host, portStr, err := net.SplitHostPort(c.listeners[0].Addr().String())
if err != nil {
return nil, err
Expand All @@ -866,7 +886,9 @@ func (c *TunaSessionClient) ListenUDP(addrsRe *nkngomobile.StringArray) (*udp.En
if err != nil {
return nil, err
}
return udp.NewEncryptUDPConn(conn), nil

c.udpConn = udp.NewEncryptUDPConn(conn)
return c.udpConn, nil
}

func (c *TunaSessionClient) DialUDP(remoteAddr string) (*udp.EncryptUDPConn, error) {
Expand Down