forked from vysecurity/autovpn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autovpn.go
86 lines (69 loc) · 1.81 KB
/
autovpn.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
package main
import (
"encoding/base64"
"fmt"
"io/ioutil"
"net/http"
"os"
"os/exec"
"os/signal"
"strings"
"syscall"
)
func check(e error) {
if e != nil {
panic(e)
}
}
func main() {
chosenCountry := "US"
if len(os.Args) > 1 && len(os.Args[1]) == 2 {
chosenCountry = os.Args[1]
}
URL := "http://www.vpngate.net/api/iphone/"
fmt.Printf("[autovpn] getting server list\n")
response, err := http.Get(URL)
check(err)
defer response.Body.Close()
csvFile, err := ioutil.ReadAll(response.Body)
check(err)
fmt.Printf("[autovpn] parsing response\n")
fmt.Printf("[autovpn] looking for %s\n", chosenCountry)
for i, line := range strings.Split(string(csvFile), "\n") {
if i <= 1 {
continue
}
splits := strings.Split(line, ",")
if len(splits) < 15 {
break
}
country := splits[6]
conf, err := base64.StdEncoding.DecodeString(splits[14])
if err != nil || chosenCountry != country {
continue
}
fmt.Printf("[autovpn] writing config file\n")
f, err := os.Create("/tmp/openvpnconf")
check(err)
_, err = f.Write(conf)
check(err)
f.Close()
fmt.Printf("[autovpn] running openvpn\n")
cmd := exec.Command("sudo", "openvpn", "/tmp/openvpnconf")
cmd.Stdout = os.Stdout
c := make(chan os.Signal, 2)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
cmd.Process.Kill()
}()
cmd.Start()
cmd.Wait()
fmt.Printf("[autovpn] try another VPN? (y/n) ")
var input string
fmt.Scanln(&input)
if strings.ToLower(input) == "n" {
os.Exit(0)
}
}
}