This repository has been archived by the owner on May 14, 2021. It is now read-only.
/
base.go
132 lines (107 loc) · 3.7 KB
/
base.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
//
// IVPN command line interface (CLI)
// https://github.com/ivpn/desktop-app-cli
//
// Created by Stelnykovych Alexandr.
// Copyright (c) 2020 Privatus Limited.
//
// This file is part of the IVPN command line interface.
//
// The IVPN command line interface 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 IVPN command line interface 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 IVPN command line interface. If not, see <https://www.gnu.org/licenses/>.
//
package commands
import (
"fmt"
"os"
"strings"
"text/tabwriter"
"time"
"github.com/ivpn/desktop-app-cli/protocol"
apitypes "github.com/ivpn/desktop-app-daemon/api/types"
"github.com/ivpn/desktop-app-daemon/protocol/types"
"github.com/ivpn/desktop-app-daemon/vpn"
)
var _proto *protocol.Client
// Initialize initializes commands. Must be called before using any command.
func Initialize(proto *protocol.Client) {
_proto = proto
}
func printAccountInfo(w *tabwriter.Writer, accountID string) *tabwriter.Writer {
if w == nil {
w = tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)
}
if len(accountID) > 0 {
return w // Do nothing in case of logged in
}
fmt.Fprintln(w, fmt.Sprintf("Account\t:\t%v", "Not logged in"))
return w
}
func printState(w *tabwriter.Writer, state vpn.State, connected types.ConnectedResp, serverInfo string, exitServerInfo string) *tabwriter.Writer {
if w == nil {
w = tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)
}
fmt.Fprintln(w, fmt.Sprintf("VPN\t:\t%v", state))
if len(serverInfo) > 0 {
fmt.Fprintln(w, fmt.Sprintf("\t\t%v", serverInfo))
if len(exitServerInfo) > 0 {
fmt.Fprintln(w, fmt.Sprintf("\t\t%v (Multi-Hop exit server)", exitServerInfo))
}
}
if state != vpn.CONNECTED {
return w
}
since := time.Unix(connected.TimeSecFrom1970, 0)
fmt.Fprintln(w, fmt.Sprintf(" Protocol\t:\t%v", connected.VpnType))
fmt.Fprintln(w, fmt.Sprintf(" Local IP\t:\t%v", connected.ClientIP))
fmt.Fprintln(w, fmt.Sprintf(" Server IP\t:\t%v", connected.ServerIP))
fmt.Fprintln(w, fmt.Sprintf(" Connected\t:\t%v", since))
return w
}
func printDNSState(w *tabwriter.Writer, dns string, servers *apitypes.ServersInfoResponse) *tabwriter.Writer {
if w == nil {
w = tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)
}
dns = strings.TrimSpace(dns)
if len(dns) == 0 {
fmt.Fprintln(w, fmt.Sprintf("DNS\t:\tDefault (auto)"))
return w
}
antitrackerText := strings.Builder{}
isAntitracker, isAtHardcore := IsAntiTrackerIP(dns, servers)
if isAtHardcore {
antitrackerText.WriteString("Enabled (Hardcore)")
} else if isAntitracker {
antitrackerText.WriteString("Enabled")
}
if antitrackerText.Len() > 0 {
fmt.Fprintln(w, fmt.Sprintf("AntiTracker\t:\t%v", antitrackerText.String()))
} else {
fmt.Fprintln(w, fmt.Sprintf("DNS\t:\t%v", dns))
}
return w
}
func printFirewallState(w *tabwriter.Writer, isEnabled, isPersistent, isAllowLAN, isAllowMulticast bool) *tabwriter.Writer {
if w == nil {
w = tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)
}
fwState := "Disabled"
if isEnabled {
fwState = "Enabled"
}
fmt.Fprintln(w, fmt.Sprintf("Firewall\t:\t%v", fwState))
fmt.Fprintln(w, fmt.Sprintf(" Allow LAN\t:\t%v", isAllowLAN))
if isPersistent {
fmt.Fprintln(w, fmt.Sprintf(" Persistent\t:%v", isPersistent))
}
return w
}