update app

This commit is contained in:
durbok
2024-12-28 15:54:49 +01:00
parent 352e98606f
commit b6bbf30bd0
7 changed files with 165 additions and 0 deletions

5
.env Normal file
View File

@@ -0,0 +1,5 @@
DB_USER=root
DB_PASSWORD=password
DB_HOST=127.0.0.1
DB_PORT=3306
DB_NAME=sessions

19
config/env.go Normal file
View File

@@ -0,0 +1,19 @@
package config
import (
"log"
"os"
"github.com/joho/godotenv"
)
func LoadEnv() {
err := godotenv.Load()
if err != nil {
log.Fatalf("Error loading .env file")
}
}
func GetEnv(key string) string {
return os.Getenv(key)
}

28
db/mysql.go Normal file
View File

@@ -0,0 +1,28 @@
package db
import (
"database/sql"
"fmt"
"log"
"tui-ssh-app/config"
_ "github.com/go-sql-driver/mysql"
)
var DB *sql.DB
func ConnectDB() {
var err error
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s",
config.GetEnv("DB_USER"),
config.GetEnv("DB_PASSWORD"),
config.GetEnv("DB_HOST"),
config.GetEnv("DB_PORT"),
config.GetEnv("DB_NAME"),
)
DB, err = sql.Open("mysql", dsn)
if err != nil {
log.Fatalf("Error connecting to database: %v", err)
}
}

13
main.go Normal file
View File

@@ -0,0 +1,13 @@
package main
import (
"tui-ssh-app/config"
"tui-ssh-app/db"
"tui-ssh-app/tui"
)
func main() {
config.LoadEnv()
db.ConnectDB()
tui.StartTUI()
}

24
mysql-compose.yml Normal file
View File

@@ -0,0 +1,24 @@
services:
mysql:
image: mysql:8.0
container_name: mysql8
hostname: mysql-8
command: '--default-authentication-plugin=mysql_native_password'
# cap_add:
# - SYS_NICE # CAP_SYS_NICE
# volumes:
# - ./conf:/etc/mysql/conf.d
# - ./dbdata:/var/lib/mysql
ports:
- "3306:3306"
environment:
# MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
MYSQL_ROOT_PASSWORD: password
MYSQL_USER: "user"
MYSQL_PASSWORD: "password"
MYSQL_DATABASE: "sessions"
healthcheck:
test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
timeout: 20s
retries: 10

49
ssh/connect.go Normal file
View File

@@ -0,0 +1,49 @@
package ssh
import (
"fmt"
"io/ioutil"
"golang.org/x/crypto/ssh"
)
type SSHConfig struct {
User string
Host string
Port string
Password string
PrivateKey string
}
func ConnectSSH(config SSHConfig) error {
var auth ssh.AuthMethod
if config.PrivateKey != "" {
key, err := ioutil.ReadFile(config.PrivateKey)
if err != nil {
return fmt.Errorf("failed to read private key: %v", err)
}
signer, err := ssh.ParsePrivateKey(key)
if err != nil {
return fmt.Errorf("failed to parse private key: %v", err)
}
auth = ssh.PublicKeys(signer)
} else {
auth = ssh.Password(config.Password)
}
clientConfig := &ssh.ClientConfig{
User: config.User,
Auth: []ssh.AuthMethod{auth},
HostKeyCallback: ssh.InsecureIgnoreHostKey(), // Use a proper callback for production!
}
client, err := ssh.Dial("tcp", fmt.Sprintf("%s:%s", config.Host, config.Port), clientConfig)
if err != nil {
return fmt.Errorf("failed to connect to SSH: %v", err)
}
defer client.Close()
fmt.Println("Connected to SSH successfully!")
return nil
}

27
tui/interface.go Normal file
View File

@@ -0,0 +1,27 @@
package tui
import (
"github.com/rivo/tview"
)
func StartTUI() {
app := tview.NewApplication()
form := tview.NewForm().
AddInputField("User", "", 20, nil, nil).
AddInputField("Host", "", 20, nil, nil).
AddInputField("Port", "22", 5, nil, nil).
AddPasswordField("Password", "", 20, '*', nil).
AddInputField("Private Key", "", 20, nil, nil).
AddButton("Connect", func() {
// Handle SSH connection logic
}).
AddButton("Quit", func() {
app.Stop()
})
form.SetBorder(true).SetTitle("SSH Connection").SetTitleAlign(tview.AlignLeft)
if err := app.SetRoot(form, true).Run(); err != nil {
panic(err)
}
}