Skip to content

Commit

Permalink
add entry GetTCPPorts/GetUDPPorts methods
Browse files Browse the repository at this point in the history
Signed-off-by: Yilun <zyl.skysniper@gmail.com>
  • Loading branch information
yilunzhang committed Apr 26, 2024
1 parent 27462ef commit 18764ac
Showing 1 changed file with 33 additions and 21 deletions.
54 changes: 33 additions & 21 deletions entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ type TunaEntry struct {
paymentStream *smux.Stream
reverseBeneficiary common.Uint160
sessionLock sync.Mutex
tcpPorts []uint32
udpPorts []uint32
}

func NewTunaEntry(service Service, serviceInfo ServiceInfo, wallet *nkn.Wallet, client *nkn.MultiClient, config *EntryConfiguration) (*TunaEntry, error) {
Expand Down Expand Up @@ -101,6 +103,29 @@ func NewTunaEntry(service Service, serviceInfo ServiceInfo, wallet *nkn.Wallet,
func (te *TunaEntry) Start(shouldReconnect bool) error {
defer te.Close()

listenIP := net.ParseIP(te.ServiceInfo.ListenIP)
if listenIP == nil {
listenIP = net.ParseIP(defaultServiceListenIP)
}

tcpPorts, err := te.listenTCP(listenIP, te.Service.TCP)
if err != nil {
return err
}
if len(tcpPorts) > 0 {
log.Printf("Serving %s on localhost tcp port %v", te.Service.Name, tcpPorts)
te.tcpPorts = tcpPorts
}

udpPorts, err := te.listenUDP(listenIP, te.Service.UDP)
if err != nil {
return err
}
if len(udpPorts) > 0 {
log.Printf("Serving %s on localhost udp port %v", te.Service.Name, udpPorts)
te.udpPorts = udpPorts
}

for {
if te.IsClosed() {
return nil
Expand Down Expand Up @@ -155,27 +180,6 @@ func (te *TunaEntry) Start(shouldReconnect bool) error {
break
}

listenIP := net.ParseIP(te.ServiceInfo.ListenIP)
if listenIP == nil {
listenIP = net.ParseIP(defaultServiceListenIP)
}

tcpPorts, err := te.listenTCP(listenIP, te.Service.TCP)
if err != nil {
return err
}
if len(tcpPorts) > 0 {
log.Printf("Serving %s on localhost tcp port %v", te.Service.Name, tcpPorts)
}

udpPorts, err := te.listenUDP(listenIP, te.Service.UDP)
if err != nil {
return err
}
if len(udpPorts) > 0 {
log.Printf("Serving %s on localhost udp port %v", te.Service.Name, udpPorts)
}

geoCloseChan := make(chan struct{})
defer close(geoCloseChan)
if te.ServiceInfo.IPFilter != nil && len(te.ServiceInfo.IPFilter.GetProviders()) > 0 {
Expand Down Expand Up @@ -296,6 +300,14 @@ func (te *TunaEntry) StartReverse(stream *smux.Stream, connMetadata *pb.Connecti
return nil
}

func (te *TunaEntry) GetTCPPorts() []uint32 {
return te.tcpPorts
}

func (te *TunaEntry) GetUDPPorts() []uint32 {
return te.udpPorts
}

func (te *TunaEntry) Close() {
te.WaitSessions()

Expand Down

0 comments on commit 18764ac

Please sign in to comment.