Skip to content

Commit

Permalink
getApiKey for cli + extracting libs from tui + minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
zveinn committed Oct 12, 2023
1 parent f445aa7 commit 7b34dc9
Show file tree
Hide file tree
Showing 9 changed files with 219 additions and 154 deletions.
Binary file added cmd/cli/cli
Binary file not shown.
74 changes: 70 additions & 4 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,35 @@ package main
import (
"log"
"os"
"runtime/debug"
"time"

tea "github.com/charmbracelet/bubbletea"
"github.com/tunnels-is/nicelandvpn-desktop/cmd/termlib"
"github.com/tunnels-is/nicelandvpn-desktop/core"
)

var FLAG_COMMAND string
var FLAG_USER string
var FLAG_PASSWORD string
const (
VERSION = "1.1.3"
PRODUCTION = true
ENABLE_INSTERFACE = true
)

var (
FLAG_COMMAND string
FLAG_USER string
FLAG_PASSWORD string
MONITOR = make(chan int, 200)
TUI *tea.Program
user *core.User
)

func main() {
core.PRODUCTION = PRODUCTION
core.ENABLE_INSTERFACE = ENABLE_INSTERFACE
core.GLOBAL_STATE.Version = VERSION

log.Println(os.Args)
// log.Println(os.Args)
if len(os.Args) < 2 {
os.Exit(1)
}
Expand All @@ -30,12 +50,58 @@ func main() {
}

func GetAPIKEy() {
log.Println("LOADING ROUTER LIST AND STUFF ...")
core.C = new(core.Config)
core.C.DebugLogging = true
core.InitPaths()
core.CreateBaseFolder()
core.InitLogfile()
go core.StartLogQueueProcessor(MONITOR)
err := core.RefreshRouterList()
if err != nil {
core.CreateErrorLog("", "Unable to find the best router for your connection: ", err)
}

user = termlib.Login()
log.Println("API KEY:", user.APIKey)
}

func CreateDummyConfig() {

}
func Connect() {

go core.StartService(MONITOR)
RoutineMonitor()
}

func RoutineMonitor() {
defer func() {
if r := recover(); r != nil {
core.CreateErrorLog("", r, string(debug.Stack()))
go RoutineMonitor()
}
}()

for {
select {
default:
time.Sleep(500 * time.Millisecond)
case ID := <-MONITOR:
if ID == 1 {
go core.StateMaintenance(MONITOR)
} else if ID == 2 {
go core.ReadFromRouterSocket(MONITOR)
} else if ID == 3 {
// TUI ONLY .. does not fire on wails GUI
// go TimedUIUpdate(MONITOR)
} else if ID == 4 {
go core.ReadFromLocalSocket(MONITOR)
} else if ID == 6 {
go core.CalculateBandwidth(MONITOR)
} else if ID == 8 {
go core.StartLogQueueProcessor(MONITOR)
}
}
}
}
29 changes: 16 additions & 13 deletions cmd/tui/loginform.go → cmd/termlib/login.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package main
package termlib

import (
"encoding/json"
"fmt"
"os"
"strings"

"github.com/charmbracelet/bubbles/cursor"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/tunnels-is/nicelandvpn-desktop/core"
Expand All @@ -15,7 +14,6 @@ import (
type loginForm struct {
focusIndex int
inputs []textinput.Model
cursorMode cursor.Mode
}

func intialModel() loginForm {
Expand All @@ -26,15 +24,15 @@ func intialModel() loginForm {
var t textinput.Model
for i := range m.inputs {
t = textinput.New()
t.Cursor.Style = cursorStyle
t.Cursor.Style = CursorStyle
t.CharLimit = 32

switch i {
case 0:
t.Placeholder = "Email"
t.Focus()
t.PromptStyle = focusedStyle
t.TextStyle = focusedStyle
t.PromptStyle = FocusedStyle
t.TextStyle = FocusedStyle
case 1:
t.Placeholder = "Password"
t.EchoMode = textinput.EchoPassword
Expand Down Expand Up @@ -87,13 +85,13 @@ func (m loginForm) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
for i := 0; i <= len(m.inputs)-1; i++ {
if i == m.focusIndex {
cmds[i] = m.inputs[i].Focus()
m.inputs[i].PromptStyle = focusedStyle
m.inputs[i].TextStyle = focusedStyle
m.inputs[i].PromptStyle = FocusedStyle
m.inputs[i].TextStyle = FocusedStyle
continue
}
m.inputs[i].Blur()
m.inputs[i].PromptStyle = noStyle
m.inputs[i].TextStyle = noStyle
m.inputs[i].PromptStyle = NoStyle
m.inputs[i].TextStyle = NoStyle
}
return m, tea.Batch(cmds...)
}
Expand Down Expand Up @@ -122,22 +120,26 @@ func (m loginForm) View() string {
}
}

button := &blurredButton
button := &BlurredButton
if m.focusIndex == len(m.inputs) {
button = &focusedButton
button = &FocusedButton
}
fmt.Fprintf(&b, "\n\n%s\n\n", *button)

return b.String()
}

func login() {
var user *core.User

func Login() (u *core.User) {
_, err := tea.NewProgram(intialModel()).Run()
if err != nil {
fmt.Printf("Could not start the login form: %s\n", err)
core.CleanupOnClose()
os.Exit(1)
}
u = user
return
}

func sendLoginRequest(creds []textinput.Model) {
Expand Down Expand Up @@ -173,4 +175,5 @@ func sendLoginRequest(creds []textinput.Model) {
core.CleanupOnClose()
os.Exit(1)
}

}
81 changes: 81 additions & 0 deletions cmd/termlib/styles.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package termlib

import (
"fmt"

"github.com/charmbracelet/bubbles/table"
"github.com/charmbracelet/lipgloss"
)

// colors taken from frontend/src/assets/style/variables.scss
var (
MainBg = lipgloss.Color("#141414")
BodyBg = lipgloss.Color("#202324")
BodyDarkBg = lipgloss.Color("#0A0B0E")

Teal = lipgloss.Color("#28ad85")
TealBorder = lipgloss.Color("#3AF4BD")
TealHover = lipgloss.Color("#20C997")

Orange = lipgloss.Color("#FF922D")
OrangeBorder = lipgloss.Color("#EF7503")
OrangeHover = lipgloss.Color("#EF7503")

LogError = lipgloss.Color("#FF0000")
LogWarning = lipgloss.Color("#FFFF00")

Lightblue = lipgloss.Color("#20bec9")
Red = lipgloss.Color("#FF5858")
White = lipgloss.Color("#FFFFFF")
Black = lipgloss.Color("#000000")

SuccessColor = lipgloss.Color("#0AB60A")
ErrorColor = lipgloss.Color("#E70808")
)

// tab style
var (
DocStyle = lipgloss.NewStyle().Padding(1, 1, 1, 1)
HighlightColor = TealHover
SelectionColor = Orange
InactiveTabStyle = lipgloss.NewStyle().Border(lipgloss.RoundedBorder(), true, true, false, true).UnsetBorderBottom().BorderForeground(HighlightColor)
ActiveTabStyle = lipgloss.NewStyle().Border(lipgloss.RoundedBorder(), true, true, false, true).UnsetBorderBottom().BorderForeground(HighlightColor).Background(SelectionColor).Foreground(Black)
WindowStyle = lipgloss.NewStyle().BorderForeground(HighlightColor).Padding(0).Border(lipgloss.NormalBorder())
)

// generic content style
var BaseStyle = lipgloss.NewStyle().
BorderStyle(lipgloss.NormalBorder()).
BorderForeground(White).
Padding(0, 1)

// table style
var TableStyle = table.Styles{
Header: lipgloss.NewStyle().Padding(0, 1).BorderStyle(lipgloss.NormalBorder()).BorderForeground(Teal).BorderBottom(true).Bold(true).Width(26),
Selected: lipgloss.NewStyle().Foreground(Black).Background(TealBorder).Bold(true),
Cell: lipgloss.NewStyle().Padding(0, 1).Width(26),
}

// login from styles
var (
FocusedStyle = lipgloss.NewStyle().Foreground(Orange)
BlurredStyle = lipgloss.NewStyle().Foreground(Teal)
CursorStyle = FocusedStyle.Copy()
NoStyle = lipgloss.NewStyle()
HelpStyle = BlurredStyle.Copy()
CursorModeHelpStyle = lipgloss.NewStyle().Foreground(TealHover)

FocusedButton = FocusedStyle.Copy().Render("[ Submit ]")
BlurredButton = fmt.Sprintf("[ %s ]", BlurredStyle.Render("Submit"))
)

// status line
var StatusStyle = lipgloss.NewStyle().Foreground(OrangeHover).Padding(0, 1).Bold(true)
var StatsStyle = lipgloss.NewStyle().Foreground(OrangeHover).Padding(0).Bold(true)

// Stats table style
var DetailedStatsStyle = table.Styles{
Header: lipgloss.NewStyle().Padding(0, 1).BorderStyle(lipgloss.NormalBorder()).BorderForeground(Teal).BorderBottom(true).Bold(true).Width(28).Foreground(Teal),
Selected: lipgloss.NewStyle().Foreground(White),
Cell: lipgloss.NewStyle().Padding(0, 1).Width(28),
}
33 changes: 1 addition & 32 deletions cmd/tui/globals.go
Original file line number Diff line number Diff line change
@@ -1,45 +1,14 @@
package main

import (
"time"

"github.com/tunnels-is/nicelandvpn-desktop/core"
"go.mongodb.org/mongo-driver/bson/primitive"
)

var (
user *User
user *core.User
PAFR core.FORWARD_REQUEST
)

// Device token struct need for the login respons from user scruct
type DEVICE_TOKEN struct {
DT string `bson:"DT"`
N string `bson:"N"`
Created time.Time `bson:"C"`
}

// use struct you get from the login request
type User struct {
ID primitive.ObjectID `json:"_id,omitempty" bson:"_id,omitempty"`
APIKey string `bson:"AK" json:"APIKey"`
Email string `bson:"E"`
TwoFactorEnabled bool `json:"TwoFactorEnabled" bson:"TFE"`
Disabled bool `bson:"D" json:"Disabled"`
Tokens []*DEVICE_TOKEN `json:"Tokens" bson:"T"`
DeviceToken *DEVICE_TOKEN `json:",omitempty" bson:"-"`

CashCode int `bson:"CSC" json:"CashCode"`
Affiliate string `bson:"AF"`
SubLevel int `bson:"SUL"`
SubExpiration time.Time `bson:"SE"`
TrialStarted time.Time `bson:"TrialStarted" json:"TrialStarted"`

CancelSub bool `json:"CancelSub" bson:"CS"`

Version string `json:"Version" bson:"-"`
}

var (
app_state_str = [10]string{
"VPN List Update", "Ready to Connect", "Version", "VPN Tunnel Ready",
Expand Down
2 changes: 1 addition & 1 deletion cmd/tui/little_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TimedUIUpdate(MONITOR chan int) {
core.GetRoutersAndAccessPoints(&PAFR)
}

core.PrepareState()
core.PrepareState()

TUI.Send(&tea.KeyMsg{
Type: 0,
Expand Down
Loading

0 comments on commit 7b34dc9

Please sign in to comment.