Skip to content

Commit

Permalink
Minor optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
yosebyte authored Dec 9, 2024
1 parent 6c7d668 commit 0c8cd22
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 26 deletions.
4 changes: 2 additions & 2 deletions cmd/passport/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"sync"
"time"

"github.com/yosebyte/passport/internal/auth"
"github.com/yosebyte/passport/internal"
"github.com/yosebyte/passport/pkg/log"
)

Expand All @@ -20,7 +20,7 @@ func authSetups(parsedURL *url.URL, whiteList *sync.Map) {
log.Info("Auth mode enabled: %v", parsedAuthURL)
go func() {
for {
if err := auth.HandleHTTP(parsedAuthURL, whiteList); err != nil {
if err := internal.HandleHTTP(parsedAuthURL, whiteList); err != nil {
log.Error("Auth mode error: %v", err)
log.Info("Restarting in 1s...")
time.Sleep(1 * time.Second)
Expand Down
2 changes: 1 addition & 1 deletion cmd/passport/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

var (
version = "dev"
version = "dev"
whiteList sync.Map
)

Expand Down
8 changes: 4 additions & 4 deletions internal/const.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package internal

const (
MaxSemaphore = 1024
MaxBufferSize = 8192
MinBufferSize = 1024
MaxUDPDeadline = 5
MaxSemaphoreLimit = 1024
MaxSignalBuffer = 1024
MaxDataBuffer = 8192
MaxUDPTimeout = 5
)
6 changes: 3 additions & 3 deletions internal/forward/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ func HandleTCP(parsedURL *url.URL, whiteList *sync.Map) error {
return err
}
defer linkListen.Close()
semaphore := make(chan struct{}, internal.MaxSemaphore)
sem := make(chan struct{}, internal.MaxSemaphoreLimit)
for {
linkConn, err := linkListen.AcceptTCP()
if err != nil {
log.Error("Unable to connect link address: [%v] %v", linkAddr, err)
time.Sleep(1 * time.Second)
continue
}
semaphore <- struct{}{}
sem <- struct{}{}
go func(linkConn *net.TCPConn) {
defer func() { <-semaphore }()
defer func() { <-sem }()
clientAddr := linkConn.RemoteAddr().String()
log.Info("Client connection established: [%v]", clientAddr)
if parsedURL.Fragment != "" {
Expand Down
10 changes: 5 additions & 5 deletions internal/forward/udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ func HandleUDP(parsedURL *url.URL, whiteList *sync.Map) error {
return err
}
defer linkConn.Close()
semaphore := make(chan struct{}, internal.MaxSemaphore)
sem := make(chan struct{}, internal.MaxSemaphoreLimit)
for {
buffer := make([]byte, internal.MaxBufferSize)
buffer := make([]byte, internal.MaxDataBuffer)
n, clientAddr, err := linkConn.ReadFromUDP(buffer)
if err != nil {
log.Error("Unable to read from client address: [%v] %v", clientAddr, err)
Expand All @@ -43,17 +43,17 @@ func HandleUDP(parsedURL *url.URL, whiteList *sync.Map) error {
continue
}
}
semaphore <- struct{}{}
sem <- struct{}{}
go func(buffer []byte, n int, clientAddr *net.UDPAddr) {
defer func() { <-semaphore }()
defer func() { <-sem }()
targetConn, err := net.DialUDP("udp", nil, targetAddr)
if err != nil {
log.Error("Unable to dial target address: [%v] %v", targetAddr, err)
return
}
defer targetConn.Close()
log.Info("Target connection established: [%v]", targetAddr)
err = targetConn.SetDeadline(time.Now().Add(internal.MaxUDPDeadline * time.Second))
err = targetConn.SetDeadline(time.Now().Add(internal.MaxUDPTimeout * time.Second))
if err != nil {
log.Error("Unable to set deadline: %v", err)
return
Expand Down
2 changes: 1 addition & 1 deletion internal/auth/http.go → internal/http.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package auth
package internal

import (
"net"
Expand Down
2 changes: 1 addition & 1 deletion internal/tunnel/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func Client(parsedURL *url.URL) error {
}
defer linkConn.Close()
log.Info("Tunnel connection established to: [%v]", linkAddr)
buffer := make([]byte, internal.MinBufferSize)
buffer := make([]byte, internal.MaxSignalBuffer)
for {
n, err := linkConn.Read(buffer)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions internal/tunnel/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func ServeTCP(parsedURL *url.URL, whiteList *sync.Map, linkAddr, targetAddr *net
}
defer targetListen.Close()
var mu sync.Mutex
semaphore := make(chan struct{}, internal.MaxSemaphore)
sem := make(chan struct{}, internal.MaxSemaphoreLimit)
for {
targetConn, err := targetListen.AcceptTCP()
if err != nil {
Expand All @@ -40,10 +40,10 @@ func ServeTCP(parsedURL *url.URL, whiteList *sync.Map, linkAddr, targetAddr *net
continue
}
}
semaphore <- struct{}{}
sem <- struct{}{}
go func(targetConn *net.TCPConn) {
defer func() {
<-semaphore
<-sem
targetConn.Close()
}()
mu.Lock()
Expand Down
12 changes: 6 additions & 6 deletions internal/tunnel/udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ func ServeUDP(parsedURL *url.URL, whiteList *sync.Map, linkAddr *net.TCPAddr, ta
}
defer targetConn.Close()
var mu sync.Mutex
semaphore := make(chan struct{}, internal.MaxSemaphore)
sem := make(chan struct{}, internal.MaxSemaphoreLimit)
for {
buffer := make([]byte, internal.MaxBufferSize)
buffer := make([]byte, internal.MaxDataBuffer)
n, clientAddr, err := targetConn.ReadFromUDP(buffer)
if err != nil {
log.Error("Unable to read from client address: [%v] %v", clientAddr, err)
Expand All @@ -45,10 +45,10 @@ func ServeUDP(parsedURL *url.URL, whiteList *sync.Map, linkAddr *net.TCPAddr, ta
log.Error("Unable to accept connections from link address: [%v] %v", linkAddr, err)
continue
}
semaphore <- struct{}{}
sem <- struct{}{}
go func(buffer []byte, n int, remoteConn *net.TCPConn, clientAddr *net.UDPAddr) {
defer func() {
<-semaphore
<-sem
remoteConn.Close()
}()
log.Info("Starting data transfer: [%v] <-> [%v]", clientAddr, targetAddr)
Expand Down Expand Up @@ -81,7 +81,7 @@ func ClientUDP(linkAddr *net.TCPAddr, targetUDPAddr *net.UDPAddr) {
}
defer remoteConn.Close()
log.Info("Remote connection established: [%v]", linkAddr)
buffer := make([]byte, internal.MaxBufferSize)
buffer := make([]byte, internal.MaxDataBuffer)
n, err := remoteConn.Read(buffer)
if err != nil {
log.Error("Unable to read from remote address: [%v] %v", linkAddr, err)
Expand All @@ -94,7 +94,7 @@ func ClientUDP(linkAddr *net.TCPAddr, targetUDPAddr *net.UDPAddr) {
}
defer targetConn.Close()
log.Info("Target connection established: [%v]", targetUDPAddr)
err = targetConn.SetDeadline(time.Now().Add(internal.MaxUDPDeadline * time.Second))
err = targetConn.SetDeadline(time.Now().Add(internal.MaxUDPTimeout * time.Second))
if err != nil {
log.Error("Unable to set deadline: %v", err)
return
Expand Down

0 comments on commit 0c8cd22

Please sign in to comment.