-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
2,236 additions
and
1,945 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.