Skip to content

Commit

Permalink
Try to fix sync bug
Browse files Browse the repository at this point in the history
  • Loading branch information
yosebyte authored Dec 6, 2024
1 parent ca63180 commit 8d0bfb5
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
5 changes: 2 additions & 3 deletions internal/tunnel/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,12 @@ func Server(parsedURL *url.URL, whiteList *sync.Map) error {
}
defer linkConn.Close()
log.Info("Tunnel connection established from: [%v]", linkConn.RemoteAddr().String())
mu := &sync.Mutex{}
errChan := make(chan error, 2)
go func() {
errChan <- ServeTCP(parsedURL, whiteList, linkAddr, targetTCPAddr, linkListen, linkConn, mu)
errChan <- ServeTCP(parsedURL, whiteList, linkAddr, targetTCPAddr, linkListen, linkConn)
}()
go func() {
errChan <- ServeUDP(parsedURL, whiteList, linkAddr, targetUDPAddr, linkListen, linkConn, mu)
errChan <- ServeUDP(parsedURL, whiteList, linkAddr, targetUDPAddr, linkListen, linkConn)
}()
return <-errChan
}
3 changes: 2 additions & 1 deletion internal/tunnel/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import (
"github.com/yosebyte/passport/pkg/log"
)

func ServeTCP(parsedURL *url.URL, whiteList *sync.Map, linkAddr, targetAddr *net.TCPAddr, linkListen *net.TCPListener, linkConn *net.TCPConn, mu *sync.Mutex) error {
func ServeTCP(parsedURL *url.URL, whiteList *sync.Map, linkAddr, targetAddr *net.TCPAddr, linkListen *net.TCPListener, linkConn *net.TCPConn) error {
targetListen, err := net.ListenTCP("tcp", targetAddr)
if err != nil {
log.Error("Unable to listen target address: [%v]", targetAddr)
return err
}
defer targetListen.Close()
var mu sync.Mutex
semaphore := make(chan struct{}, 1024)
for {
targetConn, err := targetListen.AcceptTCP()
Expand Down
6 changes: 4 additions & 2 deletions internal/tunnel/udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,21 @@ import (
"github.com/yosebyte/passport/pkg/log"
)

func ServeUDP(parsedURL *url.URL, whiteList *sync.Map, linkAddr *net.TCPAddr, targetAddr *net.UDPAddr, linkListen *net.TCPListener, linkConn *net.TCPConn, mu *sync.Mutex) error {
func ServeUDP(parsedURL *url.URL, whiteList *sync.Map, linkAddr *net.TCPAddr, targetAddr *net.UDPAddr, linkListen *net.TCPListener, linkConn *net.TCPConn) error {
targetConn, err := net.ListenUDP("udp", targetAddr)
if err != nil {
log.Error("Unable to listen target address: [%v]", targetAddr)
return err
}
defer targetConn.Close()
var mu sync.Mutex
semaphore := make(chan struct{}, 1024)
for {
buffer := make([]byte, 8192)
n, clientAddr, err := targetConn.ReadFromUDP(buffer)
if err != nil {
log.Error("Unable to read from client address: [%v] %v", clientAddr, err)
continue
break
}
if parsedURL.Fragment != "" {
clientIP := clientAddr.IP.String()
Expand Down Expand Up @@ -65,4 +66,5 @@ func ServeUDP(parsedURL *url.URL, whiteList *sync.Map, linkAddr *net.TCPAddr, ta
log.Info("Transfer completed successfully")
}(buffer, n, clientAddr)
}
return nil
}

0 comments on commit 8d0bfb5

Please sign in to comment.