Skip to content

Commit

Permalink
Verify tunnels connectivity
Browse files Browse the repository at this point in the history
  • Loading branch information
mtojek committed Mar 11, 2019
1 parent ae6e9d6 commit c6c31b6
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 20 deletions.
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,9 @@ Fetch active French proxy servers:

```
$ ohlavpn -c fr -l 5 -g
Login: user-uuid-339e68ea036644a7987307a4ae965713, Password: 97a5ae965312
https 158.255.215.125:22222 AS39326 HighSpeed Office Limited Paris France Edis France EDIS GmbH Île-de-France 75001
https 178.32.172.248:22222 AS16276 OVH SAS Paris France OVH ISP CO LTD MAXSERVER Île-de-France 75000
https 193.70.63.13:22222 AS16276 OVH SAS Gravelines France OVH OVH SAS Hauts-de-France 59820
https 51.255.175.61:22222 AS16276 OVH SAS Strasbourg France OVH SAS OVH Grand Est 67000
https 178.32.227.14:22222 AS16276 OVH SAS Roubaix France OVH SAS CO LTD MAXSERVER Hauts-de-France 59100
https://user-uuid-2188652440aa480b9b789bc8ba5a2882:[email protected]:22222 AS14061 DigitalOcean, LLC Clifton United States DigitalOcean, LLC Digital Ocean New Jersey 07014
https://user-uuid-2188652440aa480b9b789bc8ba5a2882:[email protected]:22222 AS20454 SECURED SERVERS LLC Tempe United States Secured Servers LLC Secured Servers LLC Arizona 85281
https://user-uuid-2188652440aa480b9b789bc8ba5a2882:[email protected]:22222 AS20454 SECURED SERVERS LLC Phoenix United States Secured Servers LLC Dolorem Ipsum, s.r.o Arizona 85001
https://user-uuid-2188652440aa480b9b789bc8ba5a2882:[email protected]:22222 AS20454 SECURED SERVERS LLC Tempe United States Secured Servers LLC Secured Servers LLC Arizona 85281
https://user-uuid-2188652440aa480b9b789bc8ba5a2882:[email protected]:22222 AS20454 SECURED SERVERS LLC Tempe United States Secured Servers LLC Secured Servers LLC Arizona 85281
```
26 changes: 23 additions & 3 deletions ipapi/client.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package ipapi

import (
"crypto/tls"
"encoding/json"
"fmt"
"net/http"
"net/url"
"time"
)

const baseURI = "http://ip-api.com/json"

// Client is responsible for calling IP-API.
type Client struct{}
type Client struct {
httpClient *http.Client
}

// GeoIPData stores geolocation data.
type GeoIPData struct {
Expand All @@ -30,12 +35,27 @@ func (g *GeoIPData) String() string {

// NewClient creates new instance of the IP-API client.
func NewClient() *Client {
return new(Client)
return &Client{
httpClient: http.DefaultClient,
}
}

// WithProxy method uses given proxy server to tunnel HTTP requests to IP-API.
func (c *Client) WithProxy(anURL *url.URL) *Client {
pu := http.ProxyURL(anURL)
c.httpClient = &http.Client{
Transport: &http.Transport{
Proxy: pu,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
Timeout: 5 * time.Second,
}
return c
}

// GeoIP method returns geolocation data.
func (c *Client) GeoIP(ip string) (*GeoIPData, error) {
response, err := http.Get(baseURI + "/" + ip)
response, err := c.httpClient.Get(baseURI + "/" + ip)
if err != nil {
return nil, err
}
Expand Down
10 changes: 7 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ func main() {
var countryCode string
var limit int
var geoIP bool
var verify bool

flag.StringVar(&countryCode, "c", "us", "country code")
flag.IntVar(&limit, "l", 5, "proxy server limit")
flag.BoolVar(&geoIP, "g", false, "check GeoIP data")
flag.BoolVar(&verify, "v", false, "verify proxy connectivity")
flag.Parse()

ipAPIClient := ipapi.NewClient()
vpnClient := vpn.NewClient()
err := vpnClient.Initialize()
if err != nil {
Expand All @@ -35,12 +36,15 @@ func main() {
log.Fatal("No proxy servers found")
}

fmt.Printf("Login: %s, Password: %s\n\n", tunnels.Login, tunnels.Password)

for _, tunnel := range tunnels.Servers {
fmt.Print(tunnel.String())

if geoIP {
ipAPIClient := ipapi.NewClient()
if verify {
ipAPIClient = ipAPIClient.WithProxy(tunnel.URL())
}

geoIPData, err := ipAPIClient.GeoIP(tunnel.Host)
if err != nil {
fmt.Printf("\terror checking GeoIP data: %v", err)
Expand Down
28 changes: 21 additions & 7 deletions vpn/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package vpn
import (
"encoding/json"
"fmt"
"log"
"net/http"
"net/url"
"strings"
Expand All @@ -26,22 +27,31 @@ type Client struct {

// Tunnels stores information about proxy servers
type Tunnels struct {
Login string
Password string

Servers []TunnelSettings
}

// TunnelSettings stores tunnel related settings.
type TunnelSettings struct {
Login string
Password string

Host string
Port string
Proto string
}

// String method returns string representative of the struct.
func (ts *TunnelSettings) String() string {
return fmt.Sprintf("%s\t%s:%s", ts.Proto, ts.Host, ts.Port)
return fmt.Sprintf("%s://%s:%s@%s:%s", ts.Proto, ts.Login, ts.Password, ts.Host, ts.Port)
}

// URL method returns URI representative of the struct.
func (ts *TunnelSettings) URL() *url.URL {
proxyURL, err := url.Parse(ts.String())
if err != nil {
log.Fatalf("URL must be parsed: %v", err)
}
return proxyURL
}

type initializeResponse struct {
Expand Down Expand Up @@ -100,10 +110,11 @@ func (c *Client) FindTunnels(countryCode string, limit int) (*Tunnels, error) {
return nil, err
}

login := fmt.Sprintf("user-uuid-%s", c.uuid)
password := r.AgentKey

tunnels := &Tunnels{
Login: fmt.Sprintf("user-uuid-%s", c.uuid),
Password: r.AgentKey,
Servers: []TunnelSettings{},
Servers: []TunnelSettings{},
}

proxyEndpoints, ok := r.Ztun[countryCode]
Expand All @@ -127,6 +138,9 @@ func (c *Client) FindTunnels(countryCode string, limit int) (*Tunnels, error) {
}

tunnels.Servers = append(tunnels.Servers, TunnelSettings{
Login: login,
Password: password,

Host: ipAddress,
Port: port,
Proto: protocol,
Expand Down

0 comments on commit c6c31b6

Please sign in to comment.