add version flag and interaction to make users not run the program as sudo :3

This commit is contained in:
LilliaElaine
2025-10-23 02:15:20 -04:00
parent 0048948102
commit 8dc0e85dbc
2 changed files with 82 additions and 5 deletions

View File

@ -12,7 +12,7 @@ on:
permissions:
contents: write
jobs:
build:
build-package:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
@ -25,10 +25,15 @@ jobs:
- name: install libusb
run: sudo apt update && sudo apt install -y libusb-1.0-0-dev
- name: Build
run: go build -v ./...
- name: Build Release
run: go build -v -ldflags "-X main.gitVersion=${{ github.ref_name }}" ./...
if: github.ref_type == 'tag'
- name: Release
- name: Build Non-Release
run: go build ./...
if: github.ref_type != 'tag'
- name: Release Binary
uses: softprops/action-gh-release@v2
if: github.ref_type == 'tag'
with:

74
main.go
View File

@ -7,7 +7,10 @@ import (
"image/jpeg"
"log"
"net/http"
"os"
"os/user"
"strconv"
"strings"
"syscall"
"github.com/google/gousb"
@ -16,22 +19,91 @@ import (
"github.com/kevmo314/go-uvc/pkg/descriptors"
)
const udevrule = `# Bigscreen Bigeye
KERNEL=="hidraw*", SUBSYSTEM=="hidraw", ATTRS{idVendor}=="35bd", ATTRS{idProduct}=="0202", MODE="0660", TAG+="uaccess"
SUBSYSTEM=="usb", ATTRS{idVendor}=="35bd", ATTRS{idProduct}=="0202", MODE="0660", TAG+="uaccess"
`
const path = "99-bsb-cams.rules"
func getdevice() (device string) {
ctx := gousb.NewContext()
defer ctx.Close()
dev, err := ctx.OpenDeviceWithVIDPID(0x35bd, 0x0202)
user, _ := user.Current()
if user.Username == "root" && !*sudo {
log.Print("Running As Root Isnt Reccomended For Safety, Creating A UDEV Rule To Allow Rootless Access ")
if _, err := os.Stat(path); err == nil {
log.Print("File Already Exists")
} else {
err := os.WriteFile(path, []byte(udevrule), 0644)
if err != nil {
log.Fatalf("Could not Create File: %v", err)
}
}
var response string
log.Printf("Would You Like The UDEV Rule Automatically Moved To Your UDEV DIR? It will be at /usr/lib/udev/rules.d/%v (Y/N)", path)
fmt.Scan(&response)
switch strings.ToLower(response) {
case "yes", "ye", "y":
log.Print("Moving File !!")
if _, err := os.Stat(path); err == nil {
log.Print("File Already Exists, Do You Want To Replace It? (Y/N)")
fmt.Scan(&response)
switch strings.ToLower(response) {
case "yes", "ye", "y":
log.Print("Replacing File !")
case "n", "no":
os.Exit(0)
default:
log.Fatal("Invalid Response")
}
}
err := os.Rename(path, "/usr/lib/udev/rules.d/"+path)
if err != nil {
log.Fatalf("Could Not Move File: %v", err)
}
log.Print("Please Reboot Your PC For Changes To Take Effect!!")
os.Exit(0)
case "n", "no":
log.Printf("Please move the file (%v) into your udev rule directory and reboot for it to take effect, or if you REALLLLY want to run this program as sudo append --sudo to your run command", path)
os.Exit(0)
default:
log.Fatal("Invalid Answer")
}
}
if err != nil {
log.Fatalf("Could not open a device: %v", err)
if err == gousb.ErrorAccess {
log.Print("It looks like the cameras cannot be accessed, udev file being created in this directory")
log.Printf("Creating UDEV Rule At %v", path)
if _, err := os.Stat(path); err == nil {
log.Print("File Already Exists")
} else {
err := os.WriteFile(path, []byte(udevrule), 0644)
if err != nil {
log.Fatalf("Could not Create File: %v", err)
}
}
log.Print("File Created ! Please copy to your udev directory, chown to root, and reboot for it to take effect")
os.Exit(0)
}
}
defer dev.Close()
return fmt.Sprintf("/dev/bus/usb/%03v/%03v", dev.Desc.Bus, dev.Desc.Address)
}
var gitVersion string
var verbosePtr = flag.Bool("verbose", false, "Whether or not to show libusb errors")
var port = flag.Int("port", 8080, "What Port To Output Frames To (Default is 8080)")
var version = flag.Bool("version", false, "Flag To Show Current Version")
var sudo = flag.Bool("sudo", false, "Force Program To Run As Sudo")
func main() {
flag.Parse()
if *version {
log.Print("go-bsb-cams " + gitVersion)
os.Exit(0)
}
stream := mjpeg.NewLiveStream()
device := getdevice()
// Pass your jpegBuffer frames using stream.UpdateJPEG(<your-buffer>)