Skip to content

Commit

Permalink
improve warp in warp performance
Browse files Browse the repository at this point in the history
  • Loading branch information
uoosef committed Jan 31, 2024
1 parent 6f5552c commit fa312c6
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 17 deletions.
28 changes: 11 additions & 17 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func main() {

if !*psiphonEnabled && !*gool {
// just run primary warp on bindAddress
runWarp(*bindAddress, *endpoint, "./primary/wgcf-profile.ini", *verbose, true)
runWarp(*bindAddress, *endpoint, "./primary/wgcf-profile.ini", *verbose, true, true)
} else if *psiphonEnabled && !*gool {
// run primary warp on a random tcp port and run psiphon on bind address
runWarpWithPsiphon(*bindAddress, *endpoint, *country, *verbose)
Expand All @@ -68,7 +68,7 @@ func main() {
}
}

func runWarp(bindAddress, endpoint, confPath string, verbose, wait bool) {
func runWarp(bindAddress, endpoint, confPath string, verbose, wait bool, startProxy bool) (*wiresocks.VirtualTun, int) {
// Setup channel to listen for interrupt signal (Ctrl+C)
var sigChan chan os.Signal
if wait {
Expand All @@ -86,12 +86,16 @@ func runWarp(bindAddress, endpoint, confPath string, verbose, wait bool) {
log.Fatal(err)
}

go tnet.StartProxy(bindAddress)
if startProxy {
go tnet.StartProxy(bindAddress)
}

// Wait for interrupt signal
if wait {
<-sigChan
}

return tnet, conf.Device.MTU
}

func runWarpWithPsiphon(bindAddress, endpoint, country string, verbose bool) {
Expand All @@ -101,7 +105,7 @@ func runWarpWithPsiphon(bindAddress, endpoint, country string, verbose bool) {
log.Fatal("There are no free tcp ports on Device!")
}

runWarp(warpBindAddress, endpoint, "./primary/wgcf-profile.ini", verbose, false)
runWarp(warpBindAddress, endpoint, "./primary/wgcf-profile.ini", verbose, false, true)

// Setup channel to listen for interrupt signal (Ctrl+C)
sigChan := make(chan os.Signal, 1)
Expand All @@ -117,32 +121,22 @@ func runWarpWithPsiphon(bindAddress, endpoint, country string, verbose bool) {
}

func runWarpInWarp(bindAddress, endpoint string, verbose bool) {
// make a random bind address for secondary warp
warpBindAddress, err := findFreePort("tcp")
if err != nil {
log.Fatal("There are no free tcp ports on Device!")
}

// run secondary warp
runWarp(warpBindAddress, endpoint, "./secondary/wgcf-profile.ini", verbose, false)

// wait for secondary warp
waitForPortToGetsOpenOrTimeout(warpBindAddress)
vTUN, mtu := runWarp("", endpoint, "./secondary/wgcf-profile.ini", verbose, false, false)

// run virtual endpoint
virtualEndpointBindAddress, err := findFreePort("udp")
if err != nil {
log.Fatal("There are no free udp ports on Device!")
}

f, err := wiresocks.NewSocks5UDPForwarder(virtualEndpointBindAddress, warpBindAddress, "162.159.195.1:2408")
err = wiresocks.NewVtunUDPForwarder(virtualEndpointBindAddress, "162.159.195.1:2408", vTUN, mtu)
if err != nil {
log.Fatal(err)
}
f.Start()

// run primary warp
runWarp(bindAddress, virtualEndpointBindAddress, "./primary/wgcf-profile.ini", verbose, true)
runWarp(bindAddress, virtualEndpointBindAddress, "./primary/wgcf-profile.ini", verbose, true, true)
}

func findFreePort(network string) (string, error) {
Expand Down
44 changes: 44 additions & 0 deletions wiresocks/udpfw.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,50 @@ type Socks5UDPForwarder struct {
clientAddr *net.UDPAddr
}

func NewVtunUDPForwarder(localBind, dest string, vtun *VirtualTun, mtu int) error {
localAddr, err := net.ResolveUDPAddr("udp", localBind)
if err != nil {
return err
}

listener, err := net.ListenUDP("udp", localAddr)
if err != nil {
return err
}

rconn, err := vtun.Tnet.Dial("udp", dest)
if err != nil {
return err
}

var remoteAddr *net.UDPAddr
go func() {
buffer := make([]byte, mtu)
oob := make([]byte, mtu)
n := 0
for {
n, _, _, remoteAddr, err = listener.ReadMsgUDP(buffer, oob)
if err != nil {
continue
}

rconn.Write(buffer[:n])
}
}()
go func() {
buffer := make([]byte, mtu)
for {
n, err := rconn.Read(buffer)
if err != nil {
fmt.Println("Error reading from connection:", err)
continue
}
listener.WriteMsgUDP(buffer[:n], nil, remoteAddr)
}
}()
return nil
}

func NewSocks5UDPForwarder(localBind, socks5Server, dest string) (*Socks5UDPForwarder, error) {
localAddr, err := net.ResolveUDPAddr("udp", localBind)
if err != nil {
Expand Down

0 comments on commit fa312c6

Please sign in to comment.