Add spider-net and auto docker stuff
This commit is contained in:
		
							
								
								
									
										1
									
								
								spider-net/.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								spider-net/.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
			
		||||
spider-net
 | 
			
		||||
							
								
								
									
										13
									
								
								spider-net/Dockerfile
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								spider-net/Dockerfile
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,13 @@
 | 
			
		||||
FROM golang:1.22.5
 | 
			
		||||
 | 
			
		||||
WORKDIR /app
 | 
			
		||||
 | 
			
		||||
#COPY go.mod go.sum ./
 | 
			
		||||
#RUN go mod download
 | 
			
		||||
 | 
			
		||||
#COPY *.go ./
 | 
			
		||||
#RUN CGO_ENABLED=0 GOOS=linux go build -o /spider-net
 | 
			
		||||
 | 
			
		||||
COPY spider-net /spider-net
 | 
			
		||||
 | 
			
		||||
CMD ["/spider-net"]
 | 
			
		||||
							
								
								
									
										3
									
								
								spider-net/build_and_upload.sh
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										3
									
								
								spider-net/build_and_upload.sh
									
									
									
									
									
										Executable file
									
								
							@@ -0,0 +1,3 @@
 | 
			
		||||
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -o spider-net
 | 
			
		||||
cd ..
 | 
			
		||||
./docker.sh compose up spider-net -d --build
 | 
			
		||||
							
								
								
									
										10
									
								
								spider-net/go.mod
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								spider-net/go.mod
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,10 @@
 | 
			
		||||
module spider-mqtt
 | 
			
		||||
 | 
			
		||||
go 1.22
 | 
			
		||||
 | 
			
		||||
require (
 | 
			
		||||
	github.com/eclipse/paho.mqtt.golang v1.4.3 // indirect
 | 
			
		||||
	github.com/gorilla/websocket v1.5.0 // indirect
 | 
			
		||||
	golang.org/x/net v0.8.0 // indirect
 | 
			
		||||
	golang.org/x/sync v0.1.0 // indirect
 | 
			
		||||
)
 | 
			
		||||
							
								
								
									
										8
									
								
								spider-net/go.sum
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								spider-net/go.sum
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,8 @@
 | 
			
		||||
github.com/eclipse/paho.mqtt.golang v1.4.3 h1:2kwcUGn8seMUfWndX0hGbvH8r7crgcJguQNCyp70xik=
 | 
			
		||||
github.com/eclipse/paho.mqtt.golang v1.4.3/go.mod h1:CSYvoAlsMkhYOXh/oKyxa8EcBci6dVkLCbo5tTC1RIE=
 | 
			
		||||
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
 | 
			
		||||
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
 | 
			
		||||
golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ=
 | 
			
		||||
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
 | 
			
		||||
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
 | 
			
		||||
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
 | 
			
		||||
							
								
								
									
										68
									
								
								spider-net/main.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										68
									
								
								spider-net/main.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,68 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	mqtt "github.com/eclipse/paho.mqtt.golang"
 | 
			
		||||
	"log"
 | 
			
		||||
	"net"
 | 
			
		||||
	"os"
 | 
			
		||||
	"time"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func getLocalIP() (string, error) {
 | 
			
		||||
	addrs, err := net.InterfaceAddrs()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return "", err
 | 
			
		||||
	}
 | 
			
		||||
	for _, addr := range addrs {
 | 
			
		||||
		if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
 | 
			
		||||
			if ipnet.IP.To4() != nil {
 | 
			
		||||
				return ipnet.IP.String(), nil
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return "", fmt.Errorf("no IP address found")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func publishIp(client mqtt.Client, topic string) {
 | 
			
		||||
	log.Printf("Publishing IP")
 | 
			
		||||
	ip, err := getLocalIP()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Printf("Error getting IP address: %v\n", err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	token := client.Publish(topic, 0, true, ip)
 | 
			
		||||
	token.Wait()
 | 
			
		||||
	if token.Error() != nil {
 | 
			
		||||
		log.Printf("Error publishing IP address: %v\n", token.Error())
 | 
			
		||||
	} else {
 | 
			
		||||
		log.Printf("Published IP address: %s\n", ip)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	broker := "tcp://mqtt.seeseepuff.be:1883"
 | 
			
		||||
	topic := "host/" + os.Getenv("SPIDER_HOSTNAME")
 | 
			
		||||
 | 
			
		||||
	opts := mqtt.NewClientOptions()
 | 
			
		||||
	opts.AddBroker(broker)
 | 
			
		||||
	opts.SetClientID("spider-host-client")
 | 
			
		||||
	client := mqtt.NewClient(opts)
 | 
			
		||||
 | 
			
		||||
	token := client.Connect()
 | 
			
		||||
	token.Wait()
 | 
			
		||||
	if token.Error() != nil {
 | 
			
		||||
		log.Fatalf("Error connecting to MQTT broker: %v\n", token.Error())
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	ticker := time.NewTicker(60 * time.Second)
 | 
			
		||||
	defer ticker.Stop()
 | 
			
		||||
 | 
			
		||||
	publishIp(client, topic)
 | 
			
		||||
	for {
 | 
			
		||||
		select {
 | 
			
		||||
		case <-ticker.C:
 | 
			
		||||
			publishIp(client, topic)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user