2025-01-28 09:02:18 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
// "bytes"
|
2025-01-31 16:43:35 +01:00
|
|
|
// "bufio"
|
2025-01-28 17:01:58 +01:00
|
|
|
"bytes"
|
2025-01-28 09:02:18 +01:00
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"os/user"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2025-01-31 16:43:35 +01:00
|
|
|
"regexp"
|
2025-01-28 09:02:18 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type PageData struct {
|
|
|
|
CurrentDir string
|
|
|
|
CurrentUsername string
|
|
|
|
CommandLog []CommandOutput
|
|
|
|
}
|
|
|
|
|
|
|
|
type CommandOutput struct {
|
|
|
|
Command string
|
|
|
|
Output string
|
|
|
|
Error string
|
|
|
|
}
|
|
|
|
|
2025-01-31 16:43:35 +01:00
|
|
|
type CommandNode struct {
|
|
|
|
Operator string
|
|
|
|
Left *CommandNode
|
|
|
|
Right *CommandNode
|
|
|
|
Command string
|
|
|
|
}
|
|
|
|
|
2025-01-28 09:02:18 +01:00
|
|
|
var commandLog []CommandOutput
|
2025-01-31 16:43:35 +01:00
|
|
|
var variables = make(map[string]string)
|
|
|
|
|
|
|
|
func expandCommandSubstitution(command string, currentDir string) (string, error) {
|
|
|
|
// Do not expand single quoted strings
|
|
|
|
reSingleQuotes := regexp.MustCompile(`'([^']*)'`)
|
|
|
|
singleQuotedStrings := reSingleQuotes.FindAllString(command, -1)
|
|
|
|
|
|
|
|
placeholder := "\x00PLACEHOLDER\x00"
|
|
|
|
command = reSingleQuotes.ReplaceAllLiteralString(command, placeholder)
|
|
|
|
|
|
|
|
// Expand variables
|
|
|
|
reVar := regexp.MustCompile(`\$(\w+)`)
|
|
|
|
command = reVar.ReplaceAllStringFunc(command, func(match string) string {
|
|
|
|
varName := match[1:]
|
|
|
|
if val, exists := variables[varName]; exists {
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
if envVal, exists := os.LookupEnv(varName); exists {
|
|
|
|
return envVal
|
|
|
|
}
|
|
|
|
|
|
|
|
return match
|
|
|
|
})
|
|
|
|
|
|
|
|
// Restore single-quoted strings and avoid expansion inside them
|
|
|
|
for _, sq := range singleQuotedStrings {
|
|
|
|
command = strings.Replace(command, placeholder, sq, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// expand $(...) command substitution
|
|
|
|
re := regexp.MustCompile(`\$\(([^()]+)\)`)
|
|
|
|
for {
|
|
|
|
match := re.FindStringSubmatch(command)
|
|
|
|
if match == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
subCommand := strings.TrimSpace(match[1])
|
|
|
|
subOutput, err := executeCommandTree(parseCommandTree(subCommand), currentDir)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
command = strings.Replace(command, match[0], strings.TrimSpace(subOutput), 1)
|
|
|
|
}
|
|
|
|
return command, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func setVariable(command string) bool {
|
|
|
|
re := regexp.MustCompile(`^(\w+)=(.*)$`)
|
|
|
|
match := re.FindStringSubmatch(command)
|
|
|
|
if match == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
varName := match[1]
|
|
|
|
value := strings.Trim(match[2], `"`)
|
|
|
|
|
|
|
|
if strings.HasPrefix(command, "export ") {
|
|
|
|
os.Setenv(varName, value)
|
|
|
|
} else {
|
|
|
|
variables[varName] = value
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseCommandTree(command string) *CommandNode {
|
|
|
|
if strings.Contains(command, ";") {
|
|
|
|
parts := strings.SplitN(command, ";", 2)
|
|
|
|
return &CommandNode{
|
|
|
|
Operator: ";",
|
|
|
|
Left: parseCommandTree(strings.TrimSpace(parts[0])),
|
|
|
|
Right: parseCommandTree(strings.TrimSpace(parts[1])),
|
|
|
|
}
|
|
|
|
} else if strings.Contains(command, ">") {
|
|
|
|
parts := strings.SplitN(command, ">", 2)
|
|
|
|
return &CommandNode{
|
|
|
|
Operator: ">",
|
|
|
|
Left: parseCommandTree(strings.TrimSpace(parts[0])),
|
|
|
|
Right: &CommandNode{Command: strings.TrimSpace(parts[1])},
|
|
|
|
}
|
|
|
|
} else if strings.Contains(command, "<") {
|
|
|
|
parts := strings.SplitN(command, "<", 2)
|
|
|
|
return &CommandNode{
|
|
|
|
Operator: "<",
|
|
|
|
Left: parseCommandTree(strings.TrimSpace(parts[0])),
|
|
|
|
Right: &CommandNode{Command: strings.TrimSpace(parts[1])},
|
|
|
|
}
|
|
|
|
} else if strings.Contains(command, "|") {
|
|
|
|
parts := strings.SplitN(command, "|", 2)
|
|
|
|
return &CommandNode{
|
|
|
|
Operator: "|",
|
|
|
|
Left: parseCommandTree(strings.TrimSpace(parts[0])),
|
|
|
|
Right: parseCommandTree(strings.TrimSpace(parts[1])),
|
|
|
|
}
|
|
|
|
} else if strings.Contains(command, "&&") {
|
|
|
|
parts := strings.SplitN(command, "&&", 2)
|
|
|
|
return &CommandNode{
|
|
|
|
Operator: "&&",
|
|
|
|
Left: parseCommandTree(strings.TrimSpace(parts[0])),
|
|
|
|
Right: parseCommandTree(strings.TrimSpace(parts[1])),
|
|
|
|
}
|
|
|
|
} else if strings.Contains(command, "||") {
|
|
|
|
parts := strings.SplitN(command, "||", 2)
|
|
|
|
return &CommandNode{
|
|
|
|
Operator: "||",
|
|
|
|
Left: parseCommandTree(strings.TrimSpace(parts[0])),
|
|
|
|
Right: parseCommandTree(strings.TrimSpace(parts[1])),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &CommandNode{Command: command}
|
|
|
|
}
|
|
|
|
|
|
|
|
func executeCommandTree(node *CommandNode, currentDir string) (string, error) {
|
|
|
|
if node == nil {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch node.Operator {
|
|
|
|
case "&&":
|
|
|
|
leftOutput, err := executeCommandTree(node.Left, currentDir)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if leftOutput != "" {
|
|
|
|
return executeCommandTree(node.Right, currentDir)
|
|
|
|
}
|
|
|
|
return executeCommandTree(node.Right, currentDir)
|
|
|
|
|
|
|
|
case "||":
|
|
|
|
leftOutput, err := executeCommandTree(node.Left, currentDir)
|
|
|
|
if err == nil && leftOutput != "" {
|
|
|
|
return leftOutput, nil
|
|
|
|
}
|
|
|
|
return executeCommandTree(node.Right, currentDir)
|
|
|
|
|
|
|
|
case "|":
|
|
|
|
leftOutput, err := executeCommandTree(node.Left, currentDir)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
cmdArgs := parseCommandWithQuotes(node.Right.Command)
|
|
|
|
if len(cmdArgs) == 0 {
|
|
|
|
return "", fmt.Errorf("Invalid Command")
|
|
|
|
}
|
|
|
|
cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
|
|
|
|
cmd.Dir = currentDir
|
|
|
|
cmd.Stdin = strings.NewReader(leftOutput)
|
|
|
|
var outputBuffer bytes.Buffer
|
|
|
|
cmd.Stdout = &outputBuffer
|
|
|
|
cmd.Stderr = &outputBuffer
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return outputBuffer.String(), nil
|
|
|
|
|
|
|
|
case ">":
|
|
|
|
leftOutput, err := executeCommandTree(node.Left, currentDir)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
file, err := os.Create(strings.TrimSpace(node.Right.Command))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
_, err = file.WriteString(leftOutput)
|
|
|
|
return "", err
|
|
|
|
|
|
|
|
case "<":
|
|
|
|
file, err := os.Open(strings.TrimSpace(node.Right.Command))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
cmdArgs := parseCommandWithQuotes(node.Left.Command)
|
|
|
|
if len(cmdArgs) == 0 {
|
|
|
|
return "", fmt.Errorf("Invalid command")
|
|
|
|
}
|
|
|
|
cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
|
|
|
|
cmd.Dir = currentDir
|
|
|
|
cmd.Stdin = file
|
|
|
|
var outputBuffer bytes.Buffer
|
|
|
|
cmd.Stdout = &outputBuffer
|
|
|
|
cmd.Stderr = &outputBuffer
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return outputBuffer.String(), nil
|
|
|
|
|
|
|
|
case ";":
|
|
|
|
leftOutput, _ := executeCommandTree(node.Left, currentDir)
|
|
|
|
rightOutput, _ := executeCommandTree(node.Right, currentDir)
|
|
|
|
return leftOutput + rightOutput, nil
|
|
|
|
}
|
|
|
|
return executeSimpleCommand(node.Command, currentDir).Output, nil
|
|
|
|
}
|
2025-01-28 09:02:18 +01:00
|
|
|
|
|
|
|
func executeCommand(command string, args []string, dir string) (string, error) {
|
|
|
|
cmd := exec.Command(command, args...)
|
|
|
|
cmd.Dir = dir
|
|
|
|
output, err := cmd.CombinedOutput()
|
|
|
|
return string(output), err
|
|
|
|
}
|
|
|
|
|
|
|
|
func executeSimpleCommand(command string, currentDir string) CommandOutput {
|
|
|
|
var cmdOutput CommandOutput
|
2025-01-28 17:01:58 +01:00
|
|
|
// args := strings.Fields(command)
|
|
|
|
args := parseCommandWithQuotes(command)
|
|
|
|
if len(args) == 0 {
|
2025-01-28 09:02:18 +01:00
|
|
|
return cmdOutput
|
|
|
|
}
|
|
|
|
|
2025-01-28 17:01:58 +01:00
|
|
|
cmd := exec.Command(args[0], args[1:]...)
|
2025-01-28 09:02:18 +01:00
|
|
|
cmd.Dir = currentDir
|
|
|
|
output, err := cmd.CombinedOutput()
|
|
|
|
cmdOutput.Command = command
|
|
|
|
cmdOutput.Output = string(output)
|
|
|
|
if err != nil {
|
|
|
|
cmdOutput.Error = err.Error()
|
|
|
|
}
|
|
|
|
return cmdOutput
|
|
|
|
}
|
|
|
|
|
|
|
|
func changeDirectory(command string, args []string, currentDir *string) CommandOutput {
|
|
|
|
var cmdOutput CommandOutput
|
|
|
|
|
|
|
|
if len(args) == 0 {
|
|
|
|
homeDir, _ := os.UserHomeDir()
|
|
|
|
err := os.Chdir(homeDir)
|
|
|
|
if err != nil {
|
|
|
|
cmdOutput = CommandOutput {
|
|
|
|
Command: command,
|
|
|
|
Error: "Failed to change to home directory: " + err.Error(),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cmdOutput = CommandOutput {
|
|
|
|
Command: command,
|
|
|
|
Output: "Changed to home directory: " + homeDir,
|
|
|
|
}
|
|
|
|
*currentDir = homeDir
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
newDir := args[0]
|
|
|
|
err := os.Chdir(newDir)
|
|
|
|
if err != nil {
|
|
|
|
cmdOutput = CommandOutput {
|
|
|
|
Command: command + " " + newDir,
|
|
|
|
Error: "Failed to change to directory: " + err.Error(),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cmdOutput = CommandOutput {
|
|
|
|
Command: command + " " + newDir,
|
|
|
|
Output: "Changed to directory: " + newDir,
|
|
|
|
}
|
|
|
|
*currentDir = newDir
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cmdOutput
|
|
|
|
}
|
|
|
|
|
|
|
|
func fileUploadHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method == http.MethodPost {
|
|
|
|
file, header, err := r.FormFile("file")
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "Failed to upload file", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
uploadDir := r.FormValue("uploadDir")
|
|
|
|
if uploadDir == "" {
|
|
|
|
uploadDir = "./"
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := os.Create(filepath.Join(uploadDir, header.Filename))
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "Failed to save file", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer out.Close()
|
|
|
|
|
|
|
|
_, err = io.Copy(out, file)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "Failed to write file", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2025-01-31 16:43:35 +01:00
|
|
|
cmdOutput := CommandOutput{Command: "upload", Output: "File saved successfully", Error: ""}
|
|
|
|
// fmt.Fprintf(w, "File %s uploaded successfully to %s", header.Filename, uploadDir)
|
|
|
|
commandLog = append(commandLog, cmdOutput)
|
2025-01-28 09:02:18 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-01-31 16:43:35 +01:00
|
|
|
// func fileDownloadHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// if r.Method == http.MethodPost {
|
|
|
|
// file, header, err := r.FormFile("file")
|
2025-01-28 09:02:18 +01:00
|
|
|
|
2025-01-31 16:43:35 +01:00
|
|
|
func processCommand(command string, currentDir string) CommandOutput {
|
|
|
|
var errorMsg string
|
2025-01-28 09:02:18 +01:00
|
|
|
|
2025-01-31 16:43:35 +01:00
|
|
|
// Handle variable assignments before execution
|
|
|
|
if setVariable(command) {
|
|
|
|
return CommandOutput{Command: command, Output: "", Error: ""}
|
2025-01-28 09:02:18 +01:00
|
|
|
}
|
|
|
|
|
2025-01-31 16:43:35 +01:00
|
|
|
// This handles command substitution which is $(...) in bash
|
|
|
|
expandedCommand, err := expandCommandSubstitution(command, currentDir)
|
2025-01-28 09:02:18 +01:00
|
|
|
if err != nil {
|
2025-01-31 16:43:35 +01:00
|
|
|
errorMsg = err.Error()
|
|
|
|
return CommandOutput{Command: command, Output: "", Error: errorMsg}
|
2025-01-28 09:02:18 +01:00
|
|
|
}
|
|
|
|
|
2025-01-31 16:43:35 +01:00
|
|
|
tree := parseCommandTree(expandedCommand)
|
|
|
|
output, err := executeCommandTree(tree, currentDir)
|
2025-01-28 09:02:18 +01:00
|
|
|
if err != nil {
|
2025-01-31 16:43:35 +01:00
|
|
|
errorMsg = err.Error()
|
2025-01-28 09:02:18 +01:00
|
|
|
}
|
2025-01-31 16:43:35 +01:00
|
|
|
return CommandOutput{Command: command, Output: output, Error: errorMsg}
|
2025-01-28 09:02:18 +01:00
|
|
|
}
|
|
|
|
|
2025-01-28 17:01:58 +01:00
|
|
|
func parseCommandWithQuotes(command string) []string {
|
|
|
|
var args []string
|
|
|
|
var current strings.Builder
|
|
|
|
inSingleQuote, inDoubleQuote := false, false
|
|
|
|
|
|
|
|
for i := 0; i < len(command); i++ {
|
|
|
|
c := command[i]
|
|
|
|
|
|
|
|
switch c {
|
|
|
|
case '\'':
|
|
|
|
if !inDoubleQuote {
|
|
|
|
inSingleQuote = !inSingleQuote
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
case '"':
|
|
|
|
if !inSingleQuote {
|
|
|
|
inDoubleQuote = !inDoubleQuote
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
case ' ':
|
|
|
|
if !inSingleQuote && !inDoubleQuote {
|
|
|
|
if current.Len() > 0 {
|
|
|
|
args = append(args, current.String())
|
|
|
|
current.Reset()
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
case '\\':
|
|
|
|
// Handle escaped characters
|
|
|
|
if i+1 < len(command) {
|
|
|
|
next := command[i+1]
|
|
|
|
if next == '"' || next == '\'' || next == '\\' {
|
|
|
|
current.WriteByte(next)
|
|
|
|
i++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
current.WriteByte(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the last argument
|
|
|
|
if current.Len() > 0 {
|
|
|
|
args = append(args, current.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
return args
|
|
|
|
}
|
2025-01-28 09:02:18 +01:00
|
|
|
|
|
|
|
func handler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
currentDir, _ := os.Getwd()
|
|
|
|
currentUser, _ := user.Current()
|
|
|
|
currentUsername := currentUser.Username
|
|
|
|
|
|
|
|
if r.Method == http.MethodPost {
|
|
|
|
input := r.FormValue("command")
|
|
|
|
parts := strings.Fields(input)
|
|
|
|
if len(parts) > 0 {
|
|
|
|
command := parts[0]
|
|
|
|
args := parts[1:]
|
|
|
|
if command == "cd" {
|
|
|
|
cmdOutput := changeDirectory(command, args, ¤tDir)
|
|
|
|
commandLog = append(commandLog, cmdOutput)
|
|
|
|
} else {
|
2025-01-28 17:01:58 +01:00
|
|
|
cmdOutput := processCommand(input, currentDir)
|
2025-01-28 09:02:18 +01:00
|
|
|
commandLog = append(commandLog, cmdOutput)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpl := template.Must(template.New("index").Parse(`
|
|
|
|
<!DOCTYPE html>
|
|
|
|
<html lang="en">
|
|
|
|
<head>
|
|
|
|
<meta charset="UTF-8">
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
|
<title>Go Web Shell</title>
|
2025-01-31 16:43:35 +01:00
|
|
|
<script>
|
|
|
|
document.addEventListener("DOMContentLoaded", function() {
|
|
|
|
const input = document.getElementById("command-input");
|
|
|
|
if (!input) return;
|
|
|
|
let commandHistory = JSON.parse(sessionStorage.getItem("commandHistory")) || [];
|
|
|
|
let historyIndex = commandHistory.length;
|
|
|
|
let tabIndex = -1;
|
|
|
|
let tabMatches = [];
|
|
|
|
|
|
|
|
document.querySelector("form").addEventListener("submit", function(event) {
|
|
|
|
const command = input.value.trim();
|
|
|
|
if (command) {
|
|
|
|
commandHistory.push(command);
|
|
|
|
sessionStorage.setItem("commandHistory", JSON.stringify(commandHistory));
|
|
|
|
historyIndex = commandHistory.length;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
window.addEventListener("keydown", function(event) {
|
|
|
|
if (document.activeElement !== input) return;
|
|
|
|
|
|
|
|
const cursorPos = input.selectionStart;
|
|
|
|
const textLength = input.value.length;
|
|
|
|
|
|
|
|
// Prevent default behavior for specific Ctrl+Key shortcuts
|
|
|
|
if (event.ctrlKey && ["w", "n", "p", "h", "e", "a", "k", "u", "d", "r", "t"].includes(event.key.toLowerCase())) {
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ctrl+A: Move cursor to the beginning
|
|
|
|
if (event.ctrlKey && event.key === "a") {
|
|
|
|
input.setSelectionRange(0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ctrl+E: Move cursor to the end
|
|
|
|
else if (event.ctrlKey && event.key === "e") {
|
|
|
|
input.setSelectionRange(textLength, textLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ctrl+U: Clear the input field
|
|
|
|
else if (event.ctrlKey && event.key === "u") {
|
|
|
|
input.value = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ctrl+K: Delete everything after the cursor
|
|
|
|
else if (event.ctrlKey && event.key === "k") {
|
|
|
|
input.value = input.value.substring(0, cursorPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ctrl+W: Delete the previous word
|
|
|
|
else if (event.ctrlKey && event.key === "w") {
|
|
|
|
const beforeCursor = input.value.substring(0, cursorPos);
|
|
|
|
const afterCursor = input.value.substring(cursorPos);
|
|
|
|
const newBeforeCursor = beforeCursor.replace(/\S+\s*$/, ""); // Delete last word
|
|
|
|
input.value = newBeforeCursor + afterCursor;
|
|
|
|
input.setSelectionRange(newBeforeCursor.length, newBeforeCursor.length);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ctrl+H: Delete the previous character (Backspace)
|
|
|
|
else if (event.ctrlKey && event.key === "h") {
|
|
|
|
if (cursorPos > 0) {
|
|
|
|
input.value = input.value.substring(0, cursorPos - 1) + input.value.substring(cursorPos);
|
|
|
|
input.setSelectionRange(cursorPos - 1, cursorPos - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ctrl+D: Delete character under cursor (or clear input if empty)
|
|
|
|
else if (event.ctrlKey && event.key === "d") {
|
|
|
|
if (textLength === 0) {
|
|
|
|
console.log("Ctrl+D: No input, simulating EOF");
|
|
|
|
} else if (cursorPos < textLength) {
|
|
|
|
input.value = input.value.substring(0, cursorPos) + input.value.substring(cursorPos + 1);
|
|
|
|
input.setSelectionRange(cursorPos, cursorPos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ctrl+P: Previous command (up)
|
|
|
|
else if (event.ctrlKey && event.key === "p") {
|
|
|
|
if (historyIndex > 0) {
|
|
|
|
historyIndex--;
|
|
|
|
input.value = commandHistory[historyIndex];
|
|
|
|
} else if (historyIndex === 0) {
|
|
|
|
input.value = commandHistory[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ctrl+N: Next command (down)
|
|
|
|
else if (event.ctrlKey && event.key === "n") {
|
|
|
|
if (historyIndex < commandHistory.length - 1) {
|
|
|
|
historyIndex++;
|
|
|
|
input.value = commandHistory[historyIndex];
|
|
|
|
} else {
|
|
|
|
historyIndex = commandHistory.length;
|
|
|
|
input.value = "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ctrl+R: Prevent page reload (for future reverse search)
|
|
|
|
else if (event.ctrlKey && event.key === "r") {
|
|
|
|
console.log("Reverse search triggered (not yet implemented)");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tab Completion
|
|
|
|
else if (event.key === "Tab") {
|
|
|
|
event.preventDefault();
|
|
|
|
const currentText = input.value.trim();
|
|
|
|
|
|
|
|
if (currentText === "") return;
|
|
|
|
|
|
|
|
// Find all matching commands from history
|
|
|
|
if (tabIndex === -1) {
|
|
|
|
tabMatches = commandHistory.filter(cmd => cmd.startsWith(currentText));
|
|
|
|
if (tabMatches.length === 0) return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cycle through matches
|
|
|
|
if (event.shiftKey) {
|
|
|
|
tabIndex = tabIndex > 0 ? tabIndex - 1 : tabMatches.length - 1; // Shift+Tab goes backward
|
|
|
|
} else {
|
|
|
|
tabIndex = (tabIndex + 1) % tabMatches.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
input.value = tabMatches[tabIndex];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allow Enter key to submit form
|
|
|
|
if (event.key === "Enter") {
|
|
|
|
tabIndex = -1; // Reset tab completion cycle
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}, true);
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
<script>
|
|
|
|
document.addEventListener("DOMContentLoaded", function() {
|
|
|
|
document.getElementById("command-input").addEventListener("keydown", function(event) {
|
|
|
|
if (event.key === "Enter") {
|
|
|
|
let inputVal = this.value.trim();
|
|
|
|
if (inputVal === "upload") {
|
|
|
|
document.getElementById("inputFile").click();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function requestUpload() {
|
|
|
|
let fileInput = document.getElementById("inputFile");
|
|
|
|
console.log("file is");
|
|
|
|
console.log(fileInput);
|
|
|
|
if (fileInput.files.length === 0) {
|
|
|
|
console.error("No file selected.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let file = fileInput.files[0];
|
|
|
|
let formData = new FormData();
|
|
|
|
formData.append("file", file);
|
|
|
|
|
|
|
|
fetch("http://localhost:8080/upload", {
|
|
|
|
method: "POST",
|
|
|
|
body: formData
|
|
|
|
})
|
|
|
|
.then(response => response.text())
|
|
|
|
.then(data => {
|
|
|
|
console.log("Upload succesful:", data);
|
|
|
|
document.getElementById("command-input").value = "";
|
|
|
|
})
|
|
|
|
.catch(error => console.error("Upload failed:", error));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
</script>
|
2025-01-28 09:02:18 +01:00
|
|
|
<style>
|
|
|
|
body {
|
|
|
|
background-color: #222;
|
|
|
|
color: #eee;
|
|
|
|
font-family: monospace;
|
2025-01-28 17:01:58 +01:00
|
|
|
font-size: 14pt;
|
2025-01-28 09:02:18 +01:00
|
|
|
margin: 0;
|
|
|
|
padding: 0;
|
|
|
|
height: 100vh;
|
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
|
|
|
}
|
|
|
|
#terminal {
|
|
|
|
flex: 1;
|
2025-01-28 17:01:58 +01:00
|
|
|
padding: 12px;
|
2025-01-28 09:02:18 +01:00
|
|
|
overflow-y: auto;
|
|
|
|
white-space: pre-wrap;
|
2025-01-31 16:43:35 +01:00
|
|
|
border: 1px solid white;
|
2025-01-28 09:02:18 +01:00
|
|
|
margin: 10px;
|
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
|
|
|
}
|
|
|
|
input {
|
2025-01-28 17:01:58 +01:00
|
|
|
font-size: 14pt;
|
2025-01-28 09:02:18 +01:00
|
|
|
background: #222;
|
|
|
|
color: #eee;
|
|
|
|
border: none;
|
|
|
|
width: 80%;
|
|
|
|
font-family: monospace;
|
|
|
|
}
|
|
|
|
input:focus {
|
|
|
|
outline: none;
|
|
|
|
}
|
|
|
|
span.command {
|
|
|
|
color: #75df0b;
|
|
|
|
}
|
|
|
|
span.error {
|
|
|
|
color: #ff5555;
|
|
|
|
}
|
|
|
|
span.directory {
|
|
|
|
color: #1bc9e7;
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<div id="terminal">
|
|
|
|
<span>Current Directory: {{.CurrentDir}}</span>
|
|
|
|
{{range .CommandLog}}
|
|
|
|
<div><span class="command">gommand:$ {{.Command}}</span></div>
|
|
|
|
{{if .Output}}
|
|
|
|
<div>{{.Output}}</div>
|
|
|
|
{{end}}
|
|
|
|
{{if .Error}}
|
|
|
|
<div class="error">{{.Error}}</div>
|
|
|
|
{{end}}
|
|
|
|
{{end}}
|
2025-01-28 17:01:58 +01:00
|
|
|
<form method="POST" autocomplete="off">
|
2025-01-28 09:02:18 +01:00
|
|
|
<div style="display: flex; align-items: center;">
|
2025-01-28 17:01:58 +01:00
|
|
|
<span class="command">{{.CurrentUsername}}:<span class="directory">{{.CurrentDir}}</span>$ </span>
|
2025-01-31 16:43:35 +01:00
|
|
|
<input id="command-input" type="text" name="command" placeholder="Type a command here..." autofocus required>
|
2025-01-28 09:02:18 +01:00
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</div>
|
2025-01-31 16:43:35 +01:00
|
|
|
<div>
|
|
|
|
<form id="fileUpload" action="/upload" method="POST" enctype="multipart/form-data">
|
|
|
|
<label for="inputFile">Select a file to upload:</label>
|
|
|
|
<input name="inputFile" id="inputFile" type="file">
|
|
|
|
<button id="fileUploadButton" onclick="document.getElementById('inputFile').click()">Upload</button>
|
|
|
|
</form>
|
|
|
|
</div>
|
2025-01-28 09:02:18 +01:00
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
`))
|
|
|
|
|
|
|
|
data := PageData{
|
|
|
|
CurrentDir: currentDir,
|
|
|
|
CurrentUsername: currentUsername,
|
|
|
|
CommandLog: commandLog,
|
|
|
|
}
|
|
|
|
tmpl.Execute(w, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
http.HandleFunc("/", handler)
|
|
|
|
http.HandleFunc("/upload", fileUploadHandler)
|
|
|
|
fmt.Println("Starting server on :8080")
|
|
|
|
http.ListenAndServe(":8080", nil)
|
|
|
|
}
|