diff options
| author | Jeff Carr <[email protected]> | 2025-03-12 07:54:03 -0500 | 
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2025-03-12 07:54:03 -0500 | 
| commit | a024c9ece14d3097b3f72f2dc831444afd695d86 (patch) | |
| tree | 30f7a5ea7c40b81e3c7e57605ff5d0a8e733217f | |
| parent | c2f796de1e07e59bb379fa4e0a222bc13dcfe571 (diff) | |
attempting to pass off the socket
| -rw-r--r-- | Makefile | 3 | ||||
| -rw-r--r-- | argv.go | 5 | ||||
| -rw-r--r-- | exit.go | 24 | ||||
| -rw-r--r-- | main.go | 90 | 
4 files changed, 120 insertions, 2 deletions
@@ -4,7 +4,8 @@ VERSION = $(shell git describe --tags)  BUILDTIME = $(shell date +%Y.%m.%d_%H%M)  all: proto build -	./gus --config /etc/gus/gus.text +	@#./gus --config /etc/gus/gus.text +	./gus --me  gocui: build  	./gus --gui gocui --config /etc/gus/gus.text >/tmp/gocui.log 2>&1 @@ -17,6 +17,7 @@ var argv args  type args struct {  	Verbose bool   `arg:"--verbose"                           help:"talk more"`  	Daemon  bool   `arg:"--daemon"           default:"false"  help:"run in daemon mode"` +	UseME   bool   `arg:"--me"                                help:"use /me to connect"`  	Port    int    `arg:"--port"             default:"2522"   help:"port to run on"`  	URL     string `arg:"--url"                               help:"url to use"`  	Config  string `arg:"--config"                            help:"config file (default is ~/.config/cloud/gus.text"` @@ -28,7 +29,9 @@ func (args) Version() string {  func (a args) Description() string {  	return ` -         this daemon talks to zookeeper +	"Phantastic Gus" your network squirrel + +	* Meet [the Phantastic squirrel Gus](https://www.youtube.com/watch?v=hFZFjoX2cGg)  `  } @@ -0,0 +1,24 @@ +// Copyright 2017-2025 WIT.COM Inc. All rights reserved. +// Use of this source code is governed by the GPL 3.0 + +package main + +import ( +	"os" + +	"go.wit.com/log" +) + +func okExit(note string) { +	if note != "" { +		log.Info("gus exit:", note, "ok") +	} +	me.myGui.Close() +	os.Exit(0) +} + +func badExit(err error) { +	log.Info("gus failed: ", err) +	me.myGui.Close() +	os.Exit(-1) +} @@ -40,6 +40,12 @@ func main() {  	me = new(gusconf)  	me.pollDelay = 10 * time.Second + +	if argv.UseME { +		connectME() +		okExit("") +	} +  	me.portmaps = ConfigLoad()  	me.events = EventLoad() @@ -252,3 +258,87 @@ func ioCopy(clientConn, targetConn net.Conn) {  	wg.Wait() // Wait for both copies to complete  } + +func connectME() { +	hostname, err := os.Hostname() +	if err != nil { +		fmt.Println("Error:", err) +		return +	} +	log.Println("Hostname:", hostname) +	if fqdn, err := getFQDN(hostname); err == nil { +		hostname = fqdn +	} else { +		log.Printf("Your hostname (%s) is not in DNS correctly: %v\n", hostname, err) +	} + +	localport := "25910" +	// where := "104.48.38.253:25910" +	where := "104.48.38.253:8081" +	dest, err := net.Dial("tcp", where) +	if err != nil { +		log.Printf("Failed to connect to %s %v\n", where, err) +		return +	} +	defer dest.Close() + +	// make a new event from this new connection +	log.Printf("Connected on port %s from client: %s to where = %s\n", localport, "tbd", where) + +	reader := bufio.NewReader(dest) + +	// Read one line +	line, err := reader.ReadString('\n') +	if err != nil { +		log.Info("Error reading line:", err) +		return +	} +	log.Info("gus Received:", line) + +	fmt.Fprintf(dest, "/me hostname %s\n", hostname) + +	// Read one line +	line, err = reader.ReadString('\n') +	if err != nil { +		log.Info("Error reading line:", err) +		return +	} +	log.Info("gus Received:", line) + +	// Listen on local port +	s := fmt.Sprintf("0.0.0.0:%s", localport) +	listener, err := net.Listen("tcp", s) +	if err != nil { +		log.Fatalf("Failed to listen on %s: %v", s, err) +	} +	defer listener.Close() +	log.Info("Listening on ", s) +	log.Printf("Try: remmina -c spice://localhost:%s\n", localport) + +	// Accept incoming connection +	src, err := listener.Accept() +	if err != nil { +		log.Printf("Failed to accept client connection: %v", err) +		return +	} +	log.Printf("Client connected: %s\n", src.RemoteAddr()) +	ioCopy(src, dest) +} + +func getFQDN(hostname string) (string, error) { +	// Perform a reverse lookup to get the FQDN +	addrs, err := net.LookupAddr(hostname) +	if err != nil { +		return "", fmt.Errorf("failed to resolve FQDN: %w", err) +	} + +	// Choose the first valid result +	for _, addr := range addrs { +		if strings.HasSuffix(addr, ".") { // FQDNs often end with a dot +			return strings.TrimSuffix(addr, "."), nil +		} +		return addr, nil +	} + +	return "", fmt.Errorf("no FQDN found for hostname: %s", hostname) +}  | 
