-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add experimental udp tunneling method
- Loading branch information
Showing
5 changed files
with
247 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,17 @@ | ||
package tunnel | ||
|
||
import ( | ||
"net" | ||
"net/url" | ||
"strings" | ||
"sync" | ||
|
||
"github.com/yosebyte/passport/internal/util" | ||
"github.com/yosebyte/passport/pkg/log" | ||
) | ||
|
||
func Server(parsedURL *url.URL, whiteList *sync.Map) error { | ||
linkAddr, err := net.ResolveTCPAddr("tcp", parsedURL.Host) | ||
if err != nil { | ||
log.Error("Unable to resolve link address: %v", parsedURL.Host) | ||
return err | ||
} | ||
targetAddr, err := net.ResolveTCPAddr("tcp", strings.TrimPrefix(parsedURL.Path, "/")) | ||
if err != nil { | ||
log.Error("Unable to resolve target address: %v", strings.TrimPrefix(parsedURL.Path, "/")) | ||
return err | ||
} | ||
linkListen, err := net.ListenTCP("tcp", linkAddr) | ||
if err != nil { | ||
log.Error("Unable to listen link address: [%v]", linkAddr) | ||
return err | ||
} | ||
defer linkListen.Close() | ||
targetListen, err := net.ListenTCP("tcp", targetAddr) | ||
if err != nil { | ||
log.Error("Unable to listen target address: [%v]", targetAddr) | ||
return err | ||
} | ||
defer targetListen.Close() | ||
linkConn, err := linkListen.AcceptTCP() | ||
if err != nil { | ||
log.Error("Unable to accept connections form link address: [%v]", linkAddr) | ||
return err | ||
} | ||
defer linkConn.Close() | ||
log.Info("Tunnel connection established from: [%v]", linkConn.RemoteAddr().String()) | ||
var mu sync.Mutex | ||
semaphore := make(chan struct{}, 1024) | ||
for { | ||
targetConn, err := targetListen.AcceptTCP() | ||
if err != nil { | ||
log.Error("Unable to accept connections form target address: [%v] %v", targetAddr, err) | ||
break | ||
} | ||
targetConn.SetNoDelay(true) | ||
clientAddr := targetConn.RemoteAddr().String() | ||
log.Info("Target connection established from: [%v]", clientAddr) | ||
if parsedURL.Fragment != "" { | ||
clientIP, _, err := net.SplitHostPort(clientAddr) | ||
if err != nil { | ||
log.Error("Unable to extract client IP address: [%v] %v", clientAddr, err) | ||
targetConn.Close() | ||
continue | ||
} | ||
if _, exists := whiteList.Load(clientIP); !exists { | ||
log.Warn("Unauthorized IP address blocked: [%v]", clientIP) | ||
targetConn.Close() | ||
continue | ||
} | ||
} | ||
semaphore <- struct{}{} | ||
go func(targetConn *net.TCPConn) { | ||
defer func() { <-semaphore }() | ||
mu.Lock() | ||
_, err = linkConn.Write([]byte("[PASSPORT]\n")) | ||
mu.Unlock() | ||
if err != nil { | ||
log.Error("Unable to send signal: %v", err) | ||
targetConn.Close() | ||
return | ||
} | ||
remoteConn, err := linkListen.AcceptTCP() | ||
if err != nil { | ||
log.Error("Unable to accept connections form link address: [%v] %v", linkAddr, err) | ||
return | ||
} | ||
log.Info("Starting data exchange: [%v] <-> [%v]", clientAddr, targetAddr) | ||
util.HandleConn(remoteConn, targetConn) | ||
log.Info("Connection closed successfully") | ||
}(targetConn) | ||
} | ||
return nil | ||
errChan := make(chan error, 2) | ||
go func() { | ||
errChan <- ServeTCP(parsedURL, whiteList) | ||
}() | ||
go func() { | ||
errChan <- ServeUDP(parsedURL, whiteList) | ||
}() | ||
return <-errChan | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package tunnel | ||
|
||
import ( | ||
"net" | ||
"net/url" | ||
"strings" | ||
"sync" | ||
|
||
"github.com/yosebyte/passport/internal/util" | ||
"github.com/yosebyte/passport/pkg/log" | ||
) | ||
|
||
func ServeTCP(parsedURL *url.URL, whiteList *sync.Map) error { | ||
linkAddr, err := net.ResolveTCPAddr("tcp", parsedURL.Host) | ||
if err != nil { | ||
log.Error("Unable to resolve link address: %v", parsedURL.Host) | ||
return err | ||
} | ||
targetAddr, err := net.ResolveTCPAddr("tcp", strings.TrimPrefix(parsedURL.Path, "/")) | ||
if err != nil { | ||
log.Error("Unable to resolve target address: %v", strings.TrimPrefix(parsedURL.Path, "/")) | ||
return err | ||
} | ||
linkListen, err := net.ListenTCP("tcp", linkAddr) | ||
if err != nil { | ||
log.Error("Unable to listen link address: [%v]", linkAddr) | ||
return err | ||
} | ||
defer linkListen.Close() | ||
targetListen, err := net.ListenTCP("tcp", targetAddr) | ||
if err != nil { | ||
log.Error("Unable to listen target address: [%v]", targetAddr) | ||
return err | ||
} | ||
defer targetListen.Close() | ||
linkConn, err := linkListen.AcceptTCP() | ||
if err != nil { | ||
log.Error("Unable to accept connections form link address: [%v]", linkAddr) | ||
return err | ||
} | ||
defer linkConn.Close() | ||
log.Info("Tunnel connection established from: [%v]", linkConn.RemoteAddr().String()) | ||
var mu sync.Mutex | ||
semaphore := make(chan struct{}, 1024) | ||
for { | ||
targetConn, err := targetListen.AcceptTCP() | ||
if err != nil { | ||
log.Error("Unable to accept connections form target address: [%v] %v", targetAddr, err) | ||
break | ||
} | ||
clientAddr := targetConn.RemoteAddr().String() | ||
log.Info("Target connection established from: [%v]", clientAddr) | ||
if parsedURL.Fragment != "" { | ||
clientIP, _, err := net.SplitHostPort(clientAddr) | ||
if err != nil { | ||
log.Error("Unable to extract client IP address: [%v] %v", clientAddr, err) | ||
targetConn.Close() | ||
continue | ||
} | ||
if _, exists := whiteList.Load(clientIP); !exists { | ||
log.Warn("Unauthorized IP address blocked: [%v]", clientIP) | ||
targetConn.Close() | ||
continue | ||
} | ||
} | ||
semaphore <- struct{}{} | ||
go func(targetConn *net.TCPConn) { | ||
defer func() { <-semaphore }() | ||
mu.Lock() | ||
_, err = linkConn.Write([]byte("[PASSPORT]<TCP>\n")) | ||
mu.Unlock() | ||
if err != nil { | ||
log.Error("Unable to send signal: %v", err) | ||
targetConn.Close() | ||
return | ||
} | ||
remoteConn, err := linkListen.AcceptTCP() | ||
if err != nil { | ||
log.Error("Unable to accept connections form link address: [%v] %v", linkAddr, err) | ||
return | ||
} | ||
log.Info("Starting data exchange: [%v] <-> [%v]", clientAddr, targetAddr) | ||
util.HandleConn(remoteConn, targetConn) | ||
log.Info("Connection closed successfully") | ||
}(targetConn) | ||
} | ||
return nil | ||
} |
Oops, something went wrong.