Skip to content

Commit

Permalink
added -gool
Browse files Browse the repository at this point in the history
  • Loading branch information
uoosef committed Jan 30, 2024
1 parent ed853cd commit 72ad373
Show file tree
Hide file tree
Showing 6 changed files with 340 additions and 56 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/Psiphon-Labs/psiphon-tunnel-core v0.0.0-00010101000000-000000000000
github.com/bepass-org/proxy v0.0.0-20240103080554-a7e12466f91f
github.com/go-ini/ini v1.67.0
github.com/refraction-networking/conjure v0.7.10-0.20231110193225-e4749a9dedc9
github.com/refraction-networking/utls v1.3.3
golang.org/x/crypto v0.18.0
golang.org/x/net v0.17.0
Expand Down Expand Up @@ -58,7 +59,6 @@ require (
github.com/pion/transport/v2 v2.2.3 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/quic-go/qpack v0.4.0 // indirect
github.com/refraction-networking/conjure v0.7.10-0.20231110193225-e4749a9dedc9 // indirect
github.com/refraction-networking/ed25519 v0.1.2 // indirect
github.com/refraction-networking/gotapdance v1.7.7 // indirect
github.com/refraction-networking/obfs4 v0.1.2 // indirect
Expand Down
193 changes: 152 additions & 41 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package main

import (
"context"
"flag"
"github.com/uoosef/wireguard-go/device"
"fmt"
"github.com/uoosef/wireguard-go/psiphon"
"github.com/uoosef/wireguard-go/warp"
"github.com/uoosef/wireguard-go/wiresocks"
"log"
"net"
"os"
"os/signal"
"path/filepath"
"syscall"
)

Expand All @@ -23,80 +23,141 @@ func main() {
var (
verbose = flag.Bool("v", false, "verbose")
bindAddress = flag.String("b", "127.0.0.1:8086", "socks bind address")
configFile = flag.String("c", "./wgcf-profile.ini", "ini config file path")
endpoint = flag.String("e", "notset", "warp clean ip")
license = flag.String("k", "notset", "license key")
country = flag.String("country", "", "psiphon country code in ISO 3166-1 alpha-2 format")
psiphonEnabled = flag.Bool("cfon", false, "enable psiphonEnabled over warp")
pbind = "127.0.0.1:8086"
psiphonCtx context.Context
gool = flag.Bool("gool", false, "enable warp gooling")
)

flag.Usage = usage
flag.Parse()

wiresocks.Verbose = *verbose
// check if user input is not correct
if (*psiphonEnabled && *gool) || (!*psiphonEnabled && *country != "") {
log.Println("Wrong command!")
flag.Usage()
return
}

if *psiphonEnabled {
pbind = *bindAddress
randomBind, err := findFreePort()
if err != nil {
log.Fatal("unable to find a free port :/")
}
bindAddress = &randomBind
//create necessary file structures
makeDirs()

//create identities
createPrimaryAndSecondaryIdentities(*license)

//Decide Working Scenario

if !*psiphonEnabled && !*gool {
// just run primary warp on bindAddress
runWarp(*bindAddress, *endpoint, "./primary/wgcf-profile.ini", *verbose, true)
} else if *psiphonEnabled && !*gool {
// run primary warp on a random tcp port and run psiphon on bind address
runWarpWithPsiphon(*bindAddress, *endpoint, *country, *verbose)
} else if !*psiphonEnabled && *gool {
// run warp in warp
runWarpInWarp(*bindAddress, *endpoint, *verbose)
}

// check if wgcf-profile.conf exists
if !warp.CheckProfileExists(*license) {
if *license == "notset" {
*license = ""
}
err := warp.LoadOrCreateIdentity(*license)
if err != nil {
log.Fatalf("error: %v", err)
}
//End Decide Working Scenario

// back where you where
if err := os.Chdir(".."); err != nil {
log.Fatal("Error changing to 'main' directory:", err)
}
}

func runWarp(bindAddress, endpoint, confPath string, verbose, wait bool) {
// Setup channel to listen for interrupt signal (Ctrl+C)
var sigChan chan os.Signal
if wait {
sigChan = make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
}

conf, err := wiresocks.ParseConfig(*configFile, *endpoint)
conf, err := wiresocks.ParseConfig(confPath, endpoint)
if err != nil {
log.Fatal(err)
}

logLevel := device.LogLevelVerbose
if !*verbose {
logLevel = device.LogLevelSilent
tnet, err := wiresocks.StartWireguard(conf.Device, verbose)
if err != nil {
log.Fatal(err)
}

go tnet.StartProxy(bindAddress)

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

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

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

// Setup channel to listen for interrupt signal (Ctrl+C)
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)

tnet, err := wiresocks.StartWireguard(conf.Device, logLevel)
// run psiphon
psiphonCtx := psiphon.RunPsiphon(warpBindAddress, bindAddress, country)

// Wait for interrupt signal
<-sigChan

psiphonCtx.Done()
}

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

go wiresocks.StartProxy(tnet, *bindAddress)
// run secondary warp
runWarp(warpBindAddress, endpoint, "./secondary/wgcf-profile.ini", verbose, false)

if *psiphonEnabled {
psiphonCtx = psiphon.RunPsiphon(*bindAddress, pbind, *country)
} else {
log.Println("Wiresocks started successfully")
// run virtual endpoint
virtualEndpointBindAddress, err := findFreePort("udp")
if err != nil {
log.Fatal("There are no free udp ports on Device!")
}

// Wait for interrupt signal
<-sigChan

if *psiphonEnabled {
psiphonCtx.Done()
f, err := wiresocks.NewSocks5UDPForwarder(virtualEndpointBindAddress, warpBindAddress, "162.159.195.1:2408")
if err != nil {
log.Fatal(err)
}
f.Start()

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

func findFreePort() (string, error) {
func findFreePort(network string) (string, error) {
if network == "udp" {
addr, err := net.ResolveUDPAddr("udp", "127.0.0.1:0")
if err != nil {
return "", err
}

conn, err := net.ListenUDP("udp", addr)
if err != nil {
return "", err
}
defer conn.Close()

return conn.LocalAddr().(*net.UDPAddr).String(), nil
}
// Listen on TCP port 0, which tells the OS to pick a free port.
listener, err := net.Listen("tcp", "127.0.0.1:0")
listener, err := net.Listen(network, "127.0.0.1:0")
if err != nil {
return "", err // Return error if unable to listen on a port
}
Expand All @@ -107,3 +168,53 @@ func findFreePort() (string, error) {

return addr, nil
}

func createPrimaryAndSecondaryIdentities(license string) {
// make primary identity
warp.UpdatePath("./primary")
if !warp.CheckProfileExists(license) {
err := warp.LoadOrCreateIdentity(license)
if err != nil {
log.Fatalf("error: %v", err)
}
}
// make secondary
warp.UpdatePath("./secondary")
if !warp.CheckProfileExists(license) {
err := warp.LoadOrCreateIdentity(license)
if err != nil {
log.Fatalf("error: %v", err)
}
}
}

func makeDirs() {
stuffDir := "stuff"
primaryDir := "primary"
secondaryDir := "secondary"

// Check if 'stuff' directory exists, if not create it
if _, err := os.Stat(stuffDir); os.IsNotExist(err) {
fmt.Println("'stuff' directory does not exist, creating it...")
if err := os.Mkdir(stuffDir, 0755); err != nil {
log.Fatal("Error creating 'stuff' directory:", err)
}
}

// Create 'primary' and 'secondary' directories if they don't exist
for _, dir := range []string{primaryDir, secondaryDir} {
if _, err := os.Stat(filepath.Join(stuffDir, dir)); os.IsNotExist(err) {
log.Printf("Creating '%s' directory...\n", dir)
if err := os.Mkdir(filepath.Join(stuffDir, dir), 0755); err != nil {
log.Fatalf("Error creating '%s' directory: %v\n", dir, err)
}
}
}
log.Println("'primary' and 'secondary' directories are ready")

// Change the current working directory to 'stuff'
if err := os.Chdir(stuffDir); err != nil {
log.Fatal("Error changing to 'stuff' directory:", err)
}
log.Println("Changed working directory to 'stuff'")
}
23 changes: 16 additions & 7 deletions warp/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@ import (
)

const (
apiVersion = "v0a1922"
apiURL = "https://api.cloudflareclient.com"
regURL = apiURL + "/" + apiVersion + "/reg"
identityFile = "./wgcf-identity.json"
profileFile = "./wgcf-profile.ini"
apiVersion = "v0a1922"
apiURL = "https://api.cloudflareclient.com"
regURL = apiURL + "/" + apiVersion + "/reg"
_identityFile = "wgcf-identity.json"
_profileFile = "wgcf-profile.ini"
)

var (
identityFile = "wgcf-identity.json"
profileFile = "wgcf-profile.ini"
)

var defaultHeaders = makeDefaultHeaders()
Expand Down Expand Up @@ -569,10 +574,14 @@ func removeFile(f string) {
if e != nil {
log.Fatal(e)
}
} else {
log.Printf("file %s is not exist!", f)
}
}

func UpdatePath(path string) {
identityFile = path + "/" + _identityFile
profileFile = path + "/" + _profileFile
}

func CheckProfileExists(license string) bool {
isOk := true
if !fileExist(identityFile) || !fileExist(profileFile) {
Expand Down
11 changes: 5 additions & 6 deletions wiresocks/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,22 @@ import (
type VirtualTun struct {
Tnet *netstack.Net
SystemDNS bool
Verbose bool
}

var Verbose bool

// StartProxy spawns a socks5 server.
func StartProxy(vt *VirtualTun, bindAddress string) {
func (vt *VirtualTun) StartProxy(bindAddress string) {
proxy := mixed.NewProxy(
mixed.WithBinAddress(bindAddress),
mixed.WithUserHandler(func(request *statute.ProxyRequest) error {
return generalHandler(request, vt)
return vt.generalHandler(request)
}),
)
_ = proxy.ListenAndServe()
}

func generalHandler(req *statute.ProxyRequest, vt *VirtualTun) error {
if Verbose {
func (vt *VirtualTun) generalHandler(req *statute.ProxyRequest) error {
if vt.Verbose {
log.Println(fmt.Sprintf("handling %s request to %s", req.Network, req.Destination))
}
conn, err := vt.Tnet.Dial(req.Network, req.Destination)
Expand Down
Loading

0 comments on commit 72ad373

Please sign in to comment.