Skip to content

Commit

Permalink
Merge branch '1.1.5' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
zveinn committed Nov 19, 2023
2 parents 0b4cca0 + 907bc0c commit 8fcde4a
Show file tree
Hide file tree
Showing 28 changed files with 2,236 additions and 1,945 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
139 changes: 139 additions & 0 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
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"
)

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
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

// log.Println(os.Args)
if len(os.Args) < 2 {
os.Exit(1)
}

switch os.Args[1] {
case "connect":
Connect()
case "getApiKey":
GetAPIKey()
case "createConfig":
CreateDummyConfig()
default:
os.Exit(1)
}

}

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)
}

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() {

}
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)
}
}
}
}
49 changes: 24 additions & 25 deletions cmd/tui/loginform.go → cmd/termlib/login.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
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,26 +13,25 @@ import (
type loginForm struct {
focusIndex int
inputs []textinput.Model
cursorMode cursor.Mode
}

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

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 All @@ -60,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 All @@ -87,13 +83,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,25 +118,25 @@ 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() {
_, 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
}

}

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

// fill the login form
Expand All @@ -163,14 +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
}
Loading

0 comments on commit 8fcde4a

Please sign in to comment.