Skip to content

Commit

Permalink
Final commit for patch 1.1.5
Browse files Browse the repository at this point in the history
Suashing
  • Loading branch information
zveinn committed Nov 19, 2023
1 parent 7b34dc9 commit 907bc0c
Show file tree
Hide file tree
Showing 27 changed files with 1,973 additions and 1,787 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ build/windows-custom/NicelandVPN-windows.exe
build/darwin-custom/NicelandVPN.app/Contents/MacOS/*
build/darwin-custom/NicelandVPN.app/Contents/_CodeSignature/*

cmd/tui/*.exe
cmd/tui/niceland-static-tui
cmd/tui/tui

cmd/cli/cli
cmd/cli/*.exe

node_modules
frontend/dist
files
Expand Down
Binary file removed cmd/cli/cli
Binary file not shown.
44 changes: 38 additions & 6 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package main

import (
"fmt"
"log"
"os"
"runtime/debug"
"time"

"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/muesli/termenv"
"github.com/tunnels-is/nicelandvpn-desktop/cmd/termlib"
"github.com/tunnels-is/nicelandvpn-desktop/core"
)
Expand All @@ -23,10 +26,27 @@ var (
FLAG_PASSWORD string
MONITOR = make(chan int, 200)
TUI *tea.Program
user *core.User

user *core.User
output = termenv.NewOutput(os.Stdout)
color = output.ForegroundColor()
bgcolor = output.BackgroundColor()
resetString = output.String("... exiting")

userLoginInputs = make([]textinput.Model, 4)
)

func main() {
defer func() {
// This will reset the forground and background of the terminal when exiting
resetString.Foreground(color)
resetString.Background(bgcolor)
fmt.Print("\033[H\033[2J")
fmt.Print("\033[H\033[2J")
fmt.Print("\033[H\033[2J")
fmt.Println(resetString)
}()

core.PRODUCTION = PRODUCTION
core.ENABLE_INSTERFACE = ENABLE_INSTERFACE
core.GLOBAL_STATE.Version = VERSION
Expand All @@ -40,7 +60,7 @@ func main() {
case "connect":
Connect()
case "getApiKey":
GetAPIKEy()
GetAPIKey()
case "createConfig":
CreateDummyConfig()
default:
Expand All @@ -49,21 +69,33 @@ func main() {

}

func GetAPIKEy() {
log.Println("LOADING ROUTER LIST AND STUFF ...")
func GetAPIKey() {
s := termlib.NewSpinner()
go s.Start()

core.C = new(core.Config)
core.C.DebugLogging = true
core.InitPaths()
core.CreateBaseFolder()
core.InitLogfile()
go core.StartLogQueueProcessor(MONITOR)
err := core.RefreshRouterList()
time.Sleep(2 * time.Second)
s.Stop()
if err != nil {
core.CreateErrorLog("", "Unable to find the best router for your connection: ", err)
os.Exit(1)
}

user = termlib.Login()
log.Println("API KEY:", user.APIKey)
fmt.Print("\033[H\033[2J")
termlib.Login(userLoginInputs)
log.Println("USER INPUT:", userLoginInputs[0].Value())
user = termlib.SendLoginRequest(userLoginInputs)
if user != nil {
log.Println("API KEY:", user.APIKey)
} else {
log.Println("Invalid login..")
}
}

func CreateDummyConfig() {
Expand Down
30 changes: 13 additions & 17 deletions cmd/termlib/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package termlib
import (
"encoding/json"
"fmt"
"os"
"strings"

"github.com/charmbracelet/bubbles/textinput"
Expand All @@ -16,9 +15,9 @@ type loginForm struct {
inputs []textinput.Model
}

func intialModel() loginForm {
func intialModel(userInputs []textinput.Model) loginForm {
m := loginForm{
inputs: make([]textinput.Model, 4),
inputs: userInputs,
}

var t textinput.Model
Expand Down Expand Up @@ -58,15 +57,14 @@ func (m loginForm) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c":
// core.CleanupOnClose()
sendLoginRequest(m.inputs) // I guess this is one way to exit without going into the TUI
// sendLoginRequest(m.inputs) // I guess this is one way to exit without going into the TUI
return m, tea.Quit
case "tab", "shift-tab", "enter", "up", "down":
s := msg.String()

// if hit enter while the submit button was focused
if s == "enter" && m.focusIndex == len(m.inputs) {
sendLoginRequest(m.inputs)
// sendLoginRequest(m.inputs)
return m, tea.Quit
}

Expand Down Expand Up @@ -129,20 +127,16 @@ func (m loginForm) View() string {
return b.String()
}

var user *core.User

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

}

func sendLoginRequest(creds []textinput.Model) {
func SendLoginRequest(creds []textinput.Model) (user *core.User) {
var FR core.FORWARD_REQUEST

// fill the login form
Expand All @@ -165,15 +159,17 @@ func sendLoginRequest(creds []textinput.Model) {
fmt.Println("\nCode: ", code)
fmt.Println("Log in error: ", err)
core.CleanupOnClose()
os.Exit(1)
return
}

user = new(core.User)
// unfold it in the user global
err = json.Unmarshal(respBytes, &user)
if err != nil {
fmt.Println("Response error: ", err)
core.CleanupOnClose()
os.Exit(1)
return
}

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

import (
"fmt"

"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)

var (
// Available spinners
spinners = []spinner.Spinner{
spinner.Line,
spinner.Dot,
spinner.MiniDot,
spinner.Jump,
spinner.Pulse,
spinner.Points,
spinner.Globe,
spinner.Moon,
spinner.Monkey,
}

textStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("252")).Render
spinnerStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("69"))
)

type model struct {
spinner spinner.Model
Program *tea.Program
}

func NewSpinner() (m *model) {
m = new(model)
return m
}

func (m *model) Start() {
m.ResetSpinner()
m.Program = tea.NewProgram(m)

if _, err := m.Program.Run(); err != nil {
fmt.Println("could not run program:", err)
}
}

func (m *model) Stop() {
m.Program.Quit()
}

func (m model) Init() tea.Cmd {
return m.spinner.Tick
}

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.QuitMsg:
return m, tea.Quit
case spinner.TickMsg:
var cmd tea.Cmd
m.spinner, cmd = m.spinner.Update(msg)
return m, cmd
default:
return m, nil
}
}

func (m *model) ResetSpinner() {
m.spinner = spinner.New()
m.spinner.Style = spinnerStyle
m.spinner.Spinner = spinners[6]
}

func (m model) View() (s string) {
s += fmt.Sprintf("\n %s%s%s\n\n", m.spinner.View(), " ", textStyle("Spinning..."))
return
}
6 changes: 4 additions & 2 deletions cmd/tui/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"runtime/debug"
"time"

"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/tunnels-is/nicelandvpn-desktop/core"
)
Expand All @@ -15,8 +16,9 @@ const (
)

var (
MONITOR = make(chan int, 200)
TUI *tea.Program
MONITOR = make(chan int, 200)
TUI *tea.Program
userLoginInputs = make([]textinput.Model, 4)
)

func main() {
Expand Down
3 changes: 2 additions & 1 deletion cmd/tui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,8 @@ func StartTui() {
// I do not think I can have 2 completely different models
// in bubbletea this is the only way I could figure out
// how to do it...
user = termlib.Login()
termlib.Login(userLoginInputs)
user = termlib.SendLoginRequest(userLoginInputs)

// Initial VPNs and Routers tables
// I thought it's a good idea to have the
Expand Down
Loading

0 comments on commit 907bc0c

Please sign in to comment.