small pixel drawing of a pufferfish gore

main.go

package main

import (
	"fmt"
	"log"
	"net/http"
	"os"
	"os/exec"
	"path/filepath"
	"sync"
)

var mu sync.Mutex

func main() {
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintln(w, "Welcome to Gore: Submit a Go module for execution!")
		fmt.Fprintln(w, "Use /submit?module=<module-url> to submit a Go module.")
	})
	http.HandleFunc("/submit", handleModuleSubmission)

	log.Println("Starting Gore server on :8080...")
	log.Fatal(http.ListenAndServe(":8080", nil))
}

func handleModuleSubmission(w http.ResponseWriter, r *http.Request) {
	module := r.URL.Query().Get("module")
	if module == "" {
		http.Error(w, "Module URL is required", http.StatusBadRequest)
		return
	}

	go func() {
		if err := processModule(module); err != nil {
			log.Printf("Error processing module %s: %v", module, err)
		}
	}()

	fmt.Fprintf(w, "Module %s submitted for processing.\n", module)
}

func processModule(module string) error {
	mu.Lock()
	defer mu.Unlock()

	log.Printf("Processing module: %s", module)

	// Create a temporary directory for the module
	tempDir, err := os.MkdirTemp("", "gore-*")
	if err != nil {
		return fmt.Errorf("failed to create temp dir: %v", err)
	}
	defer os.RemoveAll(tempDir)

	// `go get` the module
	cmd := exec.Command("go", "mod", "init", "gore-temp")
	cmd.Dir = tempDir
	if err := cmd.Run(); err != nil {
		return fmt.Errorf("failed to initialize module: %v", err)
	}

	cmd = exec.Command("go", "get", module)
	cmd.Dir = tempDir
	if err := cmd.Run(); err != nil {
		return fmt.Errorf("failed to get module: %v", err)
	}

	// Build the module
	outputBinary := filepath.Join(tempDir, "module-binary")
	cmd = exec.Command("go", "build", "-o", outputBinary)
	cmd.Dir = tempDir
	if err := cmd.Run(); err != nil {
		return fmt.Errorf("failed to build module: %v", err)
	}

	log.Printf("Module built: %s", outputBinary)

	// Launch QEMU VM with u-root and the built binary
	if err := launchQEMU(outputBinary); err != nil {
		return fmt.Errorf("failed to launch QEMU: %v", err)
	}

	return nil
}

func launchQEMU(binary string) error {
	// Simulate QEMU launching
	log.Printf("Launching QEMU VM with binary: %s", binary)

	// Replace this with actual QEMU invocation
	cmd := exec.Command("qemu-system-x86_64", "-kernel", binary, "-append", "console=ttyS0", "-nographic")
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	if err := cmd.Run(); err != nil {
		return fmt.Errorf("failed to start QEMU: %v", err)
	}

	return nil
}