Browse Source

move GPSd config to its own struct. Only show warning when UI is served on all interfaces. Show who (IP) is accessing the GPSd data.

blockmurder 1 month ago
parent
commit
17cc57133f
4 changed files with 53 additions and 46 deletions
  1. 20 13
      cfg/config.go
  2. 6 4
      cfg/example_config.json
  3. 24 26
      http.go
  4. 3 3
      main.go

+ 20 - 13
cfg/config.go

@@ -59,17 +59,10 @@ type Config struct {
 	Pactor    PactorConfig    `json:"pactor"`     // See PactorConfig.
 	Telnet    TelnetConfig    `json:"telnet"`     // See TelnetConfig.
 
-	// WARNING: If you enable GPSd http endpoint (gpsd_enable_http) you might
+	// WARNING: If you enable GPSd http endpoint (enable_http) you might
 	// expose your current position to anyone who has access to Pat!!!
-	//
-	// gpsd_enable_http:     enable GPSd support in web interface
-	// gpsd_use_server_time: Use server time instead of timestamp provided by
-	//                       GPSd (e.g for older GPS device with week roll-over
-	//                       issue)
-	// gpsd_addr             Address and port of GPSd server (e.g. localhost:2947)
-	GPSdEnableHttp bool    `json:"gpsd_enable_http"`
-	GPSdUseServerTime bool `json:"gpsd_use_server_time"`
-	GPSdAddr string        `json:"gpsd_addr"`
+	// See GPSdConfig.
+	GPSd GPSdConfig `json:gpsd`
 
 	// Command schedule (cron-like syntax).
 	//
@@ -199,6 +192,18 @@ type BeaconConfig struct {
 	Destination string `json:"destination"`
 }
 
+type GPSdConfig struct {
+	// enable GPSd support in web interface
+	EnableHttp bool    `json:"enable_http"`
+
+	// Use server time instead of timestamp provided by GPSd (e.g for older GPS
+	// device with week roll-over issue)
+	UseServerTime bool `json:"use_server_time"`
+
+	// Address and port of GPSd server (e.g. localhost:2947)
+	Addr string        `json:"addr"`
+}
+
 var DefaultConfig Config = Config{
 	MOTD:         []string{"Open source Winlink client - getpat.io"},
 	AuxAddrs:     []string{},
@@ -238,9 +243,11 @@ var DefaultConfig Config = Config{
 		ListenAddr: ":8774",
 		Password:   "",
 	},
-	GPSdEnableHttp:    false,
-	GPSdUseServerTime: false,
-	GPSdAddr:          "localhost:2947", // Default listen address for GPSd
+	GPSd: GPSdConfig{
+		EnableHttp:    false,
+		UseServerTime: false,
+		Addr:          "localhost:2947", // Default listen address for GPSd
+	},
 	Schedule:          map[string]string{},
 	HamlibRigs:        map[string]HamlibConfig{},
 }

+ 6 - 4
cfg/example_config.json

@@ -46,8 +46,10 @@
   },
   "schedule": {
     "*/30 * * * *": "connect offgrid"
-},
-  "gpsd_enable_http": false,
-  "gpsd_use_server_time": false,
-  "gpsd_addr": "localhost:2947"
+  },
+  "gpsd": {
+    "enable_http": false,
+    "use_server_time": false,
+    "addr": "localhost:2947",
+  },
 }

+ 24 - 26
http.go

@@ -12,6 +12,7 @@ import (
 	"html/template"
 	"io/ioutil"
 	"log"
+	"net"
 	"net/http"
 	"os"
 	"path"
@@ -62,11 +63,11 @@ var websocketHub *WSHub
 func ListenAndServe(addr string) error {
 	log.Printf("Starting HTTP service (%s)...", addr)
 
-	if config.GPSdEnableHttp {
+	if host, _, _ := net.SplitHostPort(addr); host == "" && config.GPSd.EnableHttp {
 		// can't use fmt.Printf as it would not show on the web interface
-		// TODO: maybe make a popup showing the warning?
+		// TODO: maybe make a popup showing the warning ont the web UI?
 		log.SetFlags(log.Flags() &^ (log.Ldate | log.Ltime))
-		log.Printf("\nWARNING: You have enable GPSd http endpoint (gpsd_enable_http). You might expose \n" +
+		log.Printf("\nWARNING: You have enable GPSd http endpoint (enable_http). You might expose \n" +
 		           "         your current position to anyone who has access to the Pat web interface!\n\n")
 		log.SetFlags(log.Flags() ^ (log.Ldate | log.Ltime))
 	}
@@ -353,38 +354,35 @@ func statusHandler(w http.ResponseWriter, req *http.Request) { json.NewEncoder(w
 
 func positionHandler(w http.ResponseWriter, req *http.Request) {
 	// Throw error if GPSd http endpoint is not enabled
-	if config.GPSdEnableHttp {
-		log.Println("GPS data has been accessed! Was it you?")
-		if config.GPSdAddr != ""{
-			conn, err := gpsd.Dial(config.GPSdAddr)
-			if err != nil {
-				// do not pass error message to response as GPSd address might be leaked
-				http.Error(w, "GPSd Dial failed", http.StatusInternalServerError)
-				return
-			}
-			defer conn.Close()
-
-			conn.Watch(true)
+	if config.GPSd.EnableHttp && config.GPSd.Addr != "" {
+		host, _, _ := net.SplitHostPort(req.RemoteAddr)
+		log.Printf("Location data from GPSd served to %s", host)
 
-			pos, err := conn.NextPosTimeout(5 * time.Second)
-			if err != nil {
-				http.Error(w, "GPSd get next position failed: " + err.Error(), http.StatusInternalServerError)
-				return
-			}
+		conn, err := gpsd.Dial(config.GPSd.Addr)
+		if err != nil {
+			// do not pass error message to response as GPSd address might be leaked
+			http.Error(w, "GPSd Dial failed", http.StatusInternalServerError)
+			return
+		}
+		defer conn.Close()
 
-			if config.GPSdUseServerTime {
-				pos.Time = time.Now()
-			}
+		conn.Watch(true)
 
-			json.NewEncoder(w).Encode(pos)
+		pos, err := conn.NextPosTimeout(5 * time.Second)
+		if err != nil {
+			http.Error(w, "GPSd get next position failed: " + err.Error(), http.StatusInternalServerError)
 			return
 		}
 
-		http.Error(w, "GPSd not enabled but no address set in config", http.StatusInternalServerError)
+		if config.GPSd.UseServerTime {
+			pos.Time = time.Now()
+		}
+
+		json.NewEncoder(w).Encode(pos)
 		return
 	}
 
-	http.Error(w, "GPSd not enabled in config file", http.StatusInternalServerError)
+	http.Error(w, "GPSd not enabled or address not set in config file", http.StatusInternalServerError)
 	return
 }
 

+ 3 - 3
main.go

@@ -729,8 +729,8 @@ func posReportHandle(args []string) {
 			log.Fatal(err)
 		}
 		report.Lon = &lon
-	} else if config.GPSdAddr != "" {
-		conn, err := gpsd.Dial(config.GPSdAddr)
+	} else if config.GPSd.Addr != "" {
+		conn, err := gpsd.Dial(config.GPSd.Addr)
 		if err != nil {
 			log.Fatalf("GPSd daemon: %s", err)
 		}
@@ -746,7 +746,7 @@ func posReportHandle(args []string) {
 
 		report.Lat = &pos.Lat
 		report.Lon = &pos.Lon
-		if config.GPSdUseServerTime {
+		if config.GPSd.UseServerTime {
 			report.Date = time.Now()
 		} else {
 			report.Date = pos.Time