more phansastic gus
This commit is contained in:
parent
09e7dfbb2b
commit
c941ec541e
98
main.go
98
main.go
|
@ -4,11 +4,14 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"embed"
|
"embed"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
|
sync "sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
@ -98,18 +101,47 @@ func doME(pm *Portmap, gus net.Listener) {
|
||||||
log.Info("Listening on ", s)
|
log.Info("Listening on ", s)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// Accept incoming connection
|
for {
|
||||||
clientConn, err := gus.Accept()
|
// Accept incoming connection
|
||||||
if err != nil {
|
src, err := gus.Accept()
|
||||||
log.Printf("Failed to accept client connection: %v", err)
|
if err != nil {
|
||||||
return
|
log.Printf("Failed to accept client connection: %v\n", err)
|
||||||
}
|
return
|
||||||
// log.Printf("Client connected: %s", clientConn.RemoteAddr())
|
}
|
||||||
|
|
||||||
// make a new event from this new connection
|
// make a new event from this new connection
|
||||||
log.Printf("Connected on port %d from client: %s to where = %s\n", localport, clientConn.RemoteAddr(), where)
|
log.Printf("/me Connected on port %d from client: %s to where = %s\n", localport, src.RemoteAddr(), where)
|
||||||
// Handle the connection in a separate goroutine
|
fmt.Fprintln(src, "/me hello")
|
||||||
// go handleConnection(clientConn, connect, port)
|
|
||||||
|
reader := bufio.NewReader(src)
|
||||||
|
|
||||||
|
// Read one line
|
||||||
|
line, err := reader.ReadString('\n')
|
||||||
|
if err != nil {
|
||||||
|
log.Info("Error reading line:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Info("gus got Received:", line)
|
||||||
|
parts := strings.Fields(line)
|
||||||
|
if len(parts) != 3 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if parts[0] != "/me" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if parts[1] != "hostname" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
hostname := parts[2]
|
||||||
|
msg := fmt.Sprintf("got hostname %s for %s", hostname, src.RemoteAddr())
|
||||||
|
log.Info("gus:", msg)
|
||||||
|
fmt.Fprintln(src, msg)
|
||||||
|
|
||||||
|
if hostname == "framebook.wit.com" {
|
||||||
|
// Handle the connection in a separate goroutine
|
||||||
|
go handleConnection(src, pm.Dest, int(pm.Localport))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func gus3000(pm *Portmap) {
|
func gus3000(pm *Portmap) {
|
||||||
|
@ -169,8 +201,9 @@ func handleConnection(clientConn net.Conn, where string, localport int) {
|
||||||
me.eventsChanged = true
|
me.eventsChanged = true
|
||||||
|
|
||||||
// Bidirectional copy of data
|
// Bidirectional copy of data
|
||||||
go io.Copy(targetConn, clientConn) // Client -> Target
|
// go io.Copy(targetConn, clientConn) // Client -> Target
|
||||||
io.Copy(clientConn, targetConn) // Target -> Client
|
// io.Copy(clientConn, targetConn) // Target -> Client
|
||||||
|
ioCopy(clientConn, targetConn)
|
||||||
|
|
||||||
// if the socket closes, record the close time
|
// if the socket closes, record the close time
|
||||||
e.Etime = timestamppb.New(time.Now())
|
e.Etime = timestamppb.New(time.Now())
|
||||||
|
@ -179,17 +212,46 @@ func handleConnection(clientConn net.Conn, where string, localport int) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func enablePort(port int, dest string) {
|
func enablePort(port int, dest string) {
|
||||||
|
var found bool
|
||||||
all := me.portmaps.All()
|
all := me.portmaps.All()
|
||||||
for all.Scan() {
|
for all.Scan() {
|
||||||
pm := all.Next()
|
pm := all.Next()
|
||||||
if int(pm.Localport) == port {
|
if int(pm.Localport) == port {
|
||||||
|
found = true
|
||||||
log.Info("Found port!", port)
|
log.Info("Found port!", port)
|
||||||
}
|
if pm.Enabled {
|
||||||
if pm.Enabled {
|
log.Info("portmap already enabled for", pm.Localport, "to", pm.Dest)
|
||||||
log.Info("portmap already enabled for", pm.Localport, "to", pm.Dest)
|
} else {
|
||||||
} else {
|
log.Info("portmap not enabled for", pm.Localport, "to", pm.Dest)
|
||||||
log.Info("portmap not enabled for", pm.Localport, "to", pm.Dest)
|
}
|
||||||
}
|
}
|
||||||
// go gus3000(pm)
|
// go gus3000(pm)
|
||||||
}
|
}
|
||||||
|
if !found {
|
||||||
|
log.Info("Did not find port =", port)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// should cleanly close both
|
||||||
|
func ioCopy(clientConn, targetConn net.Conn) {
|
||||||
|
defer clientConn.Close()
|
||||||
|
defer targetConn.Close()
|
||||||
|
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
wg.Add(2)
|
||||||
|
|
||||||
|
// Copy data in both directions
|
||||||
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
|
io.Copy(targetConn, clientConn) // Client -> Target
|
||||||
|
targetConn.Close() // Ensure closure on EOF/error
|
||||||
|
}()
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
|
io.Copy(clientConn, targetConn) // Target -> Client
|
||||||
|
clientConn.Close() // Ensure closure on EOF/error
|
||||||
|
}()
|
||||||
|
|
||||||
|
wg.Wait() // Wait for both copies to complete
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue