Skip to content
This repository has been archived by the owner on May 14, 2021. It is now read-only.

Commit

Permalink
(macOS) Kernel panic on macOS 11 beta (when Little Snitch installed)
Browse files Browse the repository at this point in the history
  • Loading branch information
stenya committed Nov 11, 2020
1 parent 7db45f0 commit d6694bf
Show file tree
Hide file tree
Showing 11 changed files with 467 additions and 152 deletions.
18 changes: 15 additions & 3 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const (
_sessionStatusPath = _apiPathPrefix + "/session/status"
_sessionDeletePath = _apiPathPrefix + "/session/delete"
_wgKeySetPath = _apiPathPrefix + "/session/wg/set"
_geoLookupPath = _apiPathPrefix + "/geo-lookup"
)

var log *logger.Logger
Expand Down Expand Up @@ -130,7 +131,7 @@ func (a *API) DoRequestRaw(urlPath string, method string, contentType string, re
if !strings.HasPrefix(urlPath, _apiPathPrefix) {
urlPath = path.Join(_apiPathPrefix, urlPath)
}
return a.requestRaw(urlPath, method, contentType, requestObject)
return a.requestRaw(urlPath, method, contentType, requestObject, 0)
}

// SessionNew - try to register new session
Expand All @@ -149,7 +150,7 @@ func (a *API) SessionNew(accountID string, wgPublicKey string, forceLogin bool)
PublicKey: wgPublicKey,
ForceLogin: forceLogin}

data, err := a.requestRaw(_sessionNewPath, "POST", "application/json", request)
data, err := a.requestRaw(_sessionNewPath, "POST", "application/json", request, 0)
if err != nil {
return nil, nil, nil, err
}
Expand Down Expand Up @@ -189,7 +190,7 @@ func (a *API) SessionStatus(session string) (

request := &types.SessionStatusRequest{Session: session}

data, err := a.requestRaw(_sessionStatusPath, "POST", "application/json", request)
data, err := a.requestRaw(_sessionStatusPath, "POST", "application/json", request, 0)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -247,3 +248,14 @@ func (a *API) WireGuardKeySet(session string, newPublicWgKey string, activePubli

return localIP, nil
}

// GeoLookup get geolocation
func (a *API) GeoLookup(timeoutMs int) (location *types.GeoLookupResponse, err error) {
resp := &types.GeoLookupResponse{}

if err := a.requestEx(_geoLookupPath, "GET", "", nil, resp, timeoutMs); err != nil {
return nil, err
}

return resp, nil
}
19 changes: 14 additions & 5 deletions api/api_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"net"
"net/http"
"path"
"time"
)

func (a *API) getAlternateIPs() (lastGoodIP net.IP, ipList []net.IP) {
Expand Down Expand Up @@ -69,7 +70,7 @@ func newRequest(urlPath string, method string, contentType string, body io.Reade
return req, nil
}

func (a *API) doRequest(urlPath string, method string, contentType string, request interface{}) (resp *http.Response, err error) {
func (a *API) doRequest(urlPath string, method string, contentType string, request interface{}, timeoutMs int) (resp *http.Response, err error) {
lastIP, ips := a.getAlternateIPs()

// When trying to access API server by alternate IPs (not by DNS name)
Expand All @@ -81,7 +82,11 @@ func (a *API) doRequest(urlPath string, method string, contentType string, reque
},
}
// configure http-client with preconfigured TLS transport
client := &http.Client{Transport: transCfg, Timeout: _defaultRequestTimeout}
timeout := _defaultRequestTimeout
if timeoutMs > 0 {
timeout = time.Millisecond * time.Duration(timeoutMs)
}
client := &http.Client{Transport: transCfg, Timeout: timeout}

data := []byte{}
if request != nil {
Expand Down Expand Up @@ -144,8 +149,8 @@ func (a *API) doRequest(urlPath string, method string, contentType string, reque
return firstResp, fmt.Errorf("Unable to access IVPN API server: %w", firstErr)
}

func (a *API) requestRaw(urlPath string, method string, contentType string, requestObject interface{}) (responseData []byte, err error) {
resp, err := a.doRequest(urlPath, method, contentType, requestObject)
func (a *API) requestRaw(urlPath string, method string, contentType string, requestObject interface{}, timeoutMs int) (responseData []byte, err error) {
resp, err := a.doRequest(urlPath, method, contentType, requestObject, timeoutMs)
if err != nil {
return nil, fmt.Errorf("API request failed: %w", err)
}
Expand All @@ -159,7 +164,11 @@ func (a *API) requestRaw(urlPath string, method string, contentType string, requ
}

func (a *API) request(urlPath string, method string, contentType string, requestObject interface{}, responseObject interface{}) error {
body, err := a.requestRaw(urlPath, method, contentType, requestObject)
return a.requestEx(urlPath, method, contentType, requestObject, responseObject, 0)
}

func (a *API) requestEx(urlPath string, method string, contentType string, requestObject interface{}, responseObject interface{}, timeoutMs int) error {
body, err := a.requestRaw(urlPath, method, contentType, requestObject, timeoutMs)
if err != nil {
return err
}
Expand Down
15 changes: 15 additions & 0 deletions api/types/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,18 @@ type SessionStatusResponse struct {
APIErrorResponse
ServiceStatus ServiceStatusAPIResp `json:"service_status"`
}

// GeoLookupResponse geolocation info
type GeoLookupResponse struct {
//ip_address string
//isp string
//organization string
//country string
//country_code string
//city string

Latitude float32 `json:"latitude"`
Longitude float32 `json:"longitude"`

//isIvpnServer bool
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ go 1.13

require (
github.com/fsnotify/fsnotify v1.4.9
golang.org/x/net v0.0.0-20200226051749-491c5fce7268
golang.org/x/sys v0.0.0-20200327173247-9dae0f8f5775
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f
)
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20200226051749-491c5fce7268 h1:fnuNgko6vrkrxuKfTMd+0eOz50ziv+Wi+t38KUT3j+E=
golang.org/x/net v0.0.0-20200226051749-491c5fce7268/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200505041828-1ed23360d12c h1:zJ0mtu4jCalhKg6Oaukv6iIkb+cOvDrajDH9DH46Q4M=
golang.org/x/net v0.0.0-20200904194848-62affa334b73 h1:MXfv8rhZWmFeqX3GNZRsd6vOLoaCHjYEX3qkRo3YBUA=
golang.org/x/net v0.0.0-20200904194848-62affa334b73/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b h1:uwuIcX0g4Yl1NC5XAz37xsr2lTtcqevgzYNVt49waME=
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200327173247-9dae0f8f5775 h1:TC0v2RSO1u2kn1ZugjrFXkRZAEaqMN/RW+OTZkBzmLE=
golang.org/x/sys v0.0.0-20200327173247-9dae0f8f5775/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f h1:+Nyd8tzPX9R7BWHguqsrbFdRx3WQ/1ib8I44HXV5yTA=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
44 changes: 44 additions & 0 deletions helpers/geolocation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// Daemon for IVPN Client Desktop
// https://github.com/ivpn/desktop-app-daemon
//
// Created by Stelnykovych Alexandr.
// Copyright (c) 2020 Privatus Limited.
//
// This file is part of the Daemon for IVPN Client Desktop.
//
// The Daemon for IVPN Client Desktop is free software: you can redistribute it and/or
// modify it under the terms of the GNU General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option) any later version.
//
// The Daemon for IVPN Client Desktop is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
// details.
//
// You should have received a copy of the GNU General Public License
// along with the Daemon for IVPN Client Desktop. If not, see <https://www.gnu.org/licenses/>.
//

package helpers

import "math"

// GetDistanceFromLatLonInKm calc distance betwee two points
func GetDistanceFromLatLonInKm(lat1, lon1, lat2, lon2 float64) float64 {
var R = float64(6371) // Radius of the earth in km
var dLat = deg2rad(lat2 - lat1) // deg2rad below
var dLon = deg2rad(lon2 - lon1)
var a = math.Sin(dLat/2)*math.Sin(dLat/2) +
math.Cos(deg2rad(lat1))*
math.Cos(deg2rad(lat2))*
math.Sin(dLon/2)*
math.Sin(dLon/2)
var c = 2 * math.Atan2(math.Sqrt(a), math.Sqrt(1-a))
var d = R * c // Distance in km
return d
}

func deg2rad(deg float64) float64 {
return deg * (math.Pi / 180)
}
9 changes: 9 additions & 0 deletions protocol/protocol_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,12 @@ func (p *Protocol) OnWiFiChanged(ssid string, isInsecureNetwork bool) {
SSID: ssid,
IsInsecureNetwork: isInsecureNetwork})
}

// OnPingStatus - servers ping status
func (p *Protocol) OnPingStatus(retMap map[string]int) {
var results []types.PingResultType
for k, v := range retMap {
results = append(results, types.PingResultType{Host: k, Ping: v})
}
p.notifyClients(&types.PingServersResp{PingResults: results})
}
1 change: 1 addition & 0 deletions service/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,5 @@ type IServiceEventsReceiver interface {
OnDNSChanged(dns net.IP)
OnKillSwitchStateChanged()
OnWiFiChanged(ssid string, isInsecureNetwork bool)
OnPingStatus(retMap map[string]int)
}
Loading

0 comments on commit d6694bf

Please sign in to comment.