Skip to content

Commit 7b6da0e

Browse files
author
uoosef
committed
fix importable package issue
1 parent d950e99 commit 7b6da0e

File tree

2 files changed

+249
-242
lines changed

2 files changed

+249
-242
lines changed

app/app.go

+247
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
package app
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"github.com/bepass-org/wireguard-go/psiphon"
7+
"github.com/bepass-org/wireguard-go/warp"
8+
"github.com/bepass-org/wireguard-go/wiresocks"
9+
"log"
10+
"net"
11+
"os"
12+
"os/signal"
13+
"path/filepath"
14+
"syscall"
15+
"time"
16+
)
17+
18+
func RunWarp(psiphonEnabled, gool, scan, verbose bool, country, bindAddress, endpoint, license string) {
19+
// check if user input is not correct
20+
if (psiphonEnabled && gool) || (!psiphonEnabled && country != "") {
21+
log.Println("Wrong command!")
22+
flag.Usage()
23+
return
24+
}
25+
26+
//create necessary file structures
27+
makeDirs()
28+
29+
//create identities
30+
createPrimaryAndSecondaryIdentities(license)
31+
32+
//Decide Working Scenario
33+
endpoints := []string{endpoint, endpoint}
34+
35+
if scan {
36+
endpoints = wiresocks.RunScan()
37+
log.Println("Cooling down please wait 5 seconds...")
38+
time.Sleep(5 * time.Second)
39+
}
40+
41+
if !psiphonEnabled && !gool {
42+
// just run primary warp on bindAddress
43+
runWarp(bindAddress, endpoints, "./primary/wgcf-profile.ini", verbose, true, true)
44+
} else if psiphonEnabled && !gool {
45+
// run primary warp on a random tcp port and run psiphon on bind address
46+
runWarpWithPsiphon(bindAddress, endpoints, country, verbose)
47+
} else if !psiphonEnabled && gool {
48+
// run warp in warp
49+
runWarpInWarp(bindAddress, endpoints, verbose)
50+
}
51+
52+
//End Decide Working Scenario
53+
54+
// back where you where
55+
if err := os.Chdir(".."); err != nil {
56+
log.Fatal("Error changing to 'main' directory:", err)
57+
}
58+
}
59+
60+
func runWarp(bindAddress string, endpoints []string, confPath string, verbose, wait bool, startProxy bool) (*wiresocks.VirtualTun, int) {
61+
// Setup channel to listen for interrupt signal (Ctrl+C)
62+
var sigChan chan os.Signal
63+
if wait {
64+
sigChan = make(chan os.Signal, 1)
65+
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
66+
}
67+
68+
conf, err := wiresocks.ParseConfig(confPath, endpoints[0])
69+
if err != nil {
70+
log.Fatal(err)
71+
}
72+
73+
tnet, err := wiresocks.StartWireguard(conf.Device, verbose)
74+
if err != nil {
75+
log.Fatal(err)
76+
}
77+
78+
if startProxy {
79+
go tnet.StartProxy(bindAddress)
80+
}
81+
82+
// Wait for interrupt signal
83+
if wait {
84+
log.Printf("Serving on %s\n", bindAddress)
85+
<-sigChan
86+
}
87+
88+
return tnet, conf.Device.MTU
89+
}
90+
91+
func runWarpWithPsiphon(bindAddress string, endpoints []string, country string, verbose bool) {
92+
// make a random bind address for warp
93+
warpBindAddress, err := findFreePort("tcp")
94+
if err != nil {
95+
log.Fatal("There are no free tcp ports on Device!")
96+
}
97+
98+
runWarp(warpBindAddress, endpoints, "./primary/wgcf-profile.ini", verbose, false, true)
99+
100+
// Setup channel to listen for interrupt signal (Ctrl+C)
101+
sigChan := make(chan os.Signal, 1)
102+
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
103+
104+
// run psiphon
105+
psiphonCtx := psiphon.RunPsiphon(warpBindAddress, bindAddress, country)
106+
107+
log.Printf("Serving on %s\n", bindAddress)
108+
// Wait for interrupt signal
109+
<-sigChan
110+
111+
psiphonCtx.Done()
112+
}
113+
114+
func runWarpInWarp(bindAddress string, endpoints []string, verbose bool) {
115+
// run secondary warp
116+
vTUN, mtu := runWarp("", endpoints, "./secondary/wgcf-profile.ini", verbose, false, false)
117+
118+
// run virtual endpoint
119+
virtualEndpointBindAddress, err := findFreePort("udp")
120+
if err != nil {
121+
log.Fatal("There are no free udp ports on Device!")
122+
}
123+
addr := endpoints[1]
124+
if addr == "notset" {
125+
addr, _ = wiresocks.ResolveIPPAndPort("engage.cloudflareclient.com:2408")
126+
}
127+
err = wiresocks.NewVtunUDPForwarder(virtualEndpointBindAddress, addr, vTUN, mtu+100)
128+
if err != nil {
129+
log.Fatal(err)
130+
}
131+
132+
// run primary warp
133+
runWarp(bindAddress, []string{virtualEndpointBindAddress}, "./primary/wgcf-profile.ini", verbose, true, true)
134+
}
135+
136+
func findFreePort(network string) (string, error) {
137+
if network == "udp" {
138+
addr, err := net.ResolveUDPAddr("udp", "127.0.0.1:0")
139+
if err != nil {
140+
return "", err
141+
}
142+
143+
conn, err := net.ListenUDP("udp", addr)
144+
if err != nil {
145+
return "", err
146+
}
147+
defer conn.Close()
148+
149+
return conn.LocalAddr().(*net.UDPAddr).String(), nil
150+
}
151+
// Listen on TCP port 0, which tells the OS to pick a free port.
152+
listener, err := net.Listen(network, "127.0.0.1:0")
153+
if err != nil {
154+
return "", err // Return error if unable to listen on a port
155+
}
156+
defer listener.Close() // Ensure the listener is closed when the function returns
157+
158+
// Get the port from the listener's address
159+
addr := listener.Addr().String()
160+
161+
return addr, nil
162+
}
163+
164+
func createPrimaryAndSecondaryIdentities(license string) {
165+
// make primary identity
166+
_license := license
167+
if _license == "notset" {
168+
_license = ""
169+
}
170+
warp.UpdatePath("./primary")
171+
if !warp.CheckProfileExists(license) {
172+
err := warp.LoadOrCreateIdentity(_license)
173+
if err != nil {
174+
log.Fatalf("error: %v", err)
175+
}
176+
}
177+
// make secondary
178+
warp.UpdatePath("./secondary")
179+
if !warp.CheckProfileExists(license) {
180+
err := warp.LoadOrCreateIdentity(_license)
181+
if err != nil {
182+
log.Fatalf("error: %v", err)
183+
}
184+
}
185+
}
186+
187+
func makeDirs() {
188+
stuffDir := "stuff"
189+
primaryDir := "primary"
190+
secondaryDir := "secondary"
191+
192+
// Check if 'stuff' directory exists, if not create it
193+
if _, err := os.Stat(stuffDir); os.IsNotExist(err) {
194+
fmt.Println("'stuff' directory does not exist, creating it...")
195+
if err := os.Mkdir(stuffDir, 0755); err != nil {
196+
log.Fatal("Error creating 'stuff' directory:", err)
197+
}
198+
}
199+
200+
// Create 'primary' and 'secondary' directories if they don't exist
201+
for _, dir := range []string{primaryDir, secondaryDir} {
202+
if _, err := os.Stat(filepath.Join(stuffDir, dir)); os.IsNotExist(err) {
203+
log.Printf("Creating '%s' directory...\n", dir)
204+
if err := os.Mkdir(filepath.Join(stuffDir, dir), 0755); err != nil {
205+
log.Fatalf("Error creating '%s' directory: %v\n", dir, err)
206+
}
207+
}
208+
}
209+
log.Println("'primary' and 'secondary' directories are ready")
210+
211+
// Change the current working directory to 'stuff'
212+
if err := os.Chdir(stuffDir); err != nil {
213+
log.Fatal("Error changing to 'stuff' directory:", err)
214+
}
215+
log.Println("Changed working directory to 'stuff'")
216+
}
217+
func isPortOpen(address string, timeout time.Duration) bool {
218+
// Try to establish a connection
219+
conn, err := net.DialTimeout("tcp", address, timeout)
220+
if err != nil {
221+
return false
222+
}
223+
defer conn.Close()
224+
225+
return true
226+
}
227+
228+
func waitForPortToGetsOpenOrTimeout(addressToCheck string) {
229+
timeout := 5 * time.Second
230+
checkInterval := 500 * time.Millisecond
231+
232+
// Set a deadline for when to stop checking
233+
deadline := time.Now().Add(timeout)
234+
235+
for {
236+
if time.Now().After(deadline) {
237+
log.Fatalf("Timeout reached, port %s is not open", addressToCheck)
238+
}
239+
240+
if isPortOpen(addressToCheck, checkInterval) {
241+
log.Printf("Port %s is now open", addressToCheck)
242+
break
243+
}
244+
245+
time.Sleep(checkInterval)
246+
}
247+
}

0 commit comments

Comments
 (0)