@@ -6,10 +6,12 @@ import (
6
6
"fmt"
7
7
"log/slog"
8
8
"net/netip"
9
+ "path"
9
10
10
11
"github.com/bepass-org/warp-plus/psiphon"
11
12
"github.com/bepass-org/warp-plus/warp"
12
13
"github.com/bepass-org/warp-plus/wiresocks"
14
+ "github.com/go-ini/ini"
13
15
)
14
16
15
17
const singleMTU = 1330
@@ -22,6 +24,7 @@ type WarpOptions struct {
22
24
Psiphon * PsiphonOptions
23
25
Gool bool
24
26
Scan * wiresocks.ScanOptions
27
+ CacheDir string
25
28
}
26
29
27
30
type PsiphonOptions struct {
@@ -38,14 +41,25 @@ func RunWarp(ctx context.Context, l *slog.Logger, opts WarpOptions) error {
38
41
}
39
42
40
43
// create identities
41
- if err := createPrimaryAndSecondaryIdentities (l .With ("subsystem" , "warp/account" ), opts . License ); err != nil {
44
+ if err := createPrimaryAndSecondaryIdentities (l .With ("subsystem" , "warp/account" ), opts ); err != nil {
42
45
return err
43
46
}
44
47
45
48
// Decide Working Scenario
46
49
endpoints := []string {opts .Endpoint , opts .Endpoint }
47
50
48
51
if opts .Scan != nil {
52
+ cfg , err := ini .Load (path .Join (opts .CacheDir , "primary" , "wgcf-profile.ini" ))
53
+ if err != nil {
54
+ return fmt .Errorf ("failed to read file: %w" , err )
55
+ }
56
+
57
+ // Reading the private key from the 'Interface' section
58
+ opts .Scan .PrivateKey = cfg .Section ("Interface" ).Key ("PrivateKey" ).String ()
59
+
60
+ // Reading the public key from the 'Peer' section
61
+ opts .Scan .PublicKey = cfg .Section ("Peer" ).Key ("PublicKey" ).String ()
62
+
49
63
res , err := wiresocks .RunScan (ctx , l , * opts .Scan )
50
64
if err != nil {
51
65
return err
@@ -65,22 +79,22 @@ func RunWarp(ctx context.Context, l *slog.Logger, opts WarpOptions) error {
65
79
case opts .Psiphon != nil :
66
80
l .Info ("running in Psiphon (cfon) mode" )
67
81
// run primary warp on a random tcp port and run psiphon on bind address
68
- warpErr = runWarpWithPsiphon (ctx , l , opts . Bind , endpoints [0 ], opts . Psiphon . Country )
82
+ warpErr = runWarpWithPsiphon (ctx , l , opts , endpoints [0 ])
69
83
case opts .Gool :
70
84
l .Info ("running in warp-in-warp (gool) mode" )
71
85
// run warp in warp
72
- warpErr = runWarpInWarp (ctx , l , opts . Bind , endpoints )
86
+ warpErr = runWarpInWarp (ctx , l , opts , endpoints )
73
87
default :
74
88
l .Info ("running in normal warp mode" )
75
89
// just run primary warp on bindAddress
76
- warpErr = runWarp (ctx , l , opts . Bind , endpoints [0 ])
90
+ warpErr = runWarp (ctx , l , opts , endpoints [0 ])
77
91
}
78
92
79
93
return warpErr
80
94
}
81
95
82
- func runWarp (ctx context.Context , l * slog.Logger , bind netip. AddrPort , endpoint string ) error {
83
- conf , err := wiresocks .ParseConfig ("./stuff/ primary/ wgcf-profile.ini" , endpoint )
96
+ func runWarp (ctx context.Context , l * slog.Logger , opts WarpOptions , endpoint string ) error {
97
+ conf , err := wiresocks .ParseConfig (path . Join ( opts . CacheDir , " primary" , " wgcf-profile.ini") , endpoint )
84
98
if err != nil {
85
99
return err
86
100
}
@@ -97,18 +111,18 @@ func runWarp(ctx context.Context, l *slog.Logger, bind netip.AddrPort, endpoint
97
111
return err
98
112
}
99
113
100
- _ , err = tnet .StartProxy (bind )
114
+ _ , err = tnet .StartProxy (opts . Bind )
101
115
if err != nil {
102
116
return err
103
117
}
104
118
105
- l .Info ("serving proxy" , "address" , bind )
119
+ l .Info ("serving proxy" , "address" , opts . Bind )
106
120
107
121
return nil
108
122
}
109
123
110
- func runWarpWithPsiphon (ctx context.Context , l * slog.Logger , bind netip. AddrPort , endpoint string , country string ) error {
111
- conf , err := wiresocks .ParseConfig ("./stuff/ primary/ wgcf-profile.ini" , endpoint )
124
+ func runWarpWithPsiphon (ctx context.Context , l * slog.Logger , opts WarpOptions , endpoint string ) error {
125
+ conf , err := wiresocks .ParseConfig (path . Join ( opts . CacheDir , " primary" , " wgcf-profile.ini") , endpoint )
112
126
if err != nil {
113
127
return err
114
128
}
@@ -131,19 +145,19 @@ func runWarpWithPsiphon(ctx context.Context, l *slog.Logger, bind netip.AddrPort
131
145
}
132
146
133
147
// run psiphon
134
- err = psiphon .RunPsiphon (ctx , l .With ("subsystem" , "psiphon" ), warpBind .String (), bind . String (), country )
148
+ err = psiphon .RunPsiphon (ctx , l .With ("subsystem" , "psiphon" ), warpBind .String (), opts . Bind . String (), opts . Psiphon . Country )
135
149
if err != nil {
136
150
return fmt .Errorf ("unable to run psiphon %w" , err )
137
151
}
138
152
139
- l .Info ("serving proxy" , "address" , bind )
153
+ l .Info ("serving proxy" , "address" , opts . Bind )
140
154
141
155
return nil
142
156
}
143
157
144
- func runWarpInWarp (ctx context.Context , l * slog.Logger , bind netip. AddrPort , endpoints []string ) error {
158
+ func runWarpInWarp (ctx context.Context , l * slog.Logger , opts WarpOptions , endpoints []string ) error {
145
159
// Run outer warp
146
- conf , err := wiresocks .ParseConfig ("./stuff/ primary/ wgcf-profile.ini" , endpoints [0 ])
160
+ conf , err := wiresocks .ParseConfig (path . Join ( opts . CacheDir , " primary" , " wgcf-profile.ini") , endpoints [0 ])
147
161
if err != nil {
148
162
return err
149
163
}
@@ -167,7 +181,7 @@ func runWarpInWarp(ctx context.Context, l *slog.Logger, bind netip.AddrPort, end
167
181
}
168
182
169
183
// Run inner warp
170
- conf , err = wiresocks .ParseConfig ("./stuff/ secondary/ wgcf-profile.ini" , addr .String ())
184
+ conf , err = wiresocks .ParseConfig (path . Join ( opts . CacheDir , " secondary" , " wgcf-profile.ini") , addr .String ())
171
185
if err != nil {
172
186
return err
173
187
}
@@ -183,25 +197,25 @@ func runWarpInWarp(ctx context.Context, l *slog.Logger, bind netip.AddrPort, end
183
197
return err
184
198
}
185
199
186
- _ , err = tnet .StartProxy (bind )
200
+ _ , err = tnet .StartProxy (opts . Bind )
187
201
if err != nil {
188
202
return err
189
203
}
190
204
191
- l .Info ("serving proxy" , "address" , bind )
205
+ l .Info ("serving proxy" , "address" , opts . Bind )
192
206
return nil
193
207
}
194
208
195
- func createPrimaryAndSecondaryIdentities (l * slog.Logger , license string ) error {
209
+ func createPrimaryAndSecondaryIdentities (l * slog.Logger , opts WarpOptions ) error {
196
210
// make primary identity
197
- err := warp .LoadOrCreateIdentity (l , "./stuff/ primary", license )
211
+ err := warp .LoadOrCreateIdentity (l , path . Join ( opts . CacheDir , " primary"), opts . License )
198
212
if err != nil {
199
213
l .Error ("couldn't load primary warp identity" )
200
214
return err
201
215
}
202
216
203
217
// make secondary
204
- err = warp .LoadOrCreateIdentity (l , "./stuff/ secondary", license )
218
+ err = warp .LoadOrCreateIdentity (l , path . Join ( opts . CacheDir , " secondary"), opts . License )
205
219
if err != nil {
206
220
l .Error ("couldn't load secondary warp identity" )
207
221
return err
0 commit comments